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
No line break insertions in short CSS selectors #5086
Comments
Perhaps we shouldn’t break if all of the sectors are bare element selectors. |
@j-f1 Sounds reasonable. Thinking back of the previous issues, I think that’s the primary use case. |
Just out of curiosity, was this issue addressed by any PR? |
@janosh No PR for this so far! |
Let’s go with the heuristic of not automatically breaking selectors that consist of just element selectors. |
This would undeniably be better default behavior than the current implementation. And since this is a relevant topic, I still think it's worth considering offering a config for this, whether it be toggling multiline for style selectors or setting some kind of It can sometimes be more preferable to read/write: .heading, h1, h2 {
margin: 0;
} as opposed to: .heading
h1,
h2 {
margin: 0;
} Prettier has handled this for Javascript, so what I am proposing is something similar for CSS too. For example, it's generally more preferable to read/write the following: const { Header, Footer, Nav } = require('./layout'); as opposed to: const {
Header,
Footer,
Nav,
} = require('./layout'); |
I think we should keep things as they are. Keep it simple! As of this writing, there are no 👍s on this issue. |
I think we can close issue, printing in one line unreadable |
To say the one-line code examples given above are unreadable is a bit of an over exaggeration. I think @j-f1 idea would be a great solution to this. |
Feel free to provide more examples! 👍 |
Here are some more real examples of scenarios where it's nicer to have one line: *, *:before, *:after {
box-sizing: border-box;
} ul, ol {
margin: 0;
padding: 0;
list-style: none;
} h1, h2, h3, h4, h5, h6 {
margin: 0;
} There are a lot of cases like this where it's simpler to keep things compact. Putting each selector on its own line is nice, but only really necessary when you start selecting more things. |
@lydell I'm a little surprised that this issue is even debated. I was under the impression that the point of prettier is not to be simple in and of itself but to produce ideally formatted code. I can't imagine anyone would consider this to be ideally formatted, h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
} especially when compared to this h1, h2, h3, h4, h5, h6 {
margin: 0;
} Breaking code onto multiple lines only once it exceeds a certain length is such a common practice. I don't see why it would be more difficult or less useful with CSS selectors than anywhere else. |
I think Prettier already has too many heuristics that break out of the simple rules. For example, #5939. What usually happens when we add stuff like this is that people open a new issue wondering why their code is/isn't broken across several lines in some cases. I see what you mean with the But maybe this is fine to add. I guess I'm just a little more hesitant to changes since the recent ternary change backlash: #5814 (for example). Basically every time we change something new people come and want it changed back. And this issue only has 2 👍s so far. |
@lydell I certainly understand the reluctance to introduce additional heuristics. Just to be clear though, I wasn't advocating for one. I'm suggesting to only break CSS selectors onto multiple lines if they exceed a certain length in general. Is there a reason why this isn't the current behavior? |
Ah, I see. It was decided early on to print every selector on its own line: #2057 |
Thanks for digging that up! Interestingly, that issue also didn't receive any upvotes. Seems like there is low interest in CSS selectors in general. FWIW, I believe making that change was a misstep. |
Regarding upvotes – that issue is from when Prettier was not even half a year old, and the CSS support was even younger than that. Things were moving quickly, and since then the bar for changes has grown increasingly higher. |
So is it possible to reconfigure this behavior? |
@sublimeye There's no option for this currently. |
I think @sublimeye may have been referring to reverting #2057, i.e. only breaking CSS selectors onto multiple lines if they exceed a certain line length. |
There are no plans to change this currently, either. |
@lydell It's a shame really. I understand that you're looking for an overwhelming consensus before taking any action since you probably have more important things to focus on. But with Preitter being an already opinionated formatter, I was at least hoping that even though this thread lacks popularity, that the change request shares similar values that were already made on other languages for virtually the same scenario in Javascript. I don't understand why SCSS is being treated differently. I'd least hope that we can look at this change request objectively and see that implementing it would not only make sense for Preitter, but add consistency across different languages. |
@syntag instead says shame you can help, we help you on a voluntary basis, and it’s very unpleasant to hear it |
@evilebottnawi I didn't mean to come off rude or disrespectful. I appreciate all that you do, and I understand how it is to work on a volunteer basis. I would certainly help if I had the capabilities, but I'm still fairly to new Javascript. I'm just very passionate about using Prettier for my projects and was hoping to guide this change request in a direction where it might be considered for a future update, despite its lack of popularity. |
Code Guide suggest:
airbnb/css
https://github.com/airbnb/css#selectors
|
I feel it makes better sense to split to multiple lines when it goes beyond a certain number of characters as pointed out by @syntag at #5086 (comment), @janosh at #5086 (comment), @j-f1 at #5086 (comment) Also, as per the official Prettier - Options page, it says
So why not apply the same logic to CSS as well? Put as many as 80 characters and only split when the code is untidy instead of splitting for each selector which makes it tough to scroll or decreases readability in the case of long CSS files. I feel an option to choose between the two styles would be nice to have. |
Is there any workaround? |
Hello from 2021! Have there been any updates or workarounds for this since the last post? |
@nickbclifford It seems unlikely that Prettier will change its behavior here. But a simple workaround is to use the CSS pseudo-classes Prettier will not break inside these selectors: :is(h1, h2, h3, h4, h5, h6) {
margin: 0;
} If your selectors have a common parent or child, I wouldn't even consider this a workaround but the new best practice: Old CSS section h1,
section h2,
section h3,
section h4,
section h5,
section h6 {
margin: 0;
} New CSS section :is(h1, h2, h3, h4, h5, h6) {
margin: 0;
} Browser support is 90% for |
It looks like I've the same problem.. p, h3, ul {
margin: 0;
padding: 0;
} Prettier make: p,
h3,
ul {
margin: 0;
padding: 0;
} I want to have:
Is it possible? |
@Jason-2020 I was having the same issue, and was about to have all my @shirobachi So we can have the following in css: /* prettier-ignore */
p, h3, ul {
margin: 0;
padding: 0;
} @janosh and the following in javascript: import styled from "styled-components";
import mediaQuery from "../../utils/mediaQuery";
// prettier-ignore
const Participants = styled.section`
padding: 2rem;
background: ${props => props.theme.mainWhite};
display: grid;
grid-gap: 2rem;
h1, h2, h3, h4 {
width: 100%;
padding: 0.5rem 2rem;
background: ${props => props.theme.lightGreen};
}
${mediaQuery.phone} {
h1, h2, h3, h4 {
font-size: 1rem;
}
}
${mediaQuery.tablet} {
grid-gap: 1rem;
h1, h2, h3, h4 {
grid-column: 1/-1;
}
}
${mediaQuery.minTablet} {
h1, h2, h3, h4 {
white-space: nowrap;
grid-row: 2;
}
}
`
export default Participants; Is a bit annoying to have to add a comment, and it has to be added before every block of code, or abstract syntax tree as referred to by prettier docs, that is to be excluded from formatting. |
THANK YOU. Now my meyer-reset doesn't look like complete shit every-time I add it to a project. |
Just a thought, has anyone tried to PR this idea? I wasn't able to find anything and 10000% understand the want to make sure a PR will be accepted before writing the code for it, but I wonder if that's the main thing holding this up. Clearly there's support for this being a setting, which also means those who are fans of the current use could still have their way. Even keep it as the default. |
Any updates? |
any updates? Beautify extension already does this. |
Similar problems |
Prettier 1.14.2
Playground link
I suggest reconfiguring the changes made in #1962 so as to only insert line breaks in CSS selectors when they exceed a given length. Otherwise, you end up with unnecessarily long files in cases such as the one below.
Input:
Output:
Expected behavior:
No insertion of line breaks in CSS selectors of length less than, say, 80.
The text was updated successfully, but these errors were encountered: