Merge pull request #3002 from Nami-Doc/issue3001

Disallowed `for own in`
This commit is contained in:
Jeremy Ashkenas
2013-05-26 03:43:22 -07:00
4 changed files with 9 additions and 2 deletions

View File

@@ -2584,6 +2584,9 @@
if (this.range && this.pattern) {
this.name.error('cannot pattern match over range loops');
}
if (this.own && !this.object) {
this.index.error('cannot use own with for-in');
}
this.returns = false;
}

View File

@@ -1837,6 +1837,7 @@ exports.For = class For extends While
@pattern = @name instanceof Value
@index.error 'indexes do not apply to range loops' if @range and @index
@name.error 'cannot pattern match over range loops' if @range and @pattern
@index.error 'cannot use own with for-in' if @own and not @object
@returns = false
children: ['body', 'source', 'guard', 'step']

View File

@@ -81,4 +81,7 @@ test "#2846: while with empty body", ->
CoffeeScript.compile 'while 1 then', {sourceMap: true}
test "#2944: implicit call with a regex argument", ->
CoffeeScript.compile 'o[key] /regex/'
CoffeeScript.compile 'o[key] /regex/'
test "#3001: `own` shouldn't be allowed in a `for`-`in` loop", ->
cantCompile "a for own b in c"

View File

@@ -473,7 +473,7 @@ test "#1910: loop index should be mutable within a loop iteration and immutable
arr = ([v, v + 1] for v in [0..5])
iterations = 0
for own [v0, v1], k in arr when v0
for [v0, v1], k in arr when v0
k += 3
++iterations
eq 6, k