Skip to content

Commit dd0057b

Browse files
committed
fix(formatter): incorrect indentation of assignment pattern in object
1 parent 2a63d74 commit dd0057b

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

crates/oxc_formatter/src/utils/assignment_like.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,13 @@ impl<'a> AssignmentLike<'a, '_> {
217217
}
218218
AssignmentLike::BindingProperty(property) => {
219219
if property.shorthand {
220-
// Left-hand side only
221-
if property.value.kind.is_binding_identifier() {
222-
write!(f, property.key());
220+
// Left-hand side only. See the explanation in the `has_only_left_hand_side` method.
221+
if matches!(
222+
property.value.kind,
223+
BindingPatternKind::BindingIdentifier(_)
224+
| BindingPatternKind::AssignmentPattern(_)
225+
) {
226+
write!(f, property.value());
223227
}
224228
return false;
225229
}
@@ -235,9 +239,6 @@ impl<'a> AssignmentLike<'a, '_> {
235239
} else {
236240
f.source_text().span_width(property.key.span()) + 2 < text_width_for_break
237241
}
238-
} else if property.shorthand {
239-
write!(f, property.key());
240-
false
241242
} else {
242243
let width = write_member_name(property.key(), f);
243244

@@ -480,7 +481,17 @@ impl<'a> AssignmentLike<'a, '_> {
480481
Self::VariableDeclarator(declarator) => declarator.init.is_none(),
481482
Self::PropertyDefinition(property) => property.value().is_none(),
482483
Self::BindingProperty(property) => {
483-
property.shorthand && property.value.kind.is_binding_identifier()
484+
property.shorthand
485+
&& matches!(
486+
property.value.kind,
487+
BindingPatternKind::BindingIdentifier(_)
488+
// Treats binding property has a left-hand side only
489+
// when the value is an assignment pattern,
490+
// because the `value` includes the `key` part.
491+
// e.g., `{ a = 1 }` the `a` is the `key` and `a = 1` is the
492+
// `value`, aka AssignmentPattern itself
493+
| BindingPatternKind::AssignmentPattern(_)
494+
)
484495
}
485496
Self::ObjectProperty(property) => property.shorthand,
486497
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function EmptyView({
2+
title = 'title',
3+
description = $localize`:@@search_label_no_results_hint:Try using different keywords`,
4+
}) {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
function EmptyView({
6+
title = 'title',
7+
description = $localize`:@@search_label_no_results_hint:Try using different keywords`,
8+
}) {}
9+
==================== Output ====================
10+
------------------
11+
{ printWidth: 80 }
12+
------------------
13+
function EmptyView({
14+
title = "title",
15+
description = $localize`:@@search_label_no_results_hint:Try using different keywords`,
16+
}) {}
17+
18+
-------------------
19+
{ printWidth: 100 }
20+
-------------------
21+
function EmptyView({
22+
title = "title",
23+
description = $localize`:@@search_label_no_results_hint:Try using different keywords`,
24+
}) {}
25+
26+
===================== End =====================

0 commit comments

Comments
 (0)