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
Add dashed-ident-no-missing-reference-function #5000
Comments
Sounds good to me. The following invalid syntax is caught by the csstree-validator plugin: a {
color: --my-color;
} However, it's a simple validation case that we can add a built-in rule for. I suggest the name
We'll want to use the value parser in the implementation. It parses I think we'll be able to autofix most violations. We may want to not autofix if the next node is a comma, e.g.: :root {
--main-color: #c06;
--accent-background: linear-gradient(to top, --main-color, white);
} In this example, it is unclear if Any thoughts or can I label this ready to implement? |
Great summary, @jeddy3! I think |
Also we have |
I originally thought of The examples in the original request are invalid CSS, more akin to
Good catch. There's more to unpack here. We're talking about dashed-idents, which are a specialised form of custom-idents. They're used in at least the following places: And it'll likely be used in more as it's the perferred syntax for authored custom idents. Example: @color-profile --my-profile { src: url(https://example.com/foo.icc); }
@custom-media --narrow-window (max-width: 30em);
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
:root { --my-color: red; }
@media (--narrow-window) {
:--heading {
color: color(--my-profile 1 0 .5 / .2);
background: var(--my-color);
margin: env(--my-margin);
}
} Both the environmental variables spec and the custom properties spec talk about defining, which is when a dashed-ident is created, and referencing, which is when a previously defined dashed-ident is used. We're looking at the latter here. A dashed-ident can be referenced in either a declaration-value, at-rule or selector. We can either have separate rules for these or combine them into a single rule. My preference would be separate rules.
The former would catch: .foo {
color: --my-profile 1 0 .5 / .2; /* previously color(...) */
background: --my-color; /* previously var(...) */
margin: --my-margin; /* previously env(...) */
} And also this because custom properties have declaration values: :root {
--my-prop: --my-color; /* previously var(...) */
} And the latter two would catch: @media --narrow-window {} /* previously @media (--narrow-window) {} */
@custom-selector --heading h1, h2, h3, h4, h5, h6; /* previously @custom-selector :--heading h1, h2, h3, h4, h5, h6; */ We can put the latter two on the backburner for now and focus on the former as it addresses the original request. We can't add autofix to this rule because a dashed-ident can be referenced by Revised blueprint:
How does that look? |
As there are no objections to the above blueprint, I'll label this as ready to implement. |
@jeddy3 I think better to name it |
Are you saying that the following is valid CSS because a dashed-ident doesn't have to be referenced?: a { prop: --test } If so, then we can loop back to @hudochenkov suggestion of using For example: "rules": {
"declaration-value-dashed-ident-no-missing-reference-function": [true, {
"ignoreDashedIdents": ["my-js-ident"]
}]
} With: a {
color: --my-color;
prop: --my-js-ident;
} Would only produce one violation:
Revised blueprint:
These dashed-idents are a fascinating can of worms. As such I suggest we keep this rule narrowly scoped, and I believe the above blueprint does that. |
Still found |
Initial rule done in #5317 |
Developers sometimes accidentally forget to include the
var()
function call when trying to dereference custom properties. For example, they might write:when they meant
A new rule that flags identifiers beginning with
--
that appear outside of the first argument of avar()
function.The text was updated successfully, but these errors were encountered: