From f526b1dcdbfb451ad9783e0cca3c58621138bad1 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 15 Jan 2025 03:49:13 -0500 Subject: [PATCH] fix: Suppress var() validation errors (#45) refs #40 --- docs/rules/no-invalid-properties.md | 4 ++++ src/rules/no-invalid-properties.js | 12 ++++++++++++ tests/rules/no-invalid-properties.test.js | 1 + 3 files changed, 17 insertions(+) diff --git a/docs/rules/no-invalid-properties.md b/docs/rules/no-invalid-properties.md index def5eb2..767a101 100644 --- a/docs/rules/no-invalid-properties.md +++ b/docs/rules/no-invalid-properties.md @@ -42,6 +42,10 @@ body { } ``` +### Limitations + +This rule uses the lexer from [CSSTree](https://github.com/csstree/csstree), which does not support validation of property values that contain variable references (i.e., `var(--bg-color)`). The lexer throws an error when it comes across a variable reference, and rather than displaying that error, this rule ignores it. This unfortunately means that this rule cannot properly validate properties values that contain variable references. We'll continue to work towards a solution for this. + ## When Not to Use It If you aren't concerned with invalid properties, then you can safely disable this rule. diff --git a/src/rules/no-invalid-properties.js b/src/rules/no-invalid-properties.js index 3f9f343..74e8f0b 100644 --- a/src/rules/no-invalid-properties.js +++ b/src/rules/no-invalid-properties.js @@ -56,6 +56,18 @@ export default { return; } + /* + * There's no current way to get lexing to work when a + * `var()` is present in a value. Rather than blowing up, + * we'll just ignore it. + * + * https://github.com/csstree/csstree/issues/317 + */ + + if (error.message.endsWith("var() is not supported")) { + return; + } + // unknown property context.report({ loc: { diff --git a/tests/rules/no-invalid-properties.test.js b/tests/rules/no-invalid-properties.test.js index 9ceced7..22005a8 100644 --- a/tests/rules/no-invalid-properties.test.js +++ b/tests/rules/no-invalid-properties.test.js @@ -32,6 +32,7 @@ ruleTester.run("no-invalid-properties", rule, { "a { color: red; -moz-transition: bar }", "@font-face { font-weight: 100 400 }", '@property --foo { syntax: "*"; inherits: false; }', + "a { --my-color: red; color: var(--my-color) }", ], invalid: [ {