Skip to content

Commit 1d3af8c

Browse files
committed
Simplify fix for #4464
This uses more of the existing machinery for moving class body expressions into the initializer.
1 parent becdf50 commit 1d3af8c

File tree

2 files changed

+35
-58
lines changed

2 files changed

+35
-58
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,16 +1636,17 @@ exports.Class = class Class extends Base
16361636
super()
16371637

16381638
compileNode: (o) ->
1639-
@name = @determineName()
1640-
@walkBody()
1639+
@name = @determineName()
1640+
executableBody = @walkBody()
16411641

16421642
# Special handling to allow `class expr.A extends A` declarations
16431643
parentName = @parent.base.value if @parent instanceof Value and not @parent.hasProperties()
16441644
@hasNameClash = @name? and @name is parentName
16451645

16461646
node = @
1647-
if @executableBody or @hasNameClash
1648-
node = new ExecutableClassBody node, @executableBody
1647+
1648+
if executableBody or @hasNameClash
1649+
node = new ExecutableClassBody node, executableBody
16491650
else if not @name? and o.level is LEVEL_TOP
16501651
# Anonymous classes are only valid in expressions
16511652
node = new Parens node
@@ -1665,7 +1666,7 @@ exports.Class = class Class extends Base
16651666

16661667
compileClassDeclaration: (o) ->
16671668
@ctor ?= @makeDefaultConstructor() if @externalCtor or @boundMethods.length
1668-
@ctor?.noReturn = yes
1669+
@ctor?.noReturn = true
16691670

16701671
@proxyBoundMethods() if @boundMethods.length
16711672

@@ -1677,14 +1678,10 @@ exports.Class = class Class extends Base
16771678
result.push @makeCode('extends '), @parent.compileToFragments(o)..., @makeCode ' ' if @parent
16781679

16791680
result.push @makeCode '{'
1680-
unless @passthroughBody.isEmpty() and @body.isEmpty()
1681-
@body.spaced = yes
1681+
unless @body.isEmpty()
1682+
@body.spaced = true
16821683
result.push @makeCode '\n'
1683-
unless @passthroughBody.isEmpty()
1684-
result.push @passthroughBody.compileToFragments(o, LEVEL_TOP)...
1685-
result.push @makeCode '\n\n' unless @body.isEmpty()
1686-
unless @body.isEmpty()
1687-
result.push @body.compileToFragments(o, LEVEL_TOP)...
1684+
result.push @body.compileToFragments(o, LEVEL_TOP)...
16881685
result.push @makeCode "\n#{@tab}"
16891686
result.push @makeCode '}'
16901687

@@ -1707,21 +1704,21 @@ exports.Class = class Class extends Base
17071704
if name in JS_FORBIDDEN then "_#{name}" else name
17081705

17091706
walkBody: ->
1710-
@ctor = null
1711-
@boundMethods = []
1707+
@ctor = null
1708+
@boundMethods = []
1709+
executableBody = null
17121710

1713-
initializer = []
1714-
passthroughBodyExpressions = []
1715-
{ expressions } = @body
1711+
initializer = []
1712+
{ expressions } = @body
17161713

17171714
i = 0
1718-
for expression, expressionIndex in expressions.slice()
1719-
if expression instanceof Value and expression.isObject yes
1715+
for expression in expressions.slice()
1716+
if expression instanceof Value and expression.isObject true
17201717
{ properties } = expression.base
17211718
exprs = []
17221719
end = 0
17231720
start = 0
1724-
pushSlice = -> exprs.push new Value new Obj properties[start...end], yes if end > start
1721+
pushSlice = -> exprs.push new Value new Obj properties[start...end], true if end > start
17251722

17261723
while assign = properties[end]
17271724
if initializerExpression = @addInitializerExpression assign
@@ -1734,9 +1731,6 @@ exports.Class = class Class extends Base
17341731

17351732
expressions[i..i] = exprs
17361733
i += exprs.length
1737-
else if expression instanceof Value and expression.base instanceof PassthroughLiteral
1738-
passthroughBodyExpressions.push expression
1739-
expressions.splice expressionIndex, 1
17401734
else
17411735
if initializerExpression = @addInitializerExpression expression
17421736
initializer.push initializerExpression
@@ -1752,19 +1746,15 @@ exports.Class = class Class extends Base
17521746
else if method.bound
17531747
@boundMethods.push method
17541748

1755-
@passthroughBody = new Block passthroughBodyExpressions
1756-
17571749
if initializer.length isnt expressions.length
17581750
@body.expressions = (expression.hoist() for expression in initializer)
1759-
@executableBody = new Block expressions
1760-
1751+
new Block expressions
17611752

17621753
# Add an expression to the class initializer
1763-
#
1764-
# NOTE Currently, only methods and static methods are valid in ES class initializers.
1765-
# When additional expressions become valid, this method should be updated to handle them.
17661754
addInitializerExpression: (node) ->
1767-
if @validInitializerMethod node
1755+
if node.unwrapAll() instanceof PassthroughLiteral
1756+
node
1757+
else if @validInitializerMethod node
17681758
@addInitializerMethod node
17691759
else
17701760
null

0 commit comments

Comments
 (0)