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

Skip to content

Commit 2aedbc2

Browse files
committed
Back to naked constructors.
1 parent f0b73dc commit 2aedbc2

12 files changed

Lines changed: 74 additions & 82 deletions

File tree

documentation/coffee/classes.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Animal
2-
constructor: (@name) ->
2+
(@name) ->
33

44
move: (meters) ->
55
alert @name + " moved " + meters + "m."

examples/code.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ aliquam erat volutpat. Ut wisi enim ad."
135135

136136
# Inheritance and calling super.
137137
class Animal
138-
constructor: (@name) ->
138+
(@name) ->
139139

140140
move: (meters) ->
141141
alert this.name + " moved " + meters + "m."

examples/computer_science/linked_list.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# "Classic" linked list implementation that doesn't keep track of its size.
22
class LinkedList
33

4-
constructor: ->
4+
->
55
this._head = null # Pointer to the first item in the list.
66

77

examples/potion.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ print p.name
7171
# Policeman ('Constable') print
7272

7373
class Policeman extends Person
74-
constructor: (@rank) ->
74+
(@rank) ->
7575

7676
print: ->
7777
print 'My name is ' + @name + " and I'm a " + @rank + '.'

lib/nodes.js

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

src/nodes.coffee

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ exports.Expressions = class Expressions extends Base
157157

158158
children: ['expressions']
159159

160-
constructor: (nodes) ->
160+
(nodes) ->
161161
@expressions = compact flatten nodes or []
162162

163163
# Tack an expression on to the end of this expression list.
@@ -257,7 +257,7 @@ exports.Expressions = class Expressions extends Base
257257
# `true`, `false`, `null`...
258258
exports.Literal = class Literal extends Base
259259

260-
constructor: (@value) ->
260+
(@value) ->
261261

262262
makeReturn: ->
263263
if @isPureStatement() then this else new Return this
@@ -292,7 +292,7 @@ exports.Return = class Return extends Base
292292
isStatement : YES
293293
isPureStatement: YES
294294

295-
constructor: (@expression) ->
295+
(@expression) ->
296296

297297
makeReturn: THIS
298298

@@ -313,7 +313,7 @@ exports.Value = class Value extends Base
313313
children: ['base', 'properties']
314314

315315
# A **Value** has a base and a list of property accesses.
316-
constructor: (base, props, tag) ->
316+
(base, props, tag) ->
317317
return base if not props and base instanceof Value
318318
@base = base
319319
@properties = props or []
@@ -404,7 +404,7 @@ exports.Value = class Value extends Base
404404
# at the same position.
405405
exports.Comment = class Comment extends Base
406406

407-
constructor: (@comment) ->
407+
(@comment) ->
408408

409409
isPureStatement: YES
410410
isStatement: YES
@@ -423,7 +423,7 @@ exports.Call = class Call extends Base
423423

424424
children: ['variable', 'args']
425425

426-
constructor: (variable, @args = [], @soak) ->
426+
(variable, @args = [], @soak) ->
427427
@isNew = false
428428
@isSuper = variable is 'super'
429429
@variable = if @isSuper then null else variable
@@ -526,7 +526,7 @@ exports.Extends = class Extends extends Base
526526

527527
children: ['child', 'parent']
528528

529-
constructor: (@child, @parent) ->
529+
(@child, @parent) ->
530530

531531
# Hooks one constructor into another's prototype chain.
532532
compile: (o) ->
@@ -541,7 +541,7 @@ exports.Accessor = class Accessor extends Base
541541

542542
children: ['name']
543543

544-
constructor: (@name, tag) ->
544+
(@name, tag) ->
545545
@proto = if tag is 'proto' then '.prototype' else ''
546546
@soak = tag is 'soak'
547547

@@ -558,7 +558,7 @@ exports.Index = class Index extends Base
558558

559559
children: ['index']
560560

561-
constructor: (@index) ->
561+
(@index) ->
562562

