Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be17b82

Browse files
committed
constructor: prefix is back for classes. punto.
1 parent 15bdcf7 commit be17b82

10 files changed

Lines changed: 89 additions & 79 deletions

lib/nodes.js

Lines changed: 21 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/rewriter.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ exports.Base = class Base
150150
# indented block of code -- the implementation of a function, a clause in an
151151
# `if`, `switch`, or `try`, and so on...
152152
exports.Expressions = class Expressions extends Base
153-
(nodes) ->
153+
constructor: (nodes) ->
154154
@expressions = compact flatten nodes or []
155155

156156
children: ['expressions']
@@ -251,7 +251,7 @@ exports.Expressions = class Expressions extends Base
251251
# JavaScript without translation, such as: strings, numbers,
252252
# `true`, `false`, `null`...
253253
exports.Literal = class Literal extends Base
254-
(@value) ->
254+
constructor: (@value) ->
255255

256256
makeReturn: ->
257257
if @isPureStatement() then this else new Return this
@@ -280,7 +280,7 @@ exports.Literal = class Literal extends Base
280280
# A `return` is a *pureStatement* -- wrapping it in a closure wouldn't
281281
# make sense.
282282
exports.Return = class Return extends Base
283-
(@expression) ->
283+
constructor: (@expression) ->
284284

285285
children: ['expression']
286286

@@ -301,7 +301,7 @@ exports.Return = class Return extends Base
301301
# A value, variable or literal or parenthesized, indexed or dotted into,
302302
# or vanilla.
303303
exports.Value = class Value extends Base
304-
(base, props, tag) ->
304+
constructor: (base, props, tag) ->
305305
return base if not props and base instanceof Value
306306
@base = base
307307
@properties = props or []
@@ -393,7 +393,7 @@ exports.Value = class Value extends Base
393393
# CoffeeScript passes through block comments as JavaScript block comments
394394
# at the same position.
395395
exports.Comment = class Comment extends Base
396-
(@comment) ->
396+
constructor: (@comment) ->
397397

398398
isPureStatement: YES
399399
isStatement: YES
@@ -409,7 +409,7 @@ exports.Comment = class Comment extends Base
409409
# Node for a function invocation. Takes care of converting `super()` calls into
410410
# calls against the prototype's function of the same name.
411411
exports.Call = class Call extends Base
412-
(variable, @args = [], @soak) ->
412+
constructor: (variable, @args = [], @soak) ->
413413
@isNew = false
414414
@isSuper = variable is 'super'
415415
@variable = if @isSuper then null else variable
@@ -511,7 +511,7 @@ exports.Call = class Call extends Base
511511
# After `goog.inherits` from the
512512
# [Closure Library](http://closure-library.googlecode.com/svn/docs/closureGoogBase.js.html).
513513
exports.Extends = class Extends extends Base
514-
(@child, @parent) ->
514+
constructor: (@child, @parent) ->
515515

516516
children: ['child', 'parent']
517517

@@ -525,7 +525,7 @@ exports.Extends = class Extends extends Base
525525
# A `.` accessor into a property of a value, or the `::` shorthand for
526526
# an accessor into the object's prototype.
527527
exports.Accessor = class Accessor extends Base
528-
(@name, tag) ->
528+
constructor: (@name, tag) ->
529529
@proto = if tag is 'proto' then '.prototype' else ''
530530
@soak = tag is 'soak'
531531

@@ -541,7 +541,7 @@ exports.Accessor = class Accessor extends Base
541541

542542
# A `[ ... ]` indexed accessor into an array or object.
543543
exports.Index = class Index extends Base
544-
(@index) ->
544+
constructor: (@index) ->
545545

546546
children: ['index']
547547

@@ -555,7 +555,7 @@ exports.Index = class Index extends Base
555555

556556
# An object literal, nothing fancy.
557557
exports.Obj = class Obj extends Base
558-
(props, @generated = false) ->
558+
constructor: (props, @generated = false) ->
559559
@objects = @properties = props or []
560560

561561
children: ['properties']
@@ -618,7 +618,7 @@ exports.Obj = class Obj extends Base
618618

619619
# An array literal.
620620
exports.Arr = class Arr extends Base
621-
(objs) ->
621+
constructor: (objs) ->
622622
@objects = objs or []
623623

624624
children: ['objects']
@@ -643,7 +643,7 @@ exports.Arr = class Arr extends Base
643643
# Initialize a **Class** with its name, an optional superclass, and a
644644
# list of prototype property assignments.
645645
exports.Class = class Class extends Base
646-
(@variable, @parent, @body = new Expressions) ->
646+
constructor: (@variable, @parent, @body = new Expressions) ->
647647
@boundFuncs = []
648648

