Fixes #1188, scope for self-referencing functions.

This commit is contained in:
Jeremy Ashkenas 2011-04-20 22:16:56 -04:00
parent 2f39102026
commit f3f34e9ef5
3 changed files with 24 additions and 16 deletions

View File

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

View File

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

View File

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