Skip to content

Commit

Permalink
Merge pull request #1542 from rollup/gh-1538
Browse files Browse the repository at this point in the history
init for loop scopes correctly
  • Loading branch information
Rich-Harris committed Aug 12, 2017
2 parents 5dec01d + f8f2fa7 commit c6af5c9
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ast/nodes/ForOfStatement.js
Expand Up @@ -6,7 +6,7 @@ import { UNKNOWN } from '../values.js';
export default class ForOfStatement extends Statement {
initialiseChildren () {
this.left.initialise( this.scope );
this.right.initialise( this.scope );
this.right.initialise( this.scope.parent );
this.body.initialiseAndReplaceScope ?
this.body.initialiseAndReplaceScope( this.scope ) :
this.body.initialise( this.scope );
Expand Down
8 changes: 6 additions & 2 deletions src/ast/nodes/ForStatement.js
Expand Up @@ -6,9 +6,13 @@ export default class ForStatement extends Statement {
if ( this.init ) this.init.initialise( this.scope );
if ( this.test ) this.test.initialise( this.scope );
if ( this.update ) this.update.initialise( this.scope );
this.body.initialiseAndReplaceScope ?
this.body.initialiseAndReplaceScope( this.scope ) :

if ( this.body.type === 'BlockStatement' ) {
this.body.initialiseScope( this.scope );
this.body.initialiseChildren();
} else {
this.body.initialise( this.scope );
}
}

initialiseScope ( parentScope ) {
Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'uses parent scope for right-hand-side in for-in statement (#1480)'
};
12 changes: 12 additions & 0 deletions test/function/samples/for-in-loop-head-dependencies/main.js
@@ -0,0 +1,12 @@
function foo() {
return ['x'];
}

const result = [];

for (let i in foo()) {
const foo = i;
result.push(foo);
}

assert.deepEqual(result, [0]);
@@ -1,3 +1,3 @@
module.exports = {
description: 'uses parent scope for right-hand-side in for-in statement (#1480)'
description: 'uses correct scope in for statement (#1538)'
};
8 changes: 4 additions & 4 deletions test/function/samples/for-loop-head-dependencies/main.js
@@ -1,12 +1,12 @@
function foo() {
return ['x'];
return ['x', 'y'];
}

const result = [];

for (let i in foo()) {
const foo = i;
for (let a = foo(), i = 0; i < a.length; ++i) {
const foo = a[i];
result.push(foo);
}

assert.deepEqual(result, [0]);
assert.deepEqual(result, ['x', 'y']);
@@ -0,0 +1,3 @@
module.exports = {
description: 'uses parent scope for right-hand-side in for-of statement (#1539)'
};
12 changes: 12 additions & 0 deletions test/function/samples/for-of-loop-head-dependencies/main.js
@@ -0,0 +1,12 @@
function foo() {
return ['x', 'y'];
}

const result = [];

for (const i of foo()) {
const foo = i;
result.push(i);
}

assert.deepEqual(result, ['x', 'y']);

0 comments on commit c6af5c9

Please sign in to comment.