Skip to content

Commit 0679a9a

Browse files
committed
fix(lint): no-unscoped-has follows Tailwind's & and in-* rules
A variant without & applies to the element itself, so [:has(x)] and not-[:has(x)] are anchored. A wrapper only lends its anchor when the :has() is its subject, so [:has(x)_.foo] and group-[:has(x)_.y] still report, as does in-[...], which matches an ancestor.
1 parent ffd8e73 commit 0679a9a

2 files changed

Lines changed: 66 additions & 8 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
@@ -60,6 +60,11 @@ describe("t3code/no-unscoped-has", () => {
6060
`const className = "data-[foo='_:has(x)']:p-2 [&_[data-query='_:has(foo)']]:p-2";`,
6161
);
6262

63+
rule.valid(
64+
"allows a selector without & on the element itself",
65+
`const className = "[:has(>input)]:p-2 not-[:has(>[data-slot=icon])]:ps-2";`,
66+
);
67+
6368
rule.invalid(
6469
"reports a sibling :has() with nothing anchoring it",
6570
`const className = "[&+:has([data-chat-composer-form])_[data-chat-composer-form]]:before:rounded-none";`,
@@ -100,6 +105,18 @@ describe("t3code/no-unscoped-has", () => {
100105
`const className = "group-[&_:has(x)]/row:p-2";`,
101106
);
102107

108+
rule.invalid(
109+
"reports a :has() on an ancestor of a selector without &",
110+
`const className = "[:has(x)_.foo]:p-2";`,
111+
);
112+
113+
rule.invalid(
114+
"reports a :has() on an ancestor of a group",
115+
`const className = "group-[:has(x)_.y]:p-2";`,
116+
);
117+
118+
rule.invalid("reports an in-* ancestor :has()", `const className = "in-[:has(x)]:p-2";`);
119+
103120
rule.invalid(
104121
"reports :has() anchored to the document root",
105122
`const className = "[body:has([data-dialog-open])_&]:overflow-hidden";`,

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

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const COMBINATOR_PATTERN = /[\s>+~]/u;
66
const NARROWING_PATTERN = /[.#[]|^[a-z]/iu;
77
const ROOT_COMPOUND_PATTERN = /^(?:html|body|:root)(?![\w-])/iu;
88

9-
// group-[...] and peer-[...] match the .group/.peer element; data-[...] and
10-
// aria-[...] hold attribute values, not selectors.
9+
// group-[...] and peer-[...] match the .group/.peer element, in-[...] matches an
10+
// ancestor, and data-[...] and aria-[...] hold attribute values, not selectors.
1111
const GROUP_PREFIX_PATTERN = /(?:^|:)(?:group|peer)-$/u;
12+
const ANCESTOR_PREFIX_PATTERN = /(?:^|:)in-$/u;
1213
const ATTRIBUTE_PREFIX_PATTERN = /(?:^|:)(?:data|aria)-$/u;
1314
const QUOTED_PATTERN = /(["'])(?:\\.|(?!\1).)*\1/gu;
1415

@@ -35,23 +36,62 @@ function variantGroups(token: string): { prefix: string; group: string }[] {
3536
return groups;
3637
}
3738

39+
/** Index of the ")" closing the "(" at `open`, or the selector's length. */
40+
function closingParen(selector: string, open: number): number {
41+
let depth = 0;
42+
for (let index = open; index < selector.length; index++) {
43+
if (selector[index] === "(") depth++;
44+
else if (selector[index] === ")" && --depth === 0) return index;
45+
}
46+
return selector.length;
47+
}
48+
49+
/**
50+
* Whether nothing after `from` in the enclosing branch is joined by a combinator,
51+
* i.e. the element ending at `from` is the subject that its wrapper matches.
52+
*/
53+
function isWrapperSubject(selector: string, from: number): boolean {
54+
let depth = 0;
55+
for (let index = from; index < selector.length; index++) {
56+
const char = selector[index] ?? "";
57+
if (char === "(") depth++;
58+
else if (char === ")") {
59+
if (depth === 0) return true;
60+
depth--;
61+
} else if (depth > 0) continue;
62+
else if (char === ",") return true;
63+
else if (COMBINATOR_PATTERN.test(char)) {
64+
const next = selector.slice(index).trimStart()[0];
65+
if (/\s/u.test(char) && (next === undefined || next === ")" || next === ",")) continue;
66+
return false;
67+
}
68+
}
69+
return true;
70+
}
71+
3872
/** The compound selector each `:has(` in `selector` is attached to. */
3973
function hasCompounds(selector: string): string[] {
4074
const compounds: string[] = [];
4175
let index = selector.indexOf(":has(");
4276
while (index !== -1) {
4377
let compound = "";
4478
let depth = 0;
79+
let subjectEnd = closingParen(selector, index + ":has".length) + 1;
4580
// Other branches of a selector list are skipped until the wrapper that holds
4681
// them opens. An unbalanced "(" means the :has() sits inside :not()/:is()/
47-
// :where(), so the compound outside that wrapper still applies.
82+
// :where(); the compound outside that wrapper applies only when the :has()
83+
// is in the wrapper's subject position.
4884
let skippingBranch = false;
4985
for (let position = index - 1; position >= 0; position--) {
5086
const char = selector[position] ?? "";
5187
if (char === ")") depth++;
5288
else if (char === "(") {
5389
if (depth > 0) depth--;
54-
else skippingBranch = false;
90+
else {
91+
if (!isWrapperSubject(selector, subjectEnd)) break;
92+
skippingBranch = false;
93+
subjectEnd = closingParen(selector, position) + 1;
94+
}
5595
} else if (depth === 0 && char === ",") {
5696
skippingBranch = true;
5797
continue;
@@ -93,12 +133,13 @@ function findUnscopedHasVariants(text: string): string[] {
93133
for (const { prefix, group } of variantGroups(token)) {
94134
if (ATTRIBUTE_PREFIX_PATTERN.test(prefix)) continue;
95135
// Tailwind writes spaces as "_", and "&" is the element carrying the class.
136+
// A selector without "&" applies to that element, as `&:is(...)`.
96137
const relative = group.toLowerCase().replace(QUOTED_PATTERN, '""').replaceAll("_", " ");
97138
const owner = GROUP_PREFIX_PATTERN.test(prefix) ? ".group" : ".self";
98-
const selector = relative.includes("&")
99-
? relative.replaceAll("&", owner)
100-
: owner === ".self"
101-
? relative
139+
const selector = ANCESTOR_PREFIX_PATTERN.test(prefix)
140+
? `:is(${relative.replaceAll("&", "*")}) .self`
141+
: relative.includes("&")
142+
? relative.replaceAll("&", owner)
102143
: `${owner}:is(${relative})`;
103144
const unscoped = hasCompounds(selector).some((compound) => {
104145
const anchor = withoutNegations(compound);

0 commit comments

Comments
 (0)