Typescript - Tuple elements can be optional (#8720)
This commit is contained in:
parent
3c87401714
commit
a5b5ed928d
@ -241,6 +241,11 @@ export function TSTupleType(node) {
|
||||
this.token("]");
|
||||
}
|
||||
|
||||
export function TSOptionalType(node) {
|
||||
this.print(node.typeAnnotation, node);
|
||||
this.token("?");
|
||||
}
|
||||
|
||||
export function TSUnionType(node) {
|
||||
this.tsPrintUnionOrIntersectionType(node, "|");
|
||||
}
|
||||
|
||||
1
packages/babel-generator/test/fixtures/typescript/tuple-empty/input.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/typescript/tuple-empty/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [];
|
||||
1
packages/babel-generator/test/fixtures/typescript/tuple-empty/output.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/typescript/tuple-empty/output.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [];
|
||||
1
packages/babel-generator/test/fixtures/typescript/tuple-optional/input.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/typescript/tuple-optional/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [string, number?, (string | number)?]
|
||||
1
packages/babel-generator/test/fixtures/typescript/tuple-optional/output.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/typescript/tuple-optional/output.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [string, number?, (string | number)?];
|
||||
1
packages/babel-generator/test/fixtures/typescript/tuple/input.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/typescript/tuple/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [number, number, number];
|
||||
1
packages/babel-generator/test/fixtures/typescript/tuple/output.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/typescript/tuple/output.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [number, number, number];
|
||||
@ -503,13 +503,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
const node: N.TsTupleType = this.startNode();
|
||||
node.elementTypes = this.tsParseBracketedList(
|
||||
"TupleElementTypes",
|
||||
this.tsParseType.bind(this),
|
||||
this.tsParseTupleElementType.bind(this),
|
||||
/* bracket */ true,
|
||||
/* skipFirstToken */ false,
|
||||
);
|
||||
return this.finishNode(node, "TSTupleType");
|
||||
}
|
||||
|
||||
tsParseTupleElementType(): N.TsType {
|
||||
const type = this.tsParseType();
|
||||
if (this.eat(tt.question)) {
|
||||
const optionalTypeNode: N.TsOptionalType = this.startNodeAtNode(type);
|
||||
optionalTypeNode.typeAnnotation = type;
|
||||
return this.finishNode(optionalTypeNode, "TSOptionalType");
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
tsParseParenthesizedType(): N.TsParenthesizedType {
|
||||
const node = this.startNode();
|
||||
this.expect(tt.parenL);
|
||||
|
||||
@ -1104,6 +1104,7 @@ export type TsType =
|
||||
| TsTypeLiteral
|
||||
| TsArrayType
|
||||
| TsTupleType
|
||||
| TsOptionalType
|
||||
| TsUnionOrIntersectionType
|
||||
| TsConditionalType
|
||||
| TsInferType
|
||||
@ -1184,6 +1185,11 @@ export type TsTupleType = TsTypeBase & {
|
||||
elementTypes: $ReadOnlyArray<TsType>,
|
||||
};
|
||||
|
||||
export type TsOptionalType = TsTypeBase & {
|
||||
type: "TSOptionalType",
|
||||
typeAnnotation: TsType,
|
||||
};
|
||||
|
||||
export type TsUnionOrIntersectionType = TsUnionType | TsIntersectionType;
|
||||
|
||||
export type TsUnionOrIntersectionTypeBase = TsTypeBase & {
|
||||
|
||||
1
packages/babel-parser/test/fixtures/typescript/types/tuple-empty/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/tuple-empty/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [];
|
||||
117
packages/babel-parser/test/fixtures/typescript/types/tuple-empty/output.json
vendored
Normal file
117
packages/babel-parser/test/fixtures/typescript/types/tuple-empty/output.json
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "VariableDeclaration",
|
||||
"start": 0,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start": 4,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 4,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
},
|
||||
"identifierName": "x"
|
||||
},
|
||||
"name": "x",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start": 5,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTupleType",
|
||||
"start": 7,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"elementTypes": []
|
||||
}
|
||||
}
|
||||
},
|
||||
"init": null
|
||||
}
|
||||
],
|
||||
"kind": "let"
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/typescript/types/tuple-optional/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/tuple-optional/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [string, number?, (string | number)?]
|
||||
240
packages/babel-parser/test/fixtures/typescript/types/tuple-optional/output.json
vendored
Normal file
240
packages/babel-parser/test/fixtures/typescript/types/tuple-optional/output.json
vendored
Normal file
@ -0,0 +1,240 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 44,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 44
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 44,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 44
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "VariableDeclaration",
|
||||
"start": 0,
|
||||
"end": 44,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 44
|
||||
}
|
||||
},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start": 4,
|
||||
"end": 44,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 44
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 4,
|
||||
"end": 44,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 44
|
||||
},
|
||||
"identifierName": "x"
|
||||
},
|
||||
"name": "x",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start": 5,
|
||||
"end": 44,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 44
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTupleType",
|
||||
"start": 7,
|
||||
"end": 44,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 44
|
||||
}
|
||||
},
|
||||
"elementTypes": [
|
||||
{
|
||||
"type": "TSStringKeyword",
|
||||
"start": 8,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 14
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSOptionalType",
|
||||
"start": 16,
|
||||
"end": 23,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 16
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 23
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 16,
|
||||
"end": 22,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 16
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 22
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSOptionalType",
|
||||
"start": 25,
|
||||
"end": 43,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 25
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 43
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSParenthesizedType",
|
||||
"start": 25,
|
||||
"end": 42,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 25
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 42
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSUnionType",
|
||||
"start": 26,
|
||||
"end": 41,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 26
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 41
|
||||
}
|
||||
},
|
||||
"types": [
|
||||
{
|
||||
"type": "TSStringKeyword",
|
||||
"start": 26,
|
||||
"end": 32,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 26
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 32
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 35,
|
||||
"end": 41,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 35
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 41
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"init": null
|
||||
}
|
||||
],
|
||||
"kind": "let"
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/typescript/types/tuple/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/tuple/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let x: [number, number, number];
|
||||
163
packages/babel-parser/test/fixtures/typescript/types/tuple/output.json
vendored
Normal file
163
packages/babel-parser/test/fixtures/typescript/types/tuple/output.json
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 32,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 32
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 32,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 32
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "VariableDeclaration",
|
||||
"start": 0,
|
||||
"end": 32,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 32
|
||||
}
|
||||
},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start": 4,
|
||||
"end": 31,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 31
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 4,
|
||||
"end": 31,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 31
|
||||
},
|
||||
"identifierName": "x"
|
||||
},
|
||||
"name": "x",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start": 5,
|
||||
"end": 31,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 31
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTupleType",
|
||||
"start": 7,
|
||||
"end": 31,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 31
|
||||
}
|
||||
},
|
||||
"elementTypes": [
|
||||
{
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 8,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 14
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 16,
|
||||
"end": 22,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 16
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 22
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 24,
|
||||
"end": 30,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 24
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 30
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"init": null
|
||||
}
|
||||
],
|
||||
"kind": "let"
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-plugin-transform-typescript/test/fixtures/function/tuple-parameter/input.js
vendored
Normal file
1
packages/babel-plugin-transform-typescript/test/fixtures/function/tuple-parameter/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
function foo(...args: [number, string?]) {}
|
||||
1
packages/babel-plugin-transform-typescript/test/fixtures/function/tuple-parameter/output.js
vendored
Normal file
1
packages/babel-plugin-transform-typescript/test/fixtures/function/tuple-parameter/output.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
function foo(...args) {}
|
||||
@ -825,6 +825,9 @@ export function assertTSArrayType(node: Object, opts?: Object = {}): void {
|
||||
export function assertTSTupleType(node: Object, opts?: Object = {}): void {
|
||||
assert("TSTupleType", node, opts);
|
||||
}
|
||||
export function assertTSOptionalType(node: Object, opts?: Object = {}): void {
|
||||
assert("TSOptionalType", node, opts);
|
||||
}
|
||||
export function assertTSUnionType(node: Object, opts?: Object = {}): void {
|
||||
assert("TSUnionType", node, opts);
|
||||
}
|
||||
|
||||
@ -793,6 +793,11 @@ export function TSTupleType(...args: Array<any>): Object {
|
||||
}
|
||||
export { TSTupleType as tsTupleType };
|
||||
export { TSTupleType as tSTupleType };
|
||||
export function TSOptionalType(...args: Array<any>): Object {
|
||||
return builder("TSOptionalType", ...args);
|
||||
}
|
||||
export { TSOptionalType as tsOptionalType };
|
||||
export { TSOptionalType as tSOptionalType };
|
||||
export function TSUnionType(...args: Array<any>): Object {
|
||||
return builder("TSUnionType", ...args);
|
||||
}
|
||||
|
||||
@ -213,6 +213,14 @@ defineType("TSTupleType", {
|
||||
},
|
||||
});
|
||||
|
||||
defineType("TSOptionalType", {
|
||||
aliases: ["TSType"],
|
||||
visitor: ["typeAnnotation"],
|
||||
fields: {
|
||||
typeAnnotation: validateType("TSType"),
|
||||
},
|
||||
});
|
||||
|
||||
const unionOrIntersection = {
|
||||
aliases: ["TSType"],
|
||||
visitor: ["types"],
|
||||
|
||||
@ -2672,6 +2672,20 @@ export function isTSTupleType(node: Object, opts?: Object): boolean {
|
||||
|
||||
return false;
|
||||
}
|
||||
export function isTSOptionalType(node: Object, opts?: Object): boolean {
|
||||
if (!node) return false;
|
||||
|
||||
const nodeType = node.type;
|
||||
if (nodeType === "TSOptionalType") {
|
||||
if (typeof opts === "undefined") {
|
||||
return true;
|
||||
} else {
|
||||
return shallowEqual(node, opts);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
export function isTSUnionType(node: Object, opts?: Object): boolean {
|
||||
if (!node) return false;
|
||||
|
||||
@ -4149,6 +4163,7 @@ export function isTSType(node: Object, opts?: Object): boolean {
|
||||
"TSTypeLiteral" === nodeType ||
|
||||
"TSArrayType" === nodeType ||
|
||||
"TSTupleType" === nodeType ||
|
||||
"TSOptionalType" === nodeType ||
|
||||
"TSUnionType" === nodeType ||
|
||||
"TSIntersectionType" === nodeType ||
|
||||
"TSConditionalType" === nodeType ||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user