-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[parser] Add support for TS declare modifier on fields #10484
[parser] Add support for TS declare modifier on fields #10484
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11658/ |
Note that the combined PR that I intend to merge is microsoft/TypeScript#33509. |
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.
Looks good.
this.raise(startPos, `Duplicate modifier: '${modifier}'`); | ||
} | ||
modifiers[modifier] = true; | ||
} |
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 seems to allow any modifier whitelisted by the parameter in any order; I presume that's okay? or are there some modifiers which have a specific required order (e.g. declare
first)
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.
Some modifiers must come in a specific order, some others don't. For example, abstract
and readonly
can be swapped, but public
must be before them.
We parse them using a logic similar to this one, which ensures the correct order:
this.tsParseModifiers(["public"]);
this.tsParseModifiers(["abstract", "readonly"]);
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.
Not super important, but it might be nice to add a comment around modifier order/usage.
@@ -124,6 +127,24 @@ export default (superClass: Class<Parser>): Class<Parser> => | |||
return undefined; | |||
} | |||
|
|||
tsParseModifiers<T: TsModifier>(allowedModifiers: T[]): { [*]: true } { | |||
const modifiers = {}; |
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.
I'd feel better with Object.create(null)
but it probably isn't really relevant here.
24822b8
to
3f89e3c
Compare
I'll merge this to a separate branch so that I can start working on the transform. |
* [parser] Add support for TS declare modifier on fields * Use Object.create(null) * Comment
* [parser] Add support for TS declare modifier on fields * Use Object.create(null) * Comment
* [parser] Add support for TS declare modifier on fields * Use Object.create(null) * Comment
* [parser] Add support for TS declare modifier on fields * Use Object.create(null) * Comment
* [parser] Add support for TS declare modifier on fields (#10484) * [parser] Add support for TS declare modifier on fields * Use Object.create(null) * Comment * Add support for TS declare types to types and generator (#10544) * Transform TypeScript "declare" fields (#10546) * Transform TypeScript "declare" fields * Remove multiple spaces * declareFields -> allowDeclareFields * Update after rebase
Ref: microsoft/TypeScript#33401
I also updated the parser to generate more useful errors when a class member has a disallowed modifier (e.g.
readonly method() {}
)cc @babel/typescript