-
-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Validate property values containing variables #148
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Enables validation of CSS property values that use var()
by tracking custom property definitions and replacing variables before validation.
- Tracks
var()
function calls per declaration and substitutes known custom property values for validation. - Reports errors for unknown variables (
unknownVar
) and invalid substituted values (invalidPropertyValue
). - Adds tests and updates documentation to describe variable validation behavior.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
tests/rules/no-invalid-properties.test.js | Added valid and invalid test cases covering custom property usage |
src/rules/no-invalid-properties.js | Implemented var() tracking, replacement logic, and new error type |
docs/rules/no-invalid-properties.md | Updated limitations section to explain var() validation behavior |
Comments suppressed due to low confidence (2)
src/rules/no-invalid-properties.js:17
- JSDoc tags use '@import' which is non-standard; consider using '@typedef {import("../types.js").CSSRuleDefinition}' or the equivalent JSDoc import syntax for proper type declarations.
* @import { CSSRuleDefinition } from "../types.js"
src/rules/no-invalid-properties.js:143
- [nitpick] Using
var
as a data property key can be confusing since it's a reserved term; consider renaming it tovarName
orcustomProperty
for clarity.
data: { var: name },
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request enables validation of CSS property values that include variable references through var(), ensuring that defined custom properties are used correctly.
- Introduces variable tracking and replacement in the no-invalid-properties rule
- Adds comprehensive tests covering various cases with var() usages
- Updates documentation to clarify the behavior when custom properties are defined or missing
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
tests/rules/no-invalid-properties.test.js | Adds test cases for validating both defined and undefined var() usage |
src/rules/no-invalid-properties.js | Implements custom property tracking and replacement logic for var() in declarations |
docs/rules/no-invalid-properties.md | Updates documentation to explain how var() validation is handled |
Comments suppressed due to low confidence (1)
src/rules/no-invalid-properties.js:143
- Instead of returning immediately after encountering an unknown variable, consider reporting all unknown variable errors in the block to provide comprehensive feedback.
context.report({ loc: func.children[0].loc, messageId: "unknownVar", data: { var: name }, });
We have one false positive when we have spaces inside /* eslint css/no-invalid-properties: error */
:root {
--color: red
}
a {
color: var( --color ); // Unknown property 'color' found css/no-invalid-properties
} |
Co-authored-by: Nitin Kumar <snitin315@gmail.com>
@snitin315 nice find! I'll take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Leaving it open for the second review.
@eslint/eslint-tsc can one of you take a look at this? |
/* | ||
* When using variables, check to see if the error | ||
* occurred at a location where a variable was replaced. | ||
* If so, use that location; otherwise, use the error's | ||
* reported location. | ||
*/ | ||
loc: usingVars | ||
? (varsFoundLocs.get(error.mismatchOffset) ?? | ||
node.value.loc) | ||
: error.loc, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this doesn't quite work as expected when there are multiple replacements. For example:
a { --width: 1px; border-top: var(--width) solid var(--width); }
I can see that in this case the SyntaxError has a mismatchOffset
of 10, i.e. the offset of the second var(--width)
after the replacements were applied, but varsFoundLocs
contains the offsets of the variables before the replacement. Thus there is no match and the rule reports the error's original location.
I think this can be fixed by ensuring that replaceWithOffsets
returns the offsets of the replaced locations in the output text, rather than in the original string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure I understand what you're saying -- you're saying that when there's a second var()
that contains an incorrect value the reported location is not the location of that var()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I got it. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Prerequisites checklist
What is the purpose of this pull request?
To enable validation of CSS property values containing
var()
. Previously, we just skipped validating these completely. For this to work, the custom property must be defined before it's used, such as:If a custom property is not defined before it's used, a lint violation is reported.
What changes did you make? (Give an overview)
no-invalid-properties
to track custom property values and use them as replacements for the value that is validatedvar()
is validatedRelated Issues
Is there anything you'd like reviewers to focus on?
The highlighted section of code for a violation is different when there is a
var()
vs. when there isn't.var()
(as in HEAD), the individual incorrect value is highlighted. So forborder-top: 1px solid foo
, it'sfoo
that is indicated in the location information.var()
:var()
is the violation, then it's location is highlighted and the replacement value is reported in the message.