@@ -2442,7 +2442,7 @@ exports.Obj = class Obj extends Base
2442
2442
2443
2443
children : [' properties' ]
2444
2444
2445
- isAssignable : ->
2445
+ isAssignable : ( opts ) ->
2446
2446
for prop in @properties
2447
2447
# Check for reserved words.
2448
2448
message = isUnassignable prop .unwrapAll ().value
@@ -2451,7 +2451,7 @@ exports.Obj = class Obj extends Base
2451
2451
prop = prop .value if prop instanceof Assign and
2452
2452
prop .context is ' object' and
2453
2453
prop .value ? .base not instanceof Arr
2454
- return no unless prop .isAssignable ()
2454
+ return no unless prop .isAssignable opts
2455
2455
yes
2456
2456
2457
2457
shouldCache : ->
@@ -2594,7 +2594,7 @@ exports.Obj = class Obj extends Base
2594
2594
# Shorthand property with default, e.g. `{a = 1} = b`.
2595
2595
property .nestedLhs = yes
2596
2596
else if property instanceof Splat
2597
- property .lhs = yes
2597
+ property .propagateLhs yes
2598
2598
2599
2599
astNode : (o ) ->
2600
2600
@ getAndCheckSplatProps ()
@@ -2661,11 +2661,11 @@ exports.Arr = class Arr extends Base
2661
2661
return yes for obj in @objects when obj instanceof Elision
2662
2662
no
2663
2663
2664
- isAssignable : ({allowExpansion} = {}) ->
2665
- return no unless @objects .length
2664
+ isAssignable : ({allowExpansion, allowNontrailingSplat, allowEmptyArray = no } = {}) ->
2665
+ return allowEmptyArray unless @objects .length
2666
2666
2667
2667
for obj, i in @objects
2668
- return no if obj instanceof Splat and i + 1 isnt @objects .length
2668
+ return no if not allowNontrailingSplat and obj instanceof Splat and i + 1 isnt @objects .length
2669
2669
return no unless (allowExpansion and obj instanceof Expansion) or (obj .isAssignable () and (not obj .isAtomic or obj .isAtomic ()))
2670
2670
yes
2671
2671
@@ -3455,11 +3455,24 @@ exports.Assign = class Assign extends Base
3455
3455
unfoldSoak : (o ) ->
3456
3456
unfoldSoak o, this , ' variable'
3457
3457
3458
- addScopeVariables : (o , {allowAssignmentToExpansion = no } = {}) ->
3458
+ addScopeVariables : (o, {
3459
+ # During AST generation, we need to allow assignment to these constructs
3460
+ # that are considered “unassignable” during compile-to-JS, while still
3461
+ # flagging things like `[null] = b`.
3462
+ allowAssignmentToExpansion = no ,
3463
+ allowAssignmentToNontrailingSplat = no ,
3464
+ allowAssignmentToEmptyArray = no ,
3465
+ allowAssignmentToComplexSplat = no
3466
+ } = {}) ->
3459
3467
return unless not @context or @context is ' **='
3460
3468
3461
3469
varBase = @variable .unwrapAll ()
3462
- if not varBase .isAssignable allowExpansion : allowAssignmentToExpansion
3470
+ if not varBase .isAssignable {
3471
+ allowExpansion : allowAssignmentToExpansion
3472
+ allowNontrailingSplat : allowAssignmentToNontrailingSplat
3473
+ allowEmptyArray : allowAssignmentToEmptyArray
3474
+ allowComplexSplat : allowAssignmentToComplexSplat
3475
+ }
3463
3476
@variable .error " '#{ @variable .compile o} ' can't be assigned"
3464
3477
3465
3478
varBase .eachName (name) =>
@@ -3805,7 +3818,7 @@ exports.Assign = class Assign extends Base
3805
3818
variable = @variable .unwrap ()
3806
3819
if variable instanceof IdentifierLiteral and not o .scope .check variable .value
3807
3820
@ throwUnassignableConditionalError variable .value
3808
- @ addScopeVariables o, allowAssignmentToExpansion : yes
3821
+ @ addScopeVariables o, allowAssignmentToExpansion : yes , allowAssignmentToNontrailingSplat : yes , allowAssignmentToEmptyArray : yes , allowAssignmentToComplexSplat : yes
3809
3822
super o
3810
3823
3811
3824
astType : ->
@@ -4403,8 +4416,8 @@ exports.Splat = class Splat extends Base
4403
4416
4404
4417
shouldCache : -> no
4405
4418
4406
- isAssignable : ->
4407
- return no if @name instanceof Obj or @name instanceof Parens
4419
+ isAssignable : ({ allowComplexSplat = no } = {}) ->
4420
+ return allowComplexSplat if @name instanceof Obj or @name instanceof Parens
4408
4421
@name .isAssignable () and (not @name .isAtomic or @name .isAtomic ())
4409
4422
4410
4423
assigns : (name ) ->
@@ -4417,6 +4430,11 @@ exports.Splat = class Splat extends Base
4417
4430
4418
4431
unwrap : -> @name
4419
4432
4433
+ propagateLhs : (setLhs ) ->
4434
+ @lhs = yes if setLhs
4435
+ return unless @lhs
4436
+ @name .propagateLhs ? yes
4437
+
4420
4438
astType : ->
4421
4439
if @jsx
4422
4440
' JSXSpreadAttribute'
0 commit comments