Skip to content

Commit

Permalink
add more test cases for readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Mar 12, 2019
1 parent d16e51e commit ff3a66e
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 6 deletions.
18 changes: 18 additions & 0 deletions packages/babel-parser/src/plugins/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,27 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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 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,3 +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": 63,
"end": 98,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 25
"line": 4,
"column": 34
}
},
"program": {
"type": "Program",
"start": 0,
"end": 63,
"end": 98,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 25
"line": 4,
"column": 34
}
},
"sourceType": "module",
Expand Down Expand Up @@ -351,6 +351,135 @@
}
],
"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

0 comments on commit ff3a66e

Please sign in to comment.