Fixing inconsistencies for index variables

See the tests added to test_comprehensions.coffee. Previously, after
`for i in [1..3]`, i was 4. Also, index variables were never set to
any value in comprehensions containing both a closure and a break or
return.
This commit is contained in:
Trevor Burnham
2010-10-19 14:09:16 -04:00
parent 0e395569ee
commit 5005cb606f
13 changed files with 148 additions and 7 deletions

View File

@@ -58,6 +58,8 @@
node = pair[i];
(pair[i] = node.compile(o));
}
}
return pair;
};
@@ -106,6 +108,8 @@
child = _ref2[_i];
_result.push(child.toString(idt + TAB));
}
return _result;
}).call(this).join('');
klass = override || this.constructor.name + (this.soakNode ? '?' : '');
@@ -126,8 +130,12 @@
return;
}
}
}
}
return _result;
};
Base.prototype.collectChildren = function() {
@@ -208,6 +216,8 @@
node = _ref2[_i];
_result.push(this.compileExpression(node, merge(o)));
}
return _result;
}).call(this).join('\n');
};
@@ -411,6 +421,8 @@
prop = props[_i];
(code += prop.compile(o));
}
return code;
};
Value.prototype.unfoldSoak = function(o) {
@@ -436,6 +448,8 @@
return ifn;
}
}
return null;
};
return Value;
@@ -542,6 +556,8 @@
}
ifn = If.unfoldSoak(o, call, 'variable');
}
return ifn;
};
Call.prototype.compileNode = function(o) {
@@ -556,12 +572,16 @@
return this.compileSplat(o);
}
}
args = (function() {
_result = [];
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; _j++) {
arg = _ref4[_j];
_result.push((arg.parenthetical = true) && arg.compile(o));
}
return _result;
}).call(this).join(', ');
return this.isSuper ? this.compileSuper(args, o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")");
@@ -792,6 +812,8 @@
_result.push(prop);
}
}
return _result;
}).call(this);
lastNoncom = last(nonComments);
@@ -810,6 +832,8 @@
return indent + prop.compile(o) + join;
}).call(this));
}
return _result;
}).call(this);
props = props.join('');
@@ -824,6 +848,8 @@
return true;
}
}
return false;
};
return ObjectLiteral;
@@ -852,12 +878,16 @@
return this.compileSplatLiteral(o);
}
}
objects = [];
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; i++) {
obj = _ref3[i];
code = obj.compile(o);
objects.push(obj instanceof Comment ? ("\n" + code + "\n" + (o.indent)) : (i === this.objects.length - 1 ? code : code + ', '));
}
objects = objects.join('');
return 0 < objects.indexOf('\n') ? ("[\n" + (o.indent) + objects + "\n" + (this.tab) + "]") : ("[" + objects + "]");
};
@@ -869,6 +899,8 @@
return true;
}
}
return false;
};
return ArrayLiteral;
@@ -957,6 +989,8 @@
}
props.push(prop);
}
constructor.className = className.match(/[\w\d\$_]+$/);
if (me) {
constructor.body.unshift(new Literal("" + me + " = this"));
@@ -1082,6 +1116,8 @@
}
assigns.push(new Assign(obj, val).compile(otop));
}
if (!top) {
assigns.push(valVar);
}
@@ -1159,6 +1195,8 @@
}
}
}
o.scope.startLevel();
params = (function() {
_result = [];
@@ -1166,6 +1204,8 @@
param = params[_i];
_result.push(param.compile(o));
}
return _result;
})();
if (!(empty || this.noReturn)) {
@@ -1175,6 +1215,8 @@
param = params[_i];
(o.scope.parameter(param));
}
comm = this.comment ? this.comment.compile(o) + '\n' : '';
if (this.className) {
o.indent = this.idt(2);
@@ -1262,6 +1304,8 @@
pos = this.trailings.length - idx;
o.scope.assign(trailing.compile(o), "arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]");
}
}
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + (this.index) + end + ")";
};
@@ -1291,6 +1335,8 @@
}
args[++end] = i === 0 ? code : (".concat(" + code + ")");
}
return args.join('');
};
return Splat;
@@ -1492,6 +1538,8 @@
item = _ref2[i];
_result.push("" + (i ? this.obj2 : this.obj1) + " === " + (item.compile(o)));
}
return _result;
}).call(this);
return "(" + (tests.join(' || ')) + ")";
@@ -1657,7 +1705,7 @@
return '';
};
For.prototype.compileNode = function(o) {
var body, codeInBody, forPart, guardPart, idt1, index, ivar, lastLine, lvar, name, namePart, nvar, range, ref, resultPart, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, varPart, vars;
var body, codeInBody, forPart, guardPart, idt1, index, ivar, lastLine, lvar, name, namePart, nvar, range, ref, resultPart, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, unstepPart, varPart, vars;
topLevel = del(o, 'top') && !this.returns;
range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length;
source = range ? this.source.base : this.source;
@@ -1689,6 +1737,7 @@
}
varPart = '';
guardPart = '';
unstepPart = '';
body = Expressions.wrap([this.body]);
idt1 = this.idt(1);
if (range) {
@@ -1724,10 +1773,12 @@
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
if (codeInBody && !body.containsPureStatement()) {
if (codeInBody) {
if (range) {
body.unshift(new Literal("var " + name + " = " + ivar));
}
}
if (codeInBody && !body.containsPureStatement()) {
if (namePart) {
body.unshift(new Literal("var " + namePart));
}
@@ -1754,6 +1805,9 @@
if (namePart) {
varPart = ("" + idt1 + namePart + ";\n");
}
if (forPart && name === ivar) {
unstepPart = this.step ? ("\n" + name + " -= " + (this.step.compile(o)) + ";") : ("\n" + name + "--;");
}
}
if (this.object) {
forPart = ("" + ivar + " in " + sourcePart);
@@ -1766,7 +1820,7 @@
top: true
}));
vars = range ? name : ("" + name + ", " + ivar);
return "" + resultPart + (this.tab) + "for (" + forPart + ") {" + guardPart + "\n" + varPart + body + "\n" + (this.tab) + "}" + returnResult;
return "" + resultPart + (this.tab) + "for (" + forPart + ") {" + guardPart + "\n" + varPart + body + "\n" + (this.tab) + "}\n" + unstepPart + "\n" + returnResult;
};
return For;
})();
@@ -1792,6 +1846,8 @@
pair = _ref2[_i];
pair[1].makeReturn();
}
if (this.otherwise) {
this.otherwise.makeReturn();
}
@@ -1813,11 +1869,15 @@
}
code += ("\n" + (this.idt(1)) + "case " + (condition.compile(o)) + ":");
}
code += ("\n" + (block.compile(o)));
if (!(last(exprs) instanceof Return)) {
code += ("\n" + idt + "break;");
}
}
if (this.otherwise) {
code += ("\n" + (this.idt(1)) + "default:\n" + (this.otherwise.compile(o)));
}