mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-04-11 03:00:13 -04:00
removed extra parens from soak compilations
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
return eval(CoffeeScript.compile(code, options));
|
return eval(CoffeeScript.compile(code, options));
|
||||||
};
|
};
|
||||||
CoffeeScript.run = function(code, options) {
|
CoffeeScript.run = function(code, options) {
|
||||||
((options != null) ? options.bare = true : undefined);
|
(options != null) ? options.bare = true : undefined;
|
||||||
return Function(CoffeeScript.compile(code, options))();
|
return Function(CoffeeScript.compile(code, options))();
|
||||||
};
|
};
|
||||||
if (!(typeof window !== "undefined" && window !== null)) {
|
if (!(typeof window !== "undefined" && window !== null)) {
|
||||||
|
|||||||
54
lib/nodes.js
54
lib/nodes.js
@@ -56,7 +56,7 @@
|
|||||||
return [compiled, reference];
|
return [compiled, reference];
|
||||||
}
|
}
|
||||||
}).call(this);
|
}).call(this);
|
||||||
if (((options != null) ? options.precompile : undefined)) {
|
if ((options != null) ? options.precompile : undefined) {
|
||||||
for (i = 0, _len = pair.length; i < _len; i++) {
|
for (i = 0, _len = pair.length; i < _len; i++) {
|
||||||
node = pair[i];
|
node = pair[i];
|
||||||
(pair[i] = node.compile(o));
|
(pair[i] = node.compile(o));
|
||||||
@@ -301,7 +301,7 @@
|
|||||||
Return.prototype.makeReturn = THIS;
|
Return.prototype.makeReturn = THIS;
|
||||||
Return.prototype.compile = function(o) {
|
Return.prototype.compile = function(o) {
|
||||||
var _ref2, expr;
|
var _ref2, expr;
|
||||||
expr = (((_ref2 = this.expression) != null) ? _ref2.makeReturn() : undefined);
|
expr = ((_ref2 = this.expression) != null) ? _ref2.makeReturn() : undefined;
|
||||||
if (expr && !(expr instanceof Return)) {
|
if (expr && !(expr instanceof Return)) {
|
||||||
return expr.compile(o);
|
return expr.compile(o);
|
||||||
}
|
}
|
||||||
@@ -430,16 +430,19 @@
|
|||||||
fst = new Parens(new Assign(ref, fst));
|
fst = new Parens(new Assign(ref, fst));
|
||||||
snd.base = ref;
|
snd.base = ref;
|
||||||
}
|
}
|
||||||
ifn = new If(new Existence(fst), snd, {
|
return new If(new Existence(fst), snd, {
|
||||||
soak: true
|
soak: true,
|
||||||
|
operation: this.tags.operation
|
||||||
});
|
});
|
||||||
return ifn;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
Value.wrap = function(node) {
|
||||||
|
return node instanceof Value ? node : new Value(node);
|
||||||
|
};
|
||||||
return Value;
|
return Value;
|
||||||
})();
|
}).call(this);
|
||||||
exports.Comment = (function() {
|
exports.Comment = (function() {
|
||||||
Comment = (function() {
|
Comment = (function() {
|
||||||
function Comment(_arg) {
|
function Comment(_arg) {
|
||||||
@@ -496,13 +499,13 @@
|
|||||||
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) {
|
Call.prototype.unfoldSoak = function(o) {
|
||||||
var _i, _len, _ref2, _ref3, call, ifn, left, list, rite, val;
|
var _i, _len, _ref2, _ref3, call, ifn, left, list, rite;
|
||||||
if (this.soakNode) {
|
if (this.soakNode) {
|
||||||
if (val = this.variable) {
|
if (this.variable) {
|
||||||
if (!(val instanceof Value)) {
|
if (ifn = If.unfoldSoak(o, this, 'variable')) {
|
||||||
val = new Value(val);
|
return ifn;
|
||||||
}
|
}
|
||||||
_ref2 = val.cacheReference(o), left = _ref2[0], rite = _ref2[1];
|
_ref2 = Value.wrap(this.variable).cacheReference(o), left = _ref2[0], rite = _ref2[1];
|
||||||
} else {
|
} else {
|
||||||
left = new Literal(this.superReference(o));
|
left = new Literal(this.superReference(o));
|
||||||
rite = new Value(left);
|
rite = new Value(left);
|
||||||
@@ -510,10 +513,10 @@
|
|||||||
rite = new Call(rite, this.args);
|
rite = new Call(rite, this.args);
|
||||||
rite.isNew = this.isNew;
|
rite.isNew = this.isNew;
|
||||||
left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
|
left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
|
||||||
ifn = new If(left, new Value(rite), {
|
return new If(left, new Value(rite), {
|
||||||
soak: true
|
soak: true,
|
||||||
|
operation: this.tags.operation
|
||||||
});
|
});
|
||||||
return ifn;
|
|
||||||
}
|
}
|
||||||
call = this;
|
call = this;
|
||||||
list = [];
|
list = [];
|
||||||
@@ -549,7 +552,7 @@
|
|||||||
if (ifn = this.unfoldSoak(o)) {
|
if (ifn = this.unfoldSoak(o)) {
|
||||||
return ifn.compile(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++) {
|
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; _i++) {
|
||||||
arg = _ref3[_i];
|
arg = _ref3[_i];
|
||||||
if (arg instanceof Splat) {
|
if (arg instanceof Splat) {
|
||||||
@@ -576,9 +579,7 @@
|
|||||||
return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")");
|
return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")");
|
||||||
}
|
}
|
||||||
if (!this.isNew) {
|
if (!this.isNew) {
|
||||||
if (!((base = this.variable) instanceof Value)) {
|
base = Value.wrap(this.variable);
|
||||||
base = new Value(base);
|
|
||||||
}
|
|
||||||
if ((name = base.properties.pop()) && base.isComplex()) {
|
if ((name = base.properties.pop()) && base.isComplex()) {
|
||||||
ref = o.scope.freeVariable('this');
|
ref = o.scope.freeVariable('this');
|
||||||
fun = "(" + ref + " = " + (base.compile(o)) + ")" + (name.compile(o));
|
fun = "(" + ref + " = " + (base.compile(o)) + ")" + (name.compile(o));
|
||||||
@@ -1032,11 +1033,8 @@
|
|||||||
} else {
|
} 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);
|
|
||||||
}
|
|
||||||
accessClass = IDENTIFIER.test(idx.value) ? Accessor : Index;
|
accessClass = IDENTIFIER.test(idx.value) ? Accessor : Index;
|
||||||
value.properties.push(new accessClass(idx));
|
(value = Value.wrap(value)).properties.push(new accessClass(idx));
|
||||||
return new Assign(obj, value).compile(o);
|
return new Assign(obj, value).compile(o);
|
||||||
}
|
}
|
||||||
top = del(o, 'top');
|
top = del(o, 'top');
|
||||||
@@ -1298,7 +1296,7 @@
|
|||||||
function While(condition, opts) {
|
function While(condition, opts) {
|
||||||
While.__super__.constructor.call(this);
|
While.__super__.constructor.call(this);
|
||||||
this.condition = ((opts != null) ? opts.invert : undefined) ? condition.invert() : condition;
|
this.condition = ((opts != null) ? opts.invert : undefined) ? condition.invert() : condition;
|
||||||
this.guard = ((opts != null) ? opts.guard : undefined);
|
this.guard = (opts != null) ? opts.guard : undefined;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
return While;
|
return While;
|
||||||
@@ -1826,11 +1824,11 @@
|
|||||||
If.prototype.topSensitive = YES;
|
If.prototype.topSensitive = YES;
|
||||||
If.prototype.bodyNode = function() {
|
If.prototype.bodyNode = function() {
|
||||||
var _ref2;
|
var _ref2;
|
||||||
return (((_ref2 = this.body) != null) ? _ref2.unwrap() : undefined);
|
return ((_ref2 = this.body) != null) ? _ref2.unwrap() : undefined;
|
||||||
};
|
};
|
||||||
If.prototype.elseBodyNode = function() {
|
If.prototype.elseBodyNode = function() {
|
||||||
var _ref2;
|
var _ref2;
|
||||||
return (((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined);
|
return ((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined;
|
||||||
};
|
};
|
||||||
If.prototype.addElse = function(elseBody) {
|
If.prototype.addElse = function(elseBody) {
|
||||||
if (this.isChain) {
|
if (this.isChain) {
|
||||||
@@ -1881,8 +1879,9 @@
|
|||||||
};
|
};
|
||||||
If.prototype.compileExpression = function(o) {
|
If.prototype.compileExpression = function(o) {
|
||||||
var _ref2, code;
|
var _ref2, code;
|
||||||
|
this.condition.tags.operation = true;
|
||||||
code = this.condition.compile(o) + ' ? ' + this.bodyNode().compileBare(o) + ' : ' + (((_ref2 = this.elseBodyNode()) != null) ? _ref2.compileBare(o) : undefined);
|
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;
|
return this.tags.operation ? "(" + code + ")" : code;
|
||||||
};
|
};
|
||||||
If.prototype.unfoldSoak = function() {
|
If.prototype.unfoldSoak = function() {
|
||||||
return this.soakNode && this;
|
return this.soakNode && this;
|
||||||
@@ -1894,6 +1893,9 @@
|
|||||||
}
|
}
|
||||||
parent[name] = ifn.body;
|
parent[name] = ifn.body;
|
||||||
ifn.body = new Value(parent);
|
ifn.body = new Value(parent);
|
||||||
|
if (parent.tags.operation) {
|
||||||
|
ifn.tags.operation = true;
|
||||||
|
}
|
||||||
return ifn;
|
return ifn;
|
||||||
};
|
};
|
||||||
return If;
|
return If;
|
||||||
|
|||||||
@@ -147,7 +147,7 @@
|
|||||||
}
|
}
|
||||||
_ref = this.tokens.slice(i + 1, i + 4), one = _ref[0], two = _ref[1], three = _ref[2];
|
_ref = this.tokens.slice(i + 1, i + 4), one = _ref[0], two = _ref[1], three = _ref[2];
|
||||||
tag = token[0];
|
tag = token[0];
|
||||||
return (tag === 'TERMINATOR' || tag === 'OUTDENT') && !(((two != null) ? two[0] : undefined) === ':' || ((one != null) ? one[0] : undefined) === '@' && ((three != null) ? three[0] : undefined) === ':') || tag === ',' && !((_ref2 = ((one != null) ? one[0] : undefined)) === 'IDENTIFIER' || _ref2 === 'NUMBER' || _ref2 === 'STRING' || _ref2 === '@' || _ref2 === 'TERMINATOR' || _ref2 === 'OUTDENT');
|
return (tag === 'TERMINATOR' || tag === 'OUTDENT') && !(((two != null) ? two[0] : undefined) === ':' || ((one != null) ? one[0] : undefined) === '@' && ((three != null) ? three[0] : undefined) === ':') || tag === ',' && !((_ref2 = (one != null) ? one[0] : undefined) === 'IDENTIFIER' || _ref2 === 'NUMBER' || _ref2 === 'STRING' || _ref2 === '@' || _ref2 === 'TERMINATOR' || _ref2 === 'OUTDENT');
|
||||||
};
|
};
|
||||||
action = function(token, i) {
|
action = function(token, i) {
|
||||||
return this.tokens.splice(i, 0, ['}', '}', token[2]]);
|
return this.tokens.splice(i, 0, ['}', '}', token[2]]);
|
||||||
@@ -360,7 +360,7 @@
|
|||||||
};
|
};
|
||||||
exports.Rewriter.prototype.tag = function(i) {
|
exports.Rewriter.prototype.tag = function(i) {
|
||||||
var _ref;
|
var _ref;
|
||||||
return (((_ref = this.tokens[i]) != null) ? _ref[0] : undefined);
|
return ((_ref = this.tokens[i]) != null) ? _ref[0] : undefined;
|
||||||
};
|
};
|
||||||
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']];
|
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']];
|
||||||
INVERSES = {};
|
INVERSES = {};
|
||||||
|
|||||||
@@ -98,7 +98,7 @@
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
this.setVar(temp, 'var');
|
this.setVar(temp, 'var');
|
||||||
(((_ref2 = last(this.garbage)) != null) ? _ref2.push(temp) : undefined);
|
((_ref2 = last(this.garbage)) != null) ? _ref2.push(temp) : undefined;
|
||||||
return temp;
|
return temp;
|
||||||
};
|
};
|
||||||
Scope.prototype.assign = function(name, value) {
|
Scope.prototype.assign = function(name, value) {
|
||||||
|
|||||||
@@ -403,10 +403,11 @@ exports.Value = class Value extends Base
|
|||||||
ref = new Literal o.scope.freeVariable 'ref'
|
ref = new Literal o.scope.freeVariable 'ref'
|
||||||
fst = new Parens new Assign ref, fst
|
fst = new Parens new Assign ref, fst
|
||||||
snd.base = ref
|
snd.base = ref
|
||||||
ifn = new If new Existence(fst), snd, soak: yes
|
return new If new Existence(fst), snd, soak: on, operation: @tags.operation
|
||||||
return ifn
|
|
||||||
null
|
null
|
||||||
|
|
||||||
|
@wrap: (node) -> if node instanceof Value then node else new Value node
|
||||||
|
|
||||||
#### Comment
|
#### Comment
|
||||||
|
|
||||||
# CoffeeScript passes through block comments as JavaScript block comments
|
# CoffeeScript passes through block comments as JavaScript block comments
|
||||||
@@ -463,17 +464,16 @@ exports.Call = class Call extends Base
|
|||||||
# Soaked chained invocations unfold into if/else ternary structures.
|
# Soaked chained invocations unfold into if/else ternary structures.
|
||||||
unfoldSoak: (o) ->
|
unfoldSoak: (o) ->
|
||||||
if @soakNode
|
if @soakNode
|
||||||
if val = @variable
|
if @variable
|
||||||
val = new Value val unless val instanceof Value
|
return ifn if ifn = If.unfoldSoak o, this, 'variable'
|
||||||
[left, rite] = val.cacheReference o
|
[left, rite] = Value.wrap(@variable).cacheReference o
|
||||||
else
|
else
|
||||||
left = new Literal @superReference o
|
left = new Literal @superReference o
|
||||||
rite = new Value left
|
rite = new Value left
|
||||||
rite = new Call rite, @args
|
rite = new Call rite, @args
|
||||||
rite.isNew = @isNew
|
rite.isNew = @isNew
|
||||||
left = new Literal "typeof #{ left.compile o } === \"function\""
|
left = new Literal "typeof #{ left.compile o } === \"function\""
|
||||||
ifn = new If left, new Value(rite), soak: yes
|
return new If left, new Value(rite), soak: yes, operation: @tags.operation
|
||||||
return ifn
|
|
||||||
call = this
|
call = this
|
||||||
list = []
|
list = []
|
||||||
loop
|
loop
|
||||||
@@ -518,7 +518,7 @@ exports.Call = class Call extends Base
|
|||||||
splatargs = @compileSplatArguments o
|
splatargs = @compileSplatArguments o
|
||||||
return "#{ @superReference o }.apply(this, #{splatargs})" if @isSuper
|
return "#{ @superReference o }.apply(this, #{splatargs})" if @isSuper
|
||||||
unless @isNew
|
unless @isNew
|
||||||
base = new Value base unless (base = @variable) instanceof Value
|
base = Value.wrap @variable
|
||||||
if (name = base.properties.pop()) and base.isComplex()
|
if (name = base.properties.pop()) and base.isComplex()
|
||||||
ref = o.scope.freeVariable 'this'
|
ref = o.scope.freeVariable 'this'
|
||||||
fun = "(#{ref} = #{ base.compile o })#{ name.compile o }"
|
fun = "(#{ref} = #{ base.compile o })#{ name.compile o }"
|
||||||
@@ -893,9 +893,8 @@ exports.Assign = class Assign extends Base
|
|||||||
idx = if isObject
|
idx = if isObject
|
||||||
if obj.tags.this then obj.properties[0].name else obj
|
if obj.tags.this then obj.properties[0].name else obj
|
||||||
else new Literal 0
|
else new Literal 0
|
||||||
value = new Value value unless value instanceof Value
|
|
||||||
accessClass = if IDENTIFIER.test idx.value then Accessor else Index
|
accessClass = if IDENTIFIER.test idx.value then Accessor else Index
|
||||||
value.properties.push new accessClass idx
|
(value = Value.wrap value).properties.push new accessClass idx
|
||||||
return new Assign(obj, value).compile o
|
return new Assign(obj, value).compile o
|
||||||
top = del o, 'top'
|
top = del o, 'top'
|
||||||
otop = merge o, top: yes
|
otop = merge o, top: yes
|
||||||
@@ -1581,10 +1580,11 @@ exports.If = class If extends Base
|
|||||||
|
|
||||||
# Compile the If as a conditional operator.
|
# Compile the If as a conditional operator.
|
||||||
compileExpression: (o) ->
|
compileExpression: (o) ->
|
||||||
|
@condition.tags.operation = on
|
||||||
code = @condition.compile(o) + ' ? ' +
|
code = @condition.compile(o) + ' ? ' +
|
||||||
@bodyNode().compileBare(o) + ' : ' +
|
@bodyNode().compileBare(o) + ' : ' +
|
||||||
@elseBodyNode()?.compileBare o
|
@elseBodyNode()?.compileBare o
|
||||||
if @tags.operation or @soakNode then "(#{code})" else code
|
if @tags.operation then "(#{code})" else code
|
||||||
|
|
||||||
unfoldSoak: -> @soakNode and this
|
unfoldSoak: -> @soakNode and this
|
||||||
|
|
||||||
@@ -1593,6 +1593,7 @@ exports.If = class If extends Base
|
|||||||
return unless ifn = parent[name].unfoldSoak o
|
return unless ifn = parent[name].unfoldSoak o
|
||||||
parent[name] = ifn.body
|
parent[name] = ifn.body
|
||||||
ifn.body = new Value parent
|
ifn.body = new Value parent
|
||||||
|
ifn.tags.operation = on if parent.tags.operation
|
||||||
ifn
|
ifn
|
||||||
|
|
||||||
# Faux-Nodes
|
# Faux-Nodes
|
||||||
|
|||||||
Reference in New Issue
Block a user