Skip to content

Commit e7f1b07

Browse files
committed
merge branch 'master' into 'object_rest_spread'
2 parents 2f6b13f + 794f65f commit e7f1b07

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

+10-10
Original file line numberDiff line numberDiff line change
@@ -2305,8 +2305,8 @@ exports.Assign = class Assign extends Base
23052305
# Sort 'splatsAndExpans' so we can show error at first disallowed token.
23062306
objects[splatsAndExpans.sort()[1]].error "multiple splats/expansions are disallowed in an assignment"
23072307

2308-
isSplat = splats.length
2309-
isExpans = expans.length
2308+
isSplat = splats?.length > 0
2309+
isExpans = expans?.length > 0
23102310
isObject = @variable.isObject()
23112311
isArray = @variable.isArray()
23122312

@@ -2355,7 +2355,7 @@ exports.Assign = class Assign extends Base
23552355

23562356
# "Complex" `objects` are processed in a loop.
23572357
# Examples: [a, b, {c, r...}, d], [a, ..., {b, r...}, c, d]
2358-
loopObjects = (objs, vvarTxt) =>
2358+
loopObjects = (objs, vvar, vvarTxt) =>
23592359
objSpreads = hasObjSpreads objs
23602360
for obj, i in objs
23612361
# `Elision` can be skipped.
@@ -2385,16 +2385,16 @@ exports.Assign = class Assign extends Base
23852385
assigns.push new Assign(vvar, vval, null, param: @param, subpattern: yes).compileToFragments o, LEVEL_LIST
23862386

23872387
# "Simple" `objects` can be split and compiled to arrays, [a, b, c] = arr, [a, b, c...] = arr
2388-
assignObjects = (objs, vvarTxt) =>
2388+
assignObjects = (objs, vvar, vvarTxt) =>
23892389
vvar = new Value new Arr(objs, yes)
23902390
vval = if vvarTxt instanceof Value then vvarTxt else new Value new Literal(vvarTxt)
23912391
assigns.push new Assign(vvar, vval, null, param: @param, subpattern: yes).compileToFragments o, LEVEL_LIST
23922392

2393-
processObjects = (objs, vvarTxt) ->
2393+
processObjects = (objs, vvar, vvarTxt) ->
23942394
if complexObjects objs
2395-
loopObjects objs, vvarTxt
2395+
loopObjects objs, vvar, vvarTxt
23962396
else
2397-
assignObjects objs, vvarTxt
2397+
assignObjects objs, vvar, vvarTxt
23982398

23992399
# In case there is `Splat` or `Expansion` in `objects`,
24002400
# we can split array in two simple subarrays.
@@ -2411,7 +2411,7 @@ exports.Assign = class Assign extends Base
24112411
expIdx = splatsAndExpans[0]
24122412
leftObjs = objects.slice 0, expIdx + (if isSplat then 1 else 0)
24132413
rightObjs = objects.slice expIdx + 1
2414-
processObjects leftObjs, vvarText if leftObjs.length isnt 0
2414+
processObjects leftObjs, vvar, vvarText if leftObjs.length isnt 0
24152415
if rightObjs.length isnt 0
24162416
# Slice or splice `objects`.
24172417
refExp = switch
@@ -2421,10 +2421,10 @@ exports.Assign = class Assign extends Base
24212421
restVar = refExp
24222422
refExp = o.scope.freeVariable 'ref'
24232423
assigns.push [@makeCode(refExp + ' = '), restVar.compileToFragments(o, LEVEL_LIST)...]
2424-
processObjects rightObjs, refExp
2424+
processObjects rightObjs, vvar, refExp
24252425
else
24262426
# There is no `Splat` or `Expansion` in `objects`.
2427-
processObjects objects, vvarText
2427+
processObjects objects, vvar, vvarText
24282428
assigns.push vvar unless top or @subpattern
24292429
fragments = @joinFragmentArrays assigns, ', '
24302430
if o.level < LEVEL_LIST then fragments else @wrapInParentheses fragments

test/assignment.coffee

+41
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,44 @@ test "#4673: complex destructured object spread variables", ->
927927

928928
# {{g}...} = g: 1
929929
# eq g, 1
930+
931+
test "#4878: Compile error when using destructuring with a splat or expansion in an array", ->
932+
arr = ['a', 'b', 'c', 'd']
933+
934+
f1 = (list) ->
935+
[first, ..., last] = list
936+
937+
f2 = (list) ->
938+
[first..., last] = list
939+
940+
f3 = (list) ->
941+
([first, ...] = list); first
942+
943+
f4 = (list) ->
944+
([first, ...rest] = list); rest
945+
946+
arrayEq f1(arr), arr
947+
arrayEq f2(arr), arr
948+
arrayEq f3(arr), 'a'
949+
arrayEq f4(arr), ['b', 'c', 'd']
950+
951+
foo = (list) ->
952+
ret =
953+
if list?.length > 0
954+
[first, ..., last] = list
955+
[first, last]
956+
else
957+
[]
958+
959+
arrayEq foo(arr), ['a', 'd']
960+
961+
bar = (list) ->
962+
ret =
963+
if list?.length > 0
964+
[first, ...rest] = list
965+
[first, rest]
966+
else
967+
[]
968+
969+
arrayEq bar(arr), ['a', ['b', 'c', 'd']]
970+
>>>>>>> 794f65fbd74ee9f3839a5f715116e71cf23e24c0

0 commit comments

Comments
 (0)