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

Raise recoverable error for type members with invalid modifiers #12785

Merged
merged 9 commits into from Feb 12, 2021

Conversation

sosukesuzuki
Copy link
Member

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

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 9, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 9, 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 b79fd6b:

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

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

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Is there a specific reason for ededec2? I actually preferred the previous version.

Comment on lines 597 to 605
[
"readonly",
"declare",
"abstract",
"private",
"protected",
"public",
"static",
],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[
"readonly",
"declare",
"abstract",
"private",
"protected",
"public",
"static",
],
["readonly", ...denyModifiers],

so that the two lists don't get out of sync.

@sosukesuzuki
Copy link
Member Author

sosukesuzuki commented Feb 11, 2021

Is there a specific reason for ededec2? I actually preferred the previous version.

This is because InvalidModifierOnTypeMember may be not the only error we want to raise in tsParseModifiers. But at least for now, past version is fine.

@@ -207,6 +210,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
accessibility?: N.Accessibility,
},
allowedModifiers: TsModifier[],
callback?: (startPos: number, modifer: TsModifier) => void,
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternative interface design

tsParseModifiers(
      modified: {
        [key: TsModifier]: ?true,
        accessibility?: N.Accessibility,
      },
      allowedModifiers: TsModifier[],
      disallowedModifiers: TsModifier[],
      errorTemplate: string
): void

So we can raise errors when a disallowed modifier is seen and it can be reused in other situations. Introducing callback to parsers can be very nasty.

disallowedModifiers &&
disallowedModifiers.includes(modifier)
) {
this.raise(startPos, errorTemplate, modifier);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can assume errorTemplate is non null if disallowedModifiers is provided. Overall this method not a public interface so we don't have to be defensive.

@@ -207,6 +210,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
accessibility?: N.Accessibility,
},
allowedModifiers: TsModifier[],
disallowedModifiers?: TsModifier[],
errorTemplate?: string,
): void {
for (;;) {
const startPos = this.state.start;
Copy link
Contributor

Choose a reason for hiding this comment

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

We can pass down allowedModifiers.concat(disallowedModifier) to tsParserModifier so we don't have to duplicate disallowedModifier in the tsParseModifiers call.

Copy link
Member

Choose a reason for hiding this comment

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

Nit: allowedModifiers.concat(disallowedModifier ?? [])

if (
errorTemplate &&
disallowedModifiers &&
disallowedModifiers.includes(modifier)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
disallowedModifiers.includes(modifier)
disallowedModifiers?.includes(modifier)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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 interface property with modifiers when errorRecovery: true
5 participants