From 7a2a8d279412919f5a8119dc1de08644ee4cb1b6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 11 Aug 2017 15:57:47 -0400 Subject: [PATCH] init rhs of for-in loop head with correct scope - fixes #1480 --- src/ast/nodes/ForInStatement.js | 6 +++--- .../samples/for-loop-head-dependencies/_config.js | 3 +++ .../samples/for-loop-head-dependencies/main.js | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 test/function/samples/for-loop-head-dependencies/_config.js create mode 100644 test/function/samples/for-loop-head-dependencies/main.js diff --git a/src/ast/nodes/ForInStatement.js b/src/ast/nodes/ForInStatement.js index 3f0954b8f5c..4735a89f7f8 100644 --- a/src/ast/nodes/ForInStatement.js +++ b/src/ast/nodes/ForInStatement.js @@ -6,7 +6,7 @@ import { STRING } from '../values.js'; export default class ForInStatement 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 ); @@ -14,10 +14,10 @@ export default class ForInStatement extends Statement { } initialiseScope ( parentScope ) { - this.scope = new Scope( { + this.scope = new Scope({ parent: parentScope, isBlockScope: true, isLexicalBoundary: false - } ); + }); } } diff --git a/test/function/samples/for-loop-head-dependencies/_config.js b/test/function/samples/for-loop-head-dependencies/_config.js new file mode 100644 index 00000000000..50f9834fbab --- /dev/null +++ b/test/function/samples/for-loop-head-dependencies/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'uses parent scope for right-hand-side in for-in statement (#1480)' +}; \ No newline at end of file diff --git a/test/function/samples/for-loop-head-dependencies/main.js b/test/function/samples/for-loop-head-dependencies/main.js new file mode 100644 index 00000000000..d51c8b5bcab --- /dev/null +++ b/test/function/samples/for-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]); \ No newline at end of file