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

Parse declare modifier around accessibility modifiers #11146

Merged
merged 4 commits into from Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 10 additions & 17 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -135,27 +135,24 @@ export default (superClass: Class<Parser>): Class<Parser> =>

/** Parses a list of modifiers, in any order.
* If you need a specific order, you must call this function multiple times:
* this.tsParseModifiers(["public"]);
* this.tsParseModifiers(["abstract", "readonly"]);
* this.tsParseModifiers(node, ["public"]);
* this.tsParseModifiers(node, ["abstract", "readonly"]);
*/
tsParseModifiers<T: TsModifier>(
modified: { [key: TsModifier]: ?true },
allowedModifiers: T[],
): { [key: TsModifier]: ?true, __proto__: null } {
const modifiers = Object.create(null);

): void {
while (true) {
const startPos = this.state.start;
const modifier: ?T = this.tsParseModifier(allowedModifiers);

if (!modifier) break;

if (Object.hasOwnProperty.call(modifiers, modifier)) {
if (Object.hasOwnProperty.call(modified, modifier)) {
this.raise(startPos, `Duplicate modifier: '${modifier}'`);
}
modifiers[modifier] = true;
modified[modifier] = true;
}

return modifiers;
}

tsIsListTerminator(kind: ParsingContext): boolean {
Expand Down Expand Up @@ -1922,8 +1919,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
state: { hadConstructor: boolean },
constructorAllowsSuper: boolean,
): void {
this.tsParseModifiers(member, ["declare"]);
const accessibility = this.parseAccessModifier();
if (accessibility) member.accessibility = accessibility;
this.tsParseModifiers(member, ["declare"]);

super.parseClassMember(classBody, member, state, constructorAllowsSuper);
}
Expand All @@ -1935,19 +1934,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
isStatic: boolean,
constructorAllowsSuper: boolean,
): void {
const modifiers = this.tsParseModifiers([
"abstract",
"readonly",
"declare",
]);

Object.assign(member, modifiers);
this.tsParseModifiers(member, ["abstract", "readonly", "declare"]);

const idx = this.tsTryParseIndexSignature(member);
if (idx) {
classBody.body.push(idx);

if (modifiers.abstract) {
if ((member: any).abstract) {
this.raise(
member.start,
"Index signatures cannot have the 'abstract' modifier",
Expand Down
@@ -0,0 +1,11 @@
class A {
declare static foo;
static declare foo0: string;

declare public foo1;
public declare foo2;

declare public static foo4;
public declare static foo3;
public static declare foo5;
}