diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index e3899b9409..d11a8158fb 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -935,6 +935,7 @@ export default (superClass: Class): Class => parseConstituentType: () => N.TsType, operator: TokenType, ): N.TsType { + const node: N.TsUnionType | N.TsIntersectionType = this.startNode(); this.eat(operator); let type = parseConstituentType(); if (this.match(operator)) { @@ -942,9 +943,6 @@ export default (superClass: Class): Class => while (this.eat(operator)) { types.push(parseConstituentType()); } - const node: N.TsUnionType | N.TsIntersectionType = this.startNodeAtNode( - type, - ); node.types = types; type = this.finishNode(node, kind); } diff --git a/packages/babel-parser/test/fixtures/typescript/types/union-intersection/input.ts b/packages/babel-parser/test/fixtures/typescript/types/union-intersection/input.ts index 93f391fdc4..b565cfe40d 100644 --- a/packages/babel-parser/test/fixtures/typescript/types/union-intersection/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/types/union-intersection/input.ts @@ -2,3 +2,8 @@ let union: number | null | undefined; let intersection: number & string; let precedence1: number | string & boolean; let precedence2: number & string | boolean; + +type J = number | string +type F = number & string +type K = | number | string +type M = & number & string diff --git a/packages/babel-parser/test/fixtures/typescript/types/union-intersection/output.json b/packages/babel-parser/test/fixtures/typescript/types/union-intersection/output.json index 6d659d4ac7..cb1cb09da4 100644 --- a/packages/babel-parser/test/fixtures/typescript/types/union-intersection/output.json +++ b/packages/babel-parser/test/fixtures/typescript/types/union-intersection/output.json @@ -1,9 +1,9 @@ { "type": "File", - "start":0,"end":160,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":43}}, + "start":0,"end":265,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":26}}, "program": { "type": "Program", - "start":0,"end":160,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":43}}, + "start":0,"end":265,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":26}}, "sourceType": "module", "interpreter": null, "body": [ @@ -170,6 +170,98 @@ } ], "kind": "let" + }, + { + "type": "TSTypeAliasDeclaration", + "start":162,"end":186,"loc":{"start":{"line":6,"column":0},"end":{"line":6,"column":24}}, + "id": { + "type": "Identifier", + "start":167,"end":168,"loc":{"start":{"line":6,"column":5},"end":{"line":6,"column":6},"identifierName":"J"}, + "name": "J" + }, + "typeAnnotation": { + "type": "TSUnionType", + "start":171,"end":186,"loc":{"start":{"line":6,"column":9},"end":{"line":6,"column":24}}, + "types": [ + { + "type": "TSNumberKeyword", + "start":171,"end":177,"loc":{"start":{"line":6,"column":9},"end":{"line":6,"column":15}} + }, + { + "type": "TSStringKeyword", + "start":180,"end":186,"loc":{"start":{"line":6,"column":18},"end":{"line":6,"column":24}} + } + ] + } + }, + { + "type": "TSTypeAliasDeclaration", + "start":187,"end":211,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":24}}, + "id": { + "type": "Identifier", + "start":192,"end":193,"loc":{"start":{"line":7,"column":5},"end":{"line":7,"column":6},"identifierName":"F"}, + "name": "F" + }, + "typeAnnotation": { + "type": "TSIntersectionType", + "start":196,"end":211,"loc":{"start":{"line":7,"column":9},"end":{"line":7,"column":24}}, + "types": [ + { + "type": "TSNumberKeyword", + "start":196,"end":202,"loc":{"start":{"line":7,"column":9},"end":{"line":7,"column":15}} + }, + { + "type": "TSStringKeyword", + "start":205,"end":211,"loc":{"start":{"line":7,"column":18},"end":{"line":7,"column":24}} + } + ] + } + }, + { + "type": "TSTypeAliasDeclaration", + "start":212,"end":238,"loc":{"start":{"line":8,"column":0},"end":{"line":8,"column":26}}, + "id": { + "type": "Identifier", + "start":217,"end":218,"loc":{"start":{"line":8,"column":5},"end":{"line":8,"column":6},"identifierName":"K"}, + "name": "K" + }, + "typeAnnotation": { + "type": "TSUnionType", + "start":221,"end":238,"loc":{"start":{"line":8,"column":9},"end":{"line":8,"column":26}}, + "types": [ + { + "type": "TSNumberKeyword", + "start":223,"end":229,"loc":{"start":{"line":8,"column":11},"end":{"line":8,"column":17}} + }, + { + "type": "TSStringKeyword", + "start":232,"end":238,"loc":{"start":{"line":8,"column":20},"end":{"line":8,"column":26}} + } + ] + } + }, + { + "type": "TSTypeAliasDeclaration", + "start":239,"end":265,"loc":{"start":{"line":9,"column":0},"end":{"line":9,"column":26}}, + "id": { + "type": "Identifier", + "start":244,"end":245,"loc":{"start":{"line":9,"column":5},"end":{"line":9,"column":6},"identifierName":"M"}, + "name": "M" + }, + "typeAnnotation": { + "type": "TSIntersectionType", + "start":248,"end":265,"loc":{"start":{"line":9,"column":9},"end":{"line":9,"column":26}}, + "types": [ + { + "type": "TSNumberKeyword", + "start":250,"end":256,"loc":{"start":{"line":9,"column":11},"end":{"line":9,"column":17}} + }, + { + "type": "TSStringKeyword", + "start":259,"end":265,"loc":{"start":{"line":9,"column":20},"end":{"line":9,"column":26}} + } + ] + } } ], "directives": []