Skip to content

Commit 4c0363f

Browse files
zdenkoGeoffreyBooth
authored andcommitted
Fix #5004: incorrect compiled code when a destructuring array contains accessors (#5005)
1 parent 746b0c7 commit 4c0363f

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

+3-2
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,8 @@ exports.Assign = class Assign extends Base
24262426
vvarText = ref
24272427

24282428
slicer = (type) -> (vvar, start, end = no) ->
2429-
args = [new IdentifierLiteral(vvar), new NumberLiteral(start)]
2429+
vvar = new IdentifierLiteral vvar unless vvar instanceof Value
2430+
args = [vvar, new NumberLiteral(start)]
24302431
args.push new NumberLiteral end if end
24312432
slice = new Value (new IdentifierLiteral utility type, o), [new Access new PropertyName 'call']
24322433
new Value new Call slice, args
@@ -2517,7 +2518,7 @@ exports.Assign = class Assign extends Base
25172518
if rightObjs.length isnt 0
25182519
# Slice or splice `objects`.
25192520
refExp = switch
2520-
when isSplat then compSplice objects[expIdx].unwrapAll().value, rightObjs.length * -1
2521+
when isSplat then compSplice new Value(objects[expIdx].name), rightObjs.length * -1
25212522
when isExpans then compSlice vvarText, rightObjs.length * -1
25222523
if complexObjects rightObjs
25232524
restVar = refExp

test/assignment.coffee

+32-2
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ test "#4878: Compile error when using destructuring with a splat or expansion in
959959
([first, ...] = list); first
960960

961961
f4 = (list) ->
962-
([first, ...rest] = list); rest
962+
([first, rest...] = list); rest
963963

964964
arrayEq f1(arr), arr
965965
arrayEq f2(arr), arr
@@ -979,9 +979,39 @@ test "#4878: Compile error when using destructuring with a splat or expansion in
979979
bar = (list) ->
980980
ret =
981981
if list?.length > 0
982-
[first, ...rest] = list
982+
[first, rest...] = list
983983
[first, rest]
984984
else
985985
[]
986986

987987
arrayEq bar(arr), ['a', ['b', 'c', 'd']]
988+
989+
990+
test "#5004: array destructuring with accessors", ->
991+
obj =
992+
arr: ['a', 'b', 'c', 'd']
993+
list: {}
994+
f1: ->
995+
[@first, @rest...] = @arr
996+
f2: ->
997+
[@second, @third..., @last] = @rest
998+
f3: ->
999+
[@list.a, @list.middle..., @list.d] = @arr
1000+
1001+
obj.f1()
1002+
eq obj.first, 'a'
1003+
arrayEq obj.rest, ['b', 'c', 'd']
1004+
1005+
obj.f2()
1006+
eq obj.second, 'b'
1007+
arrayEq obj.third, ['c']
1008+
eq obj.last, 'd'
1009+
1010+
obj.f3()
1011+
eq obj.list.a, 'a'
1012+
arrayEq obj.list.middle, ['b', 'c']
1013+
eq obj.list.d, 'd'
1014+
1015+
[obj.list.middle..., d] = obj.arr
1016+
eq d, 'd'
1017+
arrayEq obj.list.middle, ['a', 'b', 'c']

0 commit comments

Comments
 (0)