649649
children: ['variable', 'parent', 'body']
@@ -684,11 +684,22 @@ exports.Class = class Class extends Base
684684
base = assign.variable.base
685685
delete assign.context
686686
func = assign.value
687-
unless assign.variable.this
688-
assign.variable = new Value(new Literal(name), [new Accessor(base, 'proto')])
689-
if func instanceof Code and func.bound
690-
@boundFuncs.push base
691-
func.bound = no
687+
if base.value is 'constructor'
688+
if @ctor
689+
throw new Error 'cannot define more than one constructor in a class'
690+
if func.bound
691+
throw new Error 'cannot define a constructor as a bound function'
692+
if func instanceof Code
693+
@ctor = func
694+
else
695+
@ctor = new Assign(new Value(new Literal name), func)
696+
assign = null
697+
else
698+
unless assign.variable.this
699+
assign.variable = new Value(new Literal(name), [new Accessor(base, 'proto')])
700+
if func instanceof Code and func.bound
701+
@boundFuncs.push base
702+
func.bound = no
692703
assign
693704

694705
# Walk the body of the class, looking for prototype properties to be converted.
@@ -698,13 +709,6 @@ exports.Class = class Class extends Base
698709
for node, i in exps = child.expressions
699710
if node instanceof Value and node.isObject(true)
700711
exps[i] = compact @addProperties node, name
701-
else if node instanceof Code
702-
if @ctor
703-
throw new Error 'cannot define more than one constructor in a class'
704-
if node.bound
705-
throw new Error 'cannot define a constructor as a bound function'
706-
@ctor = node
707-
exps[i] = null
708712
child.expressions = exps = compact flatten exps
709713

710714
# Make sure that a constructor is defined for the class, and properly
@@ -743,7 +747,7 @@ exports.Class = class Class extends Base
743747
# The **Assign** is used to assign a local variable to value, or to set the
744748
# property of an object -- including within object literals.
745749
exports.Assign = class Assign extends Base
746-
(@variable, @value, @context) ->
750+
constructor: (@variable, @value, @context) ->
747751

748752
# Matchers for detecting class/method names
749753
METHOD_DEF: /^(?:(\S+)\.prototype\.|\S+?)?\b([$A-Za-z_][$\w]*)$/
@@ -860,7 +864,7 @@ exports.Assign = class Assign extends Base
860864
# When for the purposes of walking the contents of a function body, the Code
861865
# has no *children* -- they're within the inner scope.
862866
exports.Code = class Code extends Base
863-
(params, body, tag) ->
867+
constructor: (params, body, tag) ->
864868
@params = params or []
865869
@body = body or new Expressions
866870
@bound = tag is 'boundfunc'
@@ -926,7 +930,7 @@ exports.Code = class Code extends Base
926930
# these parameters can also attach themselves to the context of the function,
927931
# as well as be a splat, gathering up a group of parameters into an array.
928932
exports.Param = class Param extends Base
929-
(@name, @value, @splat) ->
933+
constructor: (@name, @value, @splat) ->
930934

931935
children: ['name', 'value']
932936

@@ -953,7 +957,7 @@ exports.Splat = class Splat extends Base
953957

954958
isAssignable: YES
955959

956-
(name) ->
960+
constructor: (name) ->
957961
@name = if name.compile then name else new Literal name
958962

959963
assigns: (name) ->
@@ -988,7 +992,7 @@ exports.Splat = class Splat extends Base
988992
# it, all other loops can be manufactured. Useful in cases where you need more
989993
# flexibility or more speed than a comprehension can provide.
990994
exports.While = class While extends Base
991-
(condition, options) ->
995+
constructor: (condition, options) ->
992996
@condition = if options?.invert then condition.invert() else condition
993997
@guard = options?.guard
994998

@@ -1038,7 +1042,7 @@ exports.While = class While extends Base
10381042
# Simple Arithmetic and logical operations. Performs some conversion from
10391043
# CoffeeScript operations into their JavaScript equivalents.
10401044
exports.Op = class Op extends Base
1041-
(op, first, second, flip) ->
1045+
constructor: (op, first, second, flip) ->
10421046
return new In first, second if op is 'in'
10431047
if op is 'new'
10441048
return first.newInstance() if first instanceof Call
@@ -1129,7 +1133,7 @@ exports.Op = class Op extends Base
11291133

11301134
#### In
11311135
exports.In = class In extends Base
1132-
(@object, @array) ->
1136+
constructor: (@object, @array) ->
11331137

11341138
children: ['object', 'array']
11351139

@@ -1164,7 +1168,7 @@ exports.In = class In extends Base
11641168

11651169
# A classic *try/catch/finally* block.
11661170
exports.Try = class Try extends Base
1167-
(@attempt, @error, @recovery, @ensure) ->
1171+
constructor: (@attempt, @error, @recovery, @ensure) ->
11681172

11691173
children: ['attempt', 'recovery', 'ensure']
11701174

@@ -1194,7 +1198,7 @@ exports.Try = class Try extends Base
11941198

