From 9fca4ab5557be4f92ba425b39fdc417a4da9a587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 5 Jul 2024 21:34:03 +0900 Subject: [PATCH] feat(es/typescript): Improve fast TS stripper (#9152) **Related issue:** - https://github.com/swc-project/swc/pull/9143#pullrequestreview-2160591522 --- .../__tests__/__snapshots__/transform.js.snap | 34 +++++++++---------- .../__tests__/transform.js | 2 +- crates/swc_fast_ts_strip/src/lib.rs | 6 ++-- .../tests/errors/modules.swc-stderr | 4 +-- .../swc_fast_ts_strip/tests/errors/modules.ts | 2 +- .../tests/fixture/modules.js | 1 + .../tests/fixture/modules.ts | 1 + .../tests/fixture/namespaces.js | 1 + .../tests/fixture/namespaces.ts | 1 + 9 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 crates/swc_fast_ts_strip/tests/fixture/modules.js create mode 100644 crates/swc_fast_ts_strip/tests/fixture/modules.ts create mode 100644 crates/swc_fast_ts_strip/tests/fixture/namespaces.js create mode 100644 crates/swc_fast_ts_strip/tests/fixture/namespaces.ts diff --git a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap index 1b1e7769ce02..17f5afa833b1 100644 --- a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap +++ b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap @@ -1,51 +1,51 @@ // 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`] = ` " x TypeScript namespace declaration is not supported in strip-only mode ,---- - 1 | module 'foo' {} - : ^^^^^^^^^^^^^^^ + 1 | module foo {} + : ^^^^^^^^^^^^^ \`---- " `; @@ -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; + " `; 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(); 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; } 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/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.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/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.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 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