From 267be70a837de9a3fccb4526f47e0fb02c04cd92 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Fri, 12 Feb 2021 11:27:46 +0100 Subject: [PATCH] Fix the other declarations --- .../src/plugins/typescript/index.js | 69 ++++++++----------- .../class/declare-new-line-abstract/input.ts | 2 + .../declare-new-line-abstract/output.json | 49 +++++++++++++ .../typescript/enum/declare-new-line/input.ts | 3 + .../enum/declare-new-line/output.json | 24 +++++++ .../interface/declare-new-line/input.ts | 3 + .../interface/declare-new-line/output.json | 49 +++++++++++++ .../type-alias/declare-new-line/input.ts | 2 + .../type-alias/declare-new-line/output.json | 53 ++++++++++++++ 9 files changed, 214 insertions(+), 40 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index adf2427a9bf9..66c86e786f1d 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1583,22 +1583,21 @@ export default (superClass: Class): Class => value: string, next: boolean, ): ?N.Declaration { + // no declaration apart from enum can be followed by a line break. switch (value) { case "abstract": - if (this.tsCheckLineTerminatorAndMatch(tt._class, next)) { - const cls: N.ClassDeclaration = node; - cls.abstract = true; - if (next) { - this.next(); - if (!this.match(tt._class)) { - this.unexpected(null, tt._class); - } + if (this.tsCheckLineTerminator(next)) { + if (this.match(tt._class)) { + const cls: N.ClassDeclaration = node; + cls.abstract = true; + return this.parseClass( + cls, + /* isStatement */ true, + /* optionalId */ false, + ); + } else { + this.unexpected(null, tt._class); } - return this.parseClass( - cls, - /* isStatement */ true, - /* optionalId */ false, - ); } break; @@ -1610,53 +1609,43 @@ export default (superClass: Class): Class => break; case "interface": - if (this.tsCheckLineTerminatorAndMatch(tt.name, next)) { - if (next) this.next(); + if (this.tsCheckLineTerminator(next) && this.match(tt.name)) { return this.tsParseInterfaceDeclaration(node); } break; case "module": - // "module" cannot be followed by a line break. - if (next) { - if (this.hasFollowingLineBreak()) break; - this.next(); - } else if (this.isLineTerminator()) { - break; - } - - if (this.match(tt.string)) { - return this.tsParseAmbientExternalModuleDeclaration(node); - } else if (this.match(tt.name)) { - return this.tsParseModuleOrNamespaceDeclaration(node); + if (this.tsCheckLineTerminator(next)) { + if (this.match(tt.string)) { + return this.tsParseAmbientExternalModuleDeclaration(node); + } else if (this.match(tt.name)) { + return this.tsParseModuleOrNamespaceDeclaration(node); + } } break; case "namespace": - // "namespace" cannot be followed by a line break. - if (next) { - if (this.hasFollowingLineBreak()) break; - this.next(); - } else if (this.isLineTerminator()) { - break; - } - - if (this.match(tt.name)) { + if (this.tsCheckLineTerminator(next) && this.match(tt.name)) { return this.tsParseModuleOrNamespaceDeclaration(node); } break; case "type": - if (this.tsCheckLineTerminatorAndMatch(tt.name, next)) { - if (next) this.next(); + if (this.tsCheckLineTerminator(next) && this.match(tt.name)) { return this.tsParseTypeAliasDeclaration(node); } break; } } - tsCheckLineTerminatorAndMatch(tokenType: TokenType, next: boolean) { - return (next || this.match(tokenType)) && !this.isLineTerminator(); + tsCheckLineTerminator(next: boolean) { + if (next) { + if (this.hasFollowingLineBreak()) return false; + this.next(); + } else if (this.isLineTerminator()) { + return false; + } + return true; } tsTryParseGenericAsyncArrowFunction( diff --git a/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts b/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts new file mode 100644 index 000000000000..8c6c81cfafcc --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts @@ -0,0 +1,2 @@ +declare abstract +class A {} diff --git a/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/output.json b/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/output.json new file mode 100644 index 000000000000..04a02c759f73 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/output.json @@ -0,0 +1,49 @@ +{ + "type": "File", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, + "errors": [ + "SyntaxError: Missing semicolon (1:7)" + ], + "program": { + "type": "Program", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "expression": { + "type": "Identifier", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7},"identifierName":"declare"}, + "name": "declare" + } + }, + { + "type": "ExpressionStatement", + "start":8,"end":16,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":16}}, + "expression": { + "type": "Identifier", + "start":8,"end":16,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":16},"identifierName":"abstract"}, + "name": "abstract" + } + }, + { + "type": "ClassDeclaration", + "start":17,"end":27,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":10}}, + "id": { + "type": "Identifier", + "start":23,"end":24,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":7},"identifierName":"A"}, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":25,"end":27,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":10}}, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/input.ts b/packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/input.ts new file mode 100644 index 000000000000..8ce2bd56c88a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/input.ts @@ -0,0 +1,3 @@ +declare enum +E +{} diff --git a/packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/output.json b/packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/output.json new file mode 100644 index 000000000000..5ec877e931ba --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/enum/declare-new-line/output.json @@ -0,0 +1,24 @@ +{ + "type": "File", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":2}}, + "program": { + "type": "Program", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":2}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSEnumDeclaration", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":2}}, + "id": { + "type": "Identifier", + "start":13,"end":14,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":1},"identifierName":"E"}, + "name": "E" + }, + "members": [], + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/input.ts b/packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/input.ts new file mode 100644 index 000000000000..2a075540d6ae --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/input.ts @@ -0,0 +1,3 @@ +declare interface +I +{} diff --git a/packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/output.json b/packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/output.json new file mode 100644 index 000000000000..5158d520298c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/interface/declare-new-line/output.json @@ -0,0 +1,49 @@ +{ + "type": "File", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":2}}, + "errors": [ + "SyntaxError: Missing semicolon (1:7)" + ], + "program": { + "type": "Program", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":2}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "expression": { + "type": "Identifier", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7},"identifierName":"declare"}, + "name": "declare" + } + }, + { + "type": "ExpressionStatement", + "start":8,"end":17,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":17}}, + "expression": { + "type": "Identifier", + "start":8,"end":17,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":17},"identifierName":"interface"}, + "name": "interface" + } + }, + { + "type": "ExpressionStatement", + "start":18,"end":19,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":1}}, + "expression": { + "type": "Identifier", + "start":18,"end":19,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":1},"identifierName":"I"}, + "name": "I" + } + }, + { + "type": "BlockStatement", + "start":20,"end":22,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":2}}, + "body": [], + "directives": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/input.ts b/packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/input.ts new file mode 100644 index 000000000000..d8433232652d --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/input.ts @@ -0,0 +1,2 @@ +declare type +T = number diff --git a/packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/output.json b/packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/output.json new file mode 100644 index 000000000000..89ebf2f1f64a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/type-alias/declare-new-line/output.json @@ -0,0 +1,53 @@ +{ + "type": "File", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, + "errors": [ + "SyntaxError: Missing semicolon (1:7)" + ], + "program": { + "type": "Program", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "expression": { + "type": "Identifier", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7},"identifierName":"declare"}, + "name": "declare" + } + }, + { + "type": "ExpressionStatement", + "start":8,"end":12,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":12}}, + "expression": { + "type": "Identifier", + "start":8,"end":12,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":12},"identifierName":"type"}, + "name": "type" + } + }, + { + "type": "ExpressionStatement", + "start":13,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":10}}, + "expression": { + "type": "AssignmentExpression", + "start":13,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":10}}, + "operator": "=", + "left": { + "type": "Identifier", + "start":13,"end":14,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":1},"identifierName":"T"}, + "name": "T" + }, + "right": { + "type": "Identifier", + "start":17,"end":23,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":10},"identifierName":"number"}, + "name": "number" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file