From c5acafe3869084a3f192a4aac0f120d5bb69e524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 6 Jul 2024 15:25:55 +0900 Subject: [PATCH] fix(es/typescript): Fix replacement logic of fast TS strip (#9163) --- crates/swc_fast_ts_strip/src/lib.rs | 7 ++++++- .../swc_fast_ts_strip/tests/fixture/declare-enum/simple.js | 4 +++- .../tests/fixture/declare-enum/with-init.js | 5 ++++- crates/swc_fast_ts_strip/tests/fixture/test-1.js | 5 +++++ crates/swc_fast_ts_strip/tests/fixture/test-1.ts | 5 +++++ 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 crates/swc_fast_ts_strip/tests/fixture/test-1.js create mode 100644 crates/swc_fast_ts_strip/tests/fixture/test-1.ts diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 7bbd7646b03b..197cce86612f 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -124,7 +124,12 @@ pub fn operate( let mut code = fm.src.to_string().into_bytes(); for r in replacements { - code[(r.0 .0 - 1) as usize..(r.1 .0 - 1) as usize].fill(b' '); + for c in &mut code[(r.0 .0 - 1) as usize..(r.1 .0 - 1) as usize] { + if *c == b'\n' { + continue; + } + *c = b' '; + } } // Assert that removal does not overlap with each other diff --git a/crates/swc_fast_ts_strip/tests/fixture/declare-enum/simple.js b/crates/swc_fast_ts_strip/tests/fixture/declare-enum/simple.js index ad80c5a8877a..f8e2202758e3 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/declare-enum/simple.js +++ b/crates/swc_fast_ts_strip/tests/fixture/declare-enum/simple.js @@ -1 +1,3 @@ - \ No newline at end of file + + + \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/declare-enum/with-init.js b/crates/swc_fast_ts_strip/tests/fixture/declare-enum/with-init.js index bc2ddb2b6b4b..c09b541c10b8 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/declare-enum/with-init.js +++ b/crates/swc_fast_ts_strip/tests/fixture/declare-enum/with-init.js @@ -1 +1,4 @@ - \ No newline at end of file + + + + \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-1.js b/crates/swc_fast_ts_strip/tests/fixture/test-1.js new file mode 100644 index 000000000000..e9e3251c601a --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/test-1.js @@ -0,0 +1,5 @@ + + + + +throw new Error('Whitespacing') \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-1.ts b/crates/swc_fast_ts_strip/tests/fixture/test-1.ts new file mode 100644 index 000000000000..6c96a883a19e --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/test-1.ts @@ -0,0 +1,5 @@ +interface Foo { + bar: string +} + +throw new Error('Whitespacing') \ No newline at end of file