563563
compile: (o) ->
564564
(if @proto then '.prototype' else '') + "[#{ @index.compile o, LEVEL_PAREN }]"
@@ -573,7 +573,7 @@ exports.Obj = class Obj extends Base
573573

574574
children: ['properties']
575575

576-
constructor: (props, @generated = false) ->
576+
(props, @generated = false) ->
577577
@objects = @properties = props or []
578578

579579
compileNode: (o) ->
@@ -637,7 +637,7 @@ exports.Arr = class Arr extends Base
637637

638638
children: ['objects']
639639

640-
constructor: (objs) ->
640+
(objs) ->
641641
@objects = objs or []
642642

643643
compileNode: (o) ->
@@ -664,7 +664,7 @@ exports.Class = class Class extends Base
664664

665665
# Initialize a **Class** with its name, an optional superclass, and a
666666
# list of prototype property assignments.
667-
constructor: (@variable, @parent, @body = new Expressions) ->
667+
(@variable, @parent, @body = new Expressions) ->
668668

669669
# Instead of generating the JavaScript string directly, we build up the
670670
# equivalent syntax tree and compile that, in pieces. You can see the
@@ -695,28 +695,24 @@ exports.Class = class Class extends Base
695695
base = assign.variable.base
696696
delete assign.context
697697
func = assign.value
698-
if base.value is 'constructor'
699-
if ctor
700-
throw new Error 'cannot define more than one constructor in a class'
701-
if func.bound
702-
throw new Error 'cannot define a constructor as a bound function'
703-
if func instanceof Code
704-
ctor = func
705-
else
706-
ctor = new Assign(new Value(lname), func)
707-
assign = null
708-
else
709-
assign.variable = new Value(lname, [new Accessor(base, 'proto')])
710-
if func instanceof Code and func.bound
711-
boundFuncs.push base
712-
func.bound = no
698+
assign.variable = new Value(lname, [new Accessor(base, 'proto')])
699+
if func instanceof Code and func.bound
700+
boundFuncs.push base
701+
func.bound = no
713702
assign
714703

715704
boundFuncs = []
716705
others = []
717706
for node, i in exps = @body.expressions
718707
if node instanceof Value and node.isObject(true)
719708
exps[i] = compact convert node
709+
else if node instanceof Code
710+
if ctor
711+
throw new Error 'cannot define more than one constructor in a class'
712+
if node.bound
713+
throw new Error 'cannot define a constructor as a bound function'
714+
ctor = node
715+
exps[i] = null
720716
else
721717
others.push node
722718

@@ -729,7 +725,7 @@ exports.Class = class Class extends Base
729725
n2.expressions[j] = compact convert expr2
730726
n2.expressions = flatten n2.expressions
731727

732-
@body.expressions = exps = flatten exps
728+
@body.expressions = exps = compact flatten exps
733729
unless ctor
734730
ctor = new Code
735731
if @parent
@@ -762,7 +758,7 @@ exports.Assign = class Assign extends Base
762758

763759
children: ['variable', 'value']
764760

765-
constructor: (@variable, @value, @context) ->
761+
(@variable, @value, @context) ->
766762

767763
assigns: (name) ->
768764
@[if @context is 'object' then 'value' else 'variable'].assigns name
@@ -877,7 +873,7 @@ exports.Code = class Code extends Base
877873

878874
children: ['params', 'body']
879875

880-
constructor: (params, body, tag) ->
876+
(params, body, tag) ->
881877
@params = params or []
882878
@body = body or new Expressions
883879
@bound = tag is 'boundfunc'
@@ -944,7 +940,7 @@ exports.Param = class Param extends Base
944940

945941
children: ['name', 'value']
946942

947-
constructor: (@name, @value, @splat) ->
943+
(@name, @value, @splat) ->
948944

949945
compile: (o) ->
950946
@name.compile o, LEVEL_LIST
@@ -969,7 +965,7 @@ exports.Splat = class Splat extends Base
969965

