Add readonly to TypeScript type modifier (#9529)

* add readonly to TSTypeOperator

* add more test cases for readonly
This commit is contained in:
Tan Li Hau
2019-03-16 13:41:52 +08:00
committed by Nicolò Ribaudo
parent 25a3825a1d
commit cc45608423
12 changed files with 306 additions and 9 deletions

View File

@@ -741,14 +741,34 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return type;
}
tsParseTypeOperator(operator: "keyof" | "unique"): N.TsTypeOperator {
tsParseTypeOperator(
operator: "keyof" | "unique" | "readonly",
): N.TsTypeOperator {
const node: N.TsTypeOperator = this.startNode();
this.expectContextual(operator);
node.operator = operator;
node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
if (operator === "readonly") {
this.tsCheckTypeAnnotationForReadOnly(node);
}
return this.finishNode(node, "TSTypeOperator");
}
tsCheckTypeAnnotationForReadOnly(node: N.Node) {
switch (node.typeAnnotation.type) {
case "TSTupleType":
case "TSArrayType":
return;
default:
this.raise(
node.operator,
"'readonly' type modifier is only permitted on array and tuple literal types.",
);
}
}
tsParseInferType(): N.TsInferType {
const node = this.startNode();
this.expectContextual("infer");
@@ -759,7 +779,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
tsParseTypeOperatorOrHigher(): N.TsType {
const operator = ["keyof", "unique"].find(kw => this.isContextual(kw));
const operator = ["keyof", "unique", "readonly"].find(kw =>
this.isContextual(kw),
);
return operator
? this.tsParseTypeOperator(operator)
: this.isContextual("infer")