From 16edacccf6ba24d6cffe960ef266645af021c356 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Thu, 4 Feb 2021 21:00:38 +0100 Subject: [PATCH 1/3] Create `TSUnionType` or `TSIntersectionType` when typealias has a leading operator --- .../src/plugins/typescript/index.js | 4 ++-- .../types/union-intersection/input.ts | 1 + .../types/union-intersection/output.json | 23 +++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index d11a8158fb7b..eaaa7506f67b 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -936,9 +936,9 @@ export default (superClass: Class): Class => operator: TokenType, ): N.TsType { const node: N.TsUnionType | N.TsIntersectionType = this.startNode(); - this.eat(operator); + const leadingOperator = this.eat(operator); let type = parseConstituentType(); - if (this.match(operator)) { + if (this.match(operator) || leadingOperator) { const types = [type]; while (this.eat(operator)) { types.push(parseConstituentType()); 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 b565cfe40d50..e94e99142aff 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 @@ -7,3 +7,4 @@ type J = number | string type F = number & string type K = | number | string type M = & number & string +type N = | number 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 cb1cb09da450..0332d9586be7 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":265,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":26}}, + "start":0,"end":283,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":17}}, "program": { "type": "Program", - "start":0,"end":265,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":26}}, + "start":0,"end":283,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":17}}, "sourceType": "module", "interpreter": null, "body": [ @@ -262,6 +262,25 @@ } ] } + }, + { + "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}} + } + ] + } } ], "directives": [] From ac22adbe90c148e84294511422ce54711c2641e7 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Fri, 5 Feb 2021 08:28:44 +0100 Subject: [PATCH 2/3] Apply code review suggestions --- .../src/plugins/typescript/index.js | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index eaaa7506f67b..94dad4c21fbf 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -936,17 +936,16 @@ export default (superClass: Class): Class => operator: TokenType, ): N.TsType { const node: N.TsUnionType | N.TsIntersectionType = this.startNode(); - const leadingOperator = this.eat(operator); - let type = parseConstituentType(); - if (this.match(operator) || leadingOperator) { - const types = [type]; - while (this.eat(operator)) { - types.push(parseConstituentType()); - } - node.types = types; - type = this.finishNode(node, kind); - } - return type; + const hasLeadingOperator = this.eat(operator); + const types = []; + do { + types.push(parseConstituentType()); + } while (this.eat(operator)); + if (types.length === 1 && !hasLeadingOperator) { + return types[0]; + } + node.types = types; + return this.finishNode(node, kind); } tsParseIntersectionTypeOrHigher(): N.TsType { From eb4b1bdf70528a6fa8f2d80f307b1f568f99a200 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Fri, 5 Feb 2021 10:39:26 +0100 Subject: [PATCH 3/3] Test `TSIntersectionType` --- .../types/union-intersection/input.ts | 1 + .../types/union-intersection/output.json | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) 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 e94e99142aff..9eaf7240b1da 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 @@ -8,3 +8,4 @@ type F = number & string type K = | number | string type M = & number & string type N = | number +type O = & 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 0332d9586be7..3b2d78bb380f 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":283,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":17}}, + "start":0,"end":301,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":17}}, "program": { "type": "Program", - "start":0,"end":283,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":17}}, + "start":0,"end":301,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":17}}, "sourceType": "module", "interpreter": null, "body": [ @@ -281,6 +281,25 @@ } ] } + }, + { + "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": []