[TS] Create TSUnionType or TSIntersectionType when typealias has a leading operator (#12758)
* Create `TSUnionType` or `TSIntersectionType` when typealias has a leading operator * Apply code review suggestions * Test `TSIntersectionType`
This commit is contained in:
parent
74ed698c2e
commit
eccbcca948
@ -936,17 +936,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
operator: TokenType,
|
operator: TokenType,
|
||||||
): N.TsType {
|
): N.TsType {
|
||||||
const node: N.TsUnionType | N.TsIntersectionType = this.startNode();
|
const node: N.TsUnionType | N.TsIntersectionType = this.startNode();
|
||||||
this.eat(operator);
|
const hasLeadingOperator = this.eat(operator);
|
||||||
let type = parseConstituentType();
|
const types = [];
|
||||||
if (this.match(operator)) {
|
do {
|
||||||
const types = [type];
|
types.push(parseConstituentType());
|
||||||
while (this.eat(operator)) {
|
} while (this.eat(operator));
|
||||||
types.push(parseConstituentType());
|
if (types.length === 1 && !hasLeadingOperator) {
|
||||||
}
|
return types[0];
|
||||||
node.types = types;
|
|
||||||
type = this.finishNode(node, kind);
|
|
||||||
}
|
}
|
||||||
return type;
|
node.types = types;
|
||||||
|
return this.finishNode(node, kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
tsParseIntersectionTypeOrHigher(): N.TsType {
|
tsParseIntersectionTypeOrHigher(): N.TsType {
|
||||||
|
|||||||
@ -7,3 +7,5 @@ type J = number | string
|
|||||||
type F = number & string
|
type F = number & string
|
||||||
type K = | number | string
|
type K = | number | string
|
||||||
type M = & number & string
|
type M = & number & string
|
||||||
|
type N = | number
|
||||||
|
type O = & string
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"type": "File",
|
"type": "File",
|
||||||
"start":0,"end":265,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":26}},
|
"start":0,"end":301,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":17}},
|
||||||
"program": {
|
"program": {
|
||||||
"type": "Program",
|
"type": "Program",
|
||||||
"start":0,"end":265,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":26}},
|
"start":0,"end":301,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":17}},
|
||||||
"sourceType": "module",
|
"sourceType": "module",
|
||||||
"interpreter": null,
|
"interpreter": null,
|
||||||
"body": [
|
"body": [
|
||||||
@ -262,6 +262,44 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "TSTypeAliasDeclaration",
|
||||||
|
"start":266,"end":283,"loc":{"start":{"line":10,"column":0},"end":{"line":10,"column":17}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":271,"end":272,"loc":{"start":{"line":10,"column":5},"end":{"line":10,"column":6},"identifierName":"N"},
|
||||||
|
"name": "N"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSUnionType",
|
||||||
|
"start":275,"end":283,"loc":{"start":{"line":10,"column":9},"end":{"line":10,"column":17}},
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"type": "TSNumberKeyword",
|
||||||
|
"start":277,"end":283,"loc":{"start":{"line":10,"column":11},"end":{"line":10,"column":17}}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "TSTypeAliasDeclaration",
|
||||||
|
"start":284,"end":301,"loc":{"start":{"line":11,"column":0},"end":{"line":11,"column":17}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":289,"end":290,"loc":{"start":{"line":11,"column":5},"end":{"line":11,"column":6},"identifierName":"O"},
|
||||||
|
"name": "O"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSIntersectionType",
|
||||||
|
"start":293,"end":301,"loc":{"start":{"line":11,"column":9},"end":{"line":11,"column":17}},
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"type": "TSStringKeyword",
|
||||||
|
"start":295,"end":301,"loc":{"start":{"line":11,"column":11},"end":{"line":11,"column":17}}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"directives": []
|
"directives": []
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user