From dc1288d31908871f8b68aad1cf8211e91458a20e Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 24 Jul 2010 12:27:11 -0700 Subject: [PATCH] slightly optimizing return values of AssignNodes. Issue #539 --- lib/cake.js | 5 ++--- lib/coffee-script.js | 3 +-- lib/command.js | 3 +-- lib/lexer.js | 9 +++------ lib/nodes.js | 14 ++++++++------ lib/scope.js | 8 +++----- src/nodes.coffee | 5 ++++- 7 files changed, 22 insertions(+), 25 deletions(-) diff --git a/lib/cake.js b/lib/cake.js index d13f5b59..2dbc93e0 100755 --- a/lib/cake.js +++ b/lib/cake.js @@ -17,12 +17,11 @@ action = _a[0]; description = _a[1]; } - tasks[name] = { + return (tasks[name] = { name: name, description: description, action: action - }; - return tasks[name]; + }); }, option: function(letter, flag, description) { return switches.push([letter, flag, description]); diff --git a/lib/coffee-script.js b/lib/coffee-script.js index 8c3d6235..a476bb0f 100644 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -51,8 +51,7 @@ }, setInput: function(tokens) { this.tokens = tokens; - this.pos = 0; - return this.pos; + return (this.pos = 0); }, upcomingInput: function() { return ""; diff --git a/lib/command.js b/lib/command.js index fc898e70..f99355fd 100644 --- a/lib/command.js +++ b/lib/command.js @@ -195,8 +195,7 @@ options.compile = options.compile || !!o.output; options.run = !(o.compile || o.print || o.lint); options.print = !!(o.print || (o.eval || o.stdio && o.compile)); - sources = options.arguments; - return sources; + return (sources = options.arguments); }; compileOptions = function(source) { var o; diff --git a/lib/lexer.js b/lib/lexer.js index 946628d3..2f7d664e 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -399,8 +399,7 @@ } else if (_d === ')') { tok[0] = 'PARAM_END'; } else if (_d === '(' || _d === 'CALL_START') { - tok[0] = 'PARAM_START'; - return tok[0]; + return (tok[0] = 'PARAM_START'); } } return true; @@ -555,8 +554,7 @@ return null; } if (typeof newTag !== "undefined" && newTag !== null) { - tok[0] = newTag; - return tok[0]; + return (tok[0] = newTag); } return tok[0]; }; @@ -566,8 +564,7 @@ return null; } if (typeof val !== "undefined" && val !== null) { - tok[1] = val; - return tok[1]; + return (tok[1] = val); } return tok[1]; }; diff --git a/lib/nodes.js b/lib/nodes.js index f9b9c1f9..bd0516af 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -394,8 +394,7 @@ part = prop.compile(o); baseline += part; complete += part; - this.last = part; - return this.last; + return (this.last = part); } }).call(this); } @@ -447,7 +446,7 @@ CallNode.prototype.superReference = function(o) { var meth, methname; methname = o.scope.method.name; - meth = (function() { + return (meth = (function() { if (o.scope.method.proto) { return "" + (o.scope.method.proto) + ".__superClass__." + methname; } else if (methname) { @@ -455,8 +454,7 @@ } else { throw new Error("cannot call super on an anonymous function."); } - })(); - return meth; + })()); }; CallNode.prototype.compileNode = function(o) { var _b, _c, _d, _e, _f, _g, _h, arg, args, compilation; @@ -836,7 +834,11 @@ return this.variable instanceof ValueNode; }; AssignNode.prototype.makeReturn = function() { - return new Expressions([this, new ReturnNode(this.variable)]); + if (this.isStatement()) { + return new Expressions([this, new ReturnNode(this.variable)]); + } else { + return AssignNode.__superClass__.makeReturn.call(this); + } }; AssignNode.prototype.isStatement = function() { return this.isValue() && (this.variable.isArray() || this.variable.isObject()); diff --git a/lib/scope.js b/lib/scope.js index 149a65c4..44db0477 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -41,8 +41,7 @@ return false; }; Scope.prototype.parameter = function(name) { - this.variables[name] = 'param'; - return this.variables[name]; + return (this.variables[name] = 'param'); }; Scope.prototype.check = function(name) { if (this.variables.hasOwnProperty(name)) { @@ -60,11 +59,10 @@ return this.tempVar; }; Scope.prototype.assign = function(name, value) { - this.variables[name] = { + return (this.variables[name] = { value: value, assigned: true - }; - return this.variables[name]; + }); }; Scope.prototype.hasDeclarations = function(body) { return body === this.expressions && this.any(function(k, val) { diff --git a/src/nodes.coffee b/src/nodes.coffee index 051e0959..74cc25a0 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -758,7 +758,10 @@ exports.AssignNode: class AssignNode extends BaseNode @variable instanceof ValueNode makeReturn: -> - return new Expressions [this, new ReturnNode(@variable)] + if @isStatement() + return new Expressions [this, new ReturnNode(@variable)] + else + super() isStatement: -> @isValue() and (@variable.isArray() or @variable.isObject())