-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[CSS] Prettier converts "invalid" no-space value to a "valid" space value (changes the meaning/behavior of CSS code) #8792
Comments
Ideally, the parser should throw the error in this case, it is not valid syntax |
It is valid "invalid" syntax (meaning the user can rely on the invalid syntax just like they can get away with malformed HTML). I don't think the parser should throw an error, just like browsers don't. |
What's invalid "invalid" syntax then? |
Plus it would be annoying for someone to run prettier in a fully-working project for the first time and it fails. |
To rely on it to achieve what exactly? The use case doesn't seem compelling unless I miss something. |
If a user has this invalid value syntax in their CSS: If it can't just leave the invalid syntax in place, what it should do instead is convert it to this: |
So there is no specific use case in which this behavior is useful. It indeed should be a syntax error. Why would people have this in their CSS files? |
I do not recommenced anyone relies on behavior of an invalid syntax such as Invalid css-vars trigger fallback behavior when referenced. Any css var that has an Recently it's been in the spotlight because of the CSS Space Toggle logic that becomes possible. Here's a summary of it: https://github.com/propjockey/css-sweeper#basics-of-space-toggle |
Easier to see here:
Validity at compute time is the most important since var(--fallback, behavior) depends on it being invalid at compute time. |
@James0x57 Yes, only |
@evilebottnawi I believe @trusktr is using The current behavior of Prettier is to change If Prettier changed I can demonstrate this in a jsbin if it would be helpful? |
Actually, an even more accurate replacement could be wrapping it in a comment if you're not opposed to that because |
No,
No, we don't modify AST. Nobody argues that this changes logic. Questions - Why it was in source code. In this case we should throw an error. It is invalid code and parser should not parse it |
Yeah, basically to implement default value handling while showing a list of available variables (or to execute boolean logic), as described in the Space Toggle documentation that @James0x57 linked to above, for example. I do agree, converting Prettier should never modify code behavior.
Prettier is modifying the AST that the browser parses.
The explanation is above. I can write, I can ship it, and I can expect the app to behave exactly a certain way... ...then Prettier breaks the app. The |
Prettier 2.0.5
Playground link
Input:
Output:
Expected behavior:
An valid "invalid" CSS property is converted into a "valid" CSS property containing a value of a single space.
Before:
https://codepen.io/trusktr/pen/2507ea1e5ecb8b8a41800335fc327349
After:
https://codepen.io/trusktr/pen/5518a13792dda7288323eee272b0b9a3
A value like
--foo:;
is "invalid" CSS, but converting it to a "valid" value like--foo: ;
introduces a new value that changes code behavior and thus changes visual output.It is important not to change the code behavior.
Sidenote: I stumbled on this info while playing with the Space Toggle technique
I found this while playing with the awesome "Space Toggle" technique.
The space toggle technique is used in @James0x57's
css-media-vars
lib, to provide variables that make it easier to write media queries within existing CSS code structure without new@media
blocks, thus without having to break code structure apart and keeping it DRY.For example (from the README), code like
can be written as
This allows the media queries to be co-located in the CSS structure, similar to what we can do with Sass, Less, and Stylus, but this relies on standard CSS spec.
As another example, I had some desktop-only code,
and I added mobile support like this:
The above
css-media-var
examples don't have to do with the issue directly, but I had noticed that an invalid value could be converted to a valid value that could override a previously valid value (like aninitial
value).More info on Space Toggles:
--toggler: ;
to--toggler:;
in the CSS code and the visual outcome will change.Very neat outcomes, especially the
css-media-vars
!Anywho, Space Toggle is only slightly related to the
--foo:;
issue, but it's how I stumbled on it.The text was updated successfully, but these errors were encountered: