From eda146334de3664ac305b1442dc3e8921ac5f984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 2 Feb 2023 12:12:32 -0500 Subject: [PATCH 1/3] polish: improve await as id error in module --- .../babel-parser/src/parser/expression.ts | 42 +++++++++---------- .../es2015/uncategorised/359/output.json | 2 +- .../es2015/uncategorised/361/output.json | 2 +- .../es2015/uncategorised/365/output.json | 2 +- .../es2015/uncategorised/367/output.json | 2 +- .../output.json | 8 ++-- 6 files changed, 28 insertions(+), 30 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index 8779b6086e39..6590a5119d13 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -2793,7 +2793,26 @@ export default abstract class ExpressionParser extends LValParser { return; } - if (word === "yield") { + if (checkKeywords && isKeyword(word)) { + this.raise(Errors.UnexpectedKeyword, { + at: startLoc, + keyword: word, + }); + return; + } + + const reservedTest = !this.state.strict + ? isReservedWord + : isBinding + ? isStrictBindReservedWord + : isStrictReservedWord; + + if (reservedTest(word, this.inModule)) { + this.raise(Errors.UnexpectedReservedWord, { + at: startLoc, + reservedWord: word, + }); + } else if (word === "yield") { if (this.prodParam.hasYield) { this.raise(Errors.YieldBindingIdentifier, { at: startLoc }); return; @@ -2818,27 +2837,6 @@ export default abstract class ExpressionParser extends LValParser { return; } } - - if (checkKeywords && isKeyword(word)) { - this.raise(Errors.UnexpectedKeyword, { - at: startLoc, - keyword: word, - }); - return; - } - - const reservedTest = !this.state.strict - ? isReservedWord - : isBinding - ? isStrictBindReservedWord - : isStrictReservedWord; - - if (reservedTest(word, this.inModule)) { - this.raise(Errors.UnexpectedReservedWord, { - at: startLoc, - reservedWord: word, - }); - } } isAwaitAllowed(): boolean { diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/359/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/359/output.json index 4a83b666bf41..fce105a68c20 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/359/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/359/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, "errors": [ - "SyntaxError: Can not use 'await' as identifier inside an async function. (1:6)" + "SyntaxError: Unexpected reserved word 'await'. (1:6)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/361/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/361/output.json index c91785f2207b..5aca97a9bfc5 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/361/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/361/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}}, "errors": [ - "SyntaxError: Can not use 'await' as identifier inside an async function. (1:8)" + "SyntaxError: Unexpected reserved word 'await'. (1:8)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/365/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/365/output.json index 3366002a3c28..e7bf901b32bd 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/365/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/365/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}}, "errors": [ - "SyntaxError: Can not use 'await' as identifier inside an async function. (1:9)" + "SyntaxError: Unexpected reserved word 'await'. (1:9)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/367/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/367/output.json index 53149d1273a7..19d4b1c58aec 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/367/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/367/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":14,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":14,"index":14}}, "errors": [ - "SyntaxError: Can not use 'await' as identifier inside an async function. (1:6)" + "SyntaxError: Unexpected reserved word 'await'. (1:6)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-using-binding-await-module/output.json b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-using-binding-await-module/output.json index 65286858f703..b1d1390b375a 100644 --- a/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-using-binding-await-module/output.json +++ b/packages/babel-parser/test/fixtures/experimental/explicit-resource-management/invalid-using-binding-await-module/output.json @@ -2,10 +2,10 @@ "type": "File", "start":0,"end":113,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":12,"column":1,"index":113}}, "errors": [ - "SyntaxError: Can not use 'await' as identifier inside an async function. (2:8)", - "SyntaxError: Can not use 'await' as identifier inside an async function. (5:8)", - "SyntaxError: Can not use 'await' as identifier inside an async function. (8:11)", - "SyntaxError: Can not use 'await' as identifier inside an async function. (11:13)" + "SyntaxError: Unexpected reserved word 'await'. (2:8)", + "SyntaxError: Unexpected reserved word 'await'. (5:8)", + "SyntaxError: Unexpected reserved word 'await'. (8:11)", + "SyntaxError: Unexpected reserved word 'await'. (11:13)" ], "program": { "type": "Program", From d0d90dda78502ff65bdc972459af114c117ebabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 2 Feb 2023 14:36:36 -0500 Subject: [PATCH 2/3] exit early when identifier is a reserved word --- packages/babel-parser/src/parser/expression.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index 6590a5119d13..ff10ce7374fc 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -2812,6 +2812,7 @@ export default abstract class ExpressionParser extends LValParser { at: startLoc, reservedWord: word, }); + return; } else if (word === "yield") { if (this.prodParam.hasYield) { this.raise(Errors.YieldBindingIdentifier, { at: startLoc }); From f96689d1e790cc96b90a27ec39af9eefc0528b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 3 Feb 2023 10:15:49 -0500 Subject: [PATCH 3/3] ignore tests/format/flow-repo/async/await_parse.js --- scripts/integration-tests/e2e-prettier.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/integration-tests/e2e-prettier.sh b/scripts/integration-tests/e2e-prettier.sh index a930b673dd87..221b3d99fbc3 100755 --- a/scripts/integration-tests/e2e-prettier.sh +++ b/scripts/integration-tests/e2e-prettier.sh @@ -47,6 +47,11 @@ echo "export default () => () => {}" > src/main/create-print-pre-check-function. rm tests/format/js/exports/jsfmt.spec.js rm tests/format/js/export-extension/jsfmt.spec.js +# https://github.com/babel/babel/pull/15400#issuecomment-1414539133 +# Ignore this test until prettier update the snapshot +# because prettier has ignored UnexpectedReservedWord error +rm tests/format/flow-repo/async/await_parse.js + yarn test "tests/format/(jsx?|misc|typescript|flow|flow-repo)/" --update-snapshot --runInBand cleanup