From 15abb76977946057997e8849feedd82052d74270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Fri, 5 Jul 2024 21:17:14 +0900 Subject: [PATCH 1/5] Add tests --- crates/swc_fast_ts_strip/tests/errors/modules.ts | 2 +- crates/swc_fast_ts_strip/tests/fixture/modules.ts | 1 + crates/swc_fast_ts_strip/tests/fixture/namespaces.ts | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 crates/swc_fast_ts_strip/tests/fixture/modules.ts create mode 100644 crates/swc_fast_ts_strip/tests/fixture/namespaces.ts diff --git a/crates/swc_fast_ts_strip/tests/errors/modules.ts b/crates/swc_fast_ts_strip/tests/errors/modules.ts index 0a6968cccaae..ad71f09e7580 100644 --- a/crates/swc_fast_ts_strip/tests/errors/modules.ts +++ b/crates/swc_fast_ts_strip/tests/errors/modules.ts @@ -1 +1 @@ -module 'foo' { } \ No newline at end of file +module aModuleKeywordNamespace { } \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/modules.ts b/crates/swc_fast_ts_strip/tests/fixture/modules.ts new file mode 100644 index 000000000000..b8327e1c4c40 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/modules.ts @@ -0,0 +1 @@ +module 'myAmbientModuleDeclaration' { } \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/namespaces.ts b/crates/swc_fast_ts_strip/tests/fixture/namespaces.ts new file mode 100644 index 000000000000..734ff610a761 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/namespaces.ts @@ -0,0 +1 @@ +declare namespace Foo { } \ No newline at end of file From 98f6a0e1158fa4fccfd08ed38bd2e4e2d905b653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Fri, 5 Jul 2024 21:18:53 +0900 Subject: [PATCH 2/5] review --- crates/swc_fast_ts_strip/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 5a0c4ef740bf..92f1f0224a41 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -8,8 +8,8 @@ use swc_common::{ }; use swc_ecma_ast::{ BindingIdent, Decorator, EsVersion, Ident, Param, Pat, Program, TsAsExpr, TsConstAssertion, - TsEnumDecl, TsInstantiation, TsModuleDecl, TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, - TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, + TsEnumDecl, TsInstantiation, TsModuleDecl, TsModuleName, TsNamespaceDecl, TsNonNullExpr, + TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, }; use swc_ecma_parser::{ parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax, TsSyntax, @@ -154,7 +154,7 @@ impl Visit for TsStrip { } fn visit_ts_module_decl(&mut self, n: &TsModuleDecl) { - if n.declare { + if n.declare || matches!(n.id, TsModuleName::Str(..)) { self.add_replacement(n.span); return; } From 44d7fb6081a9a056b3eae7054c5f94509da426f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Fri, 5 Jul 2024 21:19:45 +0900 Subject: [PATCH 3/5] Update test refs --- crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr | 4 ++-- crates/swc_fast_ts_strip/tests/fixture/modules.js | 1 + crates/swc_fast_ts_strip/tests/fixture/namespaces.js | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 crates/swc_fast_ts_strip/tests/fixture/modules.js create mode 100644 crates/swc_fast_ts_strip/tests/fixture/namespaces.js diff --git a/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr index 6f2fc101fe8a..20b6f1cdb834 100644 --- a/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr @@ -1,5 +1,5 @@ x TypeScript namespace declaration is not supported in strip-only mode ,---- - 1 | module 'foo' { } - : ^^^^^^^^^^^^^^^^ + 1 | module aModuleKeywordNamespace { } + : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `---- diff --git a/crates/swc_fast_ts_strip/tests/fixture/modules.js b/crates/swc_fast_ts_strip/tests/fixture/modules.js new file mode 100644 index 000000000000..3d0cbdae459d --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/modules.js @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/namespaces.js b/crates/swc_fast_ts_strip/tests/fixture/namespaces.js new file mode 100644 index 000000000000..a763cbee69b5 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/namespaces.js @@ -0,0 +1 @@ + \ No newline at end of file From 27486254ad86ffb03b1a41209ca21947b08dd0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Fri, 5 Jul 2024 21:24:34 +0900 Subject: [PATCH 4/5] Update jest snapshot --- .../__tests__/__snapshots__/transform.js.snap | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap index 1b1e7769ce02..6050189a43bd 100644 --- a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap +++ b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap @@ -1,44 +1,44 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`transform in strip-only mode should remove declare enum 1`] = `""`; +exports[`transform in strip-only mode should remove declare enum 1`] = `" "`; -exports[`transform in strip-only mode should remove declare enum 2`] = `""`; +exports[`transform in strip-only mode should remove declare enum 2`] = `" "`; -exports[`transform in strip-only mode should remove declare enum 3`] = `""`; +exports[`transform in strip-only mode should remove declare enum 3`] = `" "`; exports[`transform in strip-only mode should strip complex expressions 1`] = ` "const foo = { - foo: 1, - bar: "bar", - }; + foo: 1 , + bar: "bar" , + } ; const bar = "bar";" `; exports[`transform in strip-only mode should strip nonnull assertions 1`] = ` -"const foo = 1; +"const foo = 1 ; const bar = "bar";" `; exports[`transform in strip-only mode should strip satisfies 1`] = ` -"const foo = 1; +"const foo = 1 ; const bar = "bar";" `; exports[`transform in strip-only mode should strip type annotations 1`] = ` "const foo = 1; - const bar = "bar";" + const bar = "bar";" `; exports[`transform in strip-only mode should strip type assertions 1`] = ` -"const foo = 1; +"const foo = 1 ; const bar = "bar";" `; exports[`transform in strip-only mode should strip type declarations 1`] = ` "const foo = 1; - - - const bar = "bar";" + + + const bar = "bar";" `; exports[`transform in strip-only mode should throw an error when it encounters a module 1`] = ` @@ -79,7 +79,7 @@ exports[`transform in strip-only mode should throw an error with a descriptive m exports[`transform should strip types 1`] = ` " - export const foo = 1; - + export const foo = 1; + " `; From 286323d6901c3b93331b3358ada012c852fdfb1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Fri, 5 Jul 2024 21:30:36 +0900 Subject: [PATCH 5/5] Update test refs --- .../__tests__/__snapshots__/transform.js.snap | 4 ++-- bindings/binding_typescript_wasm/__tests__/transform.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap index 6050189a43bd..17f5afa833b1 100644 --- a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap +++ b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap @@ -44,8 +44,8 @@ exports[`transform in strip-only mode should strip type declarations 1`] = ` exports[`transform in strip-only mode should throw an error when it encounters a module 1`] = ` " x TypeScript namespace declaration is not supported in strip-only mode ,---- - 1 | module 'foo' {} - : ^^^^^^^^^^^^^^^ + 1 | module foo {} + : ^^^^^^^^^^^^^ \`---- " `; diff --git a/bindings/binding_typescript_wasm/__tests__/transform.js b/bindings/binding_typescript_wasm/__tests__/transform.js index 4bacd9866381..d8c9c3d5d180 100644 --- a/bindings/binding_typescript_wasm/__tests__/transform.js +++ b/bindings/binding_typescript_wasm/__tests__/transform.js @@ -133,7 +133,7 @@ describe("transform", () => { it("should throw an error when it encounters a module", async () => { await expect( - swc.transform("module 'foo' {}", { + swc.transform("module foo {}", { mode: "strip-only", }) ).rejects.toMatchSnapshot();