From f3f34e9ef5031b4cd5706f411df54c2940c33016 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 20 Apr 2011 22:16:56 -0400 Subject: [PATCH] Fixes #1188, scope for self-referencing functions. --- lib/nodes.js | 20 ++++++++++---------- src/nodes.coffee | 12 ++++++------ test/function_literals.coffee | 8 ++++++++ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index ad5b84b2..c9defbe7 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1134,6 +1134,16 @@ } } name = this.variable.compile(o, LEVEL_LIST); + if (!(this.context || this.variable.isAssignable())) { + throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned."); + } + if (!(this.context || isValue && (this.variable.namespaced || this.variable.hasProperties()))) { + if (this.param) { + o.scope.add(name, 'var'); + } else { + o.scope.find(name); + } + } if (this.value instanceof Code && (match = this.METHOD_DEF.exec(name))) { this.value.name = match[2]; if (match[1]) { @@ -1144,16 +1154,6 @@ if (this.context === 'object') { return "" + name + ": " + val; } - if (!this.variable.isAssignable()) { - throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned."); - } - if (!(this.context || isValue && (this.variable.namespaced || this.variable.hasProperties()))) { - if (this.param) { - o.scope.add(name, 'var'); - } else { - o.scope.find(name); - } - } val = name + (" " + (this.context || '=') + " ") + val; if (o.level <= LEVEL_LIST) { return val; diff --git a/src/nodes.coffee b/src/nodes.coffee index 7d7aa6c0..8335bba8 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -907,18 +907,18 @@ exports.Assign = class Assign extends Base return @compileSplice o if @variable.isSplice() return @compileConditional o if @context in ['||=', '&&=', '?='] name = @variable.compile o, LEVEL_LIST - if @value instanceof Code and match = @METHOD_DEF.exec name - @value.name = match[2] - @value.klass = match[1] if match[1] - val = @value.compile o, LEVEL_LIST - return "#{name}: #{val}" if @context is 'object' - unless @variable.isAssignable() + unless @context or @variable.isAssignable() throw SyntaxError "\"#{ @variable.compile o }\" cannot be assigned." unless @context or isValue and (@variable.namespaced or @variable.hasProperties()) if @param o.scope.add name, 'var' else o.scope.find name + if @value instanceof Code and match = @METHOD_DEF.exec name + @value.name = match[2] + @value.klass = match[1] if match[1] + val = @value.compile o, LEVEL_LIST + return "#{name}: #{val}" if @context is 'object' val = name + " #{ @context or '=' } " + val if o.level <= LEVEL_LIST then val else "(#{val})" diff --git a/test/function_literals.coffee b/test/function_literals.coffee index 64c8a150..2d6b6f5f 100644 --- a/test/function_literals.coffee +++ b/test/function_literals.coffee @@ -64,6 +64,14 @@ ok obj isnt obj.unbound() eq obj, obj.nested() +test "self-referencing functions", -> + changeMe = -> + changeMe = 2 + + changeMe() + eq changeMe, 2 + + # Parameter List Features test "splats", ->