mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
removed extra parens from compilations with assignments or conditional operators
This commit is contained in:
191
lib/nodes.js
191
lib/nodes.js
@@ -64,6 +64,10 @@
|
||||
}
|
||||
return pair;
|
||||
};
|
||||
Base.prototype.compileBare = function(o) {
|
||||
this.parenthetical = true;
|
||||
return this.compile(o);
|
||||
};
|
||||
Base.prototype.idt = function(tabs) {
|
||||
return (this.tab || '') + Array((tabs || 0) + 1).join(TAB);
|
||||
};
|
||||
@@ -210,20 +214,20 @@
|
||||
};
|
||||
Expressions.prototype.compileRoot = function(o) {
|
||||
var code;
|
||||
o.indent = (this.tab = o.bare ? '' : TAB);
|
||||
o.indent = this.tab = o.bare ? '' : TAB;
|
||||
o.scope = new Scope(null, this, null);
|
||||
code = this.compileWithDeclarations(o);
|
||||
code = code.replace(TRAILING_WHITESPACE, '');
|
||||
return o.bare ? code : ("(function() {\n" + code + "\n}).call(this);\n");
|
||||
return o.bare ? code : "(function() {\n" + code + "\n}).call(this);\n";
|
||||
};
|
||||
Expressions.prototype.compileWithDeclarations = function(o) {
|
||||
var code;
|
||||
code = this.compileNode(o);
|
||||
if (o.scope.hasAssignments(this)) {
|
||||
code = ("" + (this.tab) + "var " + (o.scope.compiledAssignments().replace(/\n/g, '$&' + this.tab)) + ";\n" + code);
|
||||
code = "" + (this.tab) + "var " + (o.scope.compiledAssignments().replace(/\n/g, '$&' + this.tab)) + ";\n" + code;
|
||||
}
|
||||
if (!o.globals && o.scope.hasDeclarations(this)) {
|
||||
code = ("" + (this.tab) + "var " + (o.scope.compiledDeclarations()) + ";\n" + code);
|
||||
code = "" + (this.tab) + "var " + (o.scope.compiledDeclarations()) + ";\n" + code;
|
||||
}
|
||||
return code;
|
||||
};
|
||||
@@ -234,7 +238,7 @@
|
||||
compiledNode = node.compile(merge(o, {
|
||||
top: true
|
||||
}));
|
||||
return node.isStatement(o) ? compiledNode : ("" + (this.idt()) + compiledNode + ";");
|
||||
return node.isStatement(o) ? compiledNode : "" + (this.idt()) + compiledNode + ";";
|
||||
};
|
||||
return Expressions;
|
||||
})();
|
||||
@@ -273,7 +277,7 @@
|
||||
var end, idt, val;
|
||||
idt = this.isStatement(o) ? this.idt() : '';
|
||||
end = this.isStatement(o) ? ';' : '';
|
||||
val = this.isReserved() ? ("\"" + (this.value) + "\"") : this.value;
|
||||
val = this.isReserved() ? "\"" + (this.value) + "\"" : this.value;
|
||||
return idt + val + end;
|
||||
};
|
||||
Literal.prototype.toString = function() {
|
||||
@@ -401,7 +405,7 @@
|
||||
}
|
||||
code = this.base.compile(o);
|
||||
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
|
||||
code = ("(" + code + ")");
|
||||
code = "(" + code + ")";
|
||||
}
|
||||
for (_i = 0, _len = props.length; _i < _len; _i++) {
|
||||
prop = props[_i];
|
||||
@@ -489,7 +493,7 @@
|
||||
if (!name) {
|
||||
throw SyntaxError('cannot call super on an anonymous function.');
|
||||
}
|
||||
return method.klass ? ("" + (method.klass) + ".__super__." + name) : ("" + name + ".__super__.constructor");
|
||||
return method.klass ? "" + (method.klass) + ".__super__." + name : "" + name + ".__super__.constructor";
|
||||
};
|
||||
Call.prototype.unfoldSoak = function(o) {
|
||||
var _i, _len, _ref2, _ref3, call, ifn, left, list, rite, val;
|
||||
@@ -545,7 +549,7 @@
|
||||
if (ifn = this.unfoldSoak(o)) {
|
||||
return ifn.compile(o);
|
||||
}
|
||||
(((_ref2 = this.variable) != null) ? (_ref2.tags.front = this.tags.front) : undefined);
|
||||
(((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined);
|
||||
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; _i++) {
|
||||
arg = _ref3[_i];
|
||||
if (arg instanceof Splat) {
|
||||
@@ -556,11 +560,11 @@
|
||||
_result = [];
|
||||
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; _j++) {
|
||||
arg = _ref4[_j];
|
||||
_result.push((arg.parenthetical = true) && arg.compile(o));
|
||||
_result.push(arg.compileBare(o));
|
||||
}
|
||||
return _result;
|
||||
}).call(this).join(', ');
|
||||
return this.isSuper ? this.compileSuper(args, o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")");
|
||||
return this.isSuper ? this.compileSuper(args, o) : "" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")";
|
||||
};
|
||||
Call.prototype.compileSuper = function(args, o) {
|
||||
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
|
||||
@@ -577,9 +581,9 @@
|
||||
}
|
||||
if ((name = base.properties.pop()) && base.isComplex()) {
|
||||
ref = o.scope.freeVariable('this');
|
||||
fun = ("(" + ref + " = " + (base.compile(o)) + ")" + (name.compile(o)));
|
||||
fun = "(" + ref + " = " + (base.compile(o)) + ")" + (name.compile(o));
|
||||
} else {
|
||||
fun = (ref = base.compile(o));
|
||||
fun = ref = base.compile(o);
|
||||
if (name) {
|
||||
fun += name.compile(o);
|
||||
}
|
||||
@@ -624,7 +628,7 @@
|
||||
Accessor.prototype.compileNode = function(o) {
|
||||
var name;
|
||||
name = this.name.compile(o);
|
||||
return this.prototype + (IS_STRING.test(name) ? ("[" + name + "]") : ("." + name));
|
||||
return this.prototype + (IS_STRING.test(name) ? "[" + name + "]" : "." + name);
|
||||
};
|
||||
Accessor.prototype.isComplex = NO;
|
||||
return Accessor;
|
||||
@@ -691,11 +695,11 @@
|
||||
}
|
||||
idx = del(o, 'index');
|
||||
step = del(o, 'step');
|
||||
vars = ("" + idx + " = " + (this.from)) + (this.to !== this.toVar ? (", " + (this.to)) : '');
|
||||
intro = ("(" + (this.fromVar) + " <= " + (this.toVar) + " ? " + idx);
|
||||
compare = ("" + intro + " <" + (this.equals) + " " + (this.toVar) + " : " + idx + " >" + (this.equals) + " " + (this.toVar) + ")");
|
||||
vars = ("" + idx + " = " + (this.from)) + (this.to !== this.toVar ? ", " + (this.to) : '');
|
||||
intro = "(" + (this.fromVar) + " <= " + (this.toVar) + " ? " + idx;
|
||||
compare = "" + intro + " <" + (this.equals) + " " + (this.toVar) + " : " + idx + " >" + (this.equals) + " " + (this.toVar) + ")";
|
||||
stepPart = step ? step.compile(o) : '1';
|
||||
incr = step ? ("" + idx + " += " + stepPart) : ("" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")");
|
||||
incr = step ? "" + idx + " += " + stepPart : "" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")";
|
||||
return "" + vars + "; " + compare + "; " + incr;
|
||||
};
|
||||
Range.prototype.compileSimple = function(o) {
|
||||
@@ -703,8 +707,8 @@
|
||||
_ref2 = [+this.fromNum, +this.toNum], from = _ref2[0], to = _ref2[1];
|
||||
idx = del(o, 'index');
|
||||
step = del(o, 'step');
|
||||
step && (step = ("" + idx + " += " + (step.compile(o))));
|
||||
return from <= to ? ("" + idx + " = " + from + "; " + idx + " <" + (this.equals) + " " + to + "; " + (step || ("" + idx + "++"))) : ("" + idx + " = " + from + "; " + idx + " >" + (this.equals) + " " + to + "; " + (step || ("" + idx + "--")));
|
||||
step && (step = "" + idx + " += " + (step.compile(o)));
|
||||
return from <= to ? "" + idx + " = " + from + "; " + idx + " <" + (this.equals) + " " + to + "; " + (step || ("" + idx + "++")) : "" + idx + " = " + from + "; " + idx + " >" + (this.equals) + " " + to + "; " + (step || ("" + idx + "--"));
|
||||
};
|
||||
Range.prototype.compileArray = function(o) {
|
||||
var _i, _ref2, _ref3, _result, body, clause, i, idt, post, pre, range, result, vars;
|
||||
@@ -722,16 +726,16 @@
|
||||
idt = this.idt(1);
|
||||
i = o.scope.freeVariable('i');
|
||||
result = o.scope.freeVariable('result');
|
||||
pre = ("\n" + idt + result + " = [];");
|
||||
pre = "\n" + idt + result + " = [];";
|
||||
if (this.fromNum && this.toNum) {
|
||||
o.index = i;
|
||||
body = this.compileSimple(o);
|
||||
} else {
|
||||
vars = ("" + i + " = " + (this.from)) + (this.to !== this.toVar ? (", " + (this.to)) : '');
|
||||
clause = ("" + (this.fromVar) + " <= " + (this.toVar) + " ?");
|
||||
body = ("var " + vars + "; " + clause + " " + i + " <" + (this.equals) + " " + (this.toVar) + " : " + i + " >" + (this.equals) + " " + (this.toVar) + "; " + clause + " " + i + " += 1 : " + i + " -= 1");
|
||||
vars = ("" + i + " = " + (this.from)) + (this.to !== this.toVar ? ", " + (this.to) : '');
|
||||
clause = "" + (this.fromVar) + " <= " + (this.toVar) + " ?";
|
||||
body = "var " + vars + "; " + clause + " " + i + " <" + (this.equals) + " " + (this.toVar) + " : " + i + " >" + (this.equals) + " " + (this.toVar) + "; " + clause + " " + i + " += 1 : " + i + " -= 1";
|
||||
}
|
||||
post = ("{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + (o.indent));
|
||||
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + (o.indent);
|
||||
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
|
||||
};
|
||||
return Range;
|
||||
@@ -763,7 +767,7 @@
|
||||
ObjectLiteral = (function() {
|
||||
function ObjectLiteral(props) {
|
||||
ObjectLiteral.__super__.constructor.call(this);
|
||||
this.objects = (this.properties = props || []);
|
||||
this.objects = this.properties = props || [];
|
||||
return this;
|
||||
};
|
||||
return ObjectLiteral;
|
||||
@@ -790,7 +794,7 @@
|
||||
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
|
||||
prop = _ref2[i];
|
||||
_result.push((function() {
|
||||
join = i === this.properties.length - 1 ? '' : (prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n');
|
||||
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
|
||||
indent = prop instanceof Comment ? '' : this.idt(1);
|
||||
if (prop instanceof Value && prop.tags["this"]) {
|
||||
prop = new Assign(prop.properties[0].name, prop, 'object');
|
||||
@@ -803,8 +807,8 @@
|
||||
return _result;
|
||||
}).call(this);
|
||||
props = props.join('');
|
||||
obj = ("{" + (props ? '\n' + props + '\n' + this.idt() : '') + "}");
|
||||
return this.tags.front ? ("(" + obj + ")") : obj;
|
||||
obj = "{" + (props ? '\n' + props + '\n' + this.idt() : '') + "}";
|
||||
return this.tags.front ? "(" + obj + ")" : obj;
|
||||
};
|
||||
ObjectLiteral.prototype.assigns = function(name) {
|
||||
var _i, _len, _ref2, prop;
|
||||
@@ -845,10 +849,10 @@
|
||||
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.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 + "]");
|
||||
return 0 < objects.indexOf('\n') ? "[\n" + (o.indent) + objects + "\n" + (this.tab) + "]" : "[" + objects + "]";
|
||||
};
|
||||
ArrayLiteral.prototype.assigns = function(name) {
|
||||
var _i, _len, _ref2, obj;
|
||||
@@ -999,7 +1003,7 @@
|
||||
this.value.name = match[2];
|
||||
this.value.klass = match[1];
|
||||
}
|
||||
val = this.value.compile(o);
|
||||
val = this.value.compileBare(o);
|
||||
if (this.context === 'object') {
|
||||
return ("" + name + ": " + val);
|
||||
}
|
||||
@@ -1010,7 +1014,7 @@
|
||||
if (stmt) {
|
||||
return ("" + (this.tab) + val + ";");
|
||||
}
|
||||
return top || this.parenthetical ? val : ("(" + val + ")");
|
||||
return top || this.parenthetical ? val : "(" + val + ")";
|
||||
};
|
||||
Assign.prototype.compilePatternMatch = function(o) {
|
||||
var _len, _ref2, _ref3, accessClass, assigns, code, i, idx, isObject, obj, objects, olength, otop, ref, splat, top, val, valVar, value;
|
||||
@@ -1026,7 +1030,7 @@
|
||||
if (obj instanceof Assign) {
|
||||
_ref2 = obj, idx = _ref2.variable.base, obj = _ref2.value;
|
||||
} else {
|
||||
idx = isObject ? (obj.tags["this"] ? obj.properties[0].name : obj) : new Literal(0);
|
||||
idx = isObject ? obj.tags["this"] ? obj.properties[0].name : obj : new Literal(0);
|
||||
}
|
||||
if (!(value instanceof Value)) {
|
||||
value = new Value(value);
|
||||
@@ -1065,7 +1069,7 @@
|
||||
splat = true;
|
||||
} else {
|
||||
if (typeof idx !== 'object') {
|
||||
idx = new Literal(splat ? ("" + valVar + ".length - " + (olength - idx)) : idx);
|
||||
idx = new Literal(splat ? "" + valVar + ".length - " + (olength - idx) : idx);
|
||||
}
|
||||
val = new Value(new Literal(valVar), [new accessClass(idx)]);
|
||||
}
|
||||
@@ -1075,7 +1079,7 @@
|
||||
assigns.push(valVar);
|
||||
}
|
||||
code = assigns.join(', ');
|
||||
return top || this.parenthetical ? code : ("(" + code + ")");
|
||||
return top || this.parenthetical ? code : "(" + code + ")";
|
||||
};
|
||||
Assign.prototype.compileSplice = function(o) {
|
||||
var from, name, plus, range, ref, to, val;
|
||||
@@ -1083,7 +1087,7 @@
|
||||
name = this.variable.compile(o);
|
||||
plus = range.exclusive ? '' : ' + 1';
|
||||
from = range.from ? range.from.compile(o) : '0';
|
||||
to = range.to ? range.to.compile(o) + ' - ' + from + plus : ("" + name + ".length");
|
||||
to = range.to ? range.to.compile(o) + ' - ' + from + plus : "" + name + ".length";
|
||||
ref = o.scope.freeVariable('ref');
|
||||
val = this.value.compile(o);
|
||||
return "([].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + ref + " = " + val + ")), " + ref + ")";
|
||||
@@ -1173,15 +1177,15 @@
|
||||
if (this.className) {
|
||||
o.indent = this.idt(2);
|
||||
}
|
||||
code = this.body.expressions.length ? ("\n" + (this.body.compileWithDeclarations(o)) + "\n") : '';
|
||||
open = this.className ? ("(function() {\n" + comm + (this.idt(1)) + "function " + (this.className) + "(") : "function(";
|
||||
close = this.className ? ("" + (code && this.idt(1)) + "};\n" + (this.idt(1)) + "return " + (this.className) + ";\n" + (this.tab) + "})()") : ("" + (code && this.tab) + "}");
|
||||
func = ("" + open + (params.join(', ')) + ") {" + code + close);
|
||||
code = this.body.expressions.length ? "\n" + (this.body.compileWithDeclarations(o)) + "\n" : '';
|
||||
open = this.className ? "(function() {\n" + comm + (this.idt(1)) + "function " + (this.className) + "(" : "function(";
|
||||
close = this.className ? "" + (code && this.idt(1)) + "};\n" + (this.idt(1)) + "return " + (this.className) + ";\n" + (this.tab) + "})()" : "" + (code && this.tab) + "}";
|
||||
func = "" + open + (params.join(', ')) + ") {" + code + close;
|
||||
o.scope.endLevel();
|
||||
if (this.bound) {
|
||||
return ("" + (utility('bind')) + "(" + func + ", " + (this.context) + ")");
|
||||
}
|
||||
return this.tags.front ? ("(" + func + ")") : func;
|
||||
return this.tags.front ? "(" + func + ")" : func;
|
||||
};
|
||||
Code.prototype.traverseChildren = function(crossScope, func) {
|
||||
return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : undefined;
|
||||
@@ -1245,7 +1249,7 @@
|
||||
o.scope.assign(len, "arguments.length");
|
||||
variadic = o.scope.freeVariable('result');
|
||||
o.scope.assign(variadic, len + ' >= ' + this.arglength);
|
||||
end = this.trailings.length ? (", " + len + " - " + (this.trailings.length)) : undefined;
|
||||
end = this.trailings.length ? ", " + len + " - " + (this.trailings.length) : undefined;
|
||||
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; idx++) {
|
||||
trailing = _ref2[idx];
|
||||
if (trailing.attach) {
|
||||
@@ -1261,7 +1265,7 @@
|
||||
};
|
||||
Splat.prototype.compileValue = function(o, name, index, trailings) {
|
||||
var trail;
|
||||
trail = trailings ? (", " + name + ".length - " + trailings) : '';
|
||||
trail = trailings ? ", " + name + ".length - " + trailings : '';
|
||||
return "" + (utility('slice')) + ".call(" + name + ", " + index + trail + ")";
|
||||
};
|
||||
Splat.compileSplattedArray = function(list, o) {
|
||||
@@ -1274,16 +1278,16 @@
|
||||
prev = args[end];
|
||||
if (!(arg instanceof Splat)) {
|
||||
if (prev && starts(prev, '[') && ends(prev, ']')) {
|
||||
args[end] = ("" + (prev.slice(0, -1)) + ", " + code + "]");
|
||||
args[end] = "" + (prev.slice(0, -1)) + ", " + code + "]";
|
||||
continue;
|
||||
}
|
||||
if (prev && starts(prev, '.concat([') && ends(prev, '])')) {
|
||||
args[end] = ("" + (prev.slice(0, -2)) + ", " + code + "])");
|
||||
args[end] = "" + (prev.slice(0, -2)) + ", " + code + "])";
|
||||
continue;
|
||||
}
|
||||
code = ("[" + code + "]");
|
||||
code = "[" + code + "]";
|
||||
}
|
||||
args[++end] = i === 0 ? code : (".concat(" + code + ")");
|
||||
args[++end] = i === 0 ? code : ".concat(" + code + ")";
|
||||
}
|
||||
return args.join('');
|
||||
};
|
||||
@@ -1315,18 +1319,17 @@
|
||||
var cond, post, pre, rvar, set, top;
|
||||
top = del(o, 'top') && !this.returns;
|
||||
o.indent = this.idt(1);
|
||||
this.condition.parenthetical = true;
|
||||
cond = this.condition.compile(o);
|
||||
cond = this.condition.compileBare(o);
|
||||
o.top = true;
|
||||
set = '';
|
||||
if (!top) {
|
||||
rvar = o.scope.freeVariable('result');
|
||||
set = ("" + (this.tab) + rvar + " = [];\n");
|
||||
set = "" + (this.tab) + rvar + " = [];\n";
|
||||
if (this.body) {
|
||||
this.body = Push.wrap(rvar, this.body);
|
||||
}
|
||||
}
|
||||
pre = ("" + set + (this.tab) + "while (" + cond + ")");
|
||||
pre = "" + set + (this.tab) + "while (" + cond + ")";
|
||||
if (this.guard) {
|
||||
this.body = Expressions.wrap([new If(this.guard, this.body)]);
|
||||
}
|
||||
@@ -1428,7 +1431,8 @@
|
||||
fst = this.first;
|
||||
ref = fst.compile(o);
|
||||
}
|
||||
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o)));
|
||||
this.second.tags.operation = false;
|
||||
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compileBare(o)));
|
||||
};
|
||||
Op.prototype.compileUnary = function(o) {
|
||||
var _ref2, parts, space;
|
||||
@@ -1478,9 +1482,9 @@
|
||||
}), {
|
||||
precompile: true
|
||||
}), obj1 = _ref2[0], obj2 = _ref2[1];
|
||||
prefix = obj1 !== obj2 ? ("" + obj1 + ", ") : '';
|
||||
code = ("" + prefix + (utility('indexOf')) + ".call(" + (this.array.compile(o)) + ", " + obj2 + ") >= 0");
|
||||
return this.parenthetical ? code : ("(" + code + ")");
|
||||
prefix = obj1 !== obj2 ? "" + obj1 + ", " : '';
|
||||
code = "" + prefix + (utility('indexOf')) + ".call(" + (this.array.compile(o)) + ", " + obj2 + ") >= 0";
|
||||
return this.parenthetical ? code : "(" + code + ")";
|
||||
};
|
||||
return In;
|
||||
})();
|
||||
@@ -1513,8 +1517,8 @@
|
||||
o.indent = this.idt(1);
|
||||
o.top = true;
|
||||
attemptPart = this.attempt.compile(o);
|
||||
errorPart = this.error ? (" (" + (this.error.compile(o)) + ") ") : ' ';
|
||||
catchPart = this.recovery ? (" catch" + errorPart + "{\n" + (this.recovery.compile(o)) + "\n" + (this.tab) + "}") : (!(this.ensure || this.recovery) ? ' catch (_e) {}' : '');
|
||||
errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' ';
|
||||
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o)) + "\n" + (this.tab) + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : '';
|
||||
finallyPart = (this.ensure || '') && ' finally {\n' + this.ensure.compile(merge(o)) + ("\n" + (this.tab) + "}");
|
||||
return "" + (this.tab) + "try {\n" + attemptPart + "\n" + (this.tab) + "}" + catchPart + finallyPart;
|
||||
};
|
||||
@@ -1552,8 +1556,8 @@
|
||||
Existence.prototype.compileNode = function(o) {
|
||||
var code;
|
||||
code = this.expression.compile(o);
|
||||
code = IDENTIFIER.test(code) && !o.scope.check(code) ? ("typeof " + code + " !== \"undefined\" && " + code + " !== null") : ("" + code + " != null");
|
||||
return this.parenthetical ? code : ("(" + code + ")");
|
||||
code = IDENTIFIER.test(code) && !o.scope.check(code) ? "typeof " + code + " !== \"undefined\" && " + code + " !== null" : "" + code + " != null";
|
||||
return this.parenthetical ? code : "(" + code + ")";
|
||||
};
|
||||
return Existence;
|
||||
})();
|
||||
@@ -1581,8 +1585,7 @@
|
||||
Parens.prototype.compileNode = function(o) {
|
||||
var code, top;
|
||||
top = del(o, 'top');
|
||||
this.expression.parenthetical = true;
|
||||
code = this.expression.compile(o);
|
||||
code = this.expression.compileBare(o);
|
||||
if (top && this.expression.isPureStatement(o)) {
|
||||
return code;
|
||||
}
|
||||
@@ -1675,26 +1678,26 @@
|
||||
step: this.step
|
||||
}));
|
||||
} else {
|
||||
svar = (sourcePart = this.source.compile(o));
|
||||
svar = sourcePart = this.source.compile(o);
|
||||
if ((name || !this.raw) && !(IDENTIFIER.test(svar) && scope.check(svar, {
|
||||
immediate: true
|
||||
}))) {
|
||||
sourcePart = ("" + (ref = scope.freeVariable('ref')) + " = " + svar);
|
||||
sourcePart = "" + (ref = scope.freeVariable('ref')) + " = " + svar;
|
||||
if (!this.object) {
|
||||
sourcePart = ("(" + sourcePart + ")");
|
||||
sourcePart = "(" + sourcePart + ")";
|
||||
}
|
||||
svar = ref;
|
||||
}
|
||||
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
|
||||
top: true
|
||||
})) : (name ? ("" + name + " = " + svar + "[" + ivar + "]") : undefined);
|
||||
})) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
|
||||
if (!this.object) {
|
||||
lvar = scope.freeVariable('len');
|
||||
stepPart = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++");
|
||||
forPart = ("" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart);
|
||||
stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o)) : "" + ivar + "++";
|
||||
forPart = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart;
|
||||
}
|
||||
}
|
||||
resultPart = rvar ? ("" + (this.tab) + rvar + " = [];\n") : '';
|
||||
resultPart = rvar ? "" + (this.tab) + rvar + " = [];\n" : '';
|
||||
returnResult = this.compileReturnValue(rvar, o);
|
||||
if (!topLevel) {
|
||||
body = Push.wrap(rvar, body);
|
||||
@@ -1730,24 +1733,24 @@
|
||||
}
|
||||
} else {
|
||||
if (namePart) {
|
||||
varPart = ("" + idt1 + namePart + ";\n");
|
||||
varPart = "" + idt1 + namePart + ";\n";
|
||||
}
|
||||
if (forPart && name === ivar) {
|
||||
unstepPart = this.step ? ("" + name + " -= " + (this.step.compile(o)) + ";") : ("" + name + "--;");
|
||||
unstepPart = this.step ? "" + name + " -= " + (this.step.compile(o)) + ";" : "" + name + "--;";
|
||||
unstepPart = ("\n" + (this.tab)) + unstepPart;
|
||||
}
|
||||
}
|
||||
if (this.object) {
|
||||
forPart = ("" + ivar + " in " + sourcePart);
|
||||
forPart = "" + ivar + " in " + sourcePart;
|
||||
if (!this.raw) {
|
||||
guardPart = ("\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;");
|
||||
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;";
|
||||
}
|
||||
}
|
||||
body = body.compile(merge(o, {
|
||||
indent: idt1,
|
||||
top: true
|
||||
}));
|
||||
vars = range ? name : ("" + name + ", " + ivar);
|
||||
vars = range ? name : "" + name + ", " + ivar;
|
||||
return "" + resultPart + (this.tab) + "for (" + forPart + ") {" + guardPart + "\n" + varPart + body + "\n" + (this.tab) + "}" + unstepPart + returnResult;
|
||||
};
|
||||
return For;
|
||||
@@ -1780,9 +1783,9 @@
|
||||
Switch.prototype.compileNode = function(o) {
|
||||
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5, block, code, condition, conditions, idt1, idt2;
|
||||
idt1 = this.idt(1);
|
||||
idt2 = (o.indent = this.idt(2));
|
||||
idt2 = o.indent = this.idt(2);
|
||||
o.top = true;
|
||||
code = ("" + (this.tab) + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {");
|
||||
code = "" + (this.tab) + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
|
||||
for (_i = 0, _len = (_ref3 = this.cases).length; _i < _len; _i++) {
|
||||
_ref4 = _ref3[_i], conditions = _ref4[0], block = _ref4[1];
|
||||
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; _j++) {
|
||||
@@ -1790,17 +1793,17 @@
|
||||
if (!this.subject) {
|
||||
condition = condition.invert().invert();
|
||||
}
|
||||
code += ("\n" + idt1 + "case " + (condition.compile(o)) + ":");
|
||||
code += "\n" + idt1 + "case " + (condition.compile(o)) + ":";
|
||||
}
|
||||
code += ("\n" + (block.compile(o)));
|
||||
code += "\n" + (block.compile(o));
|
||||
if (!(last(block.expressions) instanceof Return)) {
|
||||
code += ("\n" + idt2 + "break;");
|
||||
code += "\n" + idt2 + "break;";
|
||||
}
|
||||
}
|
||||
if (this.otherwise) {
|
||||
code += ("\n" + idt1 + "default:\n" + (this.otherwise.compile(o)));
|
||||
code += "\n" + idt1 + "default:\n" + (this.otherwise.compile(o));
|
||||
}
|
||||
code += ("\n" + (this.tab) + "}");
|
||||
code += "\n" + (this.tab) + "}";
|
||||
return code;
|
||||
};
|
||||
return Switch;
|
||||
@@ -1819,7 +1822,7 @@
|
||||
return If;
|
||||
})();
|
||||
__extends(If, Base);
|
||||
If.prototype.children = ['condition', 'body', 'elseBody', 'assigner'];
|
||||
If.prototype.children = ['condition', 'body', 'elseBody'];
|
||||
If.prototype.topSensitive = YES;
|
||||
If.prototype.bodyNode = function() {
|
||||
var _ref2;
|
||||
@@ -1842,10 +1845,6 @@
|
||||
var _ref2;
|
||||
return this.statement || (this.statement = ((o != null) ? o.top : undefined) || this.bodyNode().isStatement(o) || (((_ref2 = this.elseBodyNode()) != null) ? _ref2.isStatement(o) : undefined));
|
||||
};
|
||||
If.prototype.compileCondition = function(o) {
|
||||
this.condition.parenthetical = true;
|
||||
return this.condition.compile(o);
|
||||
};
|
||||
If.prototype.compileNode = function(o) {
|
||||
return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o);
|
||||
};
|
||||
@@ -1868,28 +1867,22 @@
|
||||
condO = merge(o);
|
||||
o.indent = this.idt(1);
|
||||
o.top = true;
|
||||
ifPart = ("if (" + (this.compileCondition(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}");
|
||||
ifPart = "if (" + (this.condition.compileBare(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}";
|
||||
if (!child) {
|
||||
ifPart = this.tab + ifPart;
|
||||
}
|
||||
if (!this.elseBody) {
|
||||
return ifPart;
|
||||
}
|
||||
return ifPart + (this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, {
|
||||
return ifPart + ' else ' + (this.isChain ? this.elseBodyNode().compile(merge(o, {
|
||||
indent: this.tab,
|
||||
chainChild: true
|
||||
})) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}"));
|
||||
})) : "{\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}");
|
||||
};
|
||||
If.prototype.compileExpression = function(o) {
|
||||
var code, elsePart, ifPart;
|
||||
this.bodyNode().tags.operation = (this.condition.tags.operation = true);
|
||||
if (this.elseBody) {
|
||||
this.elseBodyNode().tags.operation = true;
|
||||
}
|
||||
ifPart = this.condition.compile(o) + ' ? ' + this.bodyNode().compile(o);
|
||||
elsePart = this.elseBody ? this.elseBodyNode().compile(o) : 'undefined';
|
||||
code = ("" + ifPart + " : " + elsePart);
|
||||
return this.tags.operation || this.soakNode ? ("(" + code + ")") : code;
|
||||
var _ref2, code;
|
||||
code = this.condition.compile(o) + ' ? ' + this.bodyNode().compileBare(o) + ' : ' + (((_ref2 = this.elseBodyNode()) != null) ? _ref2.compileBare(o) : undefined);
|
||||
return this.tags.operation || this.soakNode ? "(" + code + ")" : code;
|
||||
};
|
||||
If.prototype.unfoldSoak = function() {
|
||||
return this.soakNode && this;
|
||||
@@ -1955,7 +1948,7 @@
|
||||
IS_STRING = /^['"]/;
|
||||
utility = function(name) {
|
||||
var ref;
|
||||
ref = ("__" + name);
|
||||
ref = "__" + name;
|
||||
Scope.root.assign(ref, UTILITIES[name]);
|
||||
return ref;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user