From 259e9e17c001eb2a03fa9244615bed5a3947d11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 24 Jul 2024 12:06:57 +0900 Subject: [PATCH 1/8] fix lints --- crates/swc_plugin_runner/src/cache.rs | 2 +- crates/swc_plugin_runner/src/wasix_runtime.rs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/swc_plugin_runner/src/cache.rs b/crates/swc_plugin_runner/src/cache.rs index 5d274b9facc7..0e750c30b71e 100644 --- a/crates/swc_plugin_runner/src/cache.rs +++ b/crates/swc_plugin_runner/src/cache.rs @@ -14,7 +14,7 @@ use swc_common::{ sync::{Lazy, OnceCell}, }; #[cfg(not(target_arch = "wasm32"))] -use wasmer::{BaseTunables, CpuFeature, Engine, Target, Triple}; +use wasmer::{sys::BaseTunables, CpuFeature, Engine, Target, Triple}; use wasmer::{Module, Store}; #[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))] use wasmer_cache::{Cache as WasmerCache, FileSystemCache, Hash}; diff --git a/crates/swc_plugin_runner/src/wasix_runtime.rs b/crates/swc_plugin_runner/src/wasix_runtime.rs index eca00d91a0a1..f2a01ac06a7b 100644 --- a/crates/swc_plugin_runner/src/wasix_runtime.rs +++ b/crates/swc_plugin_runner/src/wasix_runtime.rs @@ -12,7 +12,10 @@ use wasmer_wasix::Runtime; static ENGINE: Lazy> = Lazy::new(|| { // Use empty enumset to disable simd. use enumset::EnumSet; - use wasmer::{BaseTunables, CompilerConfig, EngineBuilder, Target, Triple}; + use wasmer::{ + sys::{BaseTunables, EngineBuilder}, + CompilerConfig, Target, Triple, + }; let mut set = EnumSet::new(); // [TODO]: Should we use is_x86_feature_detected! macro instead? From a2987b866667110d6c2d8e29b579d4710fe83dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 24 Jul 2024 12:09:11 +0900 Subject: [PATCH 2/8] fix --- crates/swc_fast_ts_strip/src/lib.rs | 77 +++++++++++++++-------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 8dcddb5e6b48..7778c80587c3 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -440,15 +440,6 @@ impl Visit for TsStrip { } } - fn visit_class_decl(&mut self, n: &ClassDecl) { - if n.declare { - self.add_replacement(n.span()); - return; - } - - n.visit_children_with(self); - } - fn visit_class(&mut self, n: &Class) { if n.is_abstract { let r#abstract = self.get_next_token(n.span_lo()); @@ -475,6 +466,15 @@ impl Visit for TsStrip { n.visit_children_with(self); } + fn visit_class_decl(&mut self, n: &ClassDecl) { + if n.declare { + self.add_replacement(n.span()); + return; + } + + n.visit_children_with(self); + } + fn visit_class_method(&mut self, n: &ClassMethod) { if n.function.body.is_none() || n.is_abstract { self.add_replacement(n.span); @@ -552,10 +552,6 @@ impl Visit for TsStrip { n.visit_children_with(self); } - fn visit_ts_index_signature(&mut self, n: &TsIndexSignature) { - self.add_replacement(n.span); - } - fn visit_export_all(&mut self, n: &ExportAll) { if n.type_only { self.add_replacement(n.span); @@ -636,29 +632,6 @@ impl Visit for TsStrip { } } - fn visit_ts_import_equals_decl(&mut self, n: &TsImportEqualsDecl) { - if n.is_type_only { - self.add_replacement(n.span); - return; - } - - HANDLER.with(|handler| { - handler.span_err( - n.span, - "TypeScript import equals declaration is not supported in strip-only mode", - ); - }); - } - - fn visit_ts_export_assignment(&mut self, n: &TsExportAssignment) { - HANDLER.with(|handler| { - handler.span_err( - n.span, - "TypeScript export assignment is not supported in strip-only mode", - ); - }); - } - fn visit_params(&mut self, n: &[Param]) { if let Some(p) = n.first().filter(|param| { matches!( @@ -710,6 +683,33 @@ impl Visit for TsStrip { }); } + fn visit_ts_export_assignment(&mut self, n: &TsExportAssignment) { + HANDLER.with(|handler| { + handler.span_err( + n.span, + "TypeScript export assignment is not supported in strip-only mode", + ); + }); + } + + fn visit_ts_import_equals_decl(&mut self, n: &TsImportEqualsDecl) { + if n.is_type_only { + self.add_replacement(n.span); + return; + } + + HANDLER.with(|handler| { + handler.span_err( + n.span, + "TypeScript import equals declaration is not supported in strip-only mode", + ); + }); + } + + fn visit_ts_index_signature(&mut self, n: &TsIndexSignature) { + self.add_replacement(n.span); + } + fn visit_ts_instantiation(&mut self, n: &TsInstantiation) { self.add_replacement(span(n.expr.span().hi, n.span.hi)); @@ -777,9 +777,10 @@ impl Visit for TsStrip { self.add_replacement(n.span); } + /// We do not strip type assertion because it's not safe. + /// + /// See https://github.com/swc-project/swc/issues/9295 fn visit_ts_type_assertion(&mut self, n: &TsTypeAssertion) { - self.add_replacement(span(n.span.lo, n.expr.span().lo)); - n.expr.visit_children_with(self); } From fb3f846c234750d3efa76bd78ec34ae779eb0d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 24 Jul 2024 12:10:04 +0900 Subject: [PATCH 3/8] Update test refs --- crates/swc_fast_ts_strip/tests/fixture/test-case-1.js | 2 +- crates/swc_fast_ts_strip/tests/fixture/unicode.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.js b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.js index e52037e8b242..62454e9ef522 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.js +++ b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.js @@ -4,7 +4,7 @@ let x /**/ /**/ = 1 ; [] ; // ^^^^^^^^^^^^^^^^^^ -( "test"); +("test"); //^^^^^^^^ class C /**/ /*︎*/ extends Array/**/ /*︎*/ /*︎*/ { diff --git a/crates/swc_fast_ts_strip/tests/fixture/unicode.js b/crates/swc_fast_ts_strip/tests/fixture/unicode.js index f5299dfd6153..62378ffc84dd 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/unicode.js +++ b/crates/swc_fast_ts_strip/tests/fixture/unicode.js @@ -1,7 +1,7 @@    function foo() { -    (void 1); throw new Error('foo'); + <任意>(void 1); throw new Error('foo'); } foo(); \ No newline at end of file From 06b827f8a60757f1ba8066fc1404a23601d0ac5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 24 Jul 2024 12:25:21 +0900 Subject: [PATCH 4/8] error --- crates/swc_fast_ts_strip/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 7778c80587c3..b5326ec363c7 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -781,6 +781,14 @@ impl Visit for TsStrip { /// /// See https://github.com/swc-project/swc/issues/9295 fn visit_ts_type_assertion(&mut self, n: &TsTypeAssertion) { + HANDLER.with(|handler| { + handler.span_err( + n.span, + "The angle-bracket syntax for type assertions, `expr`, is not supported in \ + type strip mode. Instead, use the 'as' syntax: `expr as T`.", + ); + }); + n.expr.visit_children_with(self); } From 895c6f176079ccdb475b2d3c32621baeeb08266d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 24 Jul 2024 12:26:46 +0900 Subject: [PATCH 5/8] test --- crates/swc_fast_ts_strip/tests/fixture.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/swc_fast_ts_strip/tests/fixture.rs b/crates/swc_fast_ts_strip/tests/fixture.rs index 2bdf934a4bcd..c3531a45bbb7 100644 --- a/crates/swc_fast_ts_strip/tests/fixture.rs +++ b/crates/swc_fast_ts_strip/tests/fixture.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; +use swc_common::errors::Handler; use swc_ecma_parser::TsSyntax; use swc_fast_ts_strip::{operate, Mode, Options}; use testing::NormalizedOutput; @@ -7,10 +8,11 @@ use testing::NormalizedOutput; #[testing::fixture("tests/fixture/**/*.ts")] fn test(input: PathBuf) { let input_code = std::fs::read_to_string(&input).unwrap(); + let output_stderr = input.with_extension("swc-stderr"); let output_file = input.with_extension("js"); let transform_output_file = input.with_extension("transform.js"); - testing::run_test(false, |cm, handler| { + let err = testing::run_test(false, |cm, handler| { let code = operate(&cm, handler, input_code.clone(), opts(Mode::StripOnly)) .expect("should not return Err()") .code; @@ -19,9 +21,14 @@ fn test(input: PathBuf) { .compare_to_file(output_file) .unwrap(); + if handler.has_errors() { + return Err(()); + } Ok(()) - }) - .expect("should not fail"); + }); + if let Err(err) = err { + err.compare_to_file(output_stderr).unwrap(); + } testing::run_test(false, |cm, handler| { let code = operate(&cm, handler, input_code, opts(Mode::Transform)) From 80665ba3b6366a2b8369a4de37363e5b020562a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 24 Jul 2024 12:26:53 +0900 Subject: [PATCH 6/8] Update test refs --- .../swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr | 7 +++++++ crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr create mode 100644 crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr new file mode 100644 index 000000000000..a3f700a18cb7 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr @@ -0,0 +1,7 @@ + x The angle-bracket syntax for type assertions, `expr`, is not supported in type strip mode. Instead, use the 'as' syntax: `expr as T`. + ,-[7:1] + 6 | + 7 | ("test"); + : ^^^^^^^^^^^^^^ + 8 | //^^^^^^^^ + `---- diff --git a/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr new file mode 100644 index 000000000000..f57722b500e6 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr @@ -0,0 +1,7 @@ + x The angle-bracket syntax for type assertions, `expr`, is not supported in type strip mode. Instead, use the 'as' syntax: `expr as T`. + ,-[4:1] + 3 | function foo() { + 4 | <任意>(void 1); throw new Error('foo'); + : ^^^^^^^^^^^^^^ + 5 | } + `---- From 59ffd04ae23077624b480d2949e4d608af704737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 24 Jul 2024 12:27:14 +0900 Subject: [PATCH 7/8] lint --- crates/swc_fast_ts_strip/tests/fixture.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/swc_fast_ts_strip/tests/fixture.rs b/crates/swc_fast_ts_strip/tests/fixture.rs index c3531a45bbb7..630bcaee1f1b 100644 --- a/crates/swc_fast_ts_strip/tests/fixture.rs +++ b/crates/swc_fast_ts_strip/tests/fixture.rs @@ -1,6 +1,5 @@ use std::path::PathBuf; -use swc_common::errors::Handler; use swc_ecma_parser::TsSyntax; use swc_fast_ts_strip::{operate, Mode, Options}; use testing::NormalizedOutput; From c6186dbf275aa34981cad3d358c2139a963c1664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 24 Jul 2024 13:07:12 +0900 Subject: [PATCH 8/8] Create fair-ants-roll.md --- .changeset/fair-ants-roll.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fair-ants-roll.md diff --git a/.changeset/fair-ants-roll.md b/.changeset/fair-ants-roll.md new file mode 100644 index 000000000000..ab5d4b4e5f20 --- /dev/null +++ b/.changeset/fair-ants-roll.md @@ -0,0 +1,5 @@ +--- +swc_fast_ts_strip: patch +--- + +fix(es/typescript): Preserve type assertions