mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Merge pull request #1346 from adam-f/fixedsplatscope
Fixed improper scoping of siblings to the splat argument.
This commit is contained in:
commit
004f13f0fc
4 changed files with 32 additions and 15 deletions
32
lib/nodes.js
32
lib/nodes.js
|
@ -1207,7 +1207,9 @@
|
|||
acc = IDENTIFIER.test(idx.unwrap().value || 0);
|
||||
value = new Value(value);
|
||||
value.properties.push(new (acc ? Access : Index)(idx));
|
||||
return new Assign(obj, value).compile(o);
|
||||
return new Assign(obj, value, null, {
|
||||
param: this.param
|
||||
}).compile(o, LEVEL_TOP);
|
||||
}
|
||||
vvar = value.compile(o, LEVEL_LIST);
|
||||
assigns = [];
|
||||
|
@ -1321,7 +1323,7 @@
|
|||
};
|
||||
Code.prototype.jumps = NO;
|
||||
Code.prototype.compileNode = function(o) {
|
||||
var code, exprs, i, idt, lit, p, param, ref, splats, v, val, vars, wasEmpty, _i, _j, _len, _len2, _len3, _ref2, _ref3, _ref4;
|
||||
var code, exprs, i, idt, lit, p, param, ref, splats, v, val, vars, wasEmpty, _i, _j, _k, _len, _len2, _len3, _len4, _ref2, _ref3, _ref4, _ref5;
|
||||
o.scope = new Scope(o.scope, this.body, this);
|
||||
o.scope.shared = del(o, 'sharedScope');
|
||||
o.indent += TAB;
|
||||
|
@ -1332,15 +1334,19 @@
|
|||
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
||||
param = _ref2[_i];
|
||||
if (param.splat) {
|
||||
if (param.name.value) {
|
||||
o.scope.add(param.name.value, 'var');
|
||||
_ref3 = this.params;
|
||||
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
|
||||
p = _ref3[_j];
|
||||
if (p.name.value) {
|
||||
o.scope.add(p.name.value, 'var', true);
|
||||
}
|
||||
}
|
||||
splats = new Assign(new Value(new Arr((function() {
|
||||
var _j, _len2, _ref3, _results;
|
||||
_ref3 = this.params;
|
||||
var _k, _len3, _ref4, _results;
|
||||
_ref4 = this.params;
|
||||
_results = [];
|
||||
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
|
||||
p = _ref3[_j];
|
||||
for (_k = 0, _len3 = _ref4.length; _k < _len3; _k++) {
|
||||
p = _ref4[_k];
|
||||
_results.push(p.asReference(o));
|
||||
}
|
||||
return _results;
|
||||
|
@ -1348,9 +1354,9 @@
|
|||
break;
|
||||
}
|
||||
}
|
||||
_ref3 = this.params;
|
||||
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
|
||||
param = _ref3[_j];
|
||||
_ref4 = this.params;
|
||||
for (_k = 0, _len3 = _ref4.length; _k < _len3; _k++) {
|
||||
param = _ref4[_k];
|
||||
if (param.isComplex()) {
|
||||
val = ref = param.asReference(o);
|
||||
if (param.value) {
|
||||
|
@ -1376,10 +1382,10 @@
|
|||
exprs.unshift(splats);
|
||||
}
|
||||
if (exprs.length) {
|
||||
(_ref4 = this.body.expressions).unshift.apply(_ref4, exprs);
|
||||
(_ref5 = this.body.expressions).unshift.apply(_ref5, exprs);
|
||||
}
|
||||
if (!splats) {
|
||||
for (i = 0, _len3 = vars.length; i < _len3; i++) {
|
||||
for (i = 0, _len4 = vars.length; i < _len4; i++) {
|
||||
v = vars[i];
|
||||
o.scope.parameter(vars[i] = v.compile(o));
|
||||
}
|
||||
|
|
|
@ -967,7 +967,7 @@ exports.Assign = class Assign extends Base
|
|||
acc = IDENTIFIER.test idx.unwrap().value or 0
|
||||
value = new Value value
|
||||
value.properties.push new (if acc then Access else Index) idx
|
||||
return new Assign(obj, value).compile o
|
||||
return new Assign(obj, value, null, param: @param).compile o, LEVEL_TOP
|
||||
vvar = value.compile o, LEVEL_LIST
|
||||
assigns = []
|
||||
splat = false
|
||||
|
@ -1070,7 +1070,7 @@ exports.Code = class Code extends Base
|
|||
vars = []
|
||||
exprs = []
|
||||
for param in @params when param.splat
|
||||
o.scope.add param.name.value, 'var' if param.name.value
|
||||
o.scope.add p.name.value, 'var', yes for p in @params when p.name.value
|
||||
splats = new Assign new Value(new Arr(p.asReference o for p in @params)),
|
||||
new Value new Literal 'arguments'
|
||||
break
|
||||
|
|
|
@ -320,6 +320,11 @@ test "#904: Destructuring function arguments with same-named variables in scope"
|
|||
eq nonce, a
|
||||
eq nonce, b
|
||||
|
||||
test "Simple Destructuring function arguments with same-named variables in scope", ->
|
||||
x = 1
|
||||
f = ([x]) -> x
|
||||
eq f([2]), 2
|
||||
eq x, 1
|
||||
|
||||
test "caching base value", ->
|
||||
|
||||
|
|
|
@ -20,3 +20,9 @@ test "assignment to an Object.prototype-named variable should not leak to outer
|
|||
constructor = 'word'
|
||||
)()
|
||||
ok constructor isnt 'word'
|
||||
|
||||
test "siblings of variadic arguments shouldn't break out.", ->
|
||||
x = 10
|
||||
oops = (x,args...) ->
|
||||
oops(20, 1,2,3)
|
||||
eq x, 10
|
||||
|
|
Loading…
Reference in a new issue