Skip to content

Commit c123f6e

Browse files
authored
feat: make var() regex multiline-safe in no-invalid-properties (#242)
* fix: make var() regex multiline-safe * add test
1 parent 5af3a5a commit c123f6e

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/rules/no-invalid-properties.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ import { isSyntaxMatchError, isSyntaxReferenceError } from "../util.js";
2828
/**
2929
* Regex to match var() functional notation with optional fallback.
3030
*/
31-
// eslint-disable-next-line regexp/no-super-linear-backtracking -- TODO: fix \s*(.+) to match newline characters
32-
const varFunctionPattern = /var\(\s*(--[^,\s)]+)\s*(?:,\s*(.+))?\)/iu;
31+
const varFunctionPattern = /var\(\s*(--[^,\s)]+)\s*(?:,([\s\S]+))?\)/iu;
3332

3433
/**
3534
* Parses a var() function text and extracts the custom property name and fallback.

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ ruleTester.run("no-invalid-properties", rule, {
7878
":root { --MY-COLOR: red; }\na { color: var(--my-color, blue) }",
7979
":root { --FALLBACK-COLOR: blue; }\na { color: var(--MY-COLOR, var(--FALLBACK-COLOR)) }",
8080
":root { --fallback-color: blue; }\na { color: VAR(--MY-COLOR, VaR(--fallback-color)) }",
81+
"a { color: var(--my-color,\n red) }",
82+
":root { --my-color: red; }\na { color: var(--my-color,\n blue) }",
83+
"a { color: var(--x,\n var(--y,\n blue\n )\n) }",
84+
"a { color: var(--x,\n red ) }",
85+
":root { --a: var(--b,\n 10px\n); } a { padding: var(--a); }",
8186
{
8287
code: "a { my-custom-color: red; }",
8388
languageOptions: {
@@ -975,6 +980,41 @@ ruleTester.run("no-invalid-properties", rule, {
975980
},
976981
],
977982
},
983+
{
984+
code: ":root { --a: var(--b,\n red); }\na { padding-top: var(--a); }",
985+
errors: [
986+
{
987+
messageId: "invalidPropertyValue",
988+
data: {
989+
property: "padding-top",
990+
value: "red",
991+
expected: "<length-percentage [0,∞]>",
992+
},
993+
line: 3,
994+
column: 18,
995+
endLine: 3,
996+
endColumn: 26,
997+
},
998+
],
999+
},
1000+
{
1001+
code: "a { padding-top: var(--a,\nvar(--b, red\n)\n); }",
1002+
options: [{ allowUnknownVariables: true }],
1003+
errors: [
1004+
{
1005+
messageId: "invalidPropertyValue",
1006+
data: {
1007+
property: "padding-top",
1008+
value: "red",
1009+
expected: "<length-percentage [0,∞]>",
1010+
},
1011+
line: 1,
1012+
column: 18,
1013+
endLine: 4,
1014+
endColumn: 2,
1015+
},
1016+
],
1017+
},
9781018
{
9791019
code: ":root { --a: var(--b, red); }\na { padding-top: var(--a); }",
9801020
errors: [

0 commit comments

Comments
 (0)