From 812ad554ab0aba636e25aa4270c814ceba8b2593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 6 Feb 2023 09:29:18 -0500 Subject: [PATCH] polish: improve "`await` as identifier" error in modules (#15400) --- .../babel-parser/src/parser/expression.ts | 43 +++++++++---------- .../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 ++-- scripts/integration-tests/e2e-prettier.sh | 5 +++ 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index 8779b6086e39..ff10ce7374fc 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -2793,7 +2793,27 @@ 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, + }); + return; + } else if (word === "yield") { if (this.prodParam.hasYield) { this.raise(Errors.YieldBindingIdentifier, { at: startLoc }); return; @@ -2818,27 +2838,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", diff --git a/scripts/integration-tests/e2e-prettier.sh b/scripts/integration-tests/e2e-prettier.sh index 4c12f203ee27..738ec4b40a0c 100755 --- a/scripts/integration-tests/e2e-prettier.sh +++ b/scripts/integration-tests/e2e-prettier.sh @@ -45,6 +45,11 @@ echo "export default () => () => {}" > src/main/create-print-pre-check-function. # Temporarily ignore tests, use `rm -f path/to/jsfmt.spec.js` # https://github.com/babel/babel/pull/15385#issuecomment-1409840781 +# 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