Skip to content

Raise recoverable error for type members with invalid modifiers#12785

Merged
nicolo-ribaudo merged 9 commits intobabel:mainfrom
sosukesuzuki:fix-12767
Feb 12, 2021
Merged

Raise recoverable error for type members with invalid modifiers#12785
nicolo-ribaudo merged 9 commits intobabel:mainfrom
sosukesuzuki:fix-12767

Conversation

@sosukesuzuki
Copy link
Copy Markdown
Contributor

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

@babel-bot
Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor 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.

accessibility?: N.Accessibility,
},
allowedModifiers: TsModifier[],
callback?: (startPos: number, modifer: TsModifier) => void,
Copy link
Copy Markdown
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
Copy Markdown
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.

errorTemplate?: string,
): void {
for (;;) {
const startPos = this.state.start;
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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