Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another destructuring oddity (assigning from computed properties on Objects) #4092

Open
taylorham opened this issue Mar 2, 2018 · 2 comments
Labels
area:destructuring lang:javascript Issues affecting JS type:bug Issues identifying ugly output, or a defect in the program

Comments

@taylorham
Copy link

Prettier 1.11.1
Playground link

# Options (if any):
# none

Input:

// Array destructuring, computed property
const [oneFairlyLongProp, secondLongProp, thirdReallyLongProp] = some.nested[prop];

// Object destructuring, string access
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some.nested["prop"];

// Object destructuring, closing bracket BEFORE max-print-width
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some[prop].nested;

// Object destructuring, opening bracket AFTER max-print-width (working fine!)
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some.deeplyNested[prop];

Output:

// Array destructuring, computed property
const [oneFairlyLongProp, secondLongProp, thirdReallyLongProp] = some.nested[
  prop
];

// Object destructuring, string access
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some.nested[
  "prop"
];

// Object destructuring, closing bracket BEFORE max-print-width
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some[
  prop
].nested;

// Object destructuring, opening bracket AFTER max-print-width (working fine!)
const {
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
} = some.deeplyNested[prop];

Expected behavior:
I expect the LHS of the assignment to be broken out rather than the RHS, as it does when accessing an Array item by index:

Input:

// Array indices are not broken out: expected behavior
const [oneFairlyLongProp, secondLongProp, thirdReallyLongProp] = some.index[1234];

const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some.nested[1234];

const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some[1234].nested;

const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some.deeplyNested[1234];

Output:

// Array indices are not broken out: expected behavior
const [
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
] = some.index[1234];

const {
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
} = some.nested[1234];

const {
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
} = some[1234].nested;

const {
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
} = some.deeplyNested[1234];

This happens with both Array and Object destructuring, but only when accessing an object property with bracket notation and the print-width is exceeded after an opening bracket.

I imagine the assigned reference should never be broken out unless the last line (} = some.thing) exceeds the print-width itself.

@taylorham taylorham changed the title Another destructuring oddity (accessing computed properties on Objects) Another destructuring oddity (assigning from computed properties on Objects) Mar 2, 2018
@j-f1 j-f1 added type:bug Issues identifying ugly output, or a defect in the program lang:javascript Issues affecting JS labels Mar 2, 2018
@ericanderson
Copy link
Contributor

Hmm, there are some edge cases we would need to tease out, specifically what happens when the RHS is also too long. It seems to be flat out broken right now:

Prettier 1.11.0
Playground link

--print-width 76

Input:

const {
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
} = some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf.asdf.asdf;

const fo = some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf.asdf.asdf;

Output:

const {
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
} = some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf.asdf.asdf;

const fo =
  some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf
    .asdf.asdf;

@seiyab
Copy link
Sponsor Contributor

seiyab commented Jul 27, 2023

I feel v3.0.0 works reasonably well, though it is not same as suggested one.

// Input

// Array destructuring, computed property
const [oneFairlyLongProp, secondLongProp, thirdReallyLongProp] = some.nested[prop];

// Object destructuring, string access
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } = some.nested["prop"];
// Output

// Array destructuring, computed property
const [oneFairlyLongProp, secondLongProp, thirdReallyLongProp] =
  some.nested[prop];

// Object destructuring, string access
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } =
  some.nested["prop"];

For #4092 (comment),

// Input
const {
  oneFairlyLongProp,
  secondLongProp,
  thirdReallyLongProp
} = some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf.asdf.asdf;

const fo = some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf.asdf.asdf;
// Output
const { oneFairlyLongProp, secondLongProp, thirdReallyLongProp } =
  some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf
    .asdf.asdf;

const fo =
  some.deeplyNested[1234].baz.bar.bar.bar.nbar.bar.asdfa.sdf.asdf.ad.sfa.sdf
    .asdf.asdf;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:destructuring lang:javascript Issues affecting JS type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

5 participants