diff --git a/crates/oxc_formatter/src/utils/assignment_like.rs b/crates/oxc_formatter/src/utils/assignment_like.rs index 0e9ecd81540bd..b9e4ac0befa81 100644 --- a/crates/oxc_formatter/src/utils/assignment_like.rs +++ b/crates/oxc_formatter/src/utils/assignment_like.rs @@ -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); @@ -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); diff --git a/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js b/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js index 8c81fc9c2524d..b4bb889e1a8eb 100644 --- a/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js +++ b/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js @@ -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 +}; diff --git a/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js.snap b/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js.snap index 61b8f9423b2ad..d456d62281867 100644 --- a/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js.snap +++ b/crates/oxc_formatter/tests/fixtures/js/quote-props/objects.js.snap @@ -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" } @@ -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" } -------------------------------------------- @@ -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" } ------------------------------------------ @@ -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" } ------------------------------------------- @@ -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" } -------------------------------------------- @@ -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" } --------------------------------------------- @@ -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 } --------------------------------------------------------------- @@ -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 } ---------------------------------------------------------------- @@ -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 =====================