Skip to content

Commit

Permalink
polish: improve "await as identifier" error in modules (#15400)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Feb 6, 2023
1 parent f9c2595 commit 812ad55
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
43 changes: 21 additions & 22 deletions packages/babel-parser/src/parser/expression.ts
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions scripts/integration-tests/e2e-prettier.sh
Expand Up @@ -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

0 comments on commit 812ad55

Please sign in to comment.