mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 00:32:15 +03:00
fix(es/typescript): Handle multiline type parameters in async arrow functions (#9704)
**Related issue:** - Closes https://github.com/swc-project/swc/issues/9698
This commit is contained in:
parent
b48bdee4df
commit
c5ed19c710
6
.changeset/swift-hairs-bow.md
Normal file
6
.changeset/swift-hairs-bow.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
swc_fast_ts_strip: patch
|
||||||
|
swc_core: patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(es/typescript): Handle multiline type parameters in async arrow functions
|
@ -555,6 +555,34 @@ impl Visit for TsStrip {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_arrow_expr(&mut self, n: &ArrowExpr) {
|
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 {
|
if let Some(ret) = &n.return_type {
|
||||||
self.add_replacement(ret.span);
|
self.add_replacement(ret.span);
|
||||||
|
|
||||||
@ -565,10 +593,7 @@ impl Visit for TsStrip {
|
|||||||
let span = span(r_paren.span.lo, arrow.span.lo);
|
let span = span(r_paren.span.lo, arrow.span.lo);
|
||||||
|
|
||||||
let slice = self.get_src_slice(span);
|
let slice = self.get_src_slice(span);
|
||||||
if slice
|
if slice.chars().any(is_new_line) {
|
||||||
.chars()
|
|
||||||
.any(|c| matches!(c, '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}'))
|
|
||||||
{
|
|
||||||
self.add_replacement(r_paren.span);
|
self.add_replacement(r_paren.span);
|
||||||
|
|
||||||
// Instead of moving the arrow mark, we shift the right parenthesis to the next
|
// 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.params.visit_with(self);
|
||||||
n.body.visit_with(self);
|
n.body.visit_with(self);
|
||||||
}
|
}
|
||||||
|
9
crates/swc_fast_ts_strip/tests/fixture/issue-9698.js
Normal file
9
crates/swc_fast_ts_strip/tests/fixture/issue-9698.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
let f = async (
|
||||||
|
|
||||||
|
v ) => v;
|
||||||
|
|
||||||
|
let g = async (
|
||||||
|
|
||||||
|
v
|
||||||
|
) =>
|
||||||
|
v;
|
@ -0,0 +1,2 @@
|
|||||||
|
let f = async (v)=>v;
|
||||||
|
let g = async (v)=>v;
|
9
crates/swc_fast_ts_strip/tests/fixture/issue-9698.ts
Normal file
9
crates/swc_fast_ts_strip/tests/fixture/issue-9698.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
let f = async <
|
||||||
|
T
|
||||||
|
>(v: T) => v;
|
||||||
|
|
||||||
|
let g = async <
|
||||||
|
T
|
||||||
|
>(v: T)
|
||||||
|
: Promise<any> =>
|
||||||
|
v;
|
Loading…
Reference in New Issue
Block a user