Skip to content

Prevent wrapping object properties with short keys#10335

Merged
thorn0 merged 3 commits intoprettier:mainfrom
thorn0:assignments3
Mar 2, 2021
Merged

Prevent wrapping object properties with short keys#10335
thorn0 merged 3 commits intoprettier:mainfrom
thorn0:assignments3

Conversation

@thorn0
Copy link
Copy Markdown
Member

@thorn0 thorn0 commented Feb 17, 2021

Description

Fixes #3711
It's not obvious from the changes in the snapshots, but this is able to lead to huge diffs. Still worth doing IMHO.

Checklist

  • I’ve added tests to confirm my change works.
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@thorn0 thorn0 marked this pull request as draft February 17, 2021 11:26
node.key &&
!node.computed &&
node.key.type === "Identifier" &&
node.key.name.length <= options.tabWidth + 2
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2? 😄

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was 3 initially. :D Suggestions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has to be a sufficient visual overlap for the line break to look natural. I'm having trouble with determining how long exactly it needs to be.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I also have no ideas, magic numbers scare me 😄 , maybe we just need to make cases with 2/3/4 values and look where it will be better

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added that to snapshots. Have a look.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the value 2 was based on this comment #3711 (comment)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
ab: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
abc: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
abcd: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'abcd': "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",

Should be same?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall length of the part before : is measured.

abcd → 4
'abcd' → 6
[x] → 3

}
// TODO: a more lightweight version of cleanDoc is needed that would stop once it's
// detected that the doc can't be normalized into a string.
keyDoc = cleanDoc(keyDoc);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use printDocToString?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't sound like an optimization

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't, but don't relay on how the doc is build.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printDocToString always returns a string, but I need to check that the doc consists only of strings. No line breaks, etc. How is that worse than, for example, willBreak? Doc structure checks are everywhere in Prettier's code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example label.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

label is for complex things like member chains. We definitely don't want to consider such keys "simple".

Copy link
Copy Markdown
Contributor

@sosukesuzuki sosukesuzuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String length causes a strange bug as below, but I think we can ignore it in the real world 😄

Prettier pr-10335
Playground link

--parser babel

Input:

foo = {
  // length 5
  foooo: "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
  // length 4
  fooo: "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
  // length 4? no, length 5!
  "𩸽o": "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
};

Output:

foo = {
  // length 5
  foooo:
    "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
  // length 4
  fooo: "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
  // length 4? no, length 5!
  "𩸽o":
    "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
};

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Feb 18, 2021

@sosukesuzuki We have getStringWidth for that. Good catch.

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Feb 18, 2021

getStringWidth returns 2 for that character too because it's full-width. So the output was actually correct. Now it's correct also for other full-width characters. On the other hand, the preserved quotes actually look like a bug. Shouldn't they have been removed?

upd: Turns out Prettier conservatively uses rules from ES5.1 to decide what can be unquoted.

Copy link
Copy Markdown
Member

@lipis lipis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:100

@thorn0 thorn0 merged commit 5f8fee4 into prettier:main Mar 2, 2021
@thorn0 thorn0 deleted the assignments3 branch March 2, 2021 23:43
fisker added a commit to fisker/prettier that referenced this pull request Apr 6, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Long string values in object literals sometimes look strange

5 participants