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

feat(css_parser,css_formatter): distinguish regular, custom, and dashed identifiers #1353

Merged
merged 3 commits into from
Dec 28, 2023

Conversation

faultyserver
Copy link
Contributor

Summary

Closes #1351. This updates the grammar to use distinct types for the different classes of identifiers, and (mostly) matches the CSS specification for their usage in things like at-rules and selectors.

In a follow up PR, I'll start adding the auto-lowercasing for all tokens/identifiers other than CssCustomIdentifier and CssDashedIdentifier, since those are the only case-sensitive values and can now be easily implemented directly without having to thread options around.

I left a TODO in here that will likely come up in future cases as well, for the @color-profile at-rule. The name can be <dashed-ident> | device-cmyk, where device-cmyk is a contextual literal keyword. Using parse_dashed_identifier right now would not allow that keyword as a value, and using parse_custom_identifier is technically too broad (since something like 'myProfile' is not an acceptable identifier here), but I'm not sure what the best way to represent that unique union is. I tried making an AnyCssColorProfileName that looked like CssDashedIdentifier | 'device-cmyk', but I don't think that really works with the current parser structure.

Test Plan

All of the snapshots are updated, and the formatting tests succeed without any changes, exactly as expected here.

Copy link

netlify bot commented Dec 27, 2023

Deploy Preview for biomejs canceled.

Name Link
🔨 Latest commit 0548cd3
🔍 Latest deploy log https://app.netlify.com/sites/biomejs/deploys/658cbe9624b26900084ec459

@github-actions github-actions bot added A-Parser Area: parser A-Formatter Area: formatter A-Tooling Area: internal tools L-CSS Language: CSS labels Dec 27, 2023
AnyCssValue::CssString(node) => node.format().fmt(f),
AnyCssValue::CssNumber(node) => node.format().fmt(f),
AnyCssValue::AnyCssDimension(node) => node.format().fmt(f),
AnyCssValue::CssRatio(node) => node.format().fmt(f),
AnyCssValue::AnyCssFunction(node) => node.format().fmt(f),
AnyCssValue::CssCustomProperty(node) => node.format().fmt(f),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CssCustomProperty is now CssDashedIdentifier and just directly wraps the IDENT token, rather than wrapping around an entire CssIdentifier node. This seems to be how most other parsers are handling this as well (SWC, LightningCSS, etc.).

Copy link
Contributor

Parser conformance results on

js/262

Test result main count This PR count Difference
Total 49701 49701 0
Passed 48721 48721 0
Failed 980 980 0
Panics 0 0 0
Coverage 98.03% 98.03% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 40 40 0
Passed 37 37 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.50% 92.50% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 6322 6322 0
Passed 2036 2036 0
Failed 4286 4286 0
Panics 0 0 0
Coverage 32.20% 32.20% 0.00%

ts/babel

Test result main count This PR count Difference
Total 662 662 0
Passed 592 592 0
Failed 70 70 0
Panics 0 0 0
Coverage 89.43% 89.43% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 17646 17646 0
Passed 13452 13452 0
Failed 4192 4192 0
Panics 2 2 0
Coverage 76.23% 76.23% 0.00%

Comment on lines -1214 to -1223
CssVarFunction =
'var'
'('
property: CssCustomProperty
value: CssVarFunctionValue?
')'

CssVarFunctionValue =
','
value: CssIdentifier
Copy link
Contributor Author

@faultyserver faultyserver Dec 27, 2023

Choose a reason for hiding this comment

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

I removed these nodes because they aren't used anymore now that functions in general are more broadly supported (they just become CssSimpleFunction instead).

Maybe in the future we can parse known functions as distinct node types (especially if they have specific syntax), but i think a semantic analyzer will be the better place for that in most cases.

Comment on lines +939 to 944
// NOTE: The CSS Spec uses `<ident>` for the name, but also explicitly states
// that the name is case sensitive. For simplicity, `<custom-ident>` gets the
// exact same behavior.
CssPageSelector =
type: CssIdentifier?
type: CssCustomIdentifier?
pseudos: CssPageSelectorPseudoList
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems to be the only case of the spec using <ident> but saying it's case sensitive. Rather than trying to handle a special case, I figure just using the <custom-ident> makes sense. They have the exact same syntax specification, the latter is just defined as case-sensitive and not a pre-defined CSS keyword.

Copy link
Contributor

@denbezrukov denbezrukov left a comment

Choose a reason for hiding this comment

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

I love that!

@ematipico ematipico merged commit f77d796 into main Dec 28, 2023
19 checks passed
@ematipico ematipico deleted the faulty/css-idents branch December 28, 2023 12:18
ematipico pushed a commit that referenced this pull request Dec 29, 2023
@Conaclos Conaclos added the A-Changelog Area: changelog label Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Formatter Area: formatter A-Parser Area: parser A-Tooling Area: internal tools L-CSS Language: CSS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Distinguish CSS Identifiers, Custom Identifiers, and Dashed Identifiers
4 participants