From 98af5890dac03a9acac4e62ddb62bc1f3495d9a2 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 6 Jul 2024 21:43:46 +0800 Subject: [PATCH] feat(es/typescript): Improve fast TS strip (#9167) --- .../__tests__/__snapshots__/transform.js.snap | 13 ++++- crates/swc_fast_ts_strip/src/lib.rs | 54 +++++++++++-------- .../tests/fixture/test-case-1.js | 19 ++++--- .../tests/fixture/test-case-1.ts | 7 ++- 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap index 17f5afa833b..174b5d7e75e 100644 --- a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap +++ b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap @@ -2,9 +2,18 @@ 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 = { diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 36357fcf77d..715fd5c11cb 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -168,22 +168,28 @@ impl TsStrip { &self.src[(span.lo.0 - 1) as usize..(span.hi.0 - 1) as usize] } - fn get_next_token(&self, pos: BytePos) -> &TokenAndSpan { + fn get_next_token_index(&self, pos: BytePos) -> usize { let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo); - let index = match index { + match index { Ok(index) => index, Err(index) => index, - }; - &self.tokens[index] + } + } + + fn get_next_token(&self, pos: BytePos) -> &TokenAndSpan { + &self.tokens[self.get_next_token_index(pos)] + } + + fn get_prev_token_index(&self, pos: BytePos) -> usize { + let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo); + match index { + Ok(index) => index, + Err(index) => index - 1, + } } fn get_prev_token(&self, pos: BytePos) -> &TokenAndSpan { - let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo); - let index = match index { - Ok(index) => index, - Err(index) => index - 1, - }; - &self.tokens[index] + &self.tokens[self.get_prev_token_index(pos)] } } @@ -218,7 +224,7 @@ impl Visit for TsStrip { // 1; // ``` - self.add_overwrite(ret.span_hi(), b')'); + self.add_overwrite(ret.span_hi() - BytePos(1), b')'); } } @@ -264,6 +270,10 @@ impl Visit for TsStrip { if !n.implements.is_empty() { let implements = self.get_prev_token(n.implements.first().unwrap().span_lo() - BytePos(1)); + debug_assert_eq!( + implements.token, + Token::Word(Word::Ident(IdentLike::Known(KnownIdent::Implements))) + ); let last = n.implements.last().unwrap(); let span = span(implements.span.lo, last.span.hi); @@ -279,13 +289,14 @@ impl Visit for TsStrip { return; } - let key_span = n.key.span_hi(); - let method_pos = n.function.span_lo(); + let key_pos = n.key.span_lo(); + let mut pos = n.span_lo(); + let mut index = self.get_next_token_index(pos); - let mut pos = method_pos; - while pos < key_span { - let TokenAndSpan { token, span, .. } = self.get_next_token(pos); + while pos < key_pos { + let TokenAndSpan { token, span, .. } = &self.tokens[index]; pos = span.hi; + index += 1; match token { Token::Word(Word::Ident(IdentLike::Known( KnownIdent::Public | KnownIdent::Protected | KnownIdent::Private, @@ -308,13 +319,14 @@ impl Visit for TsStrip { return; } - let key_span = n.key.span_hi(); - let method_pos = n.span_lo(); + let key_pos = n.key.span_lo(); + let mut pos = n.span_lo(); + let mut index = self.get_next_token_index(pos); - let mut pos = method_pos; - while pos < key_span { - let TokenAndSpan { token, span, .. } = self.get_next_token(pos); + while pos < key_pos { + let TokenAndSpan { token, span, .. } = &self.tokens[index]; pos = span.hi; + index += 1; match token { Token::Word(Word::Ident(IdentLike::Known( KnownIdent::Readonly 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 aa5ff2e4c28..1873dde3179 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 @@ -142,36 +142,41 @@ void 0; // `=>` spanning line cases: { ( - )=> + ) => 1 }; { ( - )=> + ) => 1 }; { ( - )=> + ) => 1 }; { ( - )=> + ) => 1 }; { ( - )=> + ) => 1 }; { (a, b, c = [] /*comment-1*/ /*comment-2*/ - )=> + ) => 1 -}; \ No newline at end of file +}; + + + ( + )=> + 1; diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.ts b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.ts index b4a4a91844d..9b1f5dc60a2 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.ts +++ b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.ts @@ -174,4 +174,9 @@ void 0; (a, b, c: D = [] as any/*comment-1*/)/*comment-2*/: any => 1 -}; \ No newline at end of file +}; + + + (): + any=> + 1;