From 6cd106eb2b99bd2a4a7c9182636b5cc1e296998f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 27 Sep 2022 15:16:13 -0400 Subject: [PATCH] disallow of as a for-using-of binding --- packages/babel-parser/src/parser/statement.ts | 29 +----------- .../input.mjs | 0 .../options.json | 4 ++ .../input.js | 0 .../options.json | 3 ++ .../input.mjs | 1 + .../options.json | 0 .../output.json | 44 +++++++++++++++++++ .../output.json | 44 ------------------- .../input.js | 1 + .../output.json | 44 +++++++++++++++++++ .../valid-for-using-binding-of-of/output.json | 44 ------------------- 12 files changed, 99 insertions(+), 115 deletions(-) rename packages/babel-parser/test/fixtures/experimental/explicit-resource-management/{valid-for-await-using-binding-of-of => invalid-for-await-using-binding-of-of}/input.mjs (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-await-using-binding-of-of/options.json rename packages/babel-parser/test/fixtures/experimental/explicit-resource-management/{valid-for-using-binding-of-of => invalid-for-using-binding-of-of}/input.js (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-using-binding-of-of/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/input.mjs rename packages/babel-parser/test/fixtures/experimental/explicit-resource-management/{valid-for-await-using-binding-of-of => valid-for-await-using-binding-escaped-of-of}/options.json (100%) create mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/output.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/output.json delete mode 100644 packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-of-of/output.json diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index c1498e317c62..20fa09c57efd 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -317,33 +317,8 @@ export default abstract class StatementParser extends ExpressionParser { startsUsingForOf(): boolean { const lookahead = this.lookahead(); if (lookahead.type === tt._of && lookahead.end - lookahead.start === 2) { - // using of ... - const originalState = this.state; - // @ts-expect-error: Second lookahead - this.state = lookahead; - const lookaheadAhead = this.lookahead(); - this.state = originalState; - if ( - lookaheadAhead.type === tt._of && - lookaheadAhead.end - lookaheadAhead.start === 2 - ) { - // using of of ... - const nextNextNextTokenStart = this.nextTokenStartSince( - lookaheadAhead.end, - ); - if ( - this.input.charCodeAt(nextNextNextTokenStart) === - charCodes.rightParenthesis - ) { - // using of of) - return false; - } else { - this.expectPlugin("explicitResourceManagement"); - return true; - } - } else { - return false; - } + // `using of` must start a for-lhs-of statement + return false; } else { this.expectPlugin("explicitResourceManagement"); return true; diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/input.mjs b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-await-using-binding-of-of/input.mjs similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/input.mjs rename to packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-await-using-binding-of-of/input.mjs diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-await-using-binding-of-of/options.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-await-using-binding-of-of/options.json new file mode 100644 index 000000000000..253b9d69fbce --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-await-using-binding-of-of/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "throws": "Unexpected token, expected \")\" (1:23)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-of-of/input.js b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-using-binding-of-of/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-of-of/input.js rename to packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-using-binding-of-of/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-using-binding-of-of/options.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-using-binding-of-of/options.json new file mode 100644 index 000000000000..9432d2ede36d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-for-using-binding-of-of/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \")\" (1:17)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/input.mjs b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/input.mjs new file mode 100644 index 000000000000..510149a318e8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/input.mjs @@ -0,0 +1 @@ +for await (using \u006ff of of); diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/options.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/options.json rename to packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/options.json diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/output.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/output.json new file mode 100644 index 000000000000..67467ec8b50a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-escaped-of-of/output.json @@ -0,0 +1,44 @@ +{ + "type": "File", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}}, + "program": { + "type": "Program", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ForOfStatement", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}}, + "await": true, + "left": { + "type": "VariableDeclaration", + "start":11,"end":24,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":24,"index":24}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":24,"index":24}}, + "id": { + "type": "Identifier", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":24,"index":24},"identifierName":"of"}, + "name": "of" + }, + "init": null + } + ], + "kind": "using" + }, + "right": { + "type": "Identifier", + "start":28,"end":30,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":30,"index":30},"identifierName":"of"}, + "name": "of" + }, + "body": { + "type": "EmptyStatement", + "start":31,"end":32,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":32,"index":32}} + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/output.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/output.json deleted file mode 100644 index 2de1a53c7200..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-await-using-binding-of-of/output.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "type": "File", - "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, - "program": { - "type": "Program", - "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, - "sourceType": "module", - "interpreter": null, - "body": [ - { - "type": "ForOfStatement", - "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, - "await": true, - "left": { - "type": "VariableDeclaration", - "start":11,"end":19,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":19,"index":19}}, - "declarations": [ - { - "type": "VariableDeclarator", - "start":17,"end":19,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":19,"index":19}}, - "id": { - "type": "Identifier", - "start":17,"end":19,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":19,"index":19},"identifierName":"of"}, - "name": "of" - }, - "init": null - } - ], - "kind": "using" - }, - "right": { - "type": "Identifier", - "start":23,"end":25,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":25,"index":25},"identifierName":"of"}, - "name": "of" - }, - "body": { - "type": "EmptyStatement", - "start":26,"end":27,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":1,"column":27,"index":27}} - } - } - ], - "directives": [] - } -} diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/input.js b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/input.js new file mode 100644 index 000000000000..c879259290dd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/input.js @@ -0,0 +1 @@ +for (using o\u0066 of of); diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/output.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/output.json new file mode 100644 index 000000000000..a4be3572d81f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-escaped-of-of/output.json @@ -0,0 +1,44 @@ +{ + "type": "File", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":26,"index":26}}, + "program": { + "type": "Program", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":26,"index":26}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ForOfStatement", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":26,"index":26}}, + "await": false, + "left": { + "type": "VariableDeclaration", + "start":5,"end":18,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":18,"index":18}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":11,"end":18,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":18,"index":18}}, + "id": { + "type": "Identifier", + "start":11,"end":18,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":18,"index":18},"identifierName":"of"}, + "name": "of" + }, + "init": null + } + ], + "kind": "using" + }, + "right": { + "type": "Identifier", + "start":22,"end":24,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":1,"column":24,"index":24},"identifierName":"of"}, + "name": "of" + }, + "body": { + "type": "EmptyStatement", + "start":25,"end":26,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":26,"index":26}} + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-of-of/output.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-of-of/output.json deleted file mode 100644 index f11379d5df61..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/valid-for-using-binding-of-of/output.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "type": "File", - "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":21,"index":21}}, - "program": { - "type": "Program", - "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":21,"index":21}}, - "sourceType": "script", - "interpreter": null, - "body": [ - { - "type": "ForOfStatement", - "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":21,"index":21}}, - "await": false, - "left": { - "type": "VariableDeclaration", - "start":5,"end":13,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":13,"index":13}}, - "declarations": [ - { - "type": "VariableDeclarator", - "start":11,"end":13,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":13,"index":13}}, - "id": { - "type": "Identifier", - "start":11,"end":13,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":13,"index":13},"identifierName":"of"}, - "name": "of" - }, - "init": null - } - ], - "kind": "using" - }, - "right": { - "type": "Identifier", - "start":17,"end":19,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":19,"index":19},"identifierName":"of"}, - "name": "of" - }, - "body": { - "type": "EmptyStatement", - "start":20,"end":21,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":21,"index":21}} - } - } - ], - "directives": [] - } -}