removing the ability to force an if/else chain to be compiled as a statement. Ternaries will now be used as much as possible.

This commit is contained in:
Jeremy Ashkenas 2010-09-22 00:41:17 -04:00
parent c50cf23b7b
commit c435647589
9 changed files with 16 additions and 54 deletions

View File

@ -18,9 +18,7 @@
xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
return CoffeeScript.run(xhr.responseText, options);
}
return xhr.readyState === 4 ? CoffeeScript.run(xhr.responseText, options) : null;
};
return xhr.send(null);
};

View File

@ -67,9 +67,7 @@
desc = task.description ? ("# " + (task.description)) : '';
puts("cake " + (name) + (spaces) + " " + (desc));
}
if (switches.length) {
return puts(oparse.help());
}
return switches.length ? puts(oparse.help()) : null;
};
missingTask = function(task) {
puts("No such task: \"" + (task) + "\"");

View File

@ -81,9 +81,7 @@
fs.readFile(source, function(err, code) {
return compileScript(source, code.toString(), base);
});
if (opts.watch) {
return watch(source, base);
}
return opts.watch ? watch(source, base) : null;
}
});
});
@ -121,13 +119,7 @@
} else {
t.output = CoffeeScript.compile(t.input, t.options);
CoffeeScript.emit('success', task);
if (o.print) {
return print(t.output);
} else if (o.compile) {
return writeJs(t.file, t.output, base);
} else if (o.lint) {
return lint(t.output);
}
return o.print ? print(t.output) : (o.compile ? writeJs(t.file, t.output, base) : (o.lint ? lint(t.output) : null));
}
} catch (err) {
CoffeeScript.emit('failure', err, task);
@ -146,9 +138,7 @@
code = '';
stdin = process.openStdin();
stdin.on('data', function(buffer) {
if (buffer) {
return code += buffer.toString();
}
return buffer ? code += buffer.toString() : null;
});
return stdin.on('end', function() {
return compileScript('stdio', code);
@ -182,9 +172,7 @@
js = ' ';
}
return fs.writeFile(jsPath, js, function(err) {
if (opts.compile && opts.watch) {
return puts("Compiled " + (source));
}
return opts.compile && opts.watch ? puts("Compiled " + (source)) : null;
});
};
return path.exists(dir, function(exists) {

View File

@ -529,7 +529,7 @@
invert: true
});
}), o("IfBlock ELSE IF Expression Block", function() {
return $1.addElse((new IfNode($4, $5)).forceStatement());
return $1.addElse(new IfNode($4, $5));
}), o("IfBlock ELSE Block", function() {
return $1.addElse($3);
})

View File

@ -146,9 +146,7 @@
BaseNode.prototype.traverseChildren = function(crossScope, func) {
return this.eachChild(function(child) {
func.apply(this, arguments);
if (child instanceof BaseNode) {
return child.traverseChildren(crossScope, func);
}
return child instanceof BaseNode ? child.traverseChildren(crossScope, func) : null;
});
};
BaseNode.prototype["class"] = 'BaseNode';
@ -1135,9 +1133,7 @@
return true;
};
CodeNode.prototype.traverseChildren = function(crossScope, func) {
if (crossScope) {
return CodeNode.__super__.traverseChildren.call(this, crossScope, func);
}
return crossScope ? CodeNode.__super__.traverseChildren.call(this, crossScope, func) : null;
};
CodeNode.prototype.toString = function(idt) {
var _i, _len, _ref2, _result, child, children;
@ -1663,15 +1659,7 @@
if (!(topLevel)) {
rvar = scope.freeVariable('result');
}
ivar = (function() {
if (codeInBody) {
return scope.freeVariable('i');
} else if (range) {
return name;
} else {
return index || scope.freeVariable('i');
}
})();
ivar = codeInBody ? scope.freeVariable('i') : (range ? name : index || scope.freeVariable('i'));
varPart = '';
guardPart = '';
body = Expressions.wrap([this.body]);
@ -1829,10 +1817,6 @@
IfNode.prototype.elseBodyNode = function() {
return this.elseBody == null ? undefined : this.elseBody.unwrap();
};
IfNode.prototype.forceStatement = function() {
this.tags.statement = true;
return this;
};
IfNode.prototype.addElse = function(elseBody, statement) {
if (this.isChain) {
this.elseBodyNode().addElse(elseBody, statement);
@ -1843,7 +1827,7 @@
return this;
};
IfNode.prototype.isStatement = function(o) {
return this.statement || (this.statement = (!!((o && o.top) || this.tags.statement || this.bodyNode().isStatement(o) || (this.elseBody && this.elseBodyNode().isStatement(o)))));
return this.statement || (this.statement = (!!((o && o.top) || this.bodyNode().isStatement(o) || (this.elseBody && this.elseBodyNode().isStatement(o)))));
};
IfNode.prototype.compileCondition = function(o) {
var _i, _len, _ref2, _result, cond, conditions;

View File

@ -433,7 +433,7 @@ case 188:this.$ = new yy.IfNode($$[$0-3+2-1], $$[$0-3+3-1], {
invert: true
});
break;
case 189:this.$ = $$[$0-5+1-1].addElse((new yy.IfNode($$[$0-5+4-1], $$[$0-5+5-1])).forceStatement());
case 189:this.$ = $$[$0-5+1-1].addElse(new yy.IfNode($$[$0-5+4-1], $$[$0-5+5-1]));
break;
case 190:this.$ = $$[$0-3+1-1].addElse($$[$0-3+3-1]);
break;
@ -701,7 +701,7 @@ exports.main = function commonjsMain(args) {
}
return exports.parser.parse(source);
}
if (typeof module !== 'undefined' && require.main === module) {
if (typeof module !== "undefined" && require.main === module) {
exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
}
}

View File

@ -273,9 +273,7 @@
return ('TERMINATOR' === (_ref2 = token[0]) || 'INDENT' === _ref2);
};
action = function(token, i) {
if (token[0] !== 'INDENT') {
return (original[0] = 'POST_' + original[0]);
}
return token[0] !== 'INDENT' ? (original[0] = 'POST_' + original[0]) : null;
};
this.detectEnd(i + 1, condition, action);
return 1;

View File

@ -511,7 +511,7 @@ grammar =
IfBlock: [
o "IF Expression Block", -> new IfNode $2, $3
o "UNLESS Expression Block", -> new IfNode $2, $3, invert: true
o "IfBlock ELSE IF Expression Block", -> $1.addElse (new IfNode($4, $5)).forceStatement()
o "IfBlock ELSE IF Expression Block", -> $1.addElse new IfNode $4, $5
o "IfBlock ELSE Block", -> $1.addElse $3
]

View File

@ -1488,10 +1488,6 @@ exports.IfNode = class IfNode extends BaseNode
bodyNode: -> @body?.unwrap()
elseBodyNode: -> @elseBody?.unwrap()
forceStatement: ->
@tags.statement = true
this
# Rewrite a chain of **IfNodes** to add a default case as the final *else*.
addElse: (elseBody, statement) ->
if @isChain
@ -1504,7 +1500,7 @@ exports.IfNode = class IfNode extends BaseNode
# The **IfNode** only compiles into a statement if either of its bodies needs
# to be a statement. Otherwise a ternary is safe.
isStatement: (o) ->
@statement or= !!((o and o.top) or @tags.statement or @bodyNode().isStatement(o) or (@elseBody and @elseBodyNode().isStatement(o)))
@statement or= !!((o and o.top) or @bodyNode().isStatement(o) or (@elseBody and @elseBodyNode().isStatement(o)))
compileCondition: (o) ->
conditions = flatten [@condition]