Implement support for declare on class fields with Flow (#11178)
* Add parser support for Flow declare fields * Add generator test * Add "allowDeclareFields" option to flow-strip-types * Add test * Update error messages * More tests
This commit is contained in:
@@ -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`.",
|
||||
@@ -2085,6 +2089,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);
|
||||
|
||||
Reference in New Issue
Block a user