-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: improve ts-only declaration parsing #17491
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
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/59875 |
|
commit: |
| } | ||
| case "global": | ||
| // `global { }` (with no `declare`) may appear inside an ambient module declaration. | ||
| // Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This todo item is also resolved now that the global module parsing has been moved to parseStatementContent when global is not yet consumed.
| if (this.isContextual(tt._interface)) { | ||
| const result = this.tsParseInterfaceDeclaration(this.startNode()); | ||
| if (result) return result; | ||
| if (!this.state.containsEsc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is redundant for tt._const, however it is more convenient to group tt._const handling with other keyword-likes.
| case tt._abstract: | ||
| case tt._declare: { | ||
| if ( | ||
| this.nextTokenIsIdentifierAndNotTSRelationalOperatorOnSameLine() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a finite number of keyword tokens allowed to follow abstract / declare. In reality most input source is valid, to simplify the lookahead logic and exit early for valid code, we check all identifiers except in, instanceof, as and satisifies: If a declaration can not be parsed, it must be an invalid production in both JS and TS.
| const node = this.startNode<N.TsTypeAliasDeclaration>(); | ||
| this.next(); // eat 'type' | ||
| const result = this.tsParseTypeAliasDeclaration(node); | ||
| if (result) return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is redundant, tsParseTypeAliasDeclaration always returns a node.
Handle TS declaration parsing in parseStatementContent such that we can align to the tsc behaviour when parsing `type as =` or `module as {}`. In both cases tsc treats `as` as an identifier name, but previously Babel parser treated it as rational operator because the ts-only declaration parsing was delayed after a JS expression is formed.
liuxingbaoyu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
Handle TS declaration parsing in
parseStatementContentsuch that we can align to the tsc behaviour when parsingtype as =ormodule as {}. In both cases tsc treatsasas an identifier name, but previously Babel parser treated it as rational operator because the ts-only declaration parsing was delayed after a JS expression is formed.This PR also fixes a similar case:
module as {}should be interpreted as aTSModuleDeclarationrather than aTSAsExpression.