11951199
# Simple node to throw an exception.
11961200
exports.Throw = class Throw extends Base
1197-
(@expression) ->
1201+
constructor: (@expression) ->
11981202

11991203
children: ['expression']
12001204

@@ -1212,7 +1216,7 @@ exports.Throw = class Throw extends Base
12121216
# similar to `.nil?` in Ruby, and avoids having to consult a JavaScript truth
12131217
# table.
12141218
exports.Existence = class Existence extends Base
1215-
(@expression) ->
1219+
constructor: (@expression) ->
12161220

12171221
children: ['expression']
12181222

@@ -1238,7 +1242,7 @@ exports.Existence = class Existence extends Base
12381242
#
12391243
# Parentheses are a good way to force any statement to become an expression.
12401244
exports.Parens = class Parens extends Base
1241-
(@expression) ->
1245+
constructor: (@expression) ->
12421246

12431247
children: ['expression']
12441248

@@ -1265,7 +1269,7 @@ exports.Parens = class Parens extends Base
12651269
# the current index of the loop as a second parameter. Unlike Ruby blocks,
12661270
# you can map and filter in a single pass.
12671271
exports.For = class For extends Base
1268-
(body, head) ->
1272+
constructor: (body, head) ->
12691273
if head.index instanceof Value
12701274
throw SyntaxError 'index cannot be a pattern matching expression'
12711275
extend this, head
@@ -1398,7 +1402,7 @@ exports.For = class For extends Base
13981402

13991403
# A JavaScript *switch* statement. Converts into a returnable expression on-demand.
14001404
exports.Switch = class Switch extends Base
1401-
(@subject, @cases, @otherwise) ->
1405+
constructor: (@subject, @cases, @otherwise) ->
14021406

14031407
children: ['subject', 'cases', 'otherwise']
14041408

@@ -1433,7 +1437,7 @@ exports.Switch = class Switch extends Base
14331437
# Single-expression **Ifs** are compiled into conditional operators if possible,
14341438
# because ternaries are already proper expressions, and don't need conversion.
14351439
exports.If = class If extends Base
1436-
(condition, @body, options = {}) ->
1440+
constructor: (condition, @body, options = {}) ->
14371441
@condition = if options.invert then condition.invert() else condition
14381442
@elseBody = null
14391443
@isChain = false

src/optparse.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports.OptionParser = class OptionParser
1313
# [short-flag, long-flag, description]
1414
#
1515
# Along with an an optional banner for the usage help.
16-
(rules, @banner) ->
16+
constructor: (rules, @banner) ->
1717
@rules = buildRules rules
1818

1919
# Parse the list of arguments, populating an `options` object with all of the

src/rewriter.coffee

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,17 @@ class exports.Rewriter
9393
# Object literals may be written with implicit braces, for simple cases.
9494
# Insert the missing braces here, so that the parser doesn't have to.
9595
addImplicitBraces: ->
96-
stack = []
97-
start = null
96+
stack = []
97+
start = null
98+
startIndent = 0
9899
condition = (token, i) ->
99100
{(i+1): one, (i+2): two, (i+3): three} = @tokens
100101
return false if 'HERECOMMENT' is one?[0]
101102
[tag] = token
102-
tag in ['TERMINATOR', 'OUTDENT'] and
103-
not (two?[0] is ':' or one?[0] is '@' and three?[0] is ':') or
104-
tag is ',' and one and
105-
one[0] not in ['IDENTIFIER', 'NUMBER', 'STRING', '@', 'TERMINATOR', 'OUTDENT', '(']
103+
(tag in ['TERMINATOR', 'OUTDENT'] and
104+
not (two?[0] is ':' or one?[0] is '@' and three?[0] is ':')) or
105+
(tag is ',' and one and
106+
one[0] not in ['IDENTIFIER', 'NUMBER', 'STRING', '@', 'TERMINATOR', 'OUTDENT', '('])
106107
action = (token, i) -> @tokens.splice i, 0, ['}', '}', token[2]]
107108
@scanTokens (token, i, tokens) ->
108109
if (tag = token[0]) in EXPRESSION_START

src/scope.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.Scope = class Scope
1717
# as well as a reference to the **Expressions** node is belongs to, which is
1818
# where it should declare its variables, and a reference to the function that
1919
# it wraps.
20-
(@parent, @expressions, @method) ->
20+
constructor:(@parent, @expressions, @method) ->
2121
@variables = [{name: 'arguments', type: 'arguments'}]
2222
@positions = {}
2323
if @parent

test/test_arguments.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ eq context.arg, 3
3535
eq context.arg.join(' '), '1 2 3'
3636

3737
class Klass
38-
(@one, @two) ->
38+
constructor: (@one, @two) ->
3939

4040
obj = new Klass 1, 2
4141

0 commit comments

Comments
 (0)