Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: async arrow functions should not be allowed after binary operator. #11284

Merged
merged 4 commits into from Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditions here corresponds to atPossibleAsync. I was meant to add base.start === this.state.potentialArrowAt here so it can be rebranded as atPossibleAsyncArrow.

atPossibleAsyncArrow(base: N.Expression): boolean {
  return (base.type === "Identifier" &&
  base.name === "async" &&
  this.state.lastTokEnd === base.end &&
  !this.canInsertSemicolon() &&
  base.end - base.start === 5 &&
  base.start === this.state.potentialArrowAt)
}

The first 5 conditions are about atPossibleAsync and the last one is hasPossibleArrow so it combines into atPossibleAsyncArrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand. I'll add the extra condition to this atPossibleAsyncArrow method, and delete the atPossibleAsync method. This means I will also change

if (!noCalls && this.atPossibleAsync(base)) {

in the Typescript plugin to

if (!noCalls && this.atPossibleAsyncArrow(base)) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are going with the base.end - base.start === 5 route, could you add a comment in the code that it's needed to check that there are no escape sequences?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half joking: If this.state.lastTokEnd >= base.end && base.start >= this.state.potentialArrowAt is always true (I don't know). We can even further simplify it to

return (base.type === "Identifier" &&
  base.name === "async" &&
  !this.canInsertSemicolon() &&
  this.state.lastTokEnd - this.state.potentialArrowAt === 5)
)

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

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1738,7 +1738,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// 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 <T>() => ...
// But it might be a call with a type argument `async<T>();`
const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(
Expand Down
@@ -0,0 +1 @@
3 + async() => 2
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:12)"
}
@@ -0,0 +1 @@
delete async () => 3;
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:16)"
}
@@ -0,0 +1 @@
4 + async<number>() => 2
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["typescript"],
"throws": "Unexpected token, expected \";\" (1:20)"
}