Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions crates/oxc_formatter/src/utils/assignment_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,12 @@ impl<'a> AssignmentLike<'a, '_> {
// Handle computed properties
if property.computed {
write!(f, ["[", property.key(), "]"]);
if property.shorthand {
false
} else {
f.source_text().span_width(property.key.span()) + 2 < text_width_for_break
}
f.source_text().span_width(property.key.span()) + 2 < text_width_for_break
} else if property.shorthand {
write!(f, property.key());
let PropertyKey::StaticIdentifier(ident) = &property.key else {
unreachable!("Expected static identifier for shorthand property");
};
write!(f, text(ident.name.as_str()));
false
} else {
let width = write_member_name(property.key(), f);
Expand All @@ -232,11 +231,7 @@ impl<'a> AssignmentLike<'a, '_> {
// Handle computed properties
if property.computed {
write!(f, ["[", property.key(), "]"]);
if property.shorthand {
false
} else {
f.source_text().span_width(property.key.span()) + 2 < text_width_for_break
}
f.source_text().span_width(property.key.span()) + 2 < text_width_for_break
} else {
let width = write_member_name(property.key(), f);

Expand Down
23 changes: 23 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,26 @@ a = {
},
'outer-2': "value"
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value"
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
'y-z': "value"
};

// Nested shorthand
a = {
outer: { x, y },
z
};
207 changes: 207 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ a = {
'outer-2': "value"
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value"
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
'y-z': "value"
};

// Nested shorthand
a = {
outer: { x, y },
z
};

==================== Output ====================
-------------------------------------------
{ printWidth: 80, quoteProps: "as-needed" }
Expand Down Expand Up @@ -124,6 +147,29 @@ a = {
"outer-2": "value",
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value",
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
"y-z": "value",
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

--------------------------------------------
{ printWidth: 100, quoteProps: "as-needed" }
--------------------------------------------
Expand Down Expand Up @@ -186,6 +232,29 @@ a = {
"outer-2": "value",
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value",
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
"y-z": "value",
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

------------------------------------------
{ printWidth: 80, quoteProps: "preserve" }
------------------------------------------
Expand Down Expand Up @@ -248,6 +317,29 @@ a = {
"outer-2": "value",
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value",
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
"y-z": "value",
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

-------------------------------------------
{ printWidth: 100, quoteProps: "preserve" }
-------------------------------------------
Expand Down Expand Up @@ -310,6 +402,29 @@ a = {
"outer-2": "value",
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value",
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
"y-z": "value",
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

--------------------------------------------
{ printWidth: 80, quoteProps: "consistent" }
--------------------------------------------
Expand Down Expand Up @@ -372,6 +487,29 @@ a = {
"outer-2": "value",
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value",
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
"y-z": "value",
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

---------------------------------------------
{ printWidth: 100, quoteProps: "consistent" }
---------------------------------------------
Expand Down Expand Up @@ -434,6 +572,29 @@ a = {
"outer-2": "value",
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: "value",
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
"y-z": "value",
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

---------------------------------------------------------------
{ printWidth: 80, quoteProps: "consistent", singleQuote: true }
---------------------------------------------------------------
Expand Down Expand Up @@ -496,6 +657,29 @@ a = {
'outer-2': 'value',
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: 'value',
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
'y-z': 'value',
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

----------------------------------------------------------------
{ printWidth: 100, quoteProps: "consistent", singleQuote: true }
----------------------------------------------------------------
Expand Down Expand Up @@ -558,4 +742,27 @@ a = {
'outer-2': 'value',
};

// Shorthand properties
a = { x };
a = { x, y };
a = { x, y, z };

// Shorthand mixed with regular properties
a = {
x,
y: 'value',
};

// Shorthand with quoted properties - quotes should not affect shorthand
a = {
x,
'y-z': 'value',
};

// Nested shorthand
a = {
outer: { x, y },
z,
};

===================== End =====================
Loading