Skip to content

Commit 3a17c53

Browse files
committed
improvements
1 parent d6614b9 commit 3a17c53

File tree

3 files changed

+21
-49
lines changed

3 files changed

+21
-49
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

+5-10
Original file line numberDiff line numberDiff line change
@@ -1494,9 +1494,10 @@ exports.Obj = class Obj extends Base
14941494
# `foo = ({a, rest..., b}) ->` -> `foo = {a, b, rest...}) ->`
14951495
reorderProperties: ->
14961496
props = @properties
1497-
splatProps = (prop for prop in props when prop instanceof Splat)
1498-
objProps = (prop for prop in props when prop not instanceof Splat)
1499-
@objects = @properties = [].concat objProps, splatProps
1497+
splatProps = (x for prop, x in props when prop instanceof Splat)
1498+
props[splatProps[1]].error "multiple spread elements are disallowed" if splatProps?.length > 1
1499+
splatProp = props.splice splatProps[0], 1
1500+
@objects = @properties = [].concat props, splatProp
15001501

15011502
compileNode: (o) ->
15021503
@reorderProperties() if @hasSplat() and @lhs
@@ -1511,10 +1512,6 @@ exports.Obj = class Obj extends Base
15111512
# CSX attributes <div id="val" attr={aaa} {props...} />
15121513
return @compileCSXAttributes o if @csx
15131514

1514-
if @hasSplat() and @lhs
1515-
splatProps = (ix for prop, ix in props when prop instanceof Splat)
1516-
props[splatProps[1]].error "multiple spread elements are disallowed" if splatProps?.length > 1
1517-
15181515
# If this object is the left-hand side of an assignment, all its children
15191516
# are too.
15201517
if @lhs
@@ -2200,9 +2197,7 @@ exports.Assign = class Assign extends Base
22002197
# know that, so that those nodes know that they’re assignable as
22012198
# destructured variables.
22022199
@variable.base.lhs = yes
2203-
# Check if @variable contains Obj with splats.
2204-
hasSplat = @variable.contains (node) -> node instanceof Obj and node.hasSplat()
2205-
return @compileDestructuring o if not @variable.isAssignable() or @variable.isArray() and hasSplat
2200+
return @compileDestructuring o if not @variable.isAssignable()
22062201

22072202
return @compileSplice o if @variable.isSplice()
22082203
return @compileConditional o if @context in ['||=', '&&=', '?=']

test/object_rest_spread.coffee

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ test "#4798 destructuring of objects with splat within arrays", ->
99
eq q.b, r.b
1010
eq q.a, a
1111

12+
arr2 = [arr[1]]
13+
[{a2...}] = arr2
14+
eq a2.a, arr2[0].a
15+
1216
test "destructuring assignment with objects and splats: ES2015", ->
1317
obj = {a: 1, b: 2, c: 3, d: 4, e: 5}
1418
throws (-> CoffeeScript.compile "{a, r..., s...} = x"), null, "multiple rest elements are disallowed"

0 commit comments

Comments
 (0)