fixing return node squashing the possiblity of a ternary, when there should be one. Issue #475

This commit is contained in:
Jeremy Ashkenas 2010-07-06 23:04:35 -04:00
parent 577daf5457
commit 358edfb21f
6 changed files with 46 additions and 128 deletions

View File

@ -153,11 +153,7 @@
});
};
return path.exists(dir, function(exists) {
if (exists) {
return compile();
} else {
return exec(("mkdir -p " + dir), compile);
}
return exists ? compile() : exec(("mkdir -p " + dir), compile);
});
};
lint = function(js) {

View File

@ -313,11 +313,7 @@
],
SimpleArgs: [
o("Expression"), o("SimpleArgs , Expression", function() {
if ($1 instanceof Array) {
return $1.concat([$3]);
} else {
return [$1].concat([$3]);
}
return $1 instanceof Array ? $1.concat([$3]) : [$1].concat([$3]);
})
],
Try: [

View File

@ -345,11 +345,7 @@
return prev[0] === '@';
}
}).call(this);
if (accessor) {
return 'accessor';
} else {
return false;
}
return accessor ? 'accessor' : false;
};
Lexer.prototype.sanitizeHeredoc = function(doc, options) {
var _d, attempt, indent, match;
@ -444,11 +440,7 @@
}
throw new Error(("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1)));
}
if (!i) {
return false;
} else {
return str.substring(0, i);
}
return !i ? false : str.substring(0, i);
};
Lexer.prototype.interpolateString = function(str, escapeQuotes) {
var _d, _e, _f, _g, _h, _i, _j, escaped, expr, group, i, idx, inner, interp, interpolated, lexer, match, nested, pi, quote, tag, tok, token, tokens, value;
@ -568,11 +560,7 @@
if (!(m = this.chunk.match(regex))) {
return false;
}
if (m) {
return m[index];
} else {
return false;
}
return m ? m[index] : false;
};
Lexer.prototype.unfinished = function() {
var prev;

View File

@ -37,11 +37,7 @@
}
top = this.topSensitive() ? this.options.top : del(this.options, 'top');
closure = this.isStatement() && !this.isPureStatement() && !top && !this.options.asStatement && !(this instanceof CommentNode) && !this.containsPureStatement();
if (closure) {
return this.compileClosure(this.options);
} else {
return this.compileNode(this.options);
}
return closure ? this.compileClosure(this.options) : this.compileNode(this.options);
};
BaseNode.prototype.compileClosure = function(o) {
this.tab = o.indent;
@ -184,11 +180,7 @@
return this;
};
Expressions.prototype.unwrap = function() {
if (this.expressions.length === 1) {
return this.expressions[0];
} else {
return this;
}
return this.expressions.length === 1 ? this.expressions[0] : this;
};
Expressions.prototype.empty = function() {
return this.expressions.length === 0;
@ -208,11 +200,7 @@
};
Expressions.prototype.compile = function(o) {
o = o || {};
if (o.scope) {
return Expressions.__superClass__.compile.call(this, o);
} else {
return this.compileRoot(o);
}
return o.scope ? Expressions.__superClass__.compile.call(this, o) : this.compileRoot(o);
};
Expressions.prototype.compileNode = function(o) {
var _b, _c, _d, _e, node;
@ -232,11 +220,7 @@
code = o.globals ? this.compileNode(o) : this.compileWithDeclarations(o);
code = code.replace(TRAILING_WHITESPACE, '');
code = code.replace(DOUBLE_PARENS, '($1)');
if (o.noWrap) {
return code;
} else {
return "(function(){\n" + code + "\n})();\n";
}
return o.noWrap ? code : ("(function(){\n" + code + "\n})();\n");
};
Expressions.prototype.compileWithDeclarations = function(o) {
var code;
@ -255,11 +239,7 @@
compiledNode = node.compile(merge(o, {
top: true
}));
if (node.isStatement()) {
return compiledNode;
} else {
return "" + (this.idt()) + compiledNode + ";";
}
return node.isStatement() ? compiledNode : ("" + (this.idt()) + compiledNode + ";");
};
return Expressions;
})();
@ -352,18 +332,10 @@
return this.hasProperties() && this.properties[this.properties.length - 1] instanceof SliceNode;
};
ValueNode.prototype.makeReturn = function() {
if (this.hasProperties()) {
return ValueNode.__superClass__.makeReturn.call(this);
} else {
return this.base.makeReturn();
}
return this.hasProperties() ? ValueNode.__superClass__.makeReturn.call(this) : this.base.makeReturn();
};
ValueNode.prototype.unwrap = function() {
if (this.properties.length) {
return this;
} else {
return this.base;
}
return this.properties.length ? this : this.base;
};
ValueNode.prototype.isStatement = function() {
return this.base.isStatement && this.base.isStatement() && !this.hasProperties();
@ -413,11 +385,7 @@
this.last = part;
}
}
if (op && this.wrapped) {
return "(" + complete + ")";
} else {
return complete;
}
return op && this.wrapped ? ("(" + complete + ")") : complete;
};
return ValueNode;
})();
@ -460,11 +428,7 @@
return this;
};
CallNode.prototype.prefix = function() {
if (this.isNew) {
return 'new ';
} else {
return '';
}
return this.isNew ? 'new ' : '';
};
CallNode.prototype.superReference = function(o) {
var meth, methname;
@ -501,11 +465,7 @@
}).call(this).join(', ');
compilation = this.isSuper ? this.compileSuper(args, o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")");
}
if (o.operation && this.wrapped) {
return "(" + compilation + ")";
} else {
return compilation;
}
return o.operation && this.wrapped ? ("(" + compilation + ")") : compilation;
};
CallNode.prototype.compileSuper = function(args, o) {
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
@ -600,11 +560,7 @@
if (this.to !== this.toVar) {
parts.push(this.to.compile(o));
}
if (parts.length) {
return "" + (parts.join('; ')) + ";";
} else {
return '';
}
return parts.length ? ("" + (parts.join('; ')) + ";") : '';
};
RangeNode.prototype.compileNode = function(o) {
var equals, idx, op, step, vars;
@ -732,11 +688,7 @@
}
}
objects = objects.join('');
if (indexOf(objects, '\n') >= 0) {
return "[\n" + (this.idt(1)) + objects + "\n" + this.tab + "]";
} else {
return "[" + objects + "]";
}
return indexOf(objects, '\n') >= 0 ? ("[\n" + (this.idt(1)) + objects + "\n" + this.tab + "]") : ("[" + objects + "]");
};
return ArrayNode;
})();
@ -876,11 +828,7 @@
if (stmt) {
return ("" + this.tab + val + ";");
}
if (top) {
return val;
} else {
return "(" + val + ")";
}
return top ? val : ("(" + val + ")");
};
AssignNode.prototype.compilePatternMatch = function(o) {
var _b, _c, _d, accessClass, assigns, code, i, idx, isString, obj, oindex, olength, splat, val, valVar, value;
@ -1036,11 +984,7 @@
SplatNode.prototype.children = ['name'];
SplatNode.prototype.compileNode = function(o) {
var _b;
if ((typeof (_b = this.index) !== "undefined" && _b !== null)) {
return this.compileParam(o);
} else {
return this.name.compile(o);
}
return (typeof (_b = this.index) !== "undefined" && _b !== null) ? this.compileParam(o) : this.name.compile(o);
};
SplatNode.prototype.compileParam = function(o) {
var _b, _c, idx, len, name, pos, trailing, variadic;
@ -1249,11 +1193,7 @@
});
this.obj1 = _b[0];
this.obj2 = _b[1];
if (this.isArray()) {
return this.compileOrTest(o);
} else {
return this.compileLoopTest(o);
}
return this.isArray() ? this.compileOrTest(o) : this.compileLoopTest(o);
};
InNode.prototype.compileOrTest = function(o) {
var _b, _c, _d, i, item, tests;
@ -1380,11 +1320,7 @@
if (code.substr(l - 1, 1) === ';') {
code = code.substr(o, l - 1);
}
if (this.expression instanceof AssignNode) {
return code;
} else {
return "(" + code + ")";
}
return this.expression instanceof AssignNode ? code : ("(" + code + ")");
};
return ParentheticalNode;
})();
@ -1600,23 +1536,19 @@
}).call(this).join(' || ');
};
IfNode.prototype.compileNode = function(o) {
if (this.isStatement()) {
return this.compileStatement(o);
} else {
return this.compileTernary(o);
}
return this.isStatement() ? this.compileStatement(o) : this.compileTernary(o);
};
IfNode.prototype.makeReturn = function() {
this.body = this.body && this.ensureExpressions(this.body.makeReturn());
this.elseBody = this.elseBody && this.ensureExpressions(this.elseBody.makeReturn());
return this;
if (this.isStatement()) {
this.body = this.body && this.ensureExpressions(this.body.makeReturn());
this.elseBody = this.elseBody && this.ensureExpressions(this.elseBody.makeReturn());
return this;
} else {
return new ReturnNode(this);
}
};
IfNode.prototype.ensureExpressions = function(node) {
if (node instanceof Expressions) {
return node;
} else {
return new Expressions([node]);
}
return node instanceof Expressions ? node : new Expressions([node]);
};
IfNode.prototype.compileStatement = function(o) {
var body, child, comDent, condO, elsePart, ifDent, ifPart;
@ -1681,11 +1613,7 @@
func = new ValueNode(func, [new AccessorNode(meth)]);
}
call = new CallNode(func, args);
if (statement) {
return Expressions.wrap([call]);
} else {
return call;
}
return statement ? Expressions.wrap([call]) : call;
}
});
UTILITIES = {

View File

@ -1290,10 +1290,10 @@ exports.IfNode: class IfNode extends BaseNode
constructor: (condition, body, tags) ->
@condition: condition
@body: body
@elseBody: null
@elseBody: null
@tags: tags or {}
@condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert
@isChain: false
@isChain: false
bodyNode: -> @body?.unwrap()
elseBodyNode: -> @elseBody?.unwrap()
@ -1345,9 +1345,12 @@ exports.IfNode: class IfNode extends BaseNode
if @isStatement() then @compileStatement(o) else @compileTernary(o)
makeReturn: ->
@body: and @ensureExpressions(@body.makeReturn())
@elseBody: and @ensureExpressions(@elseBody.makeReturn())
this
if @isStatement()
@body: and @ensureExpressions(@body.makeReturn())
@elseBody: and @ensureExpressions(@elseBody.makeReturn())
this
else
new ReturnNode this
ensureExpressions: (node) ->
if node instanceof Expressions then node else new Expressions [node]

View File

@ -60,3 +60,10 @@ else if true
else
ok result is undefined
# Return an if with no else.
func: ->
return (if false then callback())
ok func() is null