fix: async arrow functions should not be allowed after binary… (#11284)

* Forbid async arrow functions after binary operator.

This commit makes Babel throw an error when parsing
code like "3 + async() => 2".

* Make atPossibleAsync more accurate

* Change atPossibleAsync to atPossibleAsyncArrow

Add an extra test to atPossibleAsync and refactor it to
atPossibleAsyncArrow. This also fixes a bug in the Typescript plugin,
so a new test has been added.

* Add test for async arrow after unary operator
This commit is contained in:
Vedant Roy
2020-03-21 14:38:36 -04:00
committed by GitHub
parent a9b430b464
commit 0e5c1da659
8 changed files with 20 additions and 4 deletions

View File

@@ -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
);
}