From c5ed19c710fd32f5c23b2d85ff8f30cb09f58899 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 2 Nov 2024 09:10:57 +0800 Subject: [PATCH] fix(es/typescript): Handle multiline type parameters in async arrow functions (#9704) **Related issue:** - Closes https://github.com/swc-project/swc/issues/9698 --- .changeset/swift-hairs-bow.md | 6 ++++ crates/swc_fast_ts_strip/src/lib.rs | 34 ++++++++++++++++--- .../tests/fixture/issue-9698.js | 9 +++++ .../tests/fixture/issue-9698.transform.js | 2 ++ .../tests/fixture/issue-9698.ts | 9 +++++ 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 .changeset/swift-hairs-bow.md create mode 100644 crates/swc_fast_ts_strip/tests/fixture/issue-9698.js create mode 100644 crates/swc_fast_ts_strip/tests/fixture/issue-9698.transform.js create mode 100644 crates/swc_fast_ts_strip/tests/fixture/issue-9698.ts diff --git a/.changeset/swift-hairs-bow.md b/.changeset/swift-hairs-bow.md new file mode 100644 index 00000000000..4213e24b041 --- /dev/null +++ b/.changeset/swift-hairs-bow.md @@ -0,0 +1,6 @@ +--- +swc_fast_ts_strip: patch +swc_core: patch +--- + +fix(es/typescript): Handle multiline type parameters in async arrow functions diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index d7a7f6dfead..3d09d2bddd5 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -555,6 +555,34 @@ impl Visit for TsStrip { } fn visit_arrow_expr(&mut self, n: &ArrowExpr) { + #[inline(always)] + fn is_new_line(c: char) -> bool { + matches!(c, '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}') + } + + 'type_params: { + if let Some(tp) = &n.type_params { + self.add_replacement(tp.span); + + if !n.is_async { + break 'type_params; + } + + let slice = self.get_src_slice(tp.span); + if !slice.chars().any(is_new_line) { + break 'type_params; + } + + let l_paren = self.get_next_token(tp.span.hi); + debug_assert_eq!(l_paren.token, Token::LParen); + let l_paren_pos = l_paren.span.lo; + let l_lt_pos = tp.span.lo; + + self.add_overwrite(l_paren_pos, b' '); + self.add_overwrite(l_lt_pos, b'('); + } + } + if let Some(ret) = &n.return_type { self.add_replacement(ret.span); @@ -565,10 +593,7 @@ impl Visit for TsStrip { let span = span(r_paren.span.lo, arrow.span.lo); let slice = self.get_src_slice(span); - if slice - .chars() - .any(|c| matches!(c, '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}')) - { + if slice.chars().any(is_new_line) { self.add_replacement(r_paren.span); // Instead of moving the arrow mark, we shift the right parenthesis to the next @@ -597,7 +622,6 @@ impl Visit for TsStrip { } } - n.type_params.visit_with(self); n.params.visit_with(self); n.body.visit_with(self); } diff --git a/crates/swc_fast_ts_strip/tests/fixture/issue-9698.js b/crates/swc_fast_ts_strip/tests/fixture/issue-9698.js new file mode 100644 index 00000000000..c59c0d0a427 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/issue-9698.js @@ -0,0 +1,9 @@ +let f = async ( + + v ) => v; + +let g = async ( + + v + ) => + v; \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/issue-9698.transform.js b/crates/swc_fast_ts_strip/tests/fixture/issue-9698.transform.js new file mode 100644 index 00000000000..324e3286728 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/issue-9698.transform.js @@ -0,0 +1,2 @@ +let f = async (v)=>v; +let g = async (v)=>v; diff --git a/crates/swc_fast_ts_strip/tests/fixture/issue-9698.ts b/crates/swc_fast_ts_strip/tests/fixture/issue-9698.ts new file mode 100644 index 00000000000..f544b2234e5 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/fixture/issue-9698.ts @@ -0,0 +1,9 @@ +let f = async < + T +>(v: T) => v; + +let g = async < + T +>(v: T) + : Promise => + v; \ No newline at end of file