coco dc8945c: less ternaries, more returns-from-ifs. More readable.
This commit is contained in:
parent
919596aba4
commit
9c5eca9131
|
@ -21,7 +21,9 @@
|
|||
xhr.overrideMimeType('text/plain');
|
||||
}
|
||||
xhr.onreadystatechange = function() {
|
||||
return xhr.readyState === 4 ? CoffeeScript.run(xhr.responseText, options) : void 0;
|
||||
if (xhr.readyState === 4) {
|
||||
return CoffeeScript.run(xhr.responseText, options);
|
||||
}
|
||||
};
|
||||
return xhr.send(null);
|
||||
};
|
||||
|
|
|
@ -65,7 +65,9 @@
|
|||
desc = task.description ? "# " + task.description : '';
|
||||
console.log("cake " + name + spaces + " " + desc);
|
||||
}
|
||||
return switches.length ? console.log(oparse.help()) : void 0;
|
||||
if (switches.length) {
|
||||
return console.log(oparse.help());
|
||||
}
|
||||
};
|
||||
missingTask = function(task) {
|
||||
console.log("No such task: \"" + task + "\"");
|
||||
|
|
|
@ -44,7 +44,11 @@
|
|||
if (root.moduleCache) {
|
||||
root.moduleCache = {};
|
||||
}
|
||||
return path.extname(root.filename) !== '.coffee' || require.extensions ? root._compile(compile(code, options), root.filename) : root._compile(code, root.filename);
|
||||
if (path.extname(root.filename) !== '.coffee' || require.extensions) {
|
||||
return root._compile(compile(code, options), root.filename);
|
||||
} else {
|
||||
return root._compile(code, root.filename);
|
||||
}
|
||||
};
|
||||
exports.eval = function(code, options) {
|
||||
var __dirname, __filename;
|
||||
|
|
|
@ -74,7 +74,9 @@
|
|||
fs.readFile(source, function(err, code) {
|
||||
return compileScript(source, code.toString(), base);
|
||||
});
|
||||
return opts.watch ? watch(source, base) : void 0;
|
||||
if (opts.watch) {
|
||||
return watch(source, base);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -115,7 +117,13 @@
|
|||
} else {
|
||||
t.output = CoffeeScript.compile(t.input, t.options);
|
||||
CoffeeScript.emit('success', task);
|
||||
return o.print ? console.log(t.output.trim()) : o.compile ? writeJs(t.file, t.output, base) : o.lint ? lint(t.file, t.output) : void 0;
|
||||
if (o.print) {
|
||||
return console.log(t.output.trim());
|
||||
} else if (o.compile) {
|
||||
return writeJs(t.file, t.output, base);
|
||||
} else if (o.lint) {
|
||||
return lint(t.file, t.output);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
CoffeeScript.emit('failure', err, task);
|
||||
|
@ -134,7 +142,9 @@
|
|||
code = '';
|
||||
stdin = process.openStdin();
|
||||
stdin.on('data', function(buffer) {
|
||||
return buffer ? code += buffer.toString() : void 0;
|
||||
if (buffer) {
|
||||
return code += buffer.toString();
|
||||
}
|
||||
});
|
||||
return stdin.on('end', function() {
|
||||
return compileScript(null, code);
|
||||
|
@ -168,11 +178,19 @@
|
|||
js = ' ';
|
||||
}
|
||||
return fs.writeFile(jsPath, js, function(err) {
|
||||
return err ? console.log(err.message) : opts.compile && opts.watch ? console.log("Compiled " + source) : void 0;
|
||||
if (err) {
|
||||
return console.log(err.message);
|
||||
} else if (opts.compile && opts.watch) {
|
||||
return console.log("Compiled " + source);
|
||||
}
|
||||
});
|
||||
};
|
||||
return path.exists(dir, function(exists) {
|
||||
return exists ? compile() : exec("mkdir -p " + dir, compile);
|
||||
if (exists) {
|
||||
return compile();
|
||||
} else {
|
||||
return exec("mkdir -p " + dir, compile);
|
||||
}
|
||||
});
|
||||
};
|
||||
lint = function(file, js) {
|
||||
|
@ -208,7 +226,9 @@
|
|||
o.run = !(o.compile || o.print || o.lint);
|
||||
o.print = !!(o.print || (o.eval || o.stdio && o.compile));
|
||||
sources = o.arguments;
|
||||
return opts['no-wrap'] ? console.warn('--no-wrap is deprecated; please use --bare instead.') : void 0;
|
||||
if (opts['no-wrap']) {
|
||||
return console.warn('--no-wrap is deprecated; please use --bare instead.');
|
||||
}
|
||||
};
|
||||
compileOptions = function(fileName) {
|
||||
return {
|
||||
|
|
|
@ -579,7 +579,11 @@
|
|||
}), o('Expression LOGIC Expression', function() {
|
||||
return new Op($2, $1, $3);
|
||||
}), o('Expression RELATION Expression', function() {
|
||||
return $2.charAt(0) === '!' ? new Op($2.slice(1), $1, $3).invert() : new Op($2, $1, $3);
|
||||
if ($2.charAt(0) === '!') {
|
||||
return new Op($2.slice(1), $1, $3).invert();
|
||||
} else {
|
||||
return new Op($2, $1, $3);
|
||||
}
|
||||
}), o('SimpleAssignable COMPOUND_ASSIGN\
|
||||
Expression', function() {
|
||||
return new Assign($1, $3, $2);
|
||||
|
|
12
lib/lexer.js
12
lib/lexer.js
|
@ -327,7 +327,11 @@
|
|||
if (prev) {
|
||||
prev[match ? 'spaced' : 'newLine'] = true;
|
||||
}
|
||||
return match ? match[0].length : 0;
|
||||
if (match) {
|
||||
return match[0].length;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
Lexer.prototype.newlineToken = function() {
|
||||
if (this.tag() !== 'TERMINATOR') {
|
||||
|
@ -580,7 +584,11 @@
|
|||
return quote + quote;
|
||||
}
|
||||
body = body.replace(/\\([\s\S])/g, function(match, contents) {
|
||||
return contents === '\n' || contents === quote ? contents : match;
|
||||
if (contents === '\n' || contents === quote) {
|
||||
return contents;
|
||||
} else {
|
||||
return match;
|
||||
}
|
||||
});
|
||||
body = body.replace(RegExp("" + quote, "g"), '\\$&');
|
||||
return quote + this.escapeLines(body, heredoc) + quote;
|
||||
|
|
271
lib/nodes.js
271
lib/nodes.js
|
@ -28,13 +28,17 @@
|
|||
})();
|
||||
Base.prototype.compile = function(o, lvl) {
|
||||
var node;
|
||||
o = o ? extend({}, o) : {};
|
||||
o = extend({}, o);
|
||||
if (lvl != null) {
|
||||
o.level = lvl;
|
||||
}
|
||||
node = this.unfoldSoak(o) || this;
|
||||
node.tab = o.indent;
|
||||
return o.level === LEVEL_TOP || node.isPureStatement() || !node.isStatement(o) ? node.compileNode(o) : node.compileClosure(o);
|
||||
if (o.level === LEVEL_TOP || node.isPureStatement() || !node.isStatement(o)) {
|
||||
return node.compileNode(o);
|
||||
} else {
|
||||
return node.compileClosure(o);
|
||||
}
|
||||
};
|
||||
Base.prototype.compileClosure = function(o) {
|
||||
if (this.containsPureStatement()) {
|
||||
|
@ -51,7 +55,11 @@
|
|||
} else {
|
||||
ref = new Literal(reused || o.scope.freeVariable('ref'));
|
||||
sub = new Assign(ref, this);
|
||||
return level ? [sub.compile(o, level), ref.value] : [sub, ref];
|
||||
if (level) {
|
||||
return [sub.compile(o, level), ref.value];
|
||||
} else {
|
||||
return [sub, ref];
|
||||
}
|
||||
}
|
||||
};
|
||||
Base.prototype.compileLoopReference = function(o, name) {
|
||||
|
@ -172,7 +180,6 @@
|
|||
})();
|
||||
__extends(Expressions, Base);
|
||||
Expressions.prototype.children = ['expressions'];
|
||||
Expressions.prototype.isStatement = YES;
|
||||
Expressions.prototype.push = function(node) {
|
||||
this.expressions.push(node);
|
||||
return this;
|
||||
|
@ -185,11 +192,26 @@
|
|||
return this;
|
||||
};
|
||||
Expressions.prototype.unwrap = function() {
|
||||
return this.expressions.length === 1 ? this.expressions[0] : this;
|
||||
if (this.expressions.length === 1) {
|
||||
return this.expressions[0];
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
};
|
||||
Expressions.prototype.isEmpty = function() {
|
||||
return !this.expressions.length;
|
||||
};
|
||||
Expressions.prototype.isStatement = function(o) {
|
||||
var exp, _i, _len, _ref;
|
||||
_ref = this.expressions;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
exp = _ref[_i];
|
||||
if (exp.isPureStatement() || exp.isStatement(o)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
Expressions.prototype.makeReturn = function() {
|
||||
var end, idx, _ref;
|
||||
_ref = this.expressions;
|
||||
|
@ -204,20 +226,39 @@
|
|||
};
|
||||
Expressions.prototype.compile = function(o, level) {
|
||||
o == null && (o = {});
|
||||
return o.scope ? Expressions.__super__.compile.call(this, o, level) : this.compileRoot(o);
|
||||
if (o.scope) {
|
||||
return Expressions.__super__.compile.call(this, o, level);
|
||||
} else {
|
||||
return this.compileRoot(o);
|
||||
}
|
||||
};
|
||||
Expressions.prototype.compileNode = function(o) {
|
||||
var node, _i, _len, _ref, _result;
|
||||
var code, codes, node, top, _i, _len, _ref;
|
||||
this.tab = o.indent;
|
||||
return ((function() {
|
||||
_ref = this.expressions;
|
||||
_result = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
node = _ref[_i];
|
||||
_result.push(this.compileExpression(node, o));
|
||||
top = o.level === LEVEL_TOP;
|
||||
codes = [];
|
||||
_ref = this.expressions;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
node = _ref[_i];
|
||||
node = node.unwrapAll();
|
||||
node = node.unfoldSoak(o) || node;
|
||||
if (top) {
|
||||
node.front = true;
|
||||
code = node.compile(o);
|
||||
codes.push(node.isStatement(o) ? code : this.tab + code + ';');
|
||||
} else {
|
||||
codes.push(node.compile(o, LEVEL_LIST));
|
||||
}
|
||||
return _result;
|
||||
}).call(this)).join('\n');
|
||||
}
|
||||
if (top) {
|
||||
return codes.join('\n');
|
||||
}
|
||||
code = codes.join(', ') || 'void 0';
|
||||
if (codes.length > 1 && o.level >= LEVEL_LIST) {
|
||||
return "(" + code + ")";
|
||||
} else {
|
||||
return code;
|
||||
}
|
||||
};
|
||||
Expressions.prototype.compileRoot = function(o) {
|
||||
var code;
|
||||
|
@ -226,10 +267,15 @@
|
|||
o.level = LEVEL_TOP;
|
||||
code = this.compileWithDeclarations(o);
|
||||
code = code.replace(TRAILING_WHITESPACE, '');
|
||||
return o.bare ? code : "(function() {\n" + code + "\n}).call(this);\n";
|
||||
if (o.bare) {
|
||||
return code;
|
||||
} else {
|
||||
return "(function() {\n" + code + "\n}).call(this);\n";
|
||||
}
|
||||
};
|
||||
Expressions.prototype.compileWithDeclarations = function(o) {
|
||||
var code, scope;
|
||||
o.level = LEVEL_TOP;
|
||||
code = this.compileNode(o);
|
||||
scope = o.scope;
|
||||
if (scope.hasAssignments(this)) {
|
||||
|
@ -240,15 +286,6 @@
|
|||
}
|
||||
return code;
|
||||
};
|
||||
Expressions.prototype.compileExpression = function(node, o) {
|
||||
var code;
|
||||
node = node.unwrapAll();
|
||||
node = node.unfoldSoak(o) || node;
|
||||
node.front = true;
|
||||
o.level = LEVEL_TOP;
|
||||
code = node.compile(o);
|
||||
return node.isStatement(o) ? code : this.tab + code + ';';
|
||||
};
|
||||
Expressions.wrap = function(nodes) {
|
||||
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
|
||||
return nodes[0];
|
||||
|
@ -267,7 +304,11 @@
|
|||
})();
|
||||
__extends(Literal, Base);
|
||||
Literal.prototype.makeReturn = function() {
|
||||
return this.isStatement() ? this : Literal.__super__.makeReturn.call(this);
|
||||
if (this.isPureStatement()) {
|
||||
return this;
|
||||
} else {
|
||||
return new Return(this);
|
||||
}
|
||||
};
|
||||
Literal.prototype.isPureStatement = function() {
|
||||
var _ref;
|
||||
|
@ -281,7 +322,11 @@
|
|||
return name === this.value;
|
||||
};
|
||||
Literal.prototype.compile = function() {
|
||||
return this.value.reserved ? "\"" + this.value + "\"" : this.value;
|
||||
if (this.value.reserved) {
|
||||
return "\"" + this.value + "\"";
|
||||
} else {
|
||||
return this.value;
|
||||
}
|
||||
};
|
||||
Literal.prototype.toString = function() {
|
||||
return ' "' + this.value + '"';
|
||||
|
@ -304,7 +349,11 @@
|
|||
Return.prototype.compile = function(o, level) {
|
||||
var expr, _ref;
|
||||
expr = (_ref = this.expression) != null ? _ref.makeReturn() : void 0;
|
||||
return expr && !(expr instanceof Return) ? expr.compile(o, level) : Return.__super__.compile.call(this, o, level);
|
||||
if (expr && !(expr instanceof Return)) {
|
||||
return expr.compile(o, level);
|
||||
} else {
|
||||
return Return.__super__.compile.call(this, o, level);
|
||||
}
|
||||
};
|
||||
Return.prototype.compileNode = function(o) {
|
||||
o.level = LEVEL_PAREN;
|
||||
|
@ -369,10 +418,18 @@
|
|||
return !this.properties.length && this.base.assigns(name);
|
||||
};
|
||||
Value.prototype.makeReturn = function() {
|
||||
return this.properties.length ? Value.__super__.makeReturn.call(this) : this.base.makeReturn();
|
||||
if (this.properties.length) {
|
||||
return Value.__super__.makeReturn.call(this);
|
||||
} else {
|
||||
return this.base.makeReturn();
|
||||
}
|
||||
};
|
||||
Value.prototype.unwrap = function() {
|
||||
return this.properties.length ? this : this.base;
|
||||
if (this.properties.length) {
|
||||
return this;
|
||||
} else {
|
||||
return this.base;
|
||||
}
|
||||
};
|
||||
Value.prototype.cacheReference = function(o) {
|
||||
var base, bref, name, nref;
|
||||
|
@ -481,7 +538,11 @@
|
|||
if (!name) {
|
||||
throw SyntaxError('cannot call super on an anonymous function.');
|
||||
}
|
||||
return method.klass ? "" + method.klass + ".__super__." + name : "" + name + ".__super__.constructor";
|
||||
if (method.klass) {
|
||||
return "" + method.klass + ".__super__." + name;
|
||||
} else {
|
||||
return "" + name + ".__super__.constructor";
|
||||
}
|
||||
};
|
||||
Call.prototype.unfoldSoak = function(o) {
|
||||
var call, ifn, left, list, rite, _i, _len, _ref, _ref2;
|
||||
|
@ -549,7 +610,11 @@
|
|||
}
|
||||
return _result;
|
||||
}).call(this)).join(', ');
|
||||
return this.isSuper ? this.compileSuper(args, o) : (this.isNew ? 'new ' : '') + this.variable.compile(o, LEVEL_ACCESS) + ("(" + args + ")");
|
||||
if (this.isSuper) {
|
||||
return this.compileSuper(args, o);
|
||||
} else {
|
||||
return (this.isNew ? 'new ' : '') + this.variable.compile(o, LEVEL_ACCESS) + ("(" + args + ")");
|
||||
}
|
||||
};
|
||||
Call.prototype.compileSuper = function(args, o) {
|
||||
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
|
||||
|
@ -685,7 +750,11 @@
|
|||
if (rest) {
|
||||
return this.compileDynamic(o, obj, rest);
|
||||
}
|
||||
return this.front ? "(" + obj + ")" : obj;
|
||||
if (this.front) {
|
||||
return "(" + obj + ")";
|
||||
} else {
|
||||
return obj;
|
||||
}
|
||||
};
|
||||
Obj.prototype.compileDynamic = function(o, code, props) {
|
||||
var acc, i, key, oref, prop, ref, val, _len, _ref;
|
||||
|
@ -711,7 +780,11 @@
|
|||
code += "" + oref + key + " = " + val + ", ";
|
||||
}
|
||||
code += oref;
|
||||
return o.level <= LEVEL_PAREN ? code : "(" + code + ")";
|
||||
if (o.level <= LEVEL_PAREN) {
|
||||
return code;
|
||||
} else {
|
||||
return "(" + code + ")";
|
||||
}
|
||||
};
|
||||
Obj.prototype.assigns = function(name) {
|
||||
var prop, _i, _len, _ref;
|
||||
|
@ -750,7 +823,11 @@
|
|||
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 + "]";
|
||||
if (0 < objects.indexOf('\n')) {
|
||||
return "[\n" + o.indent + objects + "\n" + this.tab + "]";
|
||||
} else {
|
||||
return "[" + objects + "]";
|
||||
}
|
||||
};
|
||||
Arr.prototype.assigns = function(name) {
|
||||
var obj, _i, _len, _ref;
|
||||
|
@ -908,7 +985,11 @@
|
|||
o.scope.find(name);
|
||||
}
|
||||
val = name + (" " + (this.context || '=') + " ") + val;
|
||||
return o.level <= LEVEL_LIST ? val : "(" + val + ")";
|
||||
if (o.level <= LEVEL_LIST) {
|
||||
return val;
|
||||
} else {
|
||||
return "(" + val + ")";
|
||||
}
|
||||
};
|
||||
Assign.prototype.compilePatternMatch = function(o) {
|
||||
var acc, assigns, code, i, idx, isObject, ivar, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref, _ref2, _ref3, _ref4, _ref5, _ref6;
|
||||
|
@ -984,7 +1065,11 @@
|
|||
assigns.push(vvar);
|
||||
}
|
||||
code = assigns.join(', ');
|
||||
return o.level < LEVEL_LIST ? code : "(" + code + ")";
|
||||
if (o.level < LEVEL_LIST) {
|
||||
return code;
|
||||
} else {
|
||||
return "(" + code + ")";
|
||||
}
|
||||
};
|
||||
Assign.prototype.compileConditional = function(o) {
|
||||
var left, rite, _ref;
|
||||
|
@ -1089,10 +1174,16 @@
|
|||
if (this.bound) {
|
||||
return "" + (utility('bind')) + "(" + func + ", " + this.context + ")";
|
||||
}
|
||||
return this.front ? "(" + func + ")" : func;
|
||||
if (this.front) {
|
||||
return "(" + func + ")";
|
||||
} else {
|
||||
return func;
|
||||
}
|
||||
};
|
||||
Code.prototype.traverseChildren = function(crossScope, func) {
|
||||
return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : void 0;
|
||||
if (crossScope) {
|
||||
return Code.__super__.traverseChildren.call(this, crossScope, func);
|
||||
}
|
||||
};
|
||||
return Code;
|
||||
})();
|
||||
|
@ -1143,7 +1234,11 @@
|
|||
return this.name.assigns(name);
|
||||
};
|
||||
Splat.prototype.compile = function(o) {
|
||||
return this.index != null ? this.compileParam(o) : this.name.compile(o);
|
||||
if (this.index != null) {
|
||||
return this.compileParam(o);
|
||||
} else {
|
||||
return this.name.compile(o);
|
||||
}
|
||||
};
|
||||
Splat.compileSplattedArray = function(o, list, apply) {
|
||||
var args, base, code, i, index, node, _i, _len, _len2, _ref, _result;
|
||||
|
@ -1294,7 +1389,11 @@
|
|||
if (op = this.INVERSIONS[this.operator]) {
|
||||
this.operator = op;
|
||||
return this;
|
||||
} else return this.second ? new Parens(this).invert() : Op.__super__.invert.call(this);
|
||||
} else if (this.second) {
|
||||
return new Parens(this).invert();
|
||||
} else {
|
||||
return Op.__super__.invert.call(this);
|
||||
}
|
||||
};
|
||||
Op.prototype.unfoldSoak = function(o) {
|
||||
var _ref;
|
||||
|
@ -1321,7 +1420,11 @@
|
|||
fst = fst.slice(1, -1);
|
||||
}
|
||||
code = "" + fst + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
|
||||
return o.level < LEVEL_OP ? code : "(" + code + ")";
|
||||
if (o.level < LEVEL_OP) {
|
||||
return code;
|
||||
} else {
|
||||
return "(" + code + ")";
|
||||
}
|
||||
};
|
||||
Op.prototype.compileExistence = function(o) {
|
||||
var fst, ref;
|
||||
|
@ -1367,7 +1470,11 @@
|
|||
return this;
|
||||
};
|
||||
In.prototype.compileNode = function(o) {
|
||||
return this.array instanceof Value && this.array.isArray() ? this.compileOrTest(o) : this.compileLoopTest(o);
|
||||
if (this.array instanceof Value && this.array.isArray()) {
|
||||
return this.compileOrTest(o);
|
||||
} else {
|
||||
return this.compileLoopTest(o);
|
||||
}
|
||||
};
|
||||
In.prototype.compileOrTest = function(o) {
|
||||
var cmp, cnj, i, item, ref, sub, tests, _len, _ref, _ref2, _ref3, _result;
|
||||
|
@ -1383,7 +1490,11 @@
|
|||
return _result;
|
||||
}).call(this);
|
||||
tests = tests.join(cnj);
|
||||
return o.level < LEVEL_OP ? tests : "(" + tests + ")";
|
||||
if (o.level < LEVEL_OP) {
|
||||
return tests;
|
||||
} else {
|
||||
return "(" + tests + ")";
|
||||
}
|
||||
};
|
||||
In.prototype.compileLoopTest = function(o) {
|
||||
var code, ref, sub, _ref;
|
||||
|
@ -1393,7 +1504,11 @@
|
|||
return code;
|
||||
}
|
||||
code = sub + ', ' + code;
|
||||
return o.level < LEVEL_LIST ? code : "(" + code + ")";
|
||||
if (o.level < LEVEL_LIST) {
|
||||
return code;
|
||||
} else {
|
||||
return "(" + code + ")";
|
||||
}
|
||||
};
|
||||
In.prototype.toString = function(idt) {
|
||||
return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
|
||||
|
@ -1466,15 +1581,12 @@
|
|||
Existence.prototype.compileNode = function(o) {
|
||||
var code, sym;
|
||||
code = this.expression.compile(o);
|
||||
code = (function() {
|
||||
if (IDENTIFIER.test(code) && !o.scope.check(code)) {
|
||||
return this.negated ? "typeof " + code + " == \"undefined\" || " + code + " === null" : "typeof " + code + " != \"undefined\" && " + code + " !== null";
|
||||
} else {
|
||||
sym = this.negated ? '==' : '!=';
|
||||
return "" + code + " " + sym + " null";
|
||||
}
|
||||
}).call(this);
|
||||
return o.level <= LEVEL_COND ? code : "(" + code + ")";
|
||||
code = IDENTIFIER.test(code) && !o.scope.check(code) ? this.negated ? "typeof " + code + " == \"undefined\" || " + code + " === null" : "typeof " + code + " != \"undefined\" && " + code + " !== null" : (sym = this.negated ? '==' : '!=', "" + code + " " + sym + " null");
|
||||
if (o.level <= LEVEL_COND) {
|
||||
return code;
|
||||
} else {
|
||||
return "(" + code + ")";
|
||||
}
|
||||
};
|
||||
return Existence;
|
||||
})();
|
||||
|
@ -1506,7 +1618,11 @@
|
|||
}
|
||||
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call);
|
||||
code = expr.compile(o, LEVEL_PAREN);
|
||||
return bare ? code : "(" + code + ")";
|
||||
if (bare) {
|
||||
return code;
|
||||
} else {
|
||||
return "(" + code + ")";
|
||||
}
|
||||
};
|
||||
return Parens;
|
||||
})();
|
||||
|
@ -1623,7 +1739,11 @@
|
|||
case -1:
|
||||
return '--';
|
||||
default:
|
||||
return pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar;
|
||||
if (pvar < 0) {
|
||||
return ' -= ' + pvar.slice(1);
|
||||
} else {
|
||||
return ' += ' + pvar;
|
||||
}
|
||||
}
|
||||
})()));
|
||||
}
|
||||
|
@ -1792,19 +1912,23 @@
|
|||
return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref = this.elseBodyNode()) != null ? _ref.isStatement(o) : void 0);
|
||||
};
|
||||
If.prototype.compileNode = function(o) {
|
||||
return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o);
|
||||
};
|
||||
If.prototype.makeReturn = function() {
|
||||
if (this.isStatement()) {
|
||||
this.body && (this.body = this.ensureExpressions(this.body.makeReturn()));
|
||||
this.elseBody && (this.elseBody = this.ensureExpressions(this.elseBody.makeReturn()));
|
||||
return this;
|
||||
if (this.isStatement(o)) {
|
||||
return this.compileStatement(o);
|
||||
} else {
|
||||
return new Return(this);
|
||||
return this.compileExpression(o);
|
||||
}
|
||||
};
|
||||
If.prototype.makeReturn = function() {
|
||||
this.body && (this.body = new Expressions([this.body.makeReturn()]));
|
||||
this.elseBody && (this.elseBody = new Expressions([this.elseBody.makeReturn()]));
|
||||
return this;
|
||||
};
|
||||
If.prototype.ensureExpressions = function(node) {
|
||||
return node instanceof Expressions ? node : new Expressions([node]);
|
||||
if (node instanceof Expressions) {
|
||||
return node;
|
||||
} else {
|
||||
return new Expressions([node]);
|
||||
}
|
||||
};
|
||||
If.prototype.compileStatement = function(o) {
|
||||
var body, child, cond, ifPart;
|
||||
|
@ -1822,10 +1946,7 @@
|
|||
if (!this.elseBody) {
|
||||
return ifPart;
|
||||
}
|
||||
return ifPart + ' else ' + (this.isChain ? this.elseBodyNode().compile(merge(o, {
|
||||
indent: this.tab,
|
||||
chainChild: true
|
||||
})) : "{\n" + (this.elseBody.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}");
|
||||
return ifPart + ' else ' + (this.isChain ? (o.indent = this.tab, o.chainChild = true, this.elseBody.unwrap().compile(o, LEVEL_TOP)) : "{\n" + (this.elseBody.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}");
|
||||
};
|
||||
If.prototype.compileExpression = function(o) {
|
||||
var alt, body, code, cond;
|
||||
|
@ -1833,7 +1954,11 @@
|
|||
body = this.bodyNode().compile(o, LEVEL_LIST);
|
||||
alt = this.elseBodyNode() ? this.elseBodyNode().compile(o, LEVEL_LIST) : 'void 0';
|
||||
code = "" + cond + " ? " + body + " : " + alt;
|
||||
return o.level >= LEVEL_COND ? "(" + code + ")" : code;
|
||||
if (o.level >= LEVEL_COND) {
|
||||
return "(" + code + ")";
|
||||
} else {
|
||||
return code;
|
||||
}
|
||||
};
|
||||
If.prototype.unfoldSoak = function() {
|
||||
return this.soak && this;
|
||||
|
@ -1875,7 +2000,11 @@
|
|||
func.noReturn = noReturn;
|
||||
}
|
||||
call = new Call(func, args);
|
||||
return statement ? Expressions.wrap([call]) : call;
|
||||
if (statement) {
|
||||
return Expressions.wrap([call]);
|
||||
} else {
|
||||
return call;
|
||||
}
|
||||
},
|
||||
literalArgs: function(node) {
|
||||
return node instanceof Literal && node.value === 'arguments';
|
||||
|
|
|
@ -92,7 +92,9 @@
|
|||
break;
|
||||
}
|
||||
}
|
||||
return i ? this.tokens.splice(0, i) : void 0;
|
||||
if (i) {
|
||||
return this.tokens.splice(0, i);
|
||||
}
|
||||
};
|
||||
exports.Rewriter.prototype.removeMidExpressionNewlines = function() {
|
||||
return this.scanTokens(function(token, i, tokens) {
|
||||
|
@ -277,7 +279,9 @@
|
|||
}
|
||||
original = token;
|
||||
this.detectEnd(i + 1, condition, function(token, i) {
|
||||
return token[0] !== 'INDENT' ? original[0] = 'POST_' + original[0] : void 0;
|
||||
if (token[0] !== 'INDENT') {
|
||||
return original[0] = 'POST_' + original[0];
|
||||
}
|
||||
});
|
||||
return 1;
|
||||
});
|
||||
|
|
18
lib/scope.js
18
lib/scope.js
|
@ -27,10 +27,14 @@
|
|||
Scope.root = null;
|
||||
Scope.prototype.add = function(name, type) {
|
||||
var pos;
|
||||
return typeof (pos = this.positions[name]) === 'number' ? this.variables[pos].type = type : this.positions[name] = this.variables.push({
|
||||
name: name,
|
||||
type: type
|
||||
}) - 1;
|
||||
if (typeof (pos = this.positions[name]) === 'number') {
|
||||
return this.variables[pos].type = type;
|
||||
} else {
|
||||
return this.positions[name] = this.variables.push({
|
||||
name: name,
|
||||
type: type
|
||||
}) - 1;
|
||||
}
|
||||
};
|
||||
Scope.prototype.startLevel = function() {
|
||||
this.garbage.push([]);
|
||||
|
@ -77,7 +81,11 @@
|
|||
return !!((_ref = this.parent) != null ? _ref.check(name) : void 0);
|
||||
};
|
||||
Scope.prototype.temporary = function(name, index) {
|
||||
return name.length > 1 ? '_' + name + (index > 1 ? index : '') : '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
|
||||
if (name.length > 1) {
|
||||
return '_' + name + (index > 1 ? index : '');
|
||||
} else {
|
||||
return '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
|
||||
}
|
||||
};
|
||||
Scope.prototype.type = function(name) {
|
||||
var v, _i, _len, _ref;
|
||||
|
|
|
@ -35,7 +35,7 @@ exports.Base = class Base
|
|||
# already been asked to return the result (because statements know how to
|
||||
# return results).
|
||||
compile: (o, lvl) ->
|
||||
o = if o then extend {}, o else {}
|
||||
o = extend {}, o
|
||||
o.level = lvl if lvl?
|
||||
node = @unfoldSoak(o) or this
|
||||
node.tab = o.indent
|
||||
|
@ -160,8 +160,6 @@ exports.Expressions = class Expressions extends Base
|
|||
|
||||
children: ['expressions']
|
||||
|
||||
isStatement: YES
|
||||
|
||||
constructor: (nodes) ->
|
||||
@expressions = compact flatten nodes or []
|
||||
|
||||
|
@ -188,6 +186,11 @@ exports.Expressions = class Expressions extends Base
|
|||
isEmpty: ->
|
||||
not @expressions.length
|
||||
|
||||
isStatement: (o) ->
|
||||
for exp in @expressions when exp.isPureStatement() or exp.isStatement o
|
||||
return yes
|
||||
no
|
||||
|
||||
# An Expressions node does not return its entire body, rather it
|
||||
# ensures that the final expression is returned.
|
||||
makeReturn: ->
|
||||
|
@ -200,9 +203,25 @@ exports.Expressions = class Expressions extends Base
|
|||
compile: (o = {}, level) ->
|
||||
if o.scope then super o, level else @compileRoot o
|
||||
|
||||
# Compile all expressions within the **Expressions** body. If we need to
|
||||
# return the result, and it's an expression, simply return it. If it's a
|
||||
# statement, ask the statement to do so.
|
||||
compileNode: (o) ->
|
||||
@tab = o.indent
|
||||
(@compileExpression node, o for node in @expressions).join '\n'
|
||||
@tab = o.indent
|
||||
top = o.level is LEVEL_TOP
|
||||
codes = []
|
||||
for node in @expressions
|
||||
node = node.unwrapAll()
|
||||
node = (node.unfoldSoak(o) or node)
|
||||
if top
|
||||
node.front = true
|
||||
code = node.compile o
|
||||
codes.push if node.isStatement o then code else @tab + code + ';'
|
||||
else
|
||||
codes.push node.compile o, LEVEL_LIST
|
||||
return codes.join '\n' if top
|
||||
code = codes.join(', ') or 'void 0'
|
||||
if codes.length > 1 and o.level >= LEVEL_LIST then "(#{code})" else code
|
||||
|
||||
# If we happen to be the top-level **Expressions**, wrap everything in
|
||||
# a safety closure, unless requested not to.
|
||||
|
@ -219,6 +238,7 @@ exports.Expressions = class Expressions extends Base
|
|||
# Compile the expressions body for the contents of a function, with
|
||||
# declarations of all inner variables pushed up to the top.
|
||||
compileWithDeclarations: (o) ->
|
||||
o.level = LEVEL_TOP
|
||||
code = @compileNode o
|
||||
{scope} = o
|
||||
if scope.hasAssignments this
|
||||
|
@ -227,17 +247,6 @@ exports.Expressions = class Expressions extends Base
|
|||
code = "#{@tab}var #{ scope.compiledDeclarations() };\n#{code}"
|
||||
code
|
||||
|
||||
# Compiles a single expression within the expressions body. If we need to
|
||||
# return the result, and it's an expression, simply return it. If it's a
|
||||
# statement, ask the statement to do so.
|
||||
compileExpression: (node, o) ->
|
||||
node = node.unwrapAll()
|
||||
node = node.unfoldSoak(o) or node
|
||||
node.front = yes
|
||||
o.level = LEVEL_TOP
|
||||
code = node.compile o
|
||||
if node.isStatement o then code else @tab + code + ';'
|
||||
|
||||
# Wrap up the given nodes as an **Expressions**, unless it already happens
|
||||
# to be one.
|
||||
@wrap: (nodes) ->
|
||||
|
@ -254,7 +263,7 @@ exports.Literal = class Literal extends Base
|
|||
constructor: (@value) ->
|
||||
|
||||
makeReturn: ->
|
||||
if @isStatement() then this else super()
|
||||
if @isPureStatement() then this else new Return this
|
||||
|
||||
# Break and continue must be treated as pure statements -- they lose their
|
||||
# meaning when wrapped in a closure.
|
||||
|
@ -1467,12 +1476,9 @@ exports.If = class If extends Base
|
|||
if @isStatement o then @compileStatement o else @compileExpression o
|
||||
|
||||
makeReturn: ->
|
||||
if @isStatement()
|
||||
@body and= @ensureExpressions @body.makeReturn()
|
||||
@elseBody and= @ensureExpressions @elseBody.makeReturn()
|
||||
this
|
||||
else
|
||||
new Return this
|
||||
@body and= new Expressions [@body.makeReturn()]
|
||||
@elseBody and= new Expressions [@elseBody.makeReturn()]
|
||||
this
|
||||
|
||||
ensureExpressions: (node) ->
|
||||
if node instanceof Expressions then node else new Expressions [node]
|
||||
|
@ -1489,7 +1495,9 @@ exports.If = class If extends Base
|
|||
ifPart = @tab + ifPart unless child
|
||||
return ifPart unless @elseBody
|
||||
ifPart + ' else ' + if @isChain
|
||||
@elseBodyNode().compile merge o, indent: @tab, chainChild: true
|
||||
o.indent = @tab
|
||||
o.chainChild = yes
|
||||
@elseBody.unwrap().compile o, LEVEL_TOP
|
||||
else
|
||||
"{\n#{ @elseBody.compile o, LEVEL_TOP }\n#{@tab}}"
|
||||
|
||||
|
|
Loading…
Reference in New Issue