From 60ea39aa7076e564345d3c0c842083facf15e701 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Sat, 14 Oct 2017 14:25:42 -0500 Subject: [PATCH] Fix export extensions handling of TS declare keyword (#757) --- src/plugins/typescript.js | 40 +- .../export-with-ts/actual.js | 7 + .../export-with-ts/expected.json | 493 ++++++++++++++++++ .../export-with-ts/options.json | 4 + yarn.lock | 2 +- 5 files changed, 524 insertions(+), 22 deletions(-) create mode 100644 test/fixtures/experimental/export-extensions/export-with-ts/actual.js create mode 100644 test/fixtures/experimental/export-extensions/export-with-ts/expected.json create mode 100644 test/fixtures/experimental/export-extensions/export-with-ts/options.json diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index b2601dfd43..6483205d03 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1191,20 +1191,29 @@ export default (superClass: Class): Class => return this.finishNode(node, "TSTypeParameterInstantiation"); } + tsIsDeclarationStart(): boolean { + if (this.match(tt.name)) { + switch (this.state.value) { + case "abstract": + case "declare": + case "enum": + case "interface": + case "module": + case "namespace": + case "type": + return true; + } + } + + return false; + } + // ====================================================== // OVERRIDES // ====================================================== isExportDefaultSpecifier(): boolean { - if ( - this.match(tt.name) && - (this.state.value === "type" || - this.state.value === "interface" || - this.state.value === "enum") - ) { - return false; - } - + if (this.tsIsDeclarationStart()) return false; return super.isExportDefaultSpecifier(); } @@ -1524,18 +1533,7 @@ export default (superClass: Class): Class => // export type // Should be true for anything parsed by `tsTryParseExportDeclaration`. shouldParseExportDeclaration(): boolean { - if (this.match(tt.name)) { - switch (this.state.value) { - case "abstract": - case "declare": - case "enum": - case "interface": - case "module": - case "namespace": - case "type": - return true; - } - } + if (this.tsIsDeclarationStart()) return true; return super.shouldParseExportDeclaration(); } diff --git a/test/fixtures/experimental/export-extensions/export-with-ts/actual.js b/test/fixtures/experimental/export-extensions/export-with-ts/actual.js new file mode 100644 index 0000000000..0870df12ea --- /dev/null +++ b/test/fixtures/experimental/export-extensions/export-with-ts/actual.js @@ -0,0 +1,7 @@ +export abstract class A {} +export declare interface B {} +export enum C {} +export interface D {} +export module E {} +export namespace F {} +export type G = typeof foo; diff --git a/test/fixtures/experimental/export-extensions/export-with-ts/expected.json b/test/fixtures/experimental/export-extensions/export-with-ts/expected.json new file mode 100644 index 0000000000..7153ffedc8 --- /dev/null +++ b/test/fixtures/experimental/export-extensions/export-with-ts/expected.json @@ -0,0 +1,493 @@ +{ + "type": "File", + "start": 0, + "end": 164, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 27 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 164, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 27 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start": 7, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "A" + }, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "body": [] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 27, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 42, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 52, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + }, + "identifierName": "B" + }, + "name": "B" + }, + "body": { + "type": "TSInterfaceBody", + "start": 54, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "body": [] + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 57, + "end": 73, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 64, + "end": 73, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 69, + "end": 70, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 13 + }, + "identifierName": "C" + }, + "name": "C" + }, + "members": [] + } + }, + { + "type": "ExportNamedDeclaration", + "start": 74, + "end": 95, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 21 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 81, + "end": 95, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 91, + "end": 92, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 18 + }, + "identifierName": "D" + }, + "name": "D" + }, + "body": { + "type": "TSInterfaceBody", + "start": 93, + "end": 95, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 21 + } + }, + "body": [] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 96, + "end": 114, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 18 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 103, + "end": 114, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 110, + "end": 111, + "loc": { + "start": { + "line": 5, + "column": 14 + }, + "end": { + "line": 5, + "column": 15 + }, + "identifierName": "E" + }, + "name": "E" + }, + "body": { + "type": "TSModuleBlock", + "start": 112, + "end": 114, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 18 + } + }, + "body": [] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 115, + "end": 136, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 122, + "end": 136, + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 132, + "end": 133, + "loc": { + "start": { + "line": 6, + "column": 17 + }, + "end": { + "line": 6, + "column": 18 + }, + "identifierName": "F" + }, + "name": "F" + }, + "body": { + "type": "TSModuleBlock", + "start": 134, + "end": 136, + "loc": { + "start": { + "line": 6, + "column": 19 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "body": [] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 137, + "end": 164, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 27 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSTypeAliasDeclaration", + "start": 144, + "end": 164, + "loc": { + "start": { + "line": 7, + "column": 7 + }, + "end": { + "line": 7, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 149, + "end": 150, + "loc": { + "start": { + "line": 7, + "column": 12 + }, + "end": { + "line": 7, + "column": 13 + }, + "identifierName": "G" + }, + "name": "G" + }, + "typeAnnotation": { + "type": "TSTypeQuery", + "start": 153, + "end": 163, + "loc": { + "start": { + "line": 7, + "column": 16 + }, + "end": { + "line": 7, + "column": 26 + } + }, + "exprName": { + "type": "Identifier", + "start": 160, + "end": 163, + "loc": { + "start": { + "line": 7, + "column": 23 + }, + "end": { + "line": 7, + "column": 26 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/export-extensions/export-with-ts/options.json b/test/fixtures/experimental/export-extensions/export-with-ts/options.json new file mode 100644 index 0000000000..504e180665 --- /dev/null +++ b/test/fixtures/experimental/export-extensions/export-with-ts/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["exportExtensions", "typescript"], + "sourceType": "module" +} diff --git a/yarn.lock b/yarn.lock index 50f0503c95..53830a1f29 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2000,7 +2000,7 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -flow-bin@0.56.0: +flow-bin@^0.56.0: version "0.56.0" resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.56.0.tgz#ce43092203a344ba9bf63c0cabe95d95145f6cad"