Skip to content

Commit c5ed19c

Browse files
authored
fix(es/typescript): Handle multiline type parameters in async arrow functions (#9704)
**Related issue:** - Closes #9698
1 parent b48bdee commit c5ed19c

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

.changeset/swift-hairs-bow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_fast_ts_strip: patch
3+
swc_core: patch
4+
---
5+
6+
fix(es/typescript): Handle multiline type parameters in async arrow functions

crates/swc_fast_ts_strip/src/lib.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,34 @@ impl Visit for TsStrip {
555555
}
556556

557557
fn visit_arrow_expr(&mut self, n: &ArrowExpr) {
558+
#[inline(always)]
559+
fn is_new_line(c: char) -> bool {
560+
matches!(c, '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}')
561+
}
562+
563+
'type_params: {
564+
if let Some(tp) = &n.type_params {
565+
self.add_replacement(tp.span);
566+
567+
if !n.is_async {
568+
break 'type_params;
569+
}
570+
571+
let slice = self.get_src_slice(tp.span);
572+
if !slice.chars().any(is_new_line) {
573+
break 'type_params;
574+
}
575+
576+
let l_paren = self.get_next_token(tp.span.hi);
577+
debug_assert_eq!(l_paren.token, Token::LParen);
578+
let l_paren_pos = l_paren.span.lo;
579+
let l_lt_pos = tp.span.lo;
580+
581+
self.add_overwrite(l_paren_pos, b' ');
582+
self.add_overwrite(l_lt_pos, b'(');
583+
}
584+
}
585+
558586
if let Some(ret) = &n.return_type {
559587
self.add_replacement(ret.span);
560588

@@ -565,10 +593,7 @@ impl Visit for TsStrip {
565593
let span = span(r_paren.span.lo, arrow.span.lo);
566594

567595
let slice = self.get_src_slice(span);
568-
if slice
569-
.chars()
570-
.any(|c| matches!(c, '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}'))
571-
{
596+
if slice.chars().any(is_new_line) {
572597
self.add_replacement(r_paren.span);
573598

574599
// Instead of moving the arrow mark, we shift the right parenthesis to the next
@@ -597,7 +622,6 @@ impl Visit for TsStrip {
597622
}
598623
}
599624

600-
n.type_params.visit_with(self);
601625
n.params.visit_with(self);
602626
n.body.visit_with(self);
603627
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let f = async (
2+
3+
v ) => v;
4+
5+
let g = async (
6+
7+
v
8+
) =>
9+
v;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let f = async (v)=>v;
2+
let g = async (v)=>v;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let f = async <
2+
T
3+
>(v: T) => v;
4+
5+
let g = async <
6+
T
7+
>(v: T)
8+
: Promise<any> =>
9+
v;

0 commit comments

Comments
 (0)