diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 1d49fe0e4e..d97a429ee0 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -556,7 +556,7 @@ export default class ExpressionParser extends LValParser { ): N.Expression { const state = { optionalChainMember: false, - maybeAsyncArrow: this.atPossibleAsync(base), + maybeAsyncArrow: this.atPossibleAsyncArrow(base), stop: false, }; do { @@ -748,13 +748,15 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "TaggedTemplateExpression"); } - atPossibleAsync(base: N.Expression): boolean { + atPossibleAsyncArrow(base: N.Expression): boolean { return ( base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && - this.input.slice(base.start, base.end) === "async" + // check there are no escape sequences, such as \u{61}sync + base.end - base.start === 5 && + base.start === this.state.potentialArrowAt ); } diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 338ef9309b..41413e591b 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1738,7 +1738,7 @@ export default (superClass: Class): Class => // There are number of things we are going to "maybe" parse, like type arguments on // tagged template expressions. If any of them fail, walk it back and continue. const result = this.tsTryParseAndCatch(() => { - if (!noCalls && this.atPossibleAsync(base)) { + if (!noCalls && this.atPossibleAsyncArrow(base)) { // Almost certainly this is a generic async function `async () => ... // But it might be a call with a type argument `async();` const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction( diff --git a/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-binary-operator/input.js b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-binary-operator/input.js new file mode 100644 index 0000000000..4479b00e3d --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-binary-operator/input.js @@ -0,0 +1 @@ +3 + async() => 2 \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-binary-operator/options.json b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-binary-operator/options.json new file mode 100644 index 0000000000..f6781c9d58 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-binary-operator/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \";\" (1:12)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-unary-operator/input.js b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-unary-operator/input.js new file mode 100644 index 0000000000..8d94561ad9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-unary-operator/input.js @@ -0,0 +1 @@ +delete async () => 3; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-unary-operator/options.json b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-unary-operator/options.json new file mode 100644 index 0000000000..715f71e32a --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2016/simple-parameter-list/async-arrow-function-after-unary-operator/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \";\" (1:16)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/arrow-function/async-arrow-function-after-binary-operator/input.ts b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-arrow-function-after-binary-operator/input.ts new file mode 100644 index 0000000000..cace33f2df --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-arrow-function-after-binary-operator/input.ts @@ -0,0 +1 @@ +4 + async() => 2 diff --git a/packages/babel-parser/test/fixtures/typescript/arrow-function/async-arrow-function-after-binary-operator/options.json b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-arrow-function-after-binary-operator/options.json new file mode 100644 index 0000000000..90cd707a26 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-arrow-function-after-binary-operator/options.json @@ -0,0 +1,5 @@ +{ + "sourceType": "module", + "plugins": ["typescript"], + "throws": "Unexpected token, expected \";\" (1:20)" +}