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

Make TS InstantiationExpr parsing more permissive #2188

Merged
merged 4 commits into from Apr 19, 2022
Merged

Make TS InstantiationExpr parsing more permissive #2188

merged 4 commits into from Apr 19, 2022

Conversation

g-plane
Copy link
Contributor

@g-plane g-plane commented Apr 18, 2022

Copy link
Owner

@evanw evanw left a comment

Choose a reason for hiding this comment

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

Thanks for giving me a heads up about this change in TypeScript!

js_lexer.TSlash,
js_lexer.TSlashEquals,
js_lexer.TIdentifier:
return false
Copy link
Owner

@evanw evanw Apr 18, 2022

Choose a reason for hiding this comment

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

Where did these come from? This doesn't appear to follow what Microsoft changed in their parser. For example, consider the following code:

f<T>
import "x"

f<T>
import("x")

After their PR, TypeScript parses it like this:

f;
import "x";

f < T > import("x");

But with your PR, esbuild parses it like this:

f;
import "x";
f;
import("x");

I tested this case with import because some of the code Microsoft is now using looks like this:

function isStartOfLeftHandSideExpression(): boolean {
  switch (token()) {
    case SyntaxKind.ThisKeyword:
    case SyntaxKind.SuperKeyword:
    case SyntaxKind.NullKeyword:
    case SyntaxKind.TrueKeyword:
    case SyntaxKind.FalseKeyword:
    case SyntaxKind.NumericLiteral:
    case SyntaxKind.BigIntLiteral:
    case SyntaxKind.StringLiteral:
    case SyntaxKind.NoSubstitutionTemplateLiteral:
    case SyntaxKind.TemplateHead:
    case SyntaxKind.OpenParenToken:
    case SyntaxKind.OpenBracketToken:
    case SyntaxKind.OpenBraceToken:
    case SyntaxKind.FunctionKeyword:
    case SyntaxKind.ClassKeyword:
    case SyntaxKind.NewKeyword:
    case SyntaxKind.SlashToken:
    case SyntaxKind.SlashEqualsToken:
    case SyntaxKind.Identifier:
      return true;
    case SyntaxKind.ImportKeyword:
      return lookAhead(nextTokenIsOpenParenOrLessThanOrDot);
    default:
      return isIdentifier();
  }
}

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 forgot to handle import token, and I will add it.

They changed to use an existing function isStartOfExpression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How can I do something similar to lookAhead in esbuild?

Copy link
Owner

Choose a reason for hiding this comment

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

It's kind of hacky. I stash a copy of p.lexer on the stack and then overwrite it again after backtracking has finished. I can take this PR from here and add the backtracking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest, I hope we can have a more common method to do backtracking.

@evanw evanw merged commit 1d71dff into evanw:master Apr 19, 2022
@g-plane g-plane deleted the fix-permissive-ts-instantiation-expr branch April 19, 2022 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants