TypeScript: Support conditional types syntax (#7404)
Microsoft/TypeScript#21316 and Microsoft/TypeScript#21496
This commit is contained in:
@@ -617,11 +617,22 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return this.finishNode(node, "TSTypeOperator");
|
||||
}
|
||||
|
||||
tsParseInferType(): N.TsInferType {
|
||||
const node = this.startNode();
|
||||
this.expectContextual("infer");
|
||||
const typeParameter = this.startNode();
|
||||
typeParameter.name = this.parseIdentifierName(typeParameter.start);
|
||||
node.typeParameter = this.finishNode(typeParameter, "TypeParameter");
|
||||
return this.finishNode(node, "TSInferType");
|
||||
}
|
||||
|
||||
tsParseTypeOperatorOrHigher(): N.TsType {
|
||||
const operator = ["keyof", "unique"].find(kw => this.isContextual(kw));
|
||||
return operator
|
||||
? this.tsParseTypeOperator(operator)
|
||||
: this.tsParseArrayTypeOrHigher();
|
||||
: this.isContextual("infer")
|
||||
? this.tsParseInferType()
|
||||
: this.tsParseArrayTypeOrHigher();
|
||||
}
|
||||
|
||||
tsParseUnionOrIntersectionType(
|
||||
@@ -774,6 +785,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
tsParseType(): N.TsType {
|
||||
// Need to set `state.inType` so that we don't parse JSX in a type context.
|
||||
assert(this.state.inType);
|
||||
const type = this.tsParseNonConditionalType();
|
||||
if (this.hasPrecedingLineBreak() || !this.eat(tt._extends)) {
|
||||
return type;
|
||||
}
|
||||
const node: N.TsConditionalType = this.startNodeAtNode(type);
|
||||
node.checkType = type;
|
||||
node.extendsType = this.tsParseNonConditionalType();
|
||||
this.expect(tt.question);
|
||||
node.trueType = this.tsParseType();
|
||||
this.expect(tt.colon);
|
||||
node.falseType = this.tsParseType();
|
||||
return this.finishNode(node, "TSConditionalType");
|
||||
}
|
||||
|
||||
tsParseNonConditionalType(): N.TsType {
|
||||
if (this.tsIsStartOfFunctionType()) {
|
||||
return this.tsParseFunctionOrConstructorType("TSFunctionType");
|
||||
}
|
||||
|
||||
@@ -1154,6 +1154,19 @@ export type TsIntersectionType = TsUnionOrIntersectionTypeBase & {
|
||||
type: "TSIntersectionType",
|
||||
};
|
||||
|
||||
export type TsConditionalType = TsTypeBase & {
|
||||
type: "TSConditionalType",
|
||||
checkType: TsType,
|
||||
extendsType: TsType,
|
||||
trueType: TsType,
|
||||
falseType: tsType,
|
||||
};
|
||||
|
||||
export type InferType = TsTypeBase & {
|
||||
type: "TSInferType",
|
||||
typeParameter: TypeParameter,
|
||||
};
|
||||
|
||||
export type TsParenthesizedType = TsTypeBase & {
|
||||
type: "TSParenthesizedType",
|
||||
typeAnnotation: TsType,
|
||||
|
||||
1
packages/babylon/test/fixtures/typescript/types/conditional-infer/input.js
vendored
Normal file
1
packages/babylon/test/fixtures/typescript/types/conditional-infer/input.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
type Element<T> = T extends (infer U)[] ? U : T;
|
||||
271
packages/babylon/test/fixtures/typescript/types/conditional-infer/output.json
vendored
Normal file
271
packages/babylon/test/fixtures/typescript/types/conditional-infer/output.json
vendored
Normal file
@@ -0,0 +1,271 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 48,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 48
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 48,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 48
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"body": [
|
||||
{
|
||||
"type": "TSTypeAliasDeclaration",
|
||||
"start": 0,
|
||||
"end": 48,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 48
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 5,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
},
|
||||
"identifierName": "Element"
|
||||
},
|
||||
"name": "Element"
|
||||
},
|
||||
"typeParameters": {
|
||||
"type": "TSTypeParameterDeclaration",
|
||||
"start": 12,
|
||||
"end": 15,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 15
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"type": "TSTypeParameter",
|
||||
"start": 13,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"name": "T"
|
||||
}
|
||||
]
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSConditionalType",
|
||||
"start": 18,
|
||||
"end": 47,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 47
|
||||
}
|
||||
},
|
||||
"checkType": {
|
||||
"type": "TSTypeReference",
|
||||
"start": 18,
|
||||
"end": 19,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 19
|
||||
}
|
||||
},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start": 18,
|
||||
"end": 19,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 19
|
||||
},
|
||||
"identifierName": "T"
|
||||
},
|
||||
"name": "T"
|
||||
}
|
||||
},
|
||||
"extendsType": {
|
||||
"type": "TSArrayType",
|
||||
"start": 28,
|
||||
"end": 39,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 28
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 39
|
||||
}
|
||||
},
|
||||
"elementType": {
|
||||
"type": "TSParenthesizedType",
|
||||
"start": 28,
|
||||
"end": 37,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 28
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 37
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSInferType",
|
||||
"start": 29,
|
||||
"end": 36,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 29
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 36
|
||||
}
|
||||
},
|
||||
"typeParameter": {
|
||||
"type": "TypeParameter",
|
||||
"start": 35,
|
||||
"end": 36,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 35
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 36
|
||||
}
|
||||
},
|
||||
"name": "U"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"trueType": {
|
||||
"type": "TSTypeReference",
|
||||
"start": 42,
|
||||
"end": 43,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 42
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 43
|
||||
}
|
||||
},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start": 42,
|
||||
"end": 43,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 42
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 43
|
||||
},
|
||||
"identifierName": "U"
|
||||
},
|
||||
"name": "U"
|
||||
}
|
||||
},
|
||||
"falseType": {
|
||||
"type": "TSTypeReference",
|
||||
"start": 46,
|
||||
"end": 47,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 46
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 47
|
||||
}
|
||||
},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start": 46,
|
||||
"end": 47,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 46
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 47
|
||||
},
|
||||
"identifierName": "T"
|
||||
},
|
||||
"name": "T"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
1
packages/babylon/test/fixtures/typescript/types/conditional/input.js
vendored
Normal file
1
packages/babylon/test/fixtures/typescript/types/conditional/input.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
let x: number extends string ? boolean : null;
|
||||
175
packages/babylon/test/fixtures/typescript/types/conditional/output.json
vendored
Normal file
175
packages/babylon/test/fixtures/typescript/types/conditional/output.json
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 46,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 46
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 46,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 46
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"body": [
|
||||
{
|
||||
"type": "VariableDeclaration",
|
||||
"start": 0,
|
||||
"end": 46,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 46
|
||||
}
|
||||
},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start": 4,
|
||||
"end": 45,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 45
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 4,
|
||||
"end": 45,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 45
|
||||
},
|
||||
"identifierName": "x"
|
||||
},
|
||||
"name": "x",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start": 5,
|
||||
"end": 45,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 45
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSConditionalType",
|
||||
"start": 7,
|
||||
"end": 45,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 45
|
||||
}
|
||||
},
|
||||
"checkType": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 7,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
}
|
||||
}
|
||||
},
|
||||
"extendsType": {
|
||||
"type": "TSStringKeyword",
|
||||
"start": 22,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 22
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 28
|
||||
}
|
||||
}
|
||||
},
|
||||
"trueType": {
|
||||
"type": "TSBooleanKeyword",
|
||||
"start": 31,
|
||||
"end": 38,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 31
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 38
|
||||
}
|
||||
}
|
||||
},
|
||||
"falseType": {
|
||||
"type": "TSNullKeyword",
|
||||
"start": 41,
|
||||
"end": 45,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 41
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 45
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"init": null
|
||||
}
|
||||
],
|
||||
"kind": "let"
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user