Skip to content

Commit

Permalink
fix(es/typescript): Handle multiline type parameters in async arrow f…
Browse files Browse the repository at this point in the history
…unctions (#9704)

**Related issue:**

- Closes #9698
  • Loading branch information
magic-akari authored Nov 2, 2024
1 parent b48bdee commit c5ed19c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/swift-hairs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_fast_ts_strip: patch
swc_core: patch
---

fix(es/typescript): Handle multiline type parameters in async arrow functions
34 changes: 29 additions & 5 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -597,7 +622,6 @@ impl Visit for TsStrip {
}
}

n.type_params.visit_with(self);
n.params.visit_with(self);
n.body.visit_with(self);
}
Expand Down
9 changes: 9 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/issue-9698.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let f = async (

v ) => v;

let g = async (

v
) =>
v;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let f = async (v)=>v;
let g = async (v)=>v;
9 changes: 9 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/issue-9698.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let f = async <
T
>(v: T) => v;

let g = async <
T
>(v: T)
: Promise<any> =>
v;

0 comments on commit c5ed19c

Please sign in to comment.