@@ -1427,8 +1427,8 @@ exports.Obj = class Obj extends Base
1427
1427
1428
1428
# Check if object contains splat.
1429
1429
hasSplat : ->
1430
- splat = yes for prop in @properties when prop instanceof Splat
1431
- splat ? no
1430
+ return yes for prop in @properties when prop instanceof Splat
1431
+ no
1432
1432
1433
1433
compileNode : (o ) ->
1434
1434
props = @properties
@@ -1510,7 +1510,7 @@ exports.Obj = class Obj extends Base
1510
1510
prop .eachName iterator if prop .eachName ?
1511
1511
1512
1512
# Object spread properties. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
1513
- # `obj2 = {a: 1, obj..., c: 3, d: 4}` → `obj2 = Object.assign ({}, {a: 1}, obj, {c: 3, d: 4})`
1513
+ # `obj2 = {a: 1, obj..., c: 3, d: 4}` → `obj2 = _extends ({}, {a: 1}, obj, {c: 3, d: 4})`
1514
1514
compileSpread : (o ) ->
1515
1515
props = @properties
1516
1516
# Store object spreads.
@@ -2144,6 +2144,8 @@ exports.Assign = class Assign extends Base
2144
2144
@ checkAssignability o, name
2145
2145
if @moduleDeclaration
2146
2146
o .scope .add name .value , @moduleDeclaration
2147
+ else if @param
2148
+ o .scope .add name .value , ' var'
2147
2149
else
2148
2150
o .scope .find name .value
2149
2151
@@ -2167,31 +2169,14 @@ exports.Assign = class Assign extends Base
2167
2169
answer = compiledName .concat @ makeCode (" #{ @context or ' =' } " ), val
2168
2170
# Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assignment_without_declaration,
2169
2171
# if we’re destructuring without declaring, the destructuring assignment must be wrapped in parentheses.
2170
- if o .level > LEVEL_LIST or ( o .level is LEVEL_TOP and isValue and @variable .base instanceof Obj and not @nestedLhs and not @param )
2172
+ if o .level > LEVEL_LIST or o .level is LEVEL_TOP and isValue and @variable .base instanceof Obj and not @nestedLhs and not ( @param is yes )
2171
2173
@ wrapInParentheses answer
2172
2174
else
2173
2175
answer
2174
2176
2175
2177
# Check object destructuring variable for rest elements;
2176
2178
# can be removed once ES proposal hits Stage 4.
2177
2179
compileObjectDestruct : (o ) ->
2178
- # Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assignment_without_declaration,
2179
- # if we’re destructuring without declaring, the destructuring assignment
2180
- # must be wrapped in parentheses: `({a, b} = obj)`. Helper function
2181
- # `setScopeVar()` declares variables `a` and `b` at the top of the
2182
- # current scope.
2183
- setScopeVar = (prop ) ->
2184
- newVar = false
2185
- return if prop instanceof Assign and prop .value .base instanceof Obj
2186
- if prop instanceof Assign
2187
- if prop .value .base instanceof IdentifierLiteral
2188
- newVar = prop .value .base .compileWithoutComments o
2189
- else
2190
- newVar = prop .variable .base .compileWithoutComments o
2191
- else
2192
- newVar = prop .compileWithoutComments o
2193
- o .scope .add (newVar, ' var' , true ) if newVar
2194
-
2195
2180
# Returns a safe (cached) reference to the key for a given property
2196
2181
getPropKey = (prop ) ->
2197
2182
if prop instanceof Assign
@@ -2220,7 +2205,6 @@ exports.Assign = class Assign extends Base
2220
2205
2221
2206
for prop, index in properties
2222
2207
nestedSourceDefault = nestedSource = nestedProperties = null
2223
- setScopeVar prop .unwrap ()
2224
2208
if prop instanceof Assign
2225
2209
# prop is `k: expr`, we need to check `expr` for nested splats
2226
2210
if prop .value .isObject ? ()
@@ -2252,10 +2236,11 @@ exports.Assign = class Assign extends Base
2252
2236
restElements
2253
2237
2254
2238
# Cache the value for reuse with rest elements.
2255
- if @value .shouldCache ()
2256
- valueRefTemp = new IdentifierLiteral o .scope .freeVariable ' ref' , reserve : false
2257
- else
2258
- valueRefTemp = @value .base
2239
+ valueRefTemp =
2240
+ if @value .shouldCache ()
2241
+ new IdentifierLiteral o .scope .freeVariable ' ref' , reserve : false
2242
+ else
2243
+ @value .base
2259
2244
2260
2245
# Find all rest elements.
2261
2246
restElements = traverseRest @variable .base .properties , valueRefTemp
@@ -2266,7 +2251,7 @@ exports.Assign = class Assign extends Base
2266
2251
2267
2252
for restElement in restElements
2268
2253
value = new Call new Value (new Literal utility ' objectWithoutKeys' , o), [restElement .source , restElement .excludeProps ]
2269
- result .push new Assign restElement .name , value
2254
+ result .push new Assign new Value ( restElement .name ) , value, null , param : if @param then ' alwaysDeclare ' else null
2270
2255
2271
2256
fragments = result .compileToFragments o
2272
2257
if o .level is LEVEL_TOP
@@ -2600,7 +2585,7 @@ exports.Code = class Code extends Base
2600
2585
ifTrue = new Assign new Value (param .name ), param .value
2601
2586
exprs .push new If condition, ifTrue
2602
2587
else
2603
- exprs .push new Assign new Value (param .name ), param .asReference (o)
2588
+ exprs .push new Assign new Value (param .name ), param .asReference (o), null , param : ' alwaysDeclare '
2604
2589
2605
2590
# If this parameter comes before the splat or expansion, it will go
2606
2591
# in the function definition parameter list.
@@ -2613,25 +2598,26 @@ exports.Code = class Code extends Base
2613
2598
ref = param .asReference o
2614
2599
else
2615
2600
if param .value ? and not param .assignedInBody
2616
- ref = new Assign new Value (param .name ), param .value , null , param : yes
2601
+ ref = new Assign new Value (param .name ), param .value , null , param : ' alwaysDeclare '
2617
2602
else
2618
2603
ref = param
2619
2604
# Add this parameter’s reference(s) to the function scope.
2620
2605
if param .name instanceof Arr or param .name instanceof Obj
2621
2606
# This parameter is destructured.
2622
2607
param .name .lhs = yes
2623
- param .name .eachName (prop) ->
2624
- o .scope .parameter prop .value
2625
2608
# Compile `foo({a, b...}) ->` to `foo(arg) -> {a, b...} = arg`.
2626
2609
# Can be removed once ES proposal hits Stage 4.
2627
2610
if param .name instanceof Obj and param .name .hasSplat ()
2628
2611
splatParamName = o .scope .freeVariable ' arg'
2629
2612
o .scope .parameter splatParamName
2630
2613
ref = new Value new IdentifierLiteral splatParamName
2631
- exprs .push new Assign new Value (param .name ), ref
2614
+ exprs .push new Assign new Value (param .name ), ref, null , param : ' alwaysDeclare '
2632
2615
# Compile `foo({a, b...} = {}) ->` to `foo(arg = {}) -> {a, b...} = arg`.
2633
- if param .value ? and not param .assignedInBody
2616
+ if param .value ? and not param .assignedInBody
2634
2617
ref = new Assign ref, param .value , null , param : yes
2618
+ else unless param .shouldCache ()
2619
+ param .name .eachName (prop) ->
2620
+ o .scope .parameter prop .value
2635
2621
else
2636
2622
# This compilation of the parameter is only to get its name to add
2637
2623
# to the scope name tracking; since the compilation output here
0 commit comments