Skip to content

Commit 4120774

Browse files
authored
fix: handle missing declaration state in no-invalid-properties (eslint#290)
* fix: handle missing declaration state in `no-invalid-properties` * combine state checks into a single condition
1 parent db2c370 commit 4120774

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/rules/no-invalid-properties.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,18 @@ export default {
343343
},
344344

345345
Function() {
346-
declStack.at(-1).functionPartsStack.push([]);
346+
const state = declStack.at(-1);
347+
if (!state) {
348+
return;
349+
}
350+
state.functionPartsStack.push([]);
347351
},
348352

349353
"Function > *:not(Function)"(node) {
350354
const state = declStack.at(-1);
355+
if (!state) {
356+
return;
357+
}
351358
const parts = state.functionPartsStack.at(-1);
352359
const text = sourceCode.getText(node).trim();
353360
parts.push(text);
@@ -356,7 +363,7 @@ export default {
356363

357364
"Function:exit"(node) {
358365
const state = declStack.at(-1);
359-
if (state.skipValidation) {
366+
if (!state || state.skipValidation) {
360367
return;
361368
}
362369

tests/rules/no-invalid-properties.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ ruleTester.run("no-invalid-properties", rule, {
164164
code: ":root { --a: var(--c); --b: var(--a); }\na { color: var(--b); }",
165165
options: [{ allowUnknownVariables: true }],
166166
},
167+
"@supports (color: color(display-p3 1 1 1)) { a { --my-color: oklch(73.5% 0.1192 254.8); } }",
168+
"@supports not (color: color(display-p3 1 1 1)) { a { --my-color: rgb(31 120 50); } }",
169+
"@supports selector(:is(a)) { a { color: red; } }",
170+
"@media (color-gamut: srgb) { a { --my-color: #f4ae8a; } }",
171+
"@supports (color: color(display-p3 1 1 1)) { @media (color-gamut: p3) { a { --c: oklch(50% 0.1 120); } } }",
172+
"@import 'x.css' layer(theme);",
167173

168174
/*
169175
* CSSTree doesn't currently support custom functions properly, so leaving
@@ -427,6 +433,21 @@ ruleTester.run("no-invalid-properties", rule, {
427433
},
428434
],
429435
},
436+
{
437+
code: "@supports (color: color(display-p3 1 1 1)) { a { color: var(--my-color); } }",
438+
errors: [
439+
{
440+
messageId: "unknownVar",
441+
data: {
442+
var: "--my-color",
443+
},
444+
line: 1,
445+
column: 61,
446+
endLine: 1,
447+
endColumn: 71,
448+
},
449+
],
450+
},
430451
{
431452
code: "a { color: var(--MY-COLOR, var(--FALLBACK-COLOR)); }",
432453
errors: [
@@ -566,6 +587,23 @@ ruleTester.run("no-invalid-properties", rule, {
566587
},
567588
],
568589
},
590+
{
591+
code: ":root { --color: foo }\n@supports (color: color(display-p3 1 1 1)) { a { color: var(--color); } }",
592+
errors: [
593+
{
594+
messageId: "invalidPropertyValue",
595+
data: {
596+
property: "color",
597+
value: "foo",
598+
expected: "<color>",
599+
},
600+
line: 2,
601+
column: 57,
602+
endLine: 2,
603+
endColumn: 69,
604+
},
605+
],
606+
},
569607
{
570608
code: "a { colorr: var(--my-color); }",
571609
options: [{ allowUnknownVariables: true }],
@@ -1103,6 +1141,23 @@ ruleTester.run("no-invalid-properties", rule, {
11031141
},
11041142
],
11051143
},
1144+
{
1145+
code: "@supports (color: color(display-p3 1 1 1)) { a { color: foo } }",
1146+
errors: [
1147+
{
1148+
messageId: "invalidPropertyValue",
1149+
data: {
1150+
property: "color",
1151+
value: "foo",
1152+
expected: "<color>",
1153+
},
1154+
line: 1,
1155+
column: 57,
1156+
endLine: 1,
1157+
endColumn: 60,
1158+
},
1159+
],
1160+
},
11061161
{
11071162
code: "a { padding: calc(var(--padding-top, 1px) + 1px) 2px calc(var(--padding-bottom) + 1px); }",
11081163
errors: [

0 commit comments

Comments
 (0)