Merge pull request #3791 from lydell/issue-3502

Fix #3502: Define param variables when expansion
This commit is contained in:
Michael Ficarra 2015-01-13 20:00:32 -08:00
commit 669e7fed10
3 changed files with 19 additions and 7 deletions

View File

@ -1874,11 +1874,9 @@
}
_ref5 = this.params;
for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) {
p = _ref5[_k].name;
if (!(param instanceof Expansion)) {
if (p.value) {
o.scope.add(p.value, 'var', true);
}
p = _ref5[_k];
if (!(p instanceof Expansion) && p.name.value) {
o.scope.add(p.name.value, 'var', true);
}
}
splats = new Assign(new Value(new Arr((function() {

View File

@ -1360,8 +1360,8 @@ exports.Code = class Code extends Base
for param in @params when param not instanceof Expansion
o.scope.parameter param.asReference o
for param in @params when param.splat or param instanceof Expansion
for {name: p} in @params when param not instanceof Expansion
if p.value then o.scope.add p.value, 'var', yes
for p in @params when p not instanceof Expansion and p.name.value
o.scope.add p.name.value, 'var', yes
splats = new Assign new Value(new Arr(p.asReference o for p in @params)),
new Value new Literal 'arguments'
break

View File

@ -198,6 +198,20 @@ test "#156: parameter lists with expansion in array destructuring", ->
last
eq 3, expandArray 1, 2, 3, [1, 2, 3]
test "#3502: variable definitions and expansion", ->
a = b = 0
f = (a, ..., b) -> [a, b]
arrayEq [1, 5], f 1, 2, 3, 4, 5
eq 0, a
eq 0, b
test "variable definitions and splat", ->
a = b = 0
f = (a, middle..., b) -> [a, middle, b]
arrayEq [1, [2, 3, 4], 5], f 1, 2, 3, 4, 5
eq 0, a
eq 0, b
test "default values with function calls", ->
doesNotThrow -> CoffeeScript.compile "(x = f()) ->"