Add readonly to TypeScript type modifier (#9529)
* add readonly to TSTypeOperator * add more test cases for readonly
This commit is contained in:
committed by
Nicolò Ribaudo
parent
25a3825a1d
commit
cc45608423
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user