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

Implement support for declare on class fields with Flow #11178

Merged
merged 6 commits into from
Mar 16, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class A {
declare x
declare static y
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["flow", "classProperties"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class A {
declare x;
declare static y;
}
83 changes: 46 additions & 37 deletions packages/babel-parser/src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -1217,50 +1217,59 @@ export default class StatementParser extends ExpressionParser {
return this.finishNode(classBody, "ClassBody");
}

parseClassMember(
// returns true if the current identifier is a method/field name,
// false if it is a modifier
parseClassMemberFromModifier(
classBody: N.ClassBody,
member: N.ClassMember,
state: { hadConstructor: boolean },
constructorAllowsSuper: boolean,
): void {
let isStatic = false;
): boolean {
const containsEsc = this.state.containsEsc;
const key = this.parseIdentifier(true); // eats the modifier

if (this.match(tt.name) && this.state.value === "static") {
const key = this.parseIdentifier(true); // eats 'static'
if (this.isClassMethod()) {
const method: N.ClassMethod = (member: any);

if (this.isClassMethod()) {
const method: N.ClassMethod = (member: any);
// a method named like the modifier
method.kind = "method";
method.computed = false;
method.key = key;
method.static = false;
this.pushClassMethod(
classBody,
method,
false,
false,
/* isConstructor */ false,
false,
);
return true;
} else if (this.isClassProperty()) {
const prop: N.ClassProperty = (member: any);

// a method named 'static'
method.kind = "method";
method.computed = false;
method.key = key;
method.static = false;
this.pushClassMethod(
classBody,
method,
false,
false,
/* isConstructor */ false,
false,
);
return;
} else if (this.isClassProperty()) {
const prop: N.ClassProperty = (member: any);

// a property named 'static'
prop.computed = false;
prop.key = key;
prop.static = false;
classBody.body.push(this.parseClassProperty(prop));
return;
} else if (containsEsc) {
throw this.unexpected();
}
// a property named like the modifier
prop.computed = false;
prop.key = key;
prop.static = false;
classBody.body.push(this.parseClassProperty(prop));
return true;
} else if (containsEsc) {
throw this.unexpected();
}

// otherwise something static
isStatic = true;
return false;
}

parseClassMember(
classBody: N.ClassBody,
member: N.ClassMember,
state: { hadConstructor: boolean },
constructorAllowsSuper: boolean,
): void {
const isStatic = this.isContextual("static");

if (isStatic && this.parseClassMemberFromModifier(classBody, member)) {
// a class element named 'static'
return;
}

this.parseClassMemberWithIsStatic(
Expand Down
37 changes: 37 additions & 0 deletions packages/babel-parser/src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const FlowErrors = Object.freeze({
AmbiguousDeclareModuleKind:
"Found both `declare module.exports` and `declare export` in the same module. Modules can only have 1 since they are either an ES module or they are a CommonJS module",
AssignReservedType: "Cannot overwrite reserved type %0",
DeclareClassElement:
"The `declare` modifier can only appear on class fields.",
DeclareClassFieldInitializer:
"Initializers are not allowed in fields with the `declare` modifier.",
DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement",
EnumBooleanMemberNotInitialized:
"Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
Expand Down Expand Up @@ -2082,6 +2086,39 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

parseClassMember(
classBody: N.ClassBody,
member: any,
state: { hadConstructor: boolean },
constructorAllowsSuper: boolean,
): void {
const pos = this.state.start;
if (this.isContextual("declare")) {
if (this.parseClassMemberFromModifier(classBody, member)) {
// 'declare' is a class element name
return;
}

member.declare = true;
}

super.parseClassMember(classBody, member, state, constructorAllowsSuper);

if (member.declare) {
if (
member.type !== "ClassProperty" &&
member.type !== "ClassPrivateProperty"
) {
this.raise(pos, FlowErrors.DeclareClassElement);
} else if (member.value) {
this.raise(
member.value.start,
FlowErrors.DeclareClassFieldInitializer,
);
}
}
}

// ensure that inside flow types, we bypass the jsx parser plugin
getTokenFromCode(code: number): void {
const next = this.input.charCodeAt(this.state.pos + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A {
declare #foo = 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"type": "File",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"errors": [
"SyntaxError: Initializers are not allowed in fields with the `declare` modifier. (2:17)"
],
"program": {
"type": "Program",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 12,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 18
}
},
"declare": true,
"static": false,
"key": {
"type": "PrivateName",
"start": 20,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 14
},
"identifierName": "foo"
},
"name": "foo"
}
},
"variance": null,
"value": {
"type": "NumericLiteral",
"start": 27,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 18
}
},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
}
]
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A {
declare #foo
}