Skip to content

Commit b588731

Browse files
committed
fix(formatter): should inline ObjectPattern if it is a parameter can hug
1 parent 4283fd8 commit b588731

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

crates/oxc_formatter/src/write/object_pattern_like.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
trivia::{DanglingIndentMode, format_dangling_comments},
1010
},
1111
write,
12+
write::parameters::{get_this_param, should_hug_function_parameters},
1213
};
1314

1415
use super::{
@@ -44,6 +45,19 @@ impl GetSpan for ObjectPatternLike<'_, '_> {
4445
}
4546

4647
impl<'a> ObjectPatternLike<'a, '_> {
48+
fn is_hug_parameter(
49+
&self,
50+
node: &AstNode<'a, ObjectPattern<'a>>,
51+
f: &Formatter<'_, 'a>,
52+
) -> bool {
53+
matches!(node.grand_parent(), AstNodes::FormalParameter(param) if {
54+
matches!(param.parent, AstNodes::FormalParameters(params) if {
55+
let this_param = get_this_param(params.parent);
56+
should_hug_function_parameters(params, this_param, false, f)
57+
})
58+
})
59+
}
60+
4761
fn is_empty(&self) -> bool {
4862
match self {
4963
Self::ObjectPattern(o) => o.is_empty(),
@@ -53,13 +67,10 @@ impl<'a> ObjectPatternLike<'a, '_> {
5367

5468
fn is_inline(&self, f: &Formatter<'_, 'a>) -> bool {
5569
match self {
56-
Self::ObjectPattern(node) => match node.parent {
57-
AstNodes::FormalParameter(_) => true,
58-
AstNodes::AssignmentPattern(_) => {
59-
matches!(node.parent.parent(), AstNodes::FormalParameter(_))
60-
}
61-
_ => false,
62-
},
70+
Self::ObjectPattern(node) => {
71+
matches!(node.parent, AstNodes::FormalParameter(_))
72+
|| self.is_hug_parameter(node, f)
73+
}
6374
Self::ObjectAssignmentTarget(node) => false,
6475
}
6576
}

crates/oxc_formatter/src/write/parameters.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ pub fn can_avoid_parentheses(
301301
&& !f.comments().has_comment_in_span(arrow.params.span)
302302
}
303303

304-
pub fn should_hug_function_parameters<'a>(
305-
parameters: &AstNode<'a, FormalParameters<'a>>,
306-
this_param: Option<&AstNode<'a, TSThisParameter<'a>>>,
304+
pub fn should_hug_function_parameters(
305+
parameters: &AstNode<'_, FormalParameters<'_>>,
306+
this_param: Option<&AstNode<'_, TSThisParameter<'_>>>,
307307
parentheses_not_needed: bool,
308-
f: &Formatter<'_, 'a>,
308+
f: &Formatter<'_, '_>,
309309
) -> bool {
310310
let list = &parameters.items();
311311

crates/oxc_formatter/tests/fixtures/ts/parameters/object-pattern.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ export function useCopyToClipboard({ timeout = 2000, onCopy }: {
1212
timeout?: number;
1313
onCopy?: () => void;
1414
} = {}) {}
15+
16+
function callbackUrl(
17+
{ baseUrl, params }: { baseUrl: string; params?: string } = {
18+
baseUrl: "",
19+
params: undefined,
20+
}
21+
) { }

crates/oxc_formatter/tests/fixtures/ts/parameters/object-pattern.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ export function useCopyToClipboard({ timeout = 2000, onCopy }: {
1717
onCopy?: () => void;
1818
} = {}) {}
1919

20+
function callbackUrl(
21+
{ baseUrl, params }: { baseUrl: string; params?: string } = {
22+
baseUrl: "",
23+
params: undefined,
24+
}
25+
) { }
26+
2027
==================== Output ====================
2128
(
2229
options,
@@ -39,4 +46,11 @@ export function useCopyToClipboard({
3946
onCopy?: () => void;
4047
} = {}) {}
4148

49+
function callbackUrl(
50+
{ baseUrl, params }: { baseUrl: string; params?: string } = {
51+
baseUrl: "",
52+
params: undefined,
53+
},
54+
) {}
55+
4256
===================== End =====================

0 commit comments

Comments
 (0)