[parser] Add support for private fields in TypeScript (#10483)

* [parser] Add support for private fields in TypeScript

* Fix flow
This commit is contained in:
Nicolò Ribaudo
2019-10-29 18:27:54 +01:00
committed by GitHub
parent f1bc6c4e18
commit 1d1fab4ea2
17 changed files with 931 additions and 4 deletions

View File

@@ -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,

View File

@@ -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 &