Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readonly to TypeScript type modifier #9529

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/babel-parser/src/plugins/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,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");
Expand All @@ -721,7 +741,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")
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ export type TsParenthesizedType = TsTypeBase & {

export type TsTypeOperator = TsTypeBase & {
type: "TSTypeOperator",
operator: "keyof" | "unique",
operator: "keyof" | "unique" | "readonly",
typeAnnotation: TsType,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type T30 = readonly string; // Error
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "'readonly' type modifier is only permitted on array and tuple literal types. (1:NaN)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type T31 = readonly T; // Error
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "'readonly' type modifier is only permitted on array and tuple literal types. (1:NaN)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type T32 = readonly readonly string[]; // Error
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "'readonly' type modifier is only permitted on array and tuple literal types. (1:NaN)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type T33 = readonly Array<string>; // Error
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "'readonly' type modifier is only permitted on array and tuple literal types. (1:NaN)"
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
let x: keyof T;
let y: unique symbol;
let z: readonly number[];
let z1: readonly [number, number];
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"type": "File",
"start": 0,
"end": 37,
"end": 98,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 21
"line": 4,
"column": 34
}
},
"program": {
"type": "Program",
"start": 0,
"end": 37,
"end": 98,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 21
"line": 4,
"column": 34
}
},
"sourceType": "module",
Expand Down Expand Up @@ -239,6 +239,247 @@
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start": 38,
"end": 63,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 25
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 42,
"end": 62,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 24
}
},
"id": {
"type": "Identifier",
"start": 42,
"end": 62,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 24
},
"identifierName": "z"
},
"name": "z",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 43,
"end": 62,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 24
}
},
"typeAnnotation": {
"type": "TSTypeOperator",
"start": 45,
"end": 62,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 24
}
},
"operator": "readonly",
"typeAnnotation": {
"type": "TSArrayType",
"start": 54,
"end": 62,
"loc": {
"start": {
"line": 3,
"column": 16
},
"end": {
"line": 3,
"column": 24
}
},
"elementType": {
"type": "TSNumberKeyword",
"start": 54,
"end": 60,
"loc": {
"start": {
"line": 3,
"column": 16
},
"end": {
"line": 3,
"column": 22
}
}
}
}
}
}
},
"init": null
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start": 64,
"end": 98,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 34
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 68,
"end": 97,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 33
}
},
"id": {
"type": "Identifier",
"start": 68,
"end": 97,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 33
},
"identifierName": "z1"
},
"name": "z1",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 70,
"end": 97,
"loc": {
"start": {
"line": 4,
"column": 6
},
"end": {
"line": 4,
"column": 33
}
},
"typeAnnotation": {
"type": "TSTypeOperator",
"start": 72,
"end": 97,
"loc": {
"start": {
"line": 4,
"column": 8
},
"end": {
"line": 4,
"column": 33
}
},
"operator": "readonly",
"typeAnnotation": {
"type": "TSTupleType",
"start": 81,
"end": 97,
"loc": {
"start": {
"line": 4,
"column": 17
},
"end": {
"line": 4,
"column": 33
}
},
"elementTypes": [
{
"type": "TSNumberKeyword",
"start": 82,
"end": 88,
"loc": {
"start": {
"line": 4,
"column": 18
},
"end": {
"line": 4,
"column": 24
}
}
},
{
"type": "TSNumberKeyword",
"start": 90,
"end": 96,
"loc": {
"start": {
"line": 4,
"column": 26
},
"end": {
"line": 4,
"column": 32
}
}
}
]
}
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
Expand Down