970966
isAssignable: YES
971967

972-
constructor: (name) ->
968+
(name) ->
973969
@name = if name.compile then name else new Literal name
974970

975971
assigns: (name) ->
@@ -1009,7 +1005,7 @@ exports.While = class While extends Base
10091005

10101006
isStatement: YES
10111007

1012-
constructor: (condition, options) ->
1008+
(condition, options) ->
10131009
@condition = if options?.invert then condition.invert() else condition
10141010
@guard = options?.guard
10151011

@@ -1073,7 +1069,7 @@ exports.Op = class Op extends Base
10731069

10741070
children: ['first', 'second']
10751071

1076-
constructor: (op, first, second, flip) ->
1072+
(op, first, second, flip) ->
10771073
return new In first, second if op is 'in'
10781074
if op is 'new'
10791075
return first.newInstance() if first instanceof Call
@@ -1150,7 +1146,7 @@ exports.In = class In extends Base
11501146

11511147
invert: NEGATE
11521148

1153-
constructor: (@object, @array) ->
1149+
(@object, @array) ->
11541150

11551151
compileNode: (o) ->
11561152
if @array instanceof Value and @array.isArray()
@@ -1186,7 +1182,7 @@ exports.Try = class Try extends Base
11861182

11871183
isStatement: YES
11881184

1189-
constructor: (@attempt, @error, @recovery, @ensure) ->
1185+
(@attempt, @error, @recovery, @ensure) ->
11901186

11911187
makeReturn: ->
11921188
@attempt = @attempt .makeReturn() if @attempt
@@ -1217,7 +1213,7 @@ exports.Throw = class Throw extends Base
12171213

12181214
isStatement: YES
12191215

1220-
constructor: (@expression) ->
1216+
(@expression) ->
12211217

12221218
# A **Throw** is already a return, of sorts...
12231219
makeReturn: THIS
@@ -1236,7 +1232,7 @@ exports.Existence = class Existence extends Base
12361232

12371233
invert: NEGATE
12381234

1239-
constructor: (@expression) ->
1235+
(@expression) ->
12401236

12411237
compileNode: (o) ->
12421238
code = @expression.compile o, LEVEL_OP
@@ -1261,7 +1257,7 @@ exports.Parens = class Parens extends Base
12611257

12621258
children: ['expression']
12631259

1264-
constructor: (@expression) ->
1260+
(@expression) ->
12651261

12661262
unwrap : -> @expression
12671263
isComplex : -> @expression.isComplex()
@@ -1291,7 +1287,7 @@ exports.For = class For extends Base
12911287

12921288
isStatement: YES
12931289

1294-
constructor: (body, head) ->
1290+
(body, head) ->
12951291
if head.index instanceof Value
12961292
throw SyntaxError 'index cannot be a pattern matching expression'
12971293
extend this, head
@@ -1425,7 +1421,7 @@ exports.Switch = class Switch extends Base
14251421

14261422
isStatement: YES
14271423

1428-
constructor: (@subject, @cases, @otherwise) ->
1424+
(@subject, @cases, @otherwise) ->
14291425

14301426
makeReturn: ->
14311427
pair[1].makeReturn() for pair in @cases
@@ -1459,7 +1455,7 @@ exports.If = class If extends Base
14591455

14601456
children: ['condition', 'body', 'elseBody']
14611457

1462-
constructor: (condition, @body, options = {}) ->
1458+
(condition, @body, options = {}) ->
14631459
@condition = if options.invert then condition.invert() else condition
14641460
@elseBody = null
14651461
@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-
constructor: (rules, @banner) ->
16+
(rules, @banner) ->
1717
@rules = buildRules rules
1818

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

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-
constructor: (@parent, @expressions, @method) ->
20+
(@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-
constructor: (@one, @two) ->
38+
(@one, @two) ->
3939

4040
obj = new Klass 1, 2
4141

0 commit comments

Comments
 (0)