Skip to content

Commit

Permalink
fix: the LHS in for-of loop should not start with let (#13049)
Browse files Browse the repository at this point in the history
* fix: the LHS in for-of loop should not start with let

* Update packages/babel-parser/src/parser/statement.js

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Mar 29, 2021
1 parent 5c0d2f6 commit 8efbac4
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -57,6 +57,7 @@ export const ErrorMessages = Object.freeze({
"'from' is not allowed as an identifier after 'export default'",
ForInOfLoopInitializer:
"%0 loop variable declaration may not have an initializer",
ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
GeneratorInSingleStatementContext:
"Generators can only be declared at the top level or inside a block",
IllegalBreakContinue: "Unsyntactic %0",
Expand Down
26 changes: 21 additions & 5 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -125,6 +125,19 @@ export default class StatementParser extends ExpressionParser {
if (!this.isContextual("let")) {
return false;
}
return this.isLetKeyword(context);
}

/**
* Assuming we have seen a contextual `let`, check if it starts a variable declaration
so that `left` should be interpreted as a `let` keyword.
*
* @param {?string} context When `context` is non nullish, it will return early and _skip_ checking
if the next token after `let` is `{` or a keyword relational operator
* @returns {boolean}
* @memberof StatementParser
*/
isLetKeyword(context: ?string): boolean {
const next = this.nextTokenStart();
const nextCh = this.input.charCodeAt(next);
// For ambiguous cases, determine if a LexicalDeclaration (or only a
Expand Down Expand Up @@ -511,7 +524,8 @@ export default class StatementParser extends ExpressionParser {
return this.parseFor(node, null);
}

const isLet = this.isLet();
const startsWithLet = this.isContextual("let");
const isLet = startsWithLet && this.isLetKeyword();
if (this.match(tt._var) || this.match(tt._const) || isLet) {
const init = this.startNode();
const kind = isLet ? "let" : this.state.value;
Expand All @@ -533,11 +547,13 @@ export default class StatementParser extends ExpressionParser {

const refExpressionErrors = new ExpressionErrors();
const init = this.parseExpression(true, refExpressionErrors);
if (this.match(tt._in) || this.isContextual("of")) {
const isForOf = this.isContextual("of");
if (isForOf || this.match(tt._in)) {
if (isForOf && startsWithLet) {
this.raise(init.start, Errors.ForOfLet);
}
this.toAssignable(init, /* isLHS */ true);
const description = this.isContextual("of")
? "for-of statement"
: "for-in statement";
const description = isForOf ? "for-of statement" : "for-in statement";
this.checkLVal(init, description);
return this.parseForIn(node, init, awaitAt);
} else {
Expand Down
@@ -0,0 +1,3 @@
for (let.foo of []);
for (let().bar of []);
for (let``.bar of []);
@@ -0,0 +1,130 @@
{
"type": "File",
"start":0,"end":66,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":22}},
"errors": [
"SyntaxError: The left-hand side of a for-of loop may not start with 'let'. (1:5)",
"SyntaxError: The left-hand side of a for-of loop may not start with 'let'. (2:5)",
"SyntaxError: The left-hand side of a for-of loop may not start with 'let'. (3:5)"
],
"program": {
"type": "Program",
"start":0,"end":66,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":22}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ForOfStatement",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"await": false,
"left": {
"type": "MemberExpression",
"start":5,"end":12,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":12}},
"object": {
"type": "Identifier",
"start":5,"end":8,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":8},"identifierName":"let"},
"name": "let"
},
"computed": false,
"property": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"},
"name": "foo"
}
},
"right": {
"type": "ArrayExpression",
"start":16,"end":18,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":18}},
"elements": []
},
"body": {
"type": "EmptyStatement",
"start":19,"end":20,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":20}}
}
},
{
"type": "ForOfStatement",
"start":21,"end":43,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":22}},
"await": false,
"left": {
"type": "MemberExpression",
"start":26,"end":35,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":14}},
"object": {
"type": "CallExpression",
"start":26,"end":31,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":10}},
"callee": {
"type": "Identifier",
"start":26,"end":29,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":8},"identifierName":"let"},
"name": "let"
},
"arguments": []
},
"computed": false,
"property": {
"type": "Identifier",
"start":32,"end":35,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":14},"identifierName":"bar"},
"name": "bar"
}
},
"right": {
"type": "ArrayExpression",
"start":39,"end":41,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":20}},
"elements": []
},
"body": {
"type": "EmptyStatement",
"start":42,"end":43,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":22}}
}
},
{
"type": "ForOfStatement",
"start":44,"end":66,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":22}},
"await": false,
"left": {
"type": "MemberExpression",
"start":49,"end":58,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":14}},
"object": {
"type": "TaggedTemplateExpression",
"start":49,"end":54,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":10}},
"tag": {
"type": "Identifier",
"start":49,"end":52,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":8},"identifierName":"let"},
"name": "let"
},
"quasi": {
"type": "TemplateLiteral",
"start":52,"end":54,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":10}},
"expressions": [],
"quasis": [
{
"type": "TemplateElement",
"start":53,"end":53,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":9}},
"value": {
"raw": "",
"cooked": ""
},
"tail": true
}
]
}
},
"computed": false,
"property": {
"type": "Identifier",
"start":55,"end":58,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":14},"identifierName":"bar"},
"name": "bar"
}
},
"right": {
"type": "ArrayExpression",
"start":62,"end":64,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":20}},
"elements": []
},
"body": {
"type": "EmptyStatement",
"start":65,"end":66,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":22}}
}
}
],
"directives": []
}
}

0 comments on commit 8efbac4

Please sign in to comment.