From 39009dcfb95d2f619cb035b33b5ff03cd4c60584 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 12 Dec 2010 12:16:27 -0500 Subject: [PATCH] Fixing Issue #916. Overoptimization leading to empty var; --- lib/nodes.js | 2 +- lib/scope.js | 5 +++-- src/nodes.coffee | 2 +- src/scope.coffee | 6 ++++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 38ba2415..f8568134 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -282,7 +282,7 @@ post = this.compileNode(o); scope = o.scope; if (scope.expressions === this) { - if (!o.globals && o.scope.hasDeclarations) { + if (!o.globals && o.scope.hasDeclarations()) { code += "" + this.tab + "var " + (scope.declaredVariables().join(', ')) + ";\n"; } if (scope.hasAssignments) { diff --git a/lib/scope.js b/lib/scope.js index ce51464f..2fde772e 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -34,7 +34,6 @@ return true; } this.add(name, 'var'); - this.hasDeclarations = true; return false; }; Scope.prototype.parameter = function(name) { @@ -76,7 +75,6 @@ index++; } this.add(temp, 'var'); - this.hasDeclarations = true; return temp; }; Scope.prototype.assign = function(name, value) { @@ -86,6 +84,9 @@ }); return this.hasAssignments = true; }; + Scope.prototype.hasDeclarations = function() { + return !!this.declaredVariables().length; + }; Scope.prototype.declaredVariables = function() { var realVars, tempVars, v, _i, _len, _ref; realVars = []; diff --git a/src/nodes.coffee b/src/nodes.coffee index 64be28c6..0de8bfc2 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -245,7 +245,7 @@ exports.Expressions = class Expressions extends Base post = @compileNode o {scope} = o if scope.expressions is this - if not o.globals and o.scope.hasDeclarations + if not o.globals and o.scope.hasDeclarations() code += "#{@tab}var #{ scope.declaredVariables().join(', ') };\n" if scope.hasAssignments code += "#{@tab}var #{ multident scope.assignedVariables().join(', '), @tab };\n" diff --git a/src/scope.coffee b/src/scope.coffee index b40fceed..17a9d20a 100644 --- a/src/scope.coffee +++ b/src/scope.coffee @@ -34,7 +34,6 @@ exports.Scope = class Scope find: (name, options) -> return yes if @check name, options @add name, 'var' - @hasDeclarations = yes no # Reserve a variable name as originating from a function parameter for this @@ -68,7 +67,6 @@ exports.Scope = class Scope index = 0 index++ while @check((temp = @temporary type, index), true) @add temp, 'var' - @hasDeclarations = yes temp # Ensure that an assignment is made at the top of this scope @@ -77,6 +75,10 @@ exports.Scope = class Scope @add name, value: value, assigned: true @hasAssignments = yes + # Does this scope have any declared variables? + hasDeclarations: -> + !!@declaredVariables().length + # Return the list of variables first declared in this scope. declaredVariables: -> realVars = []