Skip to content

Commit f60ca8a

Browse files
authored
fix: arrow func single param paren should not be removed when has trailing comma with trailing comments (#536)
1 parent 7a55f98 commit f60ca8a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/generation/generate.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1762,11 +1762,21 @@ fn gen_arrow_func_expr<'a>(node: &'a ArrowExpr<'a>, context: &mut Context<'a>) -
17621762
fn is_first_param_not_identifier_or_has_type_annotation<'a>(params: &[Pat<'a>], arrow: &ArrowSignature<'a>, program: Program<'a>) -> bool {
17631763
match params.get(0) {
17641764
Some(Pat::Ident(node)) => {
1765-
node.type_ann.is_some() || node.id.optional() || node_helpers::has_surrounding_comments((*node).into(), program) && has_parens(arrow, program)
1765+
node.type_ann.is_some() || node.id.optional() || param_has_surrounding_comments((*node).into(), program) && has_parens(arrow, program)
17661766
}
17671767
_ => true,
17681768
}
17691769
}
1770+
1771+
fn param_has_surrounding_comments<'a>(param: Node<'a>, program: Program<'a>) -> bool {
1772+
if node_helpers::has_surrounding_comments(param, program) {
1773+
true
1774+
} else if let Some(comma_token) = param.next_token_fast(program).filter(|t| t.token == Token::Comma) {
1775+
!comma_token.trailing_comments_fast(program).is_empty()
1776+
} else {
1777+
false
1778+
}
1779+
}
17701780
}
17711781

17721782
fn has_parens<'a>(node: &ArrowSignature<'a>, program: Program<'a>) -> bool {

tests/specs/issues/issue0533.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
~~ arrowFunction.useParentheses: preferNone ~~
2+
== should maintain parens around params in arrow function with single param and trailing comment ==
3+
client
4+
.send(lookup)
5+
.then((
6+
resp, // $ExpectType Batch<Lookup>
7+
) => console.log(resp))
8+
.catch((
9+
err, // $ExpectType any
10+
) => console.error(err));
11+
12+
[expect]
13+
client
14+
.send(lookup)
15+
.then((
16+
resp, // $ExpectType Batch<Lookup>
17+
) => console.log(resp))
18+
.catch((
19+
err, // $ExpectType any
20+
) => console.error(err));

0 commit comments

Comments
 (0)