Skip to content

Commit cbd5fe2

Browse files
committed
destructuring assignment with empty array in object
1 parent 746b0c7 commit cbd5fe2

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

lib/coffeescript/nodes.js

+9-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

+8-2
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,13 @@ exports.Assign = class Assign extends Base
22152215
@variable.base.lhs = yes
22162216
# Check if @variable contains Obj with splats.
22172217
hasSplat = @variable.contains (node) -> node instanceof Obj and node.hasSplat()
2218-
return @compileDestructuring o if not @variable.isAssignable() or @variable.isArray() and hasSplat
2218+
# Check if `@variable` containes an empty array as a property, e.g. `{a:[]} = b`.
2219+
# An empty array is not assignable and would invoke `@compileDestructuring` method,
2220+
# which is not needed since this is valid ES syntax and can be compiled directly.
2221+
hasArrayProp = @variable.contains (node) ->
2222+
node instanceof Assign and node.context is 'object' and node.value.base instanceof Arr
2223+
if (not @variable.isAssignable() or @variable.isArray() and hasSplat) and not hasArrayProp
2224+
return @compileDestructuring o
22192225
# Object destructuring. Can be removed once ES proposal hits Stage 4.
22202226
objDestructAnswer = @compileObjectDestruct(o) if @variable.isObject() and hasSplat
22212227
return objDestructAnswer if objDestructAnswer
@@ -2226,7 +2232,7 @@ exports.Assign = class Assign extends Base
22262232

22272233
unless @context
22282234
varBase = @variable.unwrapAll()
2229-
unless varBase.isAssignable()
2235+
unless varBase.isAssignable() or hasArrayProp
22302236
@variable.error "'#{@variable.compile o}' can't be assigned"
22312237

22322238
varBase.eachName (name) =>

test/assignment.coffee

+18
Original file line numberDiff line numberDiff line change
@@ -985,3 +985,21 @@ test "#4878: Compile error when using destructuring with a splat or expansion in
985985
[]
986986

987987
arrayEq bar(arr), ['a', ['b', 'c', 'd']]
988+
989+
test "destructuring assignment with empty array in object", ->
990+
obj =
991+
a1: [1, 2]
992+
b1: 3
993+
994+
{a1:[], b1} = obj
995+
eq 'undefined', typeof a1
996+
eq b1, 3
997+
998+
obj =
999+
a2:
1000+
b2: [1, 2]
1001+
c2: 3
1002+
1003+
{a2: {b2:[]}, c2} = obj
1004+
eq 'undefined', typeof b2
1005+
eq c2, 3

0 commit comments

Comments
 (0)