Skip to content

Fix line break in CSS declaration with comma - #14208

Merged
fisker merged 9 commits into
prettier:mainfrom
mvorisek:css_comma_value_preserve_nl
Mar 23, 2023
Merged

fisker merged 9 commits into
prettier:mainfrom
mvorisek:css_comma_value_preserve_nl

Conversation

@mvorisek

@mvorisek mvorisek commented Jan 20, 2023 •

Copy link
Copy Markdown
Contributor

Description

Comma in CSS/LESS declaration/value is used to separate group of multiple values of the same meaning.

Currently line break is enforced in selectors like:

h1,
h2 {
    ...
}

but not for CSS values like:

box-shadow: -1px -1px 2px rgba(0, 0, 0, 0.3), 1px -1px 2px rgba(0, 0, 0, 0.3), -1px 2px 2px rgba(0, 0, 0, 0.3);

this PR fixes/unifies the behaviour by enforcing line break after comma in CSS declaration/value.

fix #6024, #7039, #8462
close #13260

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (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✨

@mvorisek mvorisek changed the title Enforce line break in CSS value with comma Fix line break in CSS group with comma Jan 20, 2023
@mvorisek mvorisek changed the title Fix line break in CSS group with comma Fix line break in CSS declaration with comma Jan 20, 2023
@mvorisek
mvorisek marked this pull request as ready for review January 20, 2023 15:31
Comment thread src/language-css/printer-postcss.js Resolved
@fisker

fisker commented Jan 28, 2023

Copy link
Copy Markdown
Member

@mvorisek Please check discussion in #6024 and #6024 (comment)

@mvorisek

mvorisek commented Jan 28, 2023 •

Copy link
Copy Markdown
Contributor Author

I support your #6024 (comment) as the wrapping rule should be easy to understand and not dependent on the content IMHO

#13260 is not a solution too, as any list of CSS properties will not work with LESS/SASS as name can be anything

@fisker

fisker commented Jan 28, 2023

Copy link
Copy Markdown
Member

But I agree line breaks in case like this seems unnecessary.

@mvorisek

This comment was marked as outdated.

@mvorisek

Copy link
Copy Markdown
Contributor Author

implemented in 2nd/3rd commit, CSS declarations like font-family: Arial, sans-serif; or $margin: 0, 2em, 0, 1.5em; are kept on a single line

thank you @fisker for guiding me into the right direction

@fisker fisker left a comment

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.

LGTM

Comment thread src/language-css/printer-postcss.js Resolved
mvorisek and others added 2 commits February 21, 2023 10:05
Co-authored-by: fisker Cheung <[email protected]>
@mvorisek

mvorisek commented Mar 1, 2023

Copy link
Copy Markdown
Contributor Author

Hi @fisker, can this PR be merged?

@fisker

fisker commented Mar 2, 2023

Copy link
Copy Markdown
Member

Need more reviews.

@fisker

fisker commented Mar 15, 2023

Copy link
Copy Markdown
Member

@prettier/core Need review.

@j-f1 j-f1 left a comment

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.

Nice improvement!

@mvorisek

Copy link
Copy Markdown
Contributor Author

@fisker there are more review now, would it be possible to merge this PR so it can be part of the next release?

$font-family-rich:
// custom
"Noto Sans TC", "Noto Sans SC", "Noto Sans JP",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line intentional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, any idea how to fix it?

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perfect, thank you ❤️

@fisker
fisker merged commit c6e026e into prettier:main Mar 23, 2023
@fisker

fisker commented Mar 23, 2023

Copy link
Copy Markdown
Member

Thank you for your hard work!

@fisker

fisker commented Mar 23, 2023

Copy link
Copy Markdown
Member

@mvorisek This cause failure on formatting

:root {
 --JSON: [1, "2", {"three": {"a": 1}}, [4]];
}

Any idea how to fix? #14568

@fisker

fisker commented Mar 23, 2023 •

Copy link
Copy Markdown
Member

Excluded custom props for now, but it's wrong. Not sure how to fix,

:root {
  --box-shadow:
    inset 0 0 10px #555,
    0 0 20px black;
  box-shadow: var(--box-shadow);
}

@mvorisek
mvorisek deleted the css_comma_value_preserve_nl branch March 23, 2023 12:33
@mvorisek

mvorisek commented Mar 23, 2023 •

Copy link
Copy Markdown
Contributor Author

Excluded custom props for now, but it's wrong. Not sure how to fix,

Can you link the fix PR/commit here?

I am not entirely sure what the expected result should be for JSON, I would probably expect the same JSON format as in standard JSON/JS.

@fisker

fisker commented Mar 23, 2023

Copy link
Copy Markdown
Member

Commit: 8ddaa4c

what the expected result should be for JSON

Should stay as-it-is.

@alex-kinokon

alex-kinokon commented Jul 14, 2023 •

Copy link
Copy Markdown

This seems a bit too aggressive, see #15070. It’s pretty difficult to write a font-family without triggering this unnecessarily.

@viveleroi

Copy link
Copy Markdown

We're using the postcss-modules plugin, which apparently doesn't support multiple lines for composes. When prettier rewrites this into multi-line it causes postcss to throw an error.

composes: body-copy, text-ellipsis from global;

Error: referenced class name "body-copy" in composes not found

But with the inline format above, it works fine. Maybe this is something that the modules plugin needs to support? For now I just use /* prettier-ignore */ above the line

medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When box-shadow has multiple values (separated by comma) it'd be prettier to put each value on a separate line

6 participants