1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fixing Issue #916. Overoptimization leading to empty var;

This commit is contained in:
Jeremy Ashkenas 2010-12-12 12:16:27 -05:00
parent a19ea4b662
commit 39009dcfb9
4 changed files with 9 additions and 6 deletions

View file

@ -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) {

View file

@ -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 = [];

View file

@ -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"

View file

@ -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 = []