Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat for loop body as part of loop scope #9586

Merged
merged 1 commit into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/babel-parser/src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,6 @@ export default class StatementParser extends ExpressionParser {
node.update = this.match(tt.parenR) ? null : this.parseExpression();
this.expect(tt.parenR);

this.scope.exit();
node.body =
// For the smartPipelines plugin: Disable topic references from outer
// contexts within the loop body. They are permitted in test expressions,
Expand All @@ -911,6 +910,7 @@ export default class StatementParser extends ExpressionParser {
this.parseStatement("for"),
);

this.scope.exit();
this.state.labels.pop();

return this.finishNode(node, "ForStatement");
Expand All @@ -937,7 +937,6 @@ export default class StatementParser extends ExpressionParser {
node.right = this.parseExpression();
this.expect(tt.parenR);

this.scope.exit();
node.body =
// For the smartPipelines plugin:
// Disable topic references from outer contexts within the loop body.
Expand All @@ -947,6 +946,7 @@ export default class StatementParser extends ExpressionParser {
this.parseStatement("for"),
);

this.scope.exit();
this.state.labels.pop();

return this.finishNode(node, type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (let i = 0;;) {
let i;
}
191 changes: 191 additions & 0 deletions packages/babel-parser/test/fixtures/core/scope/for-let/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"type": "File",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ForStatement",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"init": {
"type": "VariableDeclaration",
"start": 5,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 14
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 9,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
},
"identifierName": "i"
},
"name": "i"
},
"init": {
"type": "NumericLiteral",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
}
},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
],
"kind": "let"
},
"test": null,
"update": null,
"body": {
"type": "BlockStatement",
"start": 18,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "VariableDeclaration",
"start": 24,
"end": 30,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 10
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 28,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
}
},
"id": {
"type": "Identifier",
"start": 28,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "i"
},
"name": "i"
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (let i = 0;;) {
var i;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Identifier 'i' has already been declared (2:8)"
}
13 changes: 2 additions & 11 deletions scripts/tests/test262/test262_whitelist.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

annexB/language/statements/for-in/bare-initializer.js(default)
annexB/language/statements/for-in/bare-initializer.js(strict mode)
built-ins/RegExp/property-escapes/binary-property-with-value-ASCII_-_F-negated.js(default)
Expand Down Expand Up @@ -719,23 +720,13 @@ language/statements/for-in/dstr/array-rest-before-elision.js(default)
language/statements/for-in/dstr/array-rest-before-elision.js(strict mode)
language/statements/for-in/dstr/array-rest-elision-invalid.js(default)
language/statements/for-in/dstr/array-rest-elision-invalid.js(strict mode)
language/statements/for-in/head-const-bound-names-in-stmt.js(default)
language/statements/for-in/head-const-bound-names-in-stmt.js(strict mode)
language/statements/for-in/head-let-bound-names-in-stmt.js(default)
language/statements/for-in/head-let-bound-names-in-stmt.js(strict mode)
language/statements/for-of/dstr/array-rest-before-elision.js(default)
language/statements/for-of/dstr/array-rest-before-elision.js(strict mode)
language/statements/for-of/dstr/array-rest-elision-invalid.js(default)
language/statements/for-of/dstr/array-rest-elision-invalid.js(strict mode)
language/statements/for-of/head-const-bound-names-in-stmt.js(default)
language/statements/for-of/head-const-bound-names-in-stmt.js(strict mode)
language/statements/for-of/head-decl-no-expr.js(default)
language/statements/for-of/head-decl-no-expr.js(strict mode)
language/statements/for-of/head-expr-no-expr.js(default)
language/statements/for-of/head-expr-no-expr.js(strict mode)
language/statements/for-of/head-let-bound-names-in-stmt.js(default)
language/statements/for-of/head-let-bound-names-in-stmt.js(strict mode)
language/statements/for-of/head-var-no-expr.js(default)
language/statements/for-of/head-var-no-expr.js(strict mode)
language/statements/for/head-let-bound-names-in-stmt.js(default)
language/statements/for/head-let-bound-names-in-stmt.js(strict mode)
language/statements/for-of/head-var-no-expr.js(strict mode)