[parser] Add support for private fields in TypeScript (#10483)
* [parser] Add support for private fields in TypeScript * Fix flow
This commit is contained in:
@@ -1868,7 +1868,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
|
||||
parsePostMemberNameModifiers(
|
||||
methodOrProp: N.ClassMethod | N.ClassProperty,
|
||||
methodOrProp: N.ClassMethod | N.ClassProperty | N.ClassPrivateProperty,
|
||||
): void {
|
||||
const optional = this.eat(tt.question);
|
||||
if (optional) methodOrProp.optional = true;
|
||||
@@ -2007,16 +2007,45 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
if (typeParameters) node.typeParameters = typeParameters;
|
||||
}
|
||||
|
||||
parseClassProperty(node: N.ClassProperty): N.ClassProperty {
|
||||
parseClassPropertyAnnotation(
|
||||
node: N.ClassProperty | N.ClassPrivateProperty,
|
||||
): void {
|
||||
if (!node.optional && this.eat(tt.bang)) {
|
||||
node.definite = true;
|
||||
}
|
||||
|
||||
const type = this.tsTryParseTypeAnnotation();
|
||||
if (type) node.typeAnnotation = type;
|
||||
}
|
||||
|
||||
parseClassProperty(node: N.ClassProperty): N.ClassProperty {
|
||||
this.parseClassPropertyAnnotation(node);
|
||||
return super.parseClassProperty(node);
|
||||
}
|
||||
|
||||
parseClassPrivateProperty(
|
||||
node: N.ClassPrivateProperty,
|
||||
): N.ClassPrivateProperty {
|
||||
// $FlowIgnore
|
||||
if (node.abstract) {
|
||||
this.raise(
|
||||
node.start,
|
||||
"Private elements cannot have the 'abstract' modifier.",
|
||||
);
|
||||
}
|
||||
|
||||
// $FlowIgnore
|
||||
if (node.accessibility) {
|
||||
this.raise(
|
||||
node.start,
|
||||
`Private elements cannot have an accessibility modifier ('${node.accessibility}')`,
|
||||
);
|
||||
}
|
||||
|
||||
this.parseClassPropertyAnnotation(node);
|
||||
return super.parseClassPrivateProperty(node);
|
||||
}
|
||||
|
||||
pushClassMethod(
|
||||
classBody: N.ClassBody,
|
||||
method: N.ClassMethod,
|
||||
|
||||
@@ -762,7 +762,14 @@ export type ClassPrivateProperty = NodeBase & {
|
||||
value: ?Expression, // TODO: Not in spec that this is nullable.
|
||||
static: boolean,
|
||||
computed: false,
|
||||
typeAnnotation?: ?TypeAnnotation, // TODO: Not in spec
|
||||
|
||||
// Flow and Typescript
|
||||
typeAnnotation?: ?TypeAnnotationBase,
|
||||
|
||||
// TypeScript only
|
||||
optional?: true,
|
||||
definite?: true,
|
||||
readonly?: true,
|
||||
};
|
||||
|
||||
export type OptClassDeclaration = ClassBase &
|
||||
|
||||
Reference in New Issue
Block a user