Skip to content

Commit 5c42d99

Browse files
committed
fix(lint): no-unscoped-has ignores values, negated anchors and case
Only bracket groups used as variants are checked, so text like content-[':has(x)'] no longer reports. A class inside :not() no longer counts as an anchor, and :HAS() is matched case-insensitively.
1 parent f001ecd commit 5c42d99

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

‎oxlint-plugin-t3code/rules/no-unscoped-has.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ describe("t3code/no-unscoped-has", () => {
3535

3636
rule.valid("ignores prose mentioning :has()", `const note = "uses :has( for styling";`);
3737

38+
rule.valid(
39+
"ignores :has() inside an arbitrary value",
40+
`const className = "before:content-[':has(foo)']";`,
41+
);
42+
43+
rule.valid(
44+
"allows a negated :has() on the element itself",
45+
`const className = "[&:not(.collapsed):not(:has(>[data-slot=icon]))]:ps-2";`,
46+
);
47+
3848
rule.invalid(
3949
"reports a sibling :has() with nothing anchoring it",
4050
`const className = "[&+:has([data-chat-composer-form])_[data-chat-composer-form]]:before:rounded-none";`,
@@ -53,6 +63,13 @@ describe("t3code/no-unscoped-has", () => {
5363
"const className = `flex [*:has([data-open])_&]:hidden`;",
5464
);
5565

66+
rule.invalid(
67+
"reports :has() whose only anchor is negated",
68+
`const className = "[*:not(.safe):has([data-open])_&]:hidden";`,
69+
);
70+
71+
rule.invalid("reports uppercase :HAS()", `const className = "[*:HAS([data-open])_&]:hidden";`);
72+
5673
rule.invalid(
5774
"reports :has() anchored to the document root",
5875
`const className = "[body:has([data-dialog-open])_&]:overflow-hidden";`,

‎oxlint-plugin-t3code/rules/no-unscoped-has.ts‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { defineRule } from "@oxlint/plugins";
22

33
const COMBINATOR_PATTERN = /[\s>+~]/u;
4-
// A class, id, attribute, or tag at the start of the compound narrows it.
4+
// A class, id, attribute, or leading tag narrows a compound. Negated ones don't,
5+
// so `:not(...)` is removed before this check.
56
const NARROWING_PATTERN = /[.#[]|^[a-z]/iu;
67
const ROOT_COMPOUND_PATTERN = /^(?:html|body|:root)(?![\w-])/iu;
78

8-
/** Top-level `[...]` groups in a class token, i.e. Tailwind arbitrary variants and values. */
9-
function bracketGroups(token: string): string[] {
9+
/** Tailwind arbitrary variants in a class token: top-level `[...]` groups followed by `:`. */
10+
function variantGroups(token: string): string[] {
1011
const groups: string[] = [];
1112
let depth = 0;
1213
let start = -1;
@@ -17,7 +18,7 @@ function bracketGroups(token: string): string[] {
1718
depth++;
1819
} else if (char === "]" && depth > 0) {
1920
depth--;
20-
if (depth === 0) groups.push(token.slice(start, index));
21+
if (depth === 0 && token[index + 1] === ":") groups.push(token.slice(start, index));
2122
}
2223
}
2324
return groups;
@@ -45,18 +46,39 @@ function hasCompounds(selector: string): string[] {
4546
return compounds;
4647
}
4748

49+
/** `compound` without any `:not(...)`, including one left open around the `:has()`. */
50+
function withoutNegations(compound: string): string {
51+
let result = "";
52+
let index = 0;
53+
while (index < compound.length) {
54+
if (!compound.startsWith(":not(", index)) {
55+
result += compound[index];
56+
index++;
57+
continue;
58+
}
59+
let depth = 0;
60+
for (index += ":not".length; index < compound.length; index++) {
61+
if (compound[index] === "(") depth++;
62+
else if (compound[index] === ")" && --depth === 0) break;
63+
}
64+
index++;
65+
}
66+
return result;
67+
}
68+
4869
/** Arbitrary variants in `text` whose `:has()` is unanchored or anchored to the document root. */
4970
function findUnscopedHasVariants(text: string): string[] {
50-
if (!text.includes(":has(")) return [];
71+
// Selectors are ASCII case-insensitive.
72+
if (!text.toLowerCase().includes(":has(")) return [];
5173
const offenders: string[] = [];
5274
for (const token of text.split(/\s+/u)) {
53-
for (const group of bracketGroups(token)) {
54-
if (!group.includes(":has(")) continue;
75+
for (const group of variantGroups(token)) {
5576
// Tailwind writes spaces as "_", and "&" is the element carrying the class.
56-
const selector = group.replaceAll("_", " ").replaceAll("&", ".self");
57-
const unscoped = hasCompounds(selector).some(
58-
(compound) => !NARROWING_PATTERN.test(compound) || ROOT_COMPOUND_PATTERN.test(compound),
59-
);
77+
const selector = group.toLowerCase().replaceAll("_", " ").replaceAll("&", ".self");
78+
const unscoped = hasCompounds(selector).some((compound) => {
79+
const anchor = withoutNegations(compound);
80+
return !NARROWING_PATTERN.test(anchor) || ROOT_COMPOUND_PATTERN.test(anchor);
81+
});
6082
if (unscoped) offenders.push(`[${group}]`);
6183
}
6284
}

0 commit comments

Comments
 (0)