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

babel-parser(ts): Raise recoverable error for abstract interface #12771

Merged
merged 4 commits into from Feb 9, 2021

Conversation

sosukesuzuki
Copy link
Member

Q                       A
Fixed Issues? Fixes #12768
Patch: Bug Fix? Y
Tests Added + Pass? Yes
License MIT

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 6, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit da8ce69:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 6, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/39972/

@sosukesuzuki sosukesuzuki added area: errors area: typescript pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Feb 6, 2021
@nicolo-ribaudo
Copy link
Member

Could you rebase? CI should be fixed now.

@JLHwung JLHwung self-requested a review February 8, 2021 15:07
Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

LGTM with some edge cases and code nits.

node: any,
): N.ClassDeclaration | N.TsInterfaceDeclaration {
const isClass = this.match(tt._class);
if (!isClass && !this.isContextual("interface")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I prefer to move this branch to the else branch of isClass:

if (isClass) {
  parseClass(...)
} else if (this.isContextual("interface")) {
  throw on abstract interface;
} else {
  throw on unexpected token;
}

TSErrors.NonClassMethodPropertyHasAbstractModifer,
);
this.next();
return this.tsParseInterfaceDeclaration(
Copy link
Contributor

Choose a reason for hiding this comment

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

interface should not have a proceeding line break:

interface
Foo {}

is invalid. So it does not suffice to conclude that it is an interface declaration when we see a contextual interface name.

// To avoid
// abstract interface
// Foo {}
if (!this.hasPrecedingLineBreak(this.lookahead())) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a way to avoid this lookahead?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe with a combination of lineBreak.test and this.nextTokenStart.
If it works, it would be useful to extract it to a hasFollowingLineBreak() method.

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems to work fine! da8ce69

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: errors area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

babel-parser(ts): Weird AST for abstract interface when errorRecovery is true
6 participants