safer paren-wrapping for closures.

This commit is contained in:
Jeremy Ashkenas 2010-12-23 10:50:52 -08:00
parent ccfd369a77
commit f9a0bbbc20
8 changed files with 92 additions and 92 deletions

View File

@ -216,7 +216,7 @@
};
printTokens = function(tokens) {
var strings, tag, token, value;
strings = function() {
strings = (function() {
var _i, _len, _ref, _results;
_results = [];
for (_i = 0, _len = tokens.length; _i < _len; _i++) {
@ -225,7 +225,7 @@
_results.push("[" + tag + " " + value + "]");
}
return _results;
}();
})();
return printLine(strings.join(' '));
};
parseOptions = function() {

View File

@ -558,7 +558,7 @@
tokens = [];
for (name in grammar) {
alternatives = grammar[name];
grammar[name] = function() {
grammar[name] = (function() {
var _i, _j, _len, _len2, _ref, _results;
_results = [];
for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
@ -576,7 +576,7 @@
_results.push(alt);
}
return _results;
}();
})();
}
exports.parser = new Parser({
tokens: tokens.join(' '),

View File

@ -8,7 +8,7 @@
};
Rewriter = require('./rewriter').Rewriter;
_ref = require('./helpers'), count = _ref.count, starts = _ref.starts, compact = _ref.compact, last = _ref.last;
exports.Lexer = Lexer = function() {
exports.Lexer = Lexer = (function() {
function Lexer() {}
Lexer.prototype.tokenize = function(code, opts) {
var i;
@ -81,7 +81,7 @@
if (COFFEE_ALIASES.hasOwnProperty(id)) {
id = COFFEE_ALIASES[id];
}
tag = function() {
tag = (function() {
switch (id) {
case '!':
return 'UNARY';
@ -103,7 +103,7 @@
default:
return tag;
}
}();
})();
}
this.token(tag, id);
if (colon) {
@ -579,7 +579,7 @@
return quote + this.escapeLines(body, heredoc) + quote;
};
return Lexer;
}();
})();
JS_KEYWORDS = ['true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'if', 'else', 'switch', 'for', 'while', 'try', 'catch', 'finally', 'class', 'extends', 'super'];
COFFEE_KEYWORDS = ['undefined', 'then', 'unless', 'until', 'loop', 'of', 'by', 'when'];
for (op in COFFEE_ALIASES = {

View File

@ -24,7 +24,7 @@
this.negated = !this.negated;
return this;
};
exports.Base = Base = function() {
exports.Base = Base = (function() {
function Base() {}
Base.prototype.compile = function(o, lvl) {
var node;
@ -165,8 +165,8 @@
Base.prototype.unfoldSoak = NO;
Base.prototype.assigns = NO;
return Base;
}();
exports.Expressions = Expressions = function() {
})();
exports.Expressions = Expressions = (function() {
__extends(Expressions, Base);
function Expressions(nodes) {
this.expressions = compact(flatten(nodes || []));
@ -288,7 +288,9 @@
break;
}
}
o.level = LEVEL_TOP;
o = merge(o, {
level: LEVEL_TOP
});
if (i) {
rest = this.expressions.splice(i, this.expressions.length);
code = this.compileNode(o);
@ -313,8 +315,8 @@
return new Expressions(nodes);
};
return Expressions;
}();
exports.Literal = Literal = function() {
})();
exports.Literal = Literal = (function() {
__extends(Literal, Base);
function Literal(value) {
this.value = value;
@ -360,8 +362,8 @@
return ' "' + this.value + '"';
};
return Literal;
}();
exports.Return = Return = function() {
})();
exports.Return = Return = (function() {
__extends(Return, Base);
function Return(expression) {
this.expression = expression;
@ -380,12 +382,11 @@
}
};
Return.prototype.compileNode = function(o) {
o.level = LEVEL_PAREN;
return this.tab + ("return" + (this.expression ? ' ' + this.expression.compile(o) : '') + ";");
return this.tab + ("return" + (this.expression ? ' ' + this.expression.compile(o, LEVEL_PAREN) : '') + ";");
};
return Return;
}();
exports.Value = Value = function() {
})();
exports.Value = Value = (function() {
__extends(Value, Base);
function Value(base, props, tag) {
if (!props && base instanceof Value) {
@ -522,8 +523,8 @@
return null;
};
return Value;
}();
exports.Comment = Comment = function() {
})();
exports.Comment = Comment = (function() {
__extends(Comment, Base);
function Comment(comment) {
this.comment = comment;
@ -539,8 +540,8 @@
return code;
};
return Comment;
}();
exports.Call = Call = function() {
})();
exports.Call = Call = (function() {
__extends(Call, Base);
function Call(variable, args, soak) {
this.args = args != null ? args : [];
@ -633,7 +634,7 @@
if (code = Splat.compileSplattedArray(o, this.args, true)) {
return this.compileSplat(o, code);
}
args = (function() {
args = ((function() {
var _i, _len, _ref, _results;
_ref = this.args;
_results = [];
@ -642,7 +643,7 @@
_results.push(arg.compile(o, LEVEL_LIST));
}
return _results;
}.call(this)).join(', ');
}).call(this)).join(', ');
if (this.isSuper) {
return this.superReference(o) + (".call(this" + (args && ', ' + args) + ")");
} else {
@ -677,8 +678,8 @@
return "" + fun + ".apply(" + ref + ", " + splatArgs + ")";
};
return Call;
}();
exports.Extends = Extends = function() {
})();
exports.Extends = Extends = (function() {
__extends(Extends, Base);
function Extends(child, parent) {
this.child = child;
@ -690,8 +691,8 @@
return new Call(new Value(new Literal(utility('extends'))), [this.child, this.parent]).compile(o);
};
return Extends;
}();
exports.Access = Access = function() {
})();
exports.Access = Access = (function() {
__extends(Access, Base);
function Access(name, tag) {
this.name = name;
@ -706,8 +707,8 @@
};
Access.prototype.isComplex = NO;
return Access;
}();
exports.Index = Index = function() {
})();
exports.Index = Index = (function() {
__extends(Index, Base);
function Index(index) {
this.index = index;
@ -720,8 +721,8 @@
return this.index.isComplex();
};
return Index;
}();
exports.Range = Range = function() {
})();
exports.Range = Range = (function() {
__extends(Range, Base);
Range.prototype.children = ['from', 'to'];
function Range(from, to, tag) {
@ -805,8 +806,8 @@
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
};
return Range;
}();
exports.Slice = Slice = function() {
})();
exports.Slice = Slice = (function() {
__extends(Slice, Base);
Slice.prototype.children = ['range'];
function Slice(range) {
@ -824,8 +825,8 @@
return ".slice(" + fromStr + (toStr || '') + ")";
};
return Slice;
}();
exports.Obj = Obj = function() {
})();
exports.Obj = Obj = (function() {
__extends(Obj, Base);
function Obj(props, generated) {
this.generated = generated != null ? generated : false;
@ -844,7 +845,7 @@
}
idt = o.indent += TAB;
lastNoncom = this.lastNonComment(this.properties);
props = function() {
props = (function() {
var _len, _results;
_results = [];
for (i = 0, _len = props.length; i < _len; i++) {
@ -859,7 +860,7 @@
_results.push(indent + prop.compile(o, LEVEL_TOP) + join);
}
return _results;
}();
})();
props = props.join('');
obj = "{" + (props && '\n' + props + '\n' + this.tab) + "}";
if (this.front) {
@ -880,8 +881,8 @@
return false;
};
return Obj;
}();
exports.Arr = Arr = function() {
})();
exports.Arr = Arr = (function() {
__extends(Arr, Base);
function Arr(objs) {
this.objects = objs || [];
@ -896,7 +897,7 @@
if (code = Splat.compileSplattedArray(o, this.objects)) {
return code;
}
code = (function() {
code = ((function() {
var _i, _len, _ref, _results;
_ref = this.objects;
_results = [];
@ -905,7 +906,7 @@
_results.push(obj.compile(o, LEVEL_LIST));
}
return _results;
}.call(this)).join(', ');
}).call(this)).join(', ');
if (code.indexOf('\n') >= 0) {
return "[\n" + o.indent + code + "\n" + this.tab + "]";
} else {
@ -924,8 +925,8 @@
return false;
};
return Arr;
}();
exports.Class = Class = function() {
})();
exports.Class = Class = (function() {
__extends(Class, Base);
function Class(variable, parent, body) {
this.variable = variable;
@ -1056,8 +1057,8 @@
return klass.compile(o);
};
return Class;
}();
exports.Assign = Assign = function() {
})();
exports.Assign = Assign = (function() {
__extends(Assign, Base);
function Assign(variable, value, context, options) {
this.variable = variable;
@ -1225,8 +1226,8 @@
return "[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat(" + val + "))";
};
return Assign;
}();
exports.Code = Code = function() {
})();
exports.Code = Code = (function() {
__extends(Code, Base);
function Code(params, body, tag) {
this.params = params || [];
@ -1254,7 +1255,7 @@
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
param = _ref[_i];
if (param.splat) {
splats = new Assign(new Value(new Arr(function() {
splats = new Assign(new Value(new Arr((function() {
var _i, _len, _ref, _results;
_ref = this.params;
_results = [];
@ -1263,7 +1264,7 @@
_results.push(p.asReference(o));
}
return _results;
}.call(this))), new Value(new Literal('arguments')));
}).call(this))), new Value(new Literal('arguments')));
break;
}
}
@ -1322,7 +1323,7 @@
if (this.bound) {
return utility('bind') + ("(" + code + ", " + this.context + ")");
}
if (this.front) {
if (this.front || (o.level >= LEVEL_ACCESS)) {
return "(" + code + ")";
} else {
return code;
@ -1334,8 +1335,8 @@
}
};
return Code;
}();
exports.Param = Param = function() {
})();
exports.Param = Param = (function() {
__extends(Param, Base);
function Param(name, value, splat) {
this.name = name;
@ -1370,8 +1371,8 @@
return this.name.isComplex();
};
return Param;
}();
exports.Splat = Splat = function() {
})();
exports.Splat = Splat = (function() {
__extends(Splat, Base);
Splat.prototype.children = ['name'];
Splat.prototype.isAssignable = YES;
@ -1413,7 +1414,7 @@
if (index === 0) {
return args[0] + (".concat(" + (args.slice(1).join(', ')) + ")");
}
base = (function() {
base = ((function() {
var _i, _len, _ref, _results;
_ref = list.slice(0, index);
_results = [];
@ -1422,12 +1423,12 @@
_results.push(node.compile(o, LEVEL_LIST));
}
return _results;
}());
})());
return "[" + (base.join(', ')) + "].concat(" + (args.join(', ')) + ")";
};
return Splat;
}();
exports.While = While = function() {
})();
exports.While = While = (function() {
__extends(While, Base);
function While(condition, options) {
this.condition = (options != null ? options.invert : void 0) ? condition.invert() : condition;
@ -1486,8 +1487,8 @@
return code;
};
return While;
}();
exports.Op = Op = function() {
})();
exports.Op = Op = (function() {
var CONVERSIONS, INVERSIONS;
__extends(Op, Base);
function Op(op, first, second, flip) {
@ -1616,8 +1617,8 @@
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
};
return Op;
}();
exports.In = In = function() {
})();
exports.In = In = (function() {
__extends(In, Base);
function In(object, array) {
this.object = object;
@ -1636,7 +1637,7 @@
var cmp, cnj, i, item, ref, sub, tests, _ref, _ref2;
_ref = this.object.cache(o, LEVEL_OP), sub = _ref[0], ref = _ref[1];
_ref2 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref2[0], cnj = _ref2[1];
tests = function() {
tests = (function() {
var _len, _ref, _results;
_ref = this.array.base.objects;
_results = [];
@ -1645,7 +1646,7 @@
_results.push((i ? ref : sub) + cmp + item.compile(o, LEVEL_OP));
}
return _results;
}.call(this);
}).call(this);
tests = tests.join(cnj);
if (o.level < LEVEL_OP) {
return tests;
@ -1671,8 +1672,8 @@
return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
};
return In;
}();
exports.Try = Try = function() {
})();
exports.Try = Try = (function() {
__extends(Try, Base);
function Try(attempt, error, recovery, ensure) {
this.attempt = attempt;
@ -1703,8 +1704,8 @@
return ("" + this.tab + "try {\n" + (this.attempt.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" + (catchPart || '')) + (this.ensure ? " finally {\n" + (this.ensure.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : '');
};
return Try;
}();
exports.Throw = Throw = function() {
})();
exports.Throw = Throw = (function() {
__extends(Throw, Base);
function Throw(expression) {
this.expression = expression;
@ -1717,8 +1718,8 @@
return this.tab + ("throw " + (this.expression.compile(o)) + ";");
};
return Throw;
}();
exports.Existence = Existence = function() {
})();
exports.Existence = Existence = (function() {
__extends(Existence, Base);
function Existence(expression) {
this.expression = expression;
@ -1736,8 +1737,8 @@
}
};
return Existence;
}();
exports.Parens = Parens = function() {
})();
exports.Parens = Parens = (function() {
__extends(Parens, Base);
function Parens(body) {
this.body = body;
@ -1768,8 +1769,8 @@
}
};
return Parens;
}();
exports.For = For = function() {
})();
exports.For = For = (function() {
__extends(For, Base);
function For(body, source) {
var _ref;
@ -1922,8 +1923,8 @@
return defs;
};
return For;
}();
exports.Switch = Switch = function() {
})();
exports.Switch = Switch = (function() {
__extends(Switch, Base);
function Switch(subject, cases, otherwise) {
this.subject = subject;
@ -1994,8 +1995,8 @@
return code + this.tab + '}';
};
return Switch;
}();
exports.If = If = function() {
})();
exports.If = If = (function() {
__extends(If, Base);
function If(condition, body, options) {
this.body = body;
@ -2086,7 +2087,7 @@
return this.soak && this;
};
return If;
}();
})();
Push = {
wrap: function(name, exps) {
if (exps.isEmpty() || last(exps.expressions).jumps()) {

View File

@ -1,6 +1,6 @@
(function() {
var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments;
exports.OptionParser = OptionParser = function() {
exports.OptionParser = OptionParser = (function() {
function OptionParser(rules, banner) {
this.banner = banner;
this.rules = buildRules(rules);
@ -57,7 +57,7 @@
return "\n" + (lines.join('\n')) + "\n";
};
return OptionParser;
}();
})();
LONG_FLAG = /^(--\w[\w\-]+)/;
SHORT_FLAG = /^(-\w)/;
MULTI_FLAG = /^-(\w{2,})/;

View File

@ -6,7 +6,7 @@
}
return -1;
}, __slice = Array.prototype.slice;
exports.Rewriter = function() {
exports.Rewriter = (function() {
function Rewriter() {}
Rewriter.prototype.rewrite = function(tokens) {
this.tokens = tokens;
@ -337,7 +337,7 @@
return (_ref = this.tokens[i]) != null ? _ref[0] : void 0;
};
return Rewriter;
}();
})();
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']];
INVERSES = {};
EXPRESSION_START = [];

View File

@ -1,7 +1,7 @@
(function() {
var Scope, extend, last, _ref;
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
exports.Scope = Scope = function() {
exports.Scope = Scope = (function() {
Scope.root = null;
function Scope(parent, expressions, method) {
this.parent = parent;
@ -116,5 +116,5 @@
return _results;
};
return Scope;
}();
})();
}).call(this);

View File

@ -242,7 +242,7 @@ exports.Expressions = class Expressions extends Base
for exp, i in @expressions
exp = exp.unwrap()
break unless exp instanceof Comment or exp instanceof Literal
o.level = LEVEL_TOP
o = merge(o, level: LEVEL_TOP)
if i
rest = @expressions.splice i, @expressions.length
code = @compileNode o
@ -313,8 +313,7 @@ exports.Return = class Return extends Base
if expr and expr not instanceof Return then expr.compile o, level else super o, level
compileNode: (o) ->
o.level = LEVEL_PAREN
@tab + "return#{ if @expression then ' ' + @expression.compile o else '' };"
@tab + "return#{ if @expression then ' ' + @expression.compile(o, LEVEL_PAREN) else '' };"
#### Value
@ -1044,7 +1043,7 @@ exports.Code = class Code extends Base
code += '}'
return @tab + code if @ctor
return utility('bind') + "(#{code}, #{@context})" if @bound
if @front then "(#{code})" else code
if @front or (o.level >= LEVEL_ACCESS) then "(#{code})" else code
# Short-circuit `traverseChildren` method to prevent it from crossing scope boundaries
# unless `crossScope` is `true`.