From beb38d34a26016051eb0ed364ea1112f416955ac Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 28 Feb 2023 14:01:37 +0800 Subject: [PATCH] feat(es/typescript): Support `export type * from "mod"` (#6867) --- .../exportNamespace10.1.normal.js | 11 + .../exportNamespace10.2.minified.js | 11 + .../exportNamespace2.1.normal.js | 14 + .../exportNamespace2.2.minified.js | 14 + .../exportNamespace4.1.normal.js | 17 + .../exportNamespace4.2.minified.js | 16 + .../exportNamespace5.1.normal.js | 29 + .../exportNamespace5.2.minified.js | 25 + .../exportNamespace6.1.normal.js | 18 + .../exportNamespace6.2.minified.js | 17 + .../exportNamespace7.1.normal.js | 32 + .../exportNamespace7.2.minified.js | 28 + .../exportNamespace8.1.normal.js | 27 + .../exportNamespace8.2.minified.js | 25 + .../exportNamespace9.1.normal.js | 29 + .../exportNamespace9.2.minified.js | 25 + .../namedTupleMembersErrors.1.normal.js | 11 - .../namedTupleMembersErrors.2.minified.js | 11 - crates/swc_ecma_ast/src/module_decl.rs | 4 + crates/swc_ecma_dep_graph/src/lib.rs | 7 +- crates/swc_ecma_parser/src/error.rs | 2 - crates/swc_ecma_parser/src/parser/stmt.rs | 6 +- .../src/parser/stmt/module_item.rs | 5 +- .../tests/tsc/ambientShorthand_reExport.json | 1 + .../tsc/es6modulekindWithES5Target9.json | 1 + .../tsc/esnextmodulekindWithES5Target9.json | 1 + .../tests/tsc/exportNamespace1.json | 1 + .../tests/tsc/exportNamespace10.json | 236 +++++ .../tests/tsc/exportNamespace10.ts | 9 + .../tests/tsc/exportNamespace2.json | 248 +++++ .../tests/tsc/exportNamespace2.ts | 13 + .../tests/tsc/exportNamespace4.json | 243 +++++ .../tests/tsc/exportNamespace4.ts | 18 + .../tests/tsc/exportNamespace5.json | 856 ++++++++++++++++++ .../tests/tsc/exportNamespace5.ts | 26 + .../tests/tsc/exportNamespace6.json | 337 +++++++ .../tests/tsc/exportNamespace6.ts | 14 + .../tests/tsc/exportNamespace7.json | 819 +++++++++++++++++ .../tests/tsc/exportNamespace7.ts | 23 + .../tests/tsc/exportNamespace8.json | 503 ++++++++++ .../tests/tsc/exportNamespace8.ts | 17 + .../tests/tsc/exportNamespace9.json | 473 ++++++++++ .../tests/tsc/exportNamespace9.ts | 27 + .../tests/tsc/exportStar-amd.json | 3 + .../swc_ecma_parser/tests/tsc/exportStar.json | 3 + .../tests/tsc/importAssertion2.json | 1 + .../tests/tsc/jsDeclarationsExportForms.json | 3 + .../typescript/custom/issue-535/input.ts.json | 1 + .../export/test3/input.ts.json | 1 + .../src/strip.rs | 6 +- crates/swc_ecma_visit/src/lib.rs | 1 + .../src/babelify/module_decl.rs | 6 +- crates/swc_estree_compat/src/swcify/stmt.rs | 2 + 53 files changed, 4242 insertions(+), 35 deletions(-) create mode 100644 crates/swc/tests/tsc-references/exportNamespace10.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace10.2.minified.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace2.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace2.2.minified.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace4.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace4.2.minified.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace5.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace5.2.minified.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace6.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace6.2.minified.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace7.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace7.2.minified.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace8.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace8.2.minified.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace9.1.normal.js create mode 100644 crates/swc/tests/tsc-references/exportNamespace9.2.minified.js create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace10.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace10.ts create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace2.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace2.ts create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace4.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace4.ts create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace5.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace5.ts create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace6.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace6.ts create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace7.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace7.ts create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace8.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace8.ts create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace9.json create mode 100644 crates/swc_ecma_parser/tests/tsc/exportNamespace9.ts diff --git a/crates/swc/tests/tsc-references/exportNamespace10.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace10.1.normal.js new file mode 100644 index 000000000000..1ec3a1280ef4 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace10.1.normal.js @@ -0,0 +1,11 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +//// [/b.ts] +export { }; +//// [/c.ts] +import { ns } from "./b"; +var _ = new ns.A(); // Error diff --git a/crates/swc/tests/tsc-references/exportNamespace10.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace10.2.minified.js new file mode 100644 index 000000000000..3949328fd605 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace10.2.minified.js @@ -0,0 +1,11 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +//// [/b.ts] +export { }; +//// [/c.ts] +import { ns } from "./b"; +new ns.A(); diff --git a/crates/swc/tests/tsc-references/exportNamespace2.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace2.1.normal.js new file mode 100644 index 000000000000..1cea91453523 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace2.1.normal.js @@ -0,0 +1,14 @@ +//// [a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +//// [b.ts] +import * as _a from "./a"; +export { _a as a }; +//// [c.ts] +export { }; +//// [d.ts] +import { a } from "./c"; +new a.A(); // Error diff --git a/crates/swc/tests/tsc-references/exportNamespace2.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace2.2.minified.js new file mode 100644 index 000000000000..54d8a35fb793 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace2.2.minified.js @@ -0,0 +1,14 @@ +//// [a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +//// [b.ts] +import * as _a from "./a"; +export { _a as a }; +//// [c.ts] +export { }; +//// [d.ts] +import { a } from "./c"; +new a.A(); diff --git a/crates/swc/tests/tsc-references/exportNamespace4.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace4.1.normal.js new file mode 100644 index 000000000000..46a03805ce09 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace4.1.normal.js @@ -0,0 +1,17 @@ +//// [exportNamespace4.ts] +//// [a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +//// [b.ts] +export { }; +//// [c.ts] +export { }; +//// [d.ts] +import { A } from "./b"; +A; +//// [e.ts] +import { ns } from "./c"; +ns.A; diff --git a/crates/swc/tests/tsc-references/exportNamespace4.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace4.2.minified.js new file mode 100644 index 000000000000..4d9e51994e2f --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace4.2.minified.js @@ -0,0 +1,16 @@ +//// [exportNamespace4.ts] +//// [a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +//// [b.ts] +export { }; +//// [c.ts] +export { }; +//// [d.ts] +import { A } from "./b"; +//// [e.ts] +import { ns } from "./c"; +ns.A; diff --git a/crates/swc/tests/tsc-references/exportNamespace5.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace5.1.normal.js new file mode 100644 index 000000000000..314c8c79fffd --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace5.1.normal.js @@ -0,0 +1,29 @@ +//// [exportNamespace5.ts] +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +export var X = function X() { + "use strict"; + _class_call_check(this, X); +}; +//// [/b.ts] +export { X } from "./a"; +//// [/c.ts] +import { A, B as C, X } from "./b"; +var _ = new A(); // Error +var __ = new C(); // Error +var ___ = new X(); // Ok +//// [/d.ts] +export * from "./a"; +//// [/e.ts] +import { A, B, X } from "./d"; +var _ = new A(); // Ok +var __ = new B(); // Ok +var ___ = new X(); // Ok diff --git a/crates/swc/tests/tsc-references/exportNamespace5.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace5.2.minified.js new file mode 100644 index 000000000000..b81bdcba6172 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace5.2.minified.js @@ -0,0 +1,25 @@ +//// [exportNamespace5.ts] +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +export var X = function X() { + "use strict"; + _class_call_check(this, X); +}; +//// [/b.ts] +export { X } from "./a"; +//// [/c.ts] +import { A, B as C, X } from "./b"; +new A(), new C(), new X(); +//// [/d.ts] +export * from "./a"; +//// [/e.ts] +import { A, B, X } from "./d"; +new A(), new B(), new X(); diff --git a/crates/swc/tests/tsc-references/exportNamespace6.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace6.1.normal.js new file mode 100644 index 000000000000..b3009b670709 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace6.1.normal.js @@ -0,0 +1,18 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +//// [/b.ts] +export { }; +//// [/c.ts] +export * from "./b"; +//// [/d.ts] +import { A, B } from "./c"; +var _ = new A(); // Error +var __ = new B(); // Error diff --git a/crates/swc/tests/tsc-references/exportNamespace6.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace6.2.minified.js new file mode 100644 index 000000000000..09df562008be --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace6.2.minified.js @@ -0,0 +1,17 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +//// [/b.ts] +export { }; +//// [/c.ts] +export * from "./b"; +//// [/d.ts] +import { A, B } from "./c"; +new A(), new B(); diff --git a/crates/swc/tests/tsc-references/exportNamespace7.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace7.1.normal.js new file mode 100644 index 000000000000..a94803111419 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace7.1.normal.js @@ -0,0 +1,32 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +export var C = function C() { + "use strict"; + _class_call_check(this, C); +}; +//// [/b.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var C = function C() { + "use strict"; + _class_call_check(this, C); +}; +//// [/c.ts] +import { A, B, C } from "./b"; +var _ = new A(); // Error +var __ = new B(); // Error +var ___ = new C(); // Ok +//// [/d.ts] +export { }; +//// [/e.ts] +import { A, B, C } from "./d"; +var _ = new A(); // Error +var __ = new B(); // Error +var ___ = new C(); // Error diff --git a/crates/swc/tests/tsc-references/exportNamespace7.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace7.2.minified.js new file mode 100644 index 000000000000..1031618da4c7 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace7.2.minified.js @@ -0,0 +1,28 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +export var C = function C() { + "use strict"; + _class_call_check(this, C); +}; +//// [/b.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var C = function C() { + "use strict"; + _class_call_check(this, C); +}; +//// [/c.ts] +import { A, B, C } from "./b"; +new A(), new B(), new C(); +//// [/d.ts] +export { }; +//// [/e.ts] +import { A, B, C } from "./d"; +new A(), new B(), new C(); diff --git a/crates/swc/tests/tsc-references/exportNamespace8.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace8.1.normal.js new file mode 100644 index 000000000000..9ad0c87d7335 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace8.1.normal.js @@ -0,0 +1,27 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +//// [/b.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +export var C = function C() { + "use strict"; + _class_call_check(this, C); +}; +//// [/c.ts] +export * from "./b"; // Collision error +//// [/d.ts] +import { A, B, C } from "./c"; +var _ = new A(); // Error +var __ = new B(); // Ok +var ___ = new C(); // Ok diff --git a/crates/swc/tests/tsc-references/exportNamespace8.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace8.2.minified.js new file mode 100644 index 000000000000..9e77ae6c1f06 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace8.2.minified.js @@ -0,0 +1,25 @@ +//// [/a.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var A = function A() { + "use strict"; + _class_call_check(this, A); +}; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +//// [/b.ts] +import _class_call_check from "@swc/helpers/src/_class_call_check.mjs"; +export var B = function B() { + "use strict"; + _class_call_check(this, B); +}; +export var C = function C() { + "use strict"; + _class_call_check(this, C); +}; +//// [/c.ts] +export * from "./b"; +//// [/d.ts] +import { A, B, C } from "./c"; +new A(), new B(), new C(); diff --git a/crates/swc/tests/tsc-references/exportNamespace9.1.normal.js b/crates/swc/tests/tsc-references/exportNamespace9.1.normal.js new file mode 100644 index 000000000000..92934cce6822 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace9.1.normal.js @@ -0,0 +1,29 @@ +//// [/a.ts] +export { }; +//// [/b.ts] +export { }; +//// [/c.ts] +//! +//! x the name `A` is defined multiple times +//! ,-[1:1] +//! 1 | import { A } from "./b"; +//! : | +//! : `-- previous definition of `A` here +//! 2 | const A = 1; +//! : | +//! : `-- `A` redefined here +//! 3 | export { A }; +//! 4 | +//! `---- +//// [/d.ts] +import { A } from "./c"; +A; // Ok +//// [/e.ts] +export var A = 1; +//// [/f.ts] +export * from "./e"; + // Collision error +//// [/g.ts] +import { A } from "./f"; +A; + // Follow-on from collision error diff --git a/crates/swc/tests/tsc-references/exportNamespace9.2.minified.js b/crates/swc/tests/tsc-references/exportNamespace9.2.minified.js new file mode 100644 index 000000000000..6bbfb98b7d32 --- /dev/null +++ b/crates/swc/tests/tsc-references/exportNamespace9.2.minified.js @@ -0,0 +1,25 @@ +//// [/a.ts] +export { }; +//// [/b.ts] +export { }; +//// [/c.ts] +//! +//! x the name `A` is defined multiple times +//! ,-[1:1] +//! 1 | import { A } from "./b"; +//! : | +//! : `-- previous definition of `A` here +//! 2 | const A = 1; +//! : | +//! : `-- `A` redefined here +//! 3 | export { A }; +//! 4 | +//! `---- +//// [/d.ts] +import { A } from "./c"; +//// [/e.ts] +export var A = 1; +//// [/f.ts] +export * from "./e"; +//// [/g.ts] +import { A } from "./f"; diff --git a/crates/swc/tests/tsc-references/namedTupleMembersErrors.1.normal.js b/crates/swc/tests/tsc-references/namedTupleMembersErrors.1.normal.js index 47b79a1a3e14..baa07930097f 100644 --- a/crates/swc/tests/tsc-references/namedTupleMembersErrors.1.normal.js +++ b/crates/swc/tests/tsc-references/namedTupleMembersErrors.1.normal.js @@ -1,16 +1,5 @@ //// [namedTupleMembersErrors.ts] //! -//! x Only named exports may use 'export type'. -//! ,-[12:1] -//! 12 | -//! 13 | export type Trailing = [first: string, rest: ...string[]]; // dots on element disallowed -//! 14 | -//! 15 | export type OptTrailing = [first: string, rest: ...string[]?]; // dots+question on element disallowed -//! : ^^^^^^^^^^^ -//! 16 | -//! 17 | export type OptRest = [first: string, ...rest?: string[]]; // rest+optional disallowed -//! `---- -//! //! x Expected '{', got 'OptTrailing' //! ,-[12:1] //! 12 | diff --git a/crates/swc/tests/tsc-references/namedTupleMembersErrors.2.minified.js b/crates/swc/tests/tsc-references/namedTupleMembersErrors.2.minified.js index 47b79a1a3e14..baa07930097f 100644 --- a/crates/swc/tests/tsc-references/namedTupleMembersErrors.2.minified.js +++ b/crates/swc/tests/tsc-references/namedTupleMembersErrors.2.minified.js @@ -1,16 +1,5 @@ //// [namedTupleMembersErrors.ts] //! -//! x Only named exports may use 'export type'. -//! ,-[12:1] -//! 12 | -//! 13 | export type Trailing = [first: string, rest: ...string[]]; // dots on element disallowed -//! 14 | -//! 15 | export type OptTrailing = [first: string, rest: ...string[]?]; // dots+question on element disallowed -//! : ^^^^^^^^^^^ -//! 16 | -//! 17 | export type OptRest = [first: string, ...rest?: string[]]; // rest+optional disallowed -//! `---- -//! //! x Expected '{', got 'OptTrailing' //! ,-[12:1] //! 12 | diff --git a/crates/swc_ecma_ast/src/module_decl.rs b/crates/swc_ecma_ast/src/module_decl.rs index fb7da89bdb80..cefe6fdb70de 100644 --- a/crates/swc_ecma_ast/src/module_decl.rs +++ b/crates/swc_ecma_ast/src/module_decl.rs @@ -109,6 +109,9 @@ pub struct ExportAll { #[serde(rename = "source")] pub src: Box, + #[serde(rename = "typeOnly")] + pub type_only: bool, + #[serde(default)] pub asserts: Option>, } @@ -118,6 +121,7 @@ impl Take for ExportAll { Self { span: DUMMY_SP, src: Take::dummy(), + type_only: Default::default(), asserts: Take::dummy(), } } diff --git a/crates/swc_ecma_dep_graph/src/lib.rs b/crates/swc_ecma_dep_graph/src/lib.rs index 9d131e182ed9..62ff96fed604 100644 --- a/crates/swc_ecma_dep_graph/src/lib.rs +++ b/crates/swc_ecma_dep_graph/src/lib.rs @@ -147,9 +147,14 @@ impl<'a> Visit for DependencyCollector<'a> { fn visit_export_all(&mut self, node: &ast::ExportAll) { let specifier = node.src.value.clone(); let leading_comments = self.get_leading_comments(node.span); + let kind = if node.type_only { + DependencyKind::ExportType + } else { + DependencyKind::Export + }; let import_assertions = parse_import_assertions(node.asserts.as_deref()); self.items.push(DependencyDescriptor { - kind: DependencyKind::Export, + kind, is_dynamic: false, leading_comments, span: node.span, diff --git a/crates/swc_ecma_parser/src/error.rs b/crates/swc_ecma_parser/src/error.rs index 262c4b2ff77b..afe6e373bfae 100644 --- a/crates/swc_ecma_parser/src/error.rs +++ b/crates/swc_ecma_parser/src/error.rs @@ -256,7 +256,6 @@ pub enum SyntaxError { TS1273(JsWord), TS1274(JsWord), TS1277(JsWord), - TS1383, TS2206, TS2207, TS2369, @@ -656,7 +655,6 @@ impl SyntaxError { word ) .into(), - SyntaxError::TS1383 => "Only named exports may use 'export type'.".into(), SyntaxError::TS2206 => "The 'type' modifier cannot be used on a named import when \ 'import type' is used on its import statement." .into(), diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index e96eb4dc5343..8969899f7a90 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -2419,8 +2419,7 @@ export default function waitUntil(callback, options = {}) { } #[test] - #[should_panic(expected = "Only named exports may use 'export type'.")] - fn error_for_type_only_star_exports_with_name() { + fn type_only_star_exports_with_name() { let src = "export type * as bar from 'mod'"; test_parser(src, Syntax::Typescript(Default::default()), |p| { p.parse_module() @@ -2428,8 +2427,7 @@ export default function waitUntil(callback, options = {}) { } #[test] - #[should_panic(expected = "Only named exports may use 'export type'.")] - fn error_for_type_only_star_exports_without_name() { + fn type_only_star_exports_without_name() { let src = "export type * from 'mod'"; test_parser(src, Syntax::Typescript(Default::default()), |p| { p.parse_module() diff --git a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs index cf8e19f97f75..d32acd01eb13 100644 --- a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs +++ b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs @@ -544,10 +544,6 @@ impl Parser { } }; - if self.input.syntax().typescript() && type_only && !is!(self, '{') { - self.emit_err(span!(self, start), SyntaxError::TS1383) - } - if default.is_none() && is!(self, '*') && !peeked_is!(self, "as") { assert_and_bump!(self, '*'); @@ -556,6 +552,7 @@ impl Parser { return Ok(ModuleDecl::ExportAll(ExportAll { span: span!(self, start), src, + type_only, asserts, })); } diff --git a/crates/swc_ecma_parser/tests/tsc/ambientShorthand_reExport.json b/crates/swc_ecma_parser/tests/tsc/ambientShorthand_reExport.json index 1a80c1a5db25..965740fdc5d6 100644 --- a/crates/swc_ecma_parser/tests/tsc/ambientShorthand_reExport.json +++ b/crates/swc_ecma_parser/tests/tsc/ambientShorthand_reExport.json @@ -86,6 +86,7 @@ "value": "jquery", "raw": "\"jquery\"" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/tsc/es6modulekindWithES5Target9.json b/crates/swc_ecma_parser/tests/tsc/es6modulekindWithES5Target9.json index 79226f8ef4cb..8023ab937afe 100644 --- a/crates/swc_ecma_parser/tests/tsc/es6modulekindWithES5Target9.json +++ b/crates/swc_ecma_parser/tests/tsc/es6modulekindWithES5Target9.json @@ -244,6 +244,7 @@ "value": "mod", "raw": "\"mod\"" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/tsc/esnextmodulekindWithES5Target9.json b/crates/swc_ecma_parser/tests/tsc/esnextmodulekindWithES5Target9.json index 79226f8ef4cb..8023ab937afe 100644 --- a/crates/swc_ecma_parser/tests/tsc/esnextmodulekindWithES5Target9.json +++ b/crates/swc_ecma_parser/tests/tsc/esnextmodulekindWithES5Target9.json @@ -244,6 +244,7 @@ "value": "mod", "raw": "\"mod\"" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace1.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace1.json index 6e4b6607dddb..d72df8d7aac3 100644 --- a/crates/swc_ecma_parser/tests/tsc/exportNamespace1.json +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace1.json @@ -99,6 +99,7 @@ "value": "./b", "raw": "'./b'" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace10.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace10.json new file mode 100644 index 000000000000..6b2dc258fbd6 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace10.json @@ -0,0 +1,236 @@ +{ + "type": "Module", + "span": { + "start": 21, + "end": 164, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 21, + "end": 38, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 34, + "end": 35, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "declare": false, + "span": { + "start": 28, + "end": 38, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportNamedDeclaration", + "span": { + "start": 60, + "end": 91, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "span": { + "start": 67, + "end": 79, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 77, + "end": 79, + "ctxt": 0 + }, + "value": "ns", + "optional": false + } + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 85, + "end": 90, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 113, + "end": 138, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 122, + "end": 124, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 122, + "end": 124, + "ctxt": 0 + }, + "value": "ns", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 132, + "end": 137, + "ctxt": 0 + }, + "value": "./b", + "raw": "\"./b\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 139, + "end": 164, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 143, + "end": 163, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 143, + "end": 144, + "ctxt": 0 + }, + "value": "_", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 144, + "end": 150, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 146, + "end": 150, + "ctxt": 0 + }, + "typeName": { + "type": "TsQualifiedName", + "left": { + "type": "Identifier", + "span": { + "start": 146, + "end": 148, + "ctxt": 0 + }, + "value": "ns", + "optional": false + }, + "right": { + "type": "Identifier", + "span": { + "start": 149, + "end": 150, + "ctxt": 0 + }, + "value": "A", + "optional": false + } + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 153, + "end": 163, + "ctxt": 0 + }, + "callee": { + "type": "MemberExpression", + "span": { + "start": 157, + "end": 161, + "ctxt": 0 + }, + "object": { + "type": "Identifier", + "span": { + "start": 157, + "end": 159, + "ctxt": 0 + }, + "value": "ns", + "optional": false + }, + "property": { + "type": "Identifier", + "span": { + "start": 160, + "end": 161, + "ctxt": 0 + }, + "value": "A", + "optional": false + } + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace10.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace10.ts new file mode 100644 index 000000000000..05a3baf824ab --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace10.ts @@ -0,0 +1,9 @@ +// @Filename: /a.ts +export class A {} + +// @Filename: /b.ts +export type * as ns from "./a"; + +// @Filename: /c.ts +import { ns } from "./b"; +let _: ns.A = new ns.A(); // Error \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace2.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace2.json new file mode 100644 index 000000000000..56c26379070e --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace2.json @@ -0,0 +1,248 @@ +{ + "type": "Module", + "span": { + "start": 20, + "end": 203, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 20, + "end": 37, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 33, + "end": 34, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "declare": false, + "span": { + "start": 27, + "end": 37, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportNamedDeclaration", + "span": { + "start": 58, + "end": 83, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "span": { + "start": 65, + "end": 71, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 70, + "end": 71, + "ctxt": 0 + }, + "value": "a", + "optional": false + } + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 77, + "end": 82, + "ctxt": 0 + }, + "value": "./a", + "raw": "'./a'" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 104, + "end": 133, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 118, + "end": 119, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 118, + "end": 119, + "ctxt": 0 + }, + "value": "a", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 127, + "end": 132, + "ctxt": 0 + }, + "value": "./b", + "raw": "'./b'" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ExportNamedDeclaration", + "span": { + "start": 134, + "end": 147, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "span": { + "start": 143, + "end": 144, + "ctxt": 0 + }, + "orig": { + "type": "Identifier", + "span": { + "start": 143, + "end": 144, + "ctxt": 0 + }, + "value": "a", + "optional": false + }, + "exported": null, + "isTypeOnly": false + } + ], + "source": null, + "typeOnly": false, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 168, + "end": 192, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 177, + "end": 178, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 177, + "end": 178, + "ctxt": 0 + }, + "value": "a", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 186, + "end": 191, + "ctxt": 0 + }, + "value": "./c", + "raw": "'./c'" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ExpressionStatement", + "span": { + "start": 193, + "end": 203, + "ctxt": 0 + }, + "expression": { + "type": "NewExpression", + "span": { + "start": 193, + "end": 202, + "ctxt": 0 + }, + "callee": { + "type": "MemberExpression", + "span": { + "start": 197, + "end": 200, + "ctxt": 0 + }, + "object": { + "type": "Identifier", + "span": { + "start": 197, + "end": 198, + "ctxt": 0 + }, + "value": "a", + "optional": false + }, + "property": { + "type": "Identifier", + "span": { + "start": 199, + "end": 200, + "ctxt": 0 + }, + "value": "A", + "optional": false + } + }, + "arguments": [], + "typeArguments": null + } + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace2.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace2.ts new file mode 100644 index 000000000000..9f693c9def2c --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace2.ts @@ -0,0 +1,13 @@ +// @Filename: a.ts +export class A {} + +// @Filename: b.ts +export * as a from './a'; + +// @Filename: c.ts +import type { a } from './b'; +export { a }; + +// @Filename: d.ts +import { a } from './c'; +new a.A(); // Error diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace4.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace4.json new file mode 100644 index 000000000000..f2dc6d80bf62 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace4.json @@ -0,0 +1,243 @@ +{ + "type": "Module", + "span": { + "start": 43, + "end": 258, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 43, + "end": 60, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 56, + "end": 57, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "declare": false, + "span": { + "start": 50, + "end": 60, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 81, + "end": 106, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 100, + "end": 105, + "ctxt": 0 + }, + "value": "./a", + "raw": "'./a'" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ExportNamedDeclaration", + "span": { + "start": 127, + "end": 158, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "span": { + "start": 134, + "end": 146, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 144, + "end": 146, + "ctxt": 0 + }, + "value": "ns", + "optional": false + } + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 152, + "end": 157, + "ctxt": 0 + }, + "value": "./a", + "raw": "'./a'" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 179, + "end": 203, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 188, + "end": 189, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 188, + "end": 189, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 197, + "end": 202, + "ctxt": 0 + }, + "value": "./b", + "raw": "'./b'" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ExpressionStatement", + "span": { + "start": 204, + "end": 206, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 204, + "end": 205, + "ctxt": 0 + }, + "value": "A", + "optional": false + } + }, + { + "type": "ImportDeclaration", + "span": { + "start": 227, + "end": 252, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 236, + "end": 238, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 236, + "end": 238, + "ctxt": 0 + }, + "value": "ns", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 246, + "end": 251, + "ctxt": 0 + }, + "value": "./c", + "raw": "'./c'" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ExpressionStatement", + "span": { + "start": 253, + "end": 258, + "ctxt": 0 + }, + "expression": { + "type": "MemberExpression", + "span": { + "start": 253, + "end": 257, + "ctxt": 0 + }, + "object": { + "type": "Identifier", + "span": { + "start": 253, + "end": 255, + "ctxt": 0 + }, + "value": "ns", + "optional": false + }, + "property": { + "type": "Identifier", + "span": { + "start": 256, + "end": 257, + "ctxt": 0 + }, + "value": "A", + "optional": false + } + } + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace4.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace4.ts new file mode 100644 index 000000000000..1cb1c1f0d953 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace4.ts @@ -0,0 +1,18 @@ +// @declaration: true + +// @Filename: a.ts +export class A {} + +// @Filename: b.ts +export type * from './a'; + +// @Filename: c.ts +export type * as ns from './a'; + +// @Filename: d.ts +import { A } from './b'; +A; + +// @Filename: e.ts +import { ns } from './c'; +ns.A; diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace5.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace5.json new file mode 100644 index 000000000000..b591c273f0a7 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace5.json @@ -0,0 +1,856 @@ +{ + "type": "Module", + "span": { + "start": 44, + "end": 514, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 44, + "end": 61, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 57, + "end": 58, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "declare": false, + "span": { + "start": 51, + "end": 61, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 62, + "end": 79, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 75, + "end": 76, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "declare": false, + "span": { + "start": 69, + "end": 79, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 80, + "end": 97, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 93, + "end": 94, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "declare": false, + "span": { + "start": 87, + "end": 97, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 119, + "end": 144, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 138, + "end": 143, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ExportNamedDeclaration", + "span": { + "start": 145, + "end": 169, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "span": { + "start": 154, + "end": 155, + "ctxt": 0 + }, + "orig": { + "type": "Identifier", + "span": { + "start": 154, + "end": 155, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "exported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 163, + "end": 168, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 191, + "end": 226, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 200, + "end": 201, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 200, + "end": 201, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 203, + "end": 209, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 208, + "end": 209, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "imported": { + "type": "Identifier", + "span": { + "start": 203, + "end": 204, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 211, + "end": 212, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 211, + "end": 212, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 220, + "end": 225, + "ctxt": 0 + }, + "value": "./b", + "raw": "\"./b\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 227, + "end": 246, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 231, + "end": 245, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 231, + "end": 232, + "ctxt": 0 + }, + "value": "_", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 232, + "end": 235, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 234, + "end": 235, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 234, + "end": 235, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 238, + "end": 245, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 242, + "end": 243, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 258, + "end": 278, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 262, + "end": 277, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 262, + "end": 264, + "ctxt": 0 + }, + "value": "__", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 264, + "end": 267, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 266, + "end": 267, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 266, + "end": 267, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 270, + "end": 277, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 274, + "end": 275, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 289, + "end": 310, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 293, + "end": 309, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 293, + "end": 296, + "ctxt": 0 + }, + "value": "___", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 296, + "end": 299, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 298, + "end": 299, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 298, + "end": 299, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 302, + "end": 309, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 306, + "end": 307, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 338, + "end": 363, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 357, + "end": 362, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 364, + "end": 384, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 378, + "end": 383, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 406, + "end": 436, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 415, + "end": 416, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 415, + "end": 416, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 418, + "end": 419, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 418, + "end": 419, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 421, + "end": 422, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 421, + "end": 422, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 430, + "end": 435, + "ctxt": 0 + }, + "value": "./d", + "raw": "\"./d\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 437, + "end": 456, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 441, + "end": 455, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 441, + "end": 442, + "ctxt": 0 + }, + "value": "_", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 442, + "end": 445, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 444, + "end": 445, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 444, + "end": 445, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 448, + "end": 455, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 452, + "end": 453, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 465, + "end": 485, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 469, + "end": 484, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 469, + "end": 471, + "ctxt": 0 + }, + "value": "__", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 471, + "end": 474, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 473, + "end": 474, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 473, + "end": 474, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 477, + "end": 484, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 481, + "end": 482, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 493, + "end": 514, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 497, + "end": 513, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 497, + "end": 500, + "ctxt": 0 + }, + "value": "___", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 500, + "end": 503, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 502, + "end": 503, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 502, + "end": 503, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 506, + "end": 513, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 510, + "end": 511, + "ctxt": 0 + }, + "value": "X", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace5.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace5.ts new file mode 100644 index 000000000000..fd4d392fb589 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace5.ts @@ -0,0 +1,26 @@ +// @declaration: true + +// @Filename: /a.ts +export class A {} +export class B {} +export class X {} + +// @Filename: /b.ts +export type * from "./a"; +export { X } from "./a"; + +// @Filename: /c.ts +import { A, B as C, X } from "./b"; +let _: A = new A(); // Error +let __: C = new C(); // Error +let ___: X = new X(); // Ok + +// @Filename: /d.ts +export type * from "./a"; +export * from "./a"; + +// @Filename: /e.ts +import { A, B, X } from "./d"; +let _: A = new A(); // Ok +let __: B = new B(); // Ok +let ___: X = new X(); // Ok \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace6.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace6.json new file mode 100644 index 000000000000..69243c820e84 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace6.json @@ -0,0 +1,337 @@ +{ + "type": "Module", + "span": { + "start": 21, + "end": 245, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 21, + "end": 38, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 34, + "end": 35, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "declare": false, + "span": { + "start": 28, + "end": 38, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 39, + "end": 56, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 52, + "end": 53, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "declare": false, + "span": { + "start": 46, + "end": 56, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 78, + "end": 103, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 97, + "end": 102, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 125, + "end": 145, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 139, + "end": 144, + "ctxt": 0 + }, + "value": "./b", + "raw": "\"./b\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 167, + "end": 194, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 176, + "end": 177, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 176, + "end": 177, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 179, + "end": 180, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 179, + "end": 180, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 188, + "end": 193, + "ctxt": 0 + }, + "value": "./c", + "raw": "\"./c\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 195, + "end": 214, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 199, + "end": 213, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 199, + "end": 200, + "ctxt": 0 + }, + "value": "_", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 200, + "end": 203, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 202, + "end": 203, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 202, + "end": 203, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 206, + "end": 213, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 210, + "end": 211, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 225, + "end": 245, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 229, + "end": 244, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 229, + "end": 231, + "ctxt": 0 + }, + "value": "__", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 231, + "end": 234, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 233, + "end": 234, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 233, + "end": 234, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 237, + "end": 244, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 241, + "end": 242, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace6.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace6.ts new file mode 100644 index 000000000000..5574c8a7d1b6 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace6.ts @@ -0,0 +1,14 @@ +// @Filename: /a.ts +export class A {} +export class B {} + +// @Filename: /b.ts +export type * from "./a"; + +// @Filename: /c.ts +export * from "./b"; + +// @Filename: /d.ts +import { A, B } from "./c"; +let _: A = new A(); // Error +let __: B = new B(); // Error \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace7.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace7.json new file mode 100644 index 000000000000..62e69b5efe49 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace7.json @@ -0,0 +1,819 @@ +{ + "type": "Module", + "span": { + "start": 21, + "end": 462, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 21, + "end": 38, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 34, + "end": 35, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "declare": false, + "span": { + "start": 28, + "end": 38, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 39, + "end": 56, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 52, + "end": 53, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "declare": false, + "span": { + "start": 46, + "end": 56, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 57, + "end": 74, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 70, + "end": 71, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "declare": false, + "span": { + "start": 64, + "end": 74, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 96, + "end": 121, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 115, + "end": 120, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ExportDeclaration", + "span": { + "start": 122, + "end": 139, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 135, + "end": 136, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "declare": false, + "span": { + "start": 129, + "end": 139, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ImportDeclaration", + "span": { + "start": 161, + "end": 191, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 170, + "end": 171, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 170, + "end": 171, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 173, + "end": 174, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 173, + "end": 174, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 176, + "end": 177, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 176, + "end": 177, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 185, + "end": 190, + "ctxt": 0 + }, + "value": "./b", + "raw": "\"./b\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 192, + "end": 211, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 196, + "end": 210, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 196, + "end": 197, + "ctxt": 0 + }, + "value": "_", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 197, + "end": 200, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 199, + "end": 200, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 199, + "end": 200, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 203, + "end": 210, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 207, + "end": 208, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 222, + "end": 242, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 226, + "end": 241, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 226, + "end": 228, + "ctxt": 0 + }, + "value": "__", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 228, + "end": 231, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 230, + "end": 231, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 230, + "end": 231, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 234, + "end": 241, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 238, + "end": 239, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 252, + "end": 273, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 256, + "end": 272, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 256, + "end": 259, + "ctxt": 0 + }, + "value": "___", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 259, + "end": 262, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 261, + "end": 262, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 261, + "end": 262, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 265, + "end": 272, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 269, + "end": 270, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 301, + "end": 326, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 320, + "end": 325, + "ctxt": 0 + }, + "value": "./b", + "raw": "\"./b\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 348, + "end": 378, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 357, + "end": 358, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 357, + "end": 358, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 360, + "end": 361, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 360, + "end": 361, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 363, + "end": 364, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 363, + "end": 364, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 372, + "end": 377, + "ctxt": 0 + }, + "value": "./d", + "raw": "\"./d\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 379, + "end": 398, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 383, + "end": 397, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 383, + "end": 384, + "ctxt": 0 + }, + "value": "_", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 384, + "end": 387, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 386, + "end": 387, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 386, + "end": 387, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 390, + "end": 397, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 394, + "end": 395, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 410, + "end": 430, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 414, + "end": 429, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 414, + "end": 416, + "ctxt": 0 + }, + "value": "__", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 416, + "end": 419, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 418, + "end": 419, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 418, + "end": 419, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 422, + "end": 429, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 426, + "end": 427, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 441, + "end": 462, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 445, + "end": 461, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 445, + "end": 448, + "ctxt": 0 + }, + "value": "___", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 448, + "end": 451, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 450, + "end": 451, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 450, + "end": 451, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 454, + "end": 461, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 458, + "end": 459, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace7.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace7.ts new file mode 100644 index 000000000000..7fa568849776 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace7.ts @@ -0,0 +1,23 @@ +// @Filename: /a.ts +export class A {} +export class B {} +export class C {} + +// @Filename: /b.ts +export type * from "./a"; +export class C {} + +// @Filename: /c.ts +import { A, B, C } from "./b"; +let _: A = new A(); // Error +let __: B = new B(); // Error +let ___: C = new C(); // Ok + +// @Filename: /d.ts +export type * from "./b"; + +// @Filename: /e.ts +import { A, B, C } from "./d"; +let _: A = new A(); // Error +let __: B = new B(); // Error +let ___: C = new C(); // Error diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace8.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace8.json new file mode 100644 index 000000000000..3752222238d7 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace8.json @@ -0,0 +1,503 @@ +{ + "type": "Module", + "span": { + "start": 21, + "end": 333, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 21, + "end": 38, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 34, + "end": 35, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "declare": false, + "span": { + "start": 28, + "end": 38, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 39, + "end": 56, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 52, + "end": 53, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "declare": false, + "span": { + "start": 46, + "end": 56, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 78, + "end": 95, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 91, + "end": 92, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "declare": false, + "span": { + "start": 85, + "end": 95, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 96, + "end": 113, + "ctxt": 0 + }, + "declaration": { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 109, + "end": 110, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "declare": false, + "span": { + "start": 103, + "end": 113, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 135, + "end": 160, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 154, + "end": 159, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 161, + "end": 181, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 175, + "end": 180, + "ctxt": 0 + }, + "value": "./b", + "raw": "\"./b\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 222, + "end": 252, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 231, + "end": 232, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 231, + "end": 232, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 234, + "end": 235, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 234, + "end": 235, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "imported": null, + "isTypeOnly": false + }, + { + "type": "ImportSpecifier", + "span": { + "start": 237, + "end": 238, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 237, + "end": 238, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 246, + "end": 251, + "ctxt": 0 + }, + "value": "./c", + "raw": "\"./c\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 253, + "end": 272, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 257, + "end": 271, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 257, + "end": 258, + "ctxt": 0 + }, + "value": "_", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 258, + "end": 261, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 260, + "end": 261, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 260, + "end": 261, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 264, + "end": 271, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 268, + "end": 269, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 284, + "end": 304, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 288, + "end": 303, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 288, + "end": 290, + "ctxt": 0 + }, + "value": "__", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 290, + "end": 293, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 292, + "end": 293, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 292, + "end": 293, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 296, + "end": 303, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 300, + "end": 301, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + }, + { + "type": "VariableDeclaration", + "span": { + "start": 312, + "end": 333, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 316, + "end": 332, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 316, + "end": 319, + "ctxt": 0 + }, + "value": "___", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 319, + "end": 322, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 321, + "end": 322, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 321, + "end": 322, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "typeParams": null + } + } + }, + "init": { + "type": "NewExpression", + "span": { + "start": 325, + "end": 332, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 329, + "end": 330, + "ctxt": 0 + }, + "value": "C", + "optional": false + }, + "arguments": [], + "typeArguments": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace8.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace8.ts new file mode 100644 index 000000000000..2fab2f62f2bc --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace8.ts @@ -0,0 +1,17 @@ +// @Filename: /a.ts +export class A {} +export class B {} + +// @Filename: /b.ts +export class B {} +export class C {} + +// @Filename: /c.ts +export type * from "./a"; +export * from "./b"; // Collision error + +// @Filename: /d.ts +import { A, B, C } from "./c"; +let _: A = new A(); // Error +let __: B = new B(); // Ok +let ___: C = new C(); // Ok diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace9.json b/crates/swc_ecma_parser/tests/tsc/exportNamespace9.json new file mode 100644 index 000000000000..0f95a6572713 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace9.json @@ -0,0 +1,473 @@ +{ + "type": "Module", + "span": { + "start": 21, + "end": 420, + "ctxt": 0 + }, + "body": [ + { + "type": "ExportDeclaration", + "span": { + "start": 21, + "end": 44, + "ctxt": 0 + }, + "declaration": { + "type": "TsTypeAliasDeclaration", + "span": { + "start": 28, + "end": 44, + "ctxt": 0 + }, + "declare": false, + "id": { + "type": "Identifier", + "span": { + "start": 33, + "end": 34, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 37, + "end": 43, + "ctxt": 0 + }, + "kind": "number" + } + } + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 66, + "end": 91, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 85, + "end": 90, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 113, + "end": 137, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 122, + "end": 123, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 122, + "end": 123, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 131, + "end": 136, + "ctxt": 0 + }, + "value": "./b", + "raw": "\"./b\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "VariableDeclaration", + "span": { + "start": 138, + "end": 150, + "ctxt": 0 + }, + "kind": "const", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 144, + "end": 149, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 144, + "end": 145, + "ctxt": 0 + }, + "value": "A", + "optional": false, + "typeAnnotation": null + }, + "init": { + "type": "NumericLiteral", + "span": { + "start": 148, + "end": 149, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + }, + "definite": false + } + ] + }, + { + "type": "ExportNamedDeclaration", + "span": { + "start": 151, + "end": 164, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "span": { + "start": 160, + "end": 161, + "ctxt": 0 + }, + "orig": { + "type": "Identifier", + "span": { + "start": 160, + "end": 161, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "exported": null, + "isTypeOnly": false + } + ], + "source": null, + "typeOnly": false, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 186, + "end": 210, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 195, + "end": 196, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 195, + "end": 196, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 204, + "end": 209, + "ctxt": 0 + }, + "value": "./c", + "raw": "\"./c\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ExpressionStatement", + "span": { + "start": 211, + "end": 213, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 211, + "end": 212, + "ctxt": 0 + }, + "value": "A", + "optional": false + } + }, + { + "type": "TsTypeAliasDeclaration", + "span": { + "start": 220, + "end": 231, + "ctxt": 0 + }, + "declare": false, + "id": { + "type": "Identifier", + "span": { + "start": 225, + "end": 226, + "ctxt": 0 + }, + "value": "_", + "optional": false + }, + "typeParams": null, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 229, + "end": 230, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 229, + "end": 230, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + }, + { + "type": "ExportDeclaration", + "span": { + "start": 253, + "end": 272, + "ctxt": 0 + }, + "declaration": { + "type": "VariableDeclaration", + "span": { + "start": 260, + "end": 272, + "ctxt": 0 + }, + "kind": "const", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 266, + "end": 271, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 266, + "end": 267, + "ctxt": 0 + }, + "value": "A", + "optional": false, + "typeAnnotation": null + }, + "init": { + "type": "NumericLiteral", + "span": { + "start": 270, + "end": 271, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + }, + "definite": false + } + ] + } + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 294, + "end": 314, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 308, + "end": 313, + "ctxt": 0 + }, + "value": "./e", + "raw": "\"./e\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ExportAllDeclaration", + "span": { + "start": 315, + "end": 340, + "ctxt": 0 + }, + "source": { + "type": "StringLiteral", + "span": { + "start": 334, + "end": 339, + "ctxt": 0 + }, + "value": "./a", + "raw": "\"./a\"" + }, + "typeOnly": true, + "asserts": null + }, + { + "type": "ImportDeclaration", + "span": { + "start": 381, + "end": 405, + "ctxt": 0 + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "span": { + "start": 390, + "end": 391, + "ctxt": 0 + }, + "local": { + "type": "Identifier", + "span": { + "start": 390, + "end": 391, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "imported": null, + "isTypeOnly": false + } + ], + "source": { + "type": "StringLiteral", + "span": { + "start": 399, + "end": 404, + "ctxt": 0 + }, + "value": "./f", + "raw": "\"./f\"" + }, + "typeOnly": false, + "asserts": null + }, + { + "type": "ExpressionStatement", + "span": { + "start": 406, + "end": 408, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 406, + "end": 407, + "ctxt": 0 + }, + "value": "A", + "optional": false + } + }, + { + "type": "TsTypeAliasDeclaration", + "span": { + "start": 409, + "end": 420, + "ctxt": 0 + }, + "declare": false, + "id": { + "type": "Identifier", + "span": { + "start": 414, + "end": 415, + "ctxt": 0 + }, + "value": "_", + "optional": false + }, + "typeParams": null, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 418, + "end": 419, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 418, + "end": 419, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/tsc/exportNamespace9.ts b/crates/swc_ecma_parser/tests/tsc/exportNamespace9.ts new file mode 100644 index 000000000000..b3693b8b8672 --- /dev/null +++ b/crates/swc_ecma_parser/tests/tsc/exportNamespace9.ts @@ -0,0 +1,27 @@ +// @Filename: /a.ts +export type A = number; + +// @Filename: /b.ts +export type * from "./a"; + +// @Filename: /c.ts +import { A } from "./b"; +const A = 1; +export { A }; + +// @Filename: /d.ts +import { A } from "./c"; +A; // Ok +type _ = A; + +// @Filename: /e.ts +export const A = 1; + +// @Filename: /f.ts +export * from "./e"; +export type * from "./a"; // Collision error + +// @Filename: /g.ts +import { A } from "./f"; +A; +type _ = A; // Follow-on from collision error diff --git a/crates/swc_ecma_parser/tests/tsc/exportStar-amd.json b/crates/swc_ecma_parser/tests/tsc/exportStar-amd.json index 2a77be66cb73..88b61f97b0c0 100644 --- a/crates/swc_ecma_parser/tests/tsc/exportStar-amd.json +++ b/crates/swc_ecma_parser/tests/tsc/exportStar-amd.json @@ -382,6 +382,7 @@ "value": "./t1", "raw": "\"./t1\"" }, + "typeOnly": false, "asserts": null }, { @@ -401,6 +402,7 @@ "value": "./t2", "raw": "\"./t2\"" }, + "typeOnly": false, "asserts": null }, { @@ -420,6 +422,7 @@ "value": "./t3", "raw": "\"./t3\"" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/tsc/exportStar.json b/crates/swc_ecma_parser/tests/tsc/exportStar.json index d10b305ca4a2..20086d50730d 100644 --- a/crates/swc_ecma_parser/tests/tsc/exportStar.json +++ b/crates/swc_ecma_parser/tests/tsc/exportStar.json @@ -382,6 +382,7 @@ "value": "./t1", "raw": "\"./t1\"" }, + "typeOnly": false, "asserts": null }, { @@ -401,6 +402,7 @@ "value": "./t2", "raw": "\"./t2\"" }, + "typeOnly": false, "asserts": null }, { @@ -420,6 +422,7 @@ "value": "./t3", "raw": "\"./t3\"" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/tsc/importAssertion2.json b/crates/swc_ecma_parser/tests/tsc/importAssertion2.json index 8301dc804ca7..97877b7bdfb7 100644 --- a/crates/swc_ecma_parser/tests/tsc/importAssertion2.json +++ b/crates/swc_ecma_parser/tests/tsc/importAssertion2.json @@ -270,6 +270,7 @@ "value": "./0", "raw": "'./0'" }, + "typeOnly": false, "asserts": { "type": "ObjectExpression", "span": { diff --git a/crates/swc_ecma_parser/tests/tsc/jsDeclarationsExportForms.json b/crates/swc_ecma_parser/tests/tsc/jsDeclarationsExportForms.json index 20c0d5e7fe02..007d513ab5bd 100644 --- a/crates/swc_ecma_parser/tests/tsc/jsDeclarationsExportForms.json +++ b/crates/swc_ecma_parser/tests/tsc/jsDeclarationsExportForms.json @@ -99,6 +99,7 @@ "value": "./cls", "raw": "\"./cls\"" }, + "typeOnly": false, "asserts": null }, { @@ -118,6 +119,7 @@ "value": "./func", "raw": "\"./func\"" }, + "typeOnly": false, "asserts": null }, { @@ -137,6 +139,7 @@ "value": "./cls", "raw": "\"./cls\"" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/typescript/custom/issue-535/input.ts.json b/crates/swc_ecma_parser/tests/typescript/custom/issue-535/input.ts.json index b7053e215e0d..8c99c0a349ef 100644 --- a/crates/swc_ecma_parser/tests/typescript/custom/issue-535/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/custom/issue-535/input.ts.json @@ -23,6 +23,7 @@ "value": "test", "raw": "\"test\"" }, + "typeOnly": false, "asserts": null }, { diff --git a/crates/swc_ecma_parser/tests/typescript/import-assertions/export/test3/input.ts.json b/crates/swc_ecma_parser/tests/typescript/import-assertions/export/test3/input.ts.json index 798f44f22a6f..5975df6e9e35 100644 --- a/crates/swc_ecma_parser/tests/typescript/import-assertions/export/test3/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/import-assertions/export/test3/input.ts.json @@ -23,6 +23,7 @@ "value": "./foo.js", "raw": "'./foo.js'" }, + "typeOnly": false, "asserts": { "type": "ObjectExpression", "span": { diff --git a/crates/swc_ecma_transforms_typescript/src/strip.rs b/crates/swc_ecma_transforms_typescript/src/strip.rs index 4b0131f528ee..fc720053ea47 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip.rs @@ -1094,6 +1094,10 @@ where | ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport { type_only: true, .. + })) + | ModuleItem::ModuleDecl(ModuleDecl::ExportAll(ExportAll { + type_only: true, + .. })) => continue, ModuleItem::ModuleDecl(ModuleDecl::TsImportEquals(v)) if matches!( @@ -1730,7 +1734,7 @@ fn is_ts_namespace_body_concrete(b: &TsNamespaceBody) -> bool { ModuleDecl::ExportNamed(d) => !d.type_only, ModuleDecl::ExportDefaultDecl(_) => true, ModuleDecl::ExportDefaultExpr(_) => true, - ModuleDecl::ExportAll(_) => true, + ModuleDecl::ExportAll(d) => !d.type_only, ModuleDecl::TsImportEquals(_) => true, ModuleDecl::TsExportAssignment(..) => true, ModuleDecl::TsNamespaceExport(..) => true, diff --git a/crates/swc_ecma_visit/src/lib.rs b/crates/swc_ecma_visit/src/lib.rs index 03aef579cc9b..ebbc161e7140 100644 --- a/crates/swc_ecma_visit/src/lib.rs +++ b/crates/swc_ecma_visit/src/lib.rs @@ -1058,6 +1058,7 @@ define!({ pub struct ExportAll { pub span: Span, pub src: Box, + pub type_only: bool, pub asserts: Option>, } pub struct NamedExport { diff --git a/crates/swc_estree_compat/src/babelify/module_decl.rs b/crates/swc_estree_compat/src/babelify/module_decl.rs index e789ffa6fcd0..4d49dc8c375f 100644 --- a/crates/swc_estree_compat/src/babelify/module_decl.rs +++ b/crates/swc_estree_compat/src/babelify/module_decl.rs @@ -179,7 +179,11 @@ impl Babelify for ExportAll { base: ctx.base(self.span), source: self.src.babelify(ctx), assertions: convert_import_asserts(self.asserts, ctx), - export_kind: None, + export_kind: if self.type_only { + Some(ExportKind::Type) + } else { + None + }, } } } diff --git a/crates/swc_estree_compat/src/swcify/stmt.rs b/crates/swc_estree_compat/src/swcify/stmt.rs index 771cc7e508e6..d45a81b29bf0 100644 --- a/crates/swc_estree_compat/src/swcify/stmt.rs +++ b/crates/swc_estree_compat/src/swcify/stmt.rs @@ -435,6 +435,7 @@ impl Swcify for ExportAllDeclaration { ExportAll { span: ctx.span(&self.base), src: self.source.swcify(ctx).into(), + type_only: self.export_kind == Some(ExportKind::Type), asserts: self .assertions .swcify(ctx) @@ -777,6 +778,7 @@ impl Swcify for DeclareExportAllDeclaration { ExportAll { span: ctx.span(&self.base), src: self.source.swcify(ctx).into(), + type_only: self.export_kind == Some(ExportKind::Type), asserts: Default::default(), } }