Skip to content

Commit 05d45e9

Browse files
helixbassGeoffreyBooth
authored andcommitted
astAsBlockIfNeeded(); eachName checkAssignability (#5258)
👍
1 parent 69521b9 commit 05d45e9

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,10 +1454,10 @@ exports.Value = class Value extends Base
14541454
return new If new Existence(fst), snd, soak: on
14551455
no
14561456

1457-
eachName: (iterator) ->
1457+
eachName: (iterator, {checkAssignability = yes} = {}) ->
14581458
if @hasProperties()
14591459
iterator @
1460-
else if @base.isAssignable()
1460+
else if not checkAssignability or @base.isAssignable()
14611461
@base.eachName iterator
14621462
else
14631463
@error 'tried to assign to unassignable value'
@@ -1654,7 +1654,7 @@ exports.JSXExpressionContainer = class JSXExpressionContainer extends Base
16541654

16551655
astProperties: (o) ->
16561656
return
1657-
expression: @expression.ast o
1657+
expression: astAsBlockIfNeeded @expression, o
16581658

16591659
exports.JSXEmptyExpression = class JSXEmptyExpression extends Base
16601660

@@ -5071,7 +5071,7 @@ exports.StringWithInterpolations = class StringWithInterpolations extends Base
50715071
emptyInterpolation
50725072
else
50735073
expression.unwrapAll()
5074-
expressions.push node.ast o
5074+
expressions.push astAsBlockIfNeeded node, o
50755075

50765076
{expressions, quasis, @quote}
50775077

@@ -5253,8 +5253,8 @@ exports.For = class For extends While
52535253
addToScope = (name) ->
52545254
alreadyDeclared = o.scope.find name.value
52555255
name.isDeclaration = not alreadyDeclared
5256-
@name?.eachName addToScope
5257-
@index?.eachName addToScope
5256+
@name?.eachName addToScope, checkAssignability: no
5257+
@index?.eachName addToScope, checkAssignability: no
52585258
super o
52595259

52605260
astType: -> 'For'
@@ -5702,6 +5702,14 @@ sniffDirectives = (expressions, {notFinalExpression} = {}) ->
57025702
.withLocationDataFrom expression
57035703
index++
57045704
5705+
astAsBlockIfNeeded = (node, o) ->
5706+
unwrapped = node.unwrap()
5707+
if unwrapped instanceof Block and unwrapped.expressions.length > 1
5708+
unwrapped.makeReturn null, yes
5709+
unwrapped.ast o, LEVEL_TOP
5710+
else
5711+
node.ast o, LEVEL_PAREN
5712+
57055713
# Helpers for `mergeLocationData` and `mergeAstLocationData` below.
57065714
lesser = (a, b) -> if a < b then a else b
57075715
greater = (a, b) -> if a > b then a else b

test/abstract_syntax_tree.coffee

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,28 @@ test "AST as expected for JSXTag node", ->
613613
type: 'JSXIdentifier'
614614
name: 'a'
615615

616+
testExpression '''
617+
<div b={
618+
c
619+
d
620+
} />
621+
''',
622+
type: 'JSXElement'
623+
openingElement:
624+
attributes: [
625+
value:
626+
type: 'JSXExpressionContainer'
627+
expression:
628+
type: 'BlockStatement'
629+
body: [
630+
type: 'ExpressionStatement'
631+
,
632+
type: 'ExpressionStatement'
633+
expression:
634+
returns: yes
635+
]
636+
]
637+
616638
test "AST as expected for ComputedPropertyName node", ->
617639
testExpression '[fn]: ->',
618640
type: 'ObjectExpression'
@@ -3335,6 +3357,27 @@ test "AST as expected for StringWithInterpolations node", ->
33353357
]
33363358
quote: '"'
33373359

3360+
testExpression '''
3361+
a "#{
3362+
b
3363+
c
3364+
}"
3365+
''',
3366+
type: 'CallExpression'
3367+
arguments: [
3368+
type: 'TemplateLiteral'
3369+
expressions: [
3370+
type: 'BlockStatement'
3371+
body: [
3372+
type: 'ExpressionStatement'
3373+
,
3374+
type: 'ExpressionStatement'
3375+
expression:
3376+
returns: yes
3377+
]
3378+
]
3379+
]
3380+
33383381
test "AST as expected for For node", ->
33393382
testStatement 'for x, i in arr when x? then return',
33403383
type: 'For'
@@ -3544,6 +3587,14 @@ test "AST as expected for For node", ->
35443587
postfix: no
35453588
await: no
35463589

3590+
testStatement '''
3591+
for [x..., y] in z
3592+
y()
3593+
''',
3594+
type: 'For'
3595+
name:
3596+
type: 'ArrayPattern'
3597+
35473598
# # TODO: Figure out the purpose of `pattern` and `returns`.
35483599

35493600
test "AST as expected for Switch node", ->

0 commit comments

Comments
 (0)