Skip to content

Your app looks AI-generated. Vincent Van Gogh noticed. An agent skill that finds and fixes AI-looking UI and UX in mobile apps, web apps, and websites.

fix(rules): require interactive role and tabIndex for clickable-div exemption (#123)

Papito0849037e16eae34
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 408d05e..123e0a9 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -998,14 +998,16 @@ const rules = [
for (const m of matches(/<(?:div|span|li|td|img|a)\b[^<>]*?\s(?:onClick|@click|v-on:click|on:click)\s*=/, code)) {
const end = tagEnd(code, m.index + m[0].length);
const tag = code.slice(m.index, end === -1 ? undefined : end);
- if (/^<a\b/.test(tag)) {
- // An anchor is a real link only when it has an href; a bare
- // <a onClick> is not focusable and is not announced as a control.
- // The one escape hatch is a button role with a tabIndex, which
- // makes it behave like a button for keyboard and screen readers.
- if (/(?:^|[\s{:[])href\]?\s*=/.test(tag)) continue;
- if (/\brole\s*=\s*["'{]*button/.test(tag) && /\btab[iI]ndex\s*=/.test(tag)) continue;
- } else if (/\brole\s*=|\btab[iI]ndex\s*=/.test(tag)) continue;
+ if (/^<a\b/.test(tag) && /(?:^|[\s{:[])href\]?\s*=/.test(tag)) continue;
+ // A non-semantic clickable element (or href-less anchor) is only
+ // keyboard- and screen-reader-accessible when it has both an
+ // interactive ARIA role and a tabIndex.
+ if (
+ /\brole\s*=\s*["'{`]*\s*(?:button|link|checkbox|menuitem(?:checkbox|radio)?|option|radio|switch|tab|textbox|combobox|gridcell|searchbox|slider|spinbutton|treeitem)\b/.test(tag) &&
+ /\btab[iI]ndex\s*=/.test(tag)
+ ) {
+ continue;
+ }
hits.push(m.index);
}
return hits;
diff --git a/test/detect.test.js b/test/detect.test.js
index 4a57397..57c03aa 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -740,6 +740,23 @@ test("clickable-div flags a href-less anchor with onClick", () => {
assert.deepStrictEqual(findingsFor('<a onClick={go} tabIndex={0}>Go</a>\n').map((f) => f.rule), ["clickable-div"]);
});
+test("clickable-div requires both an interactive role and tabIndex on div/span/li/td/img", () => {
+ const rulesFor = (src, file = "a.tsx") =>
+ scanSource(src, file)
+ .filter((f) => f.rule === "clickable-div")
+ .map((f) => f.rule);
+ assert.deepStrictEqual(rulesFor('<div role="button" onClick={open}>Open</div>\n'), ["clickable-div"]);
+ assert.deepStrictEqual(rulesFor('<div tabIndex={0} onClick={open}>Open</div>\n'), ["clickable-div"]);
+ assert.deepStrictEqual(rulesFor('<div role="presentation" tabIndex={0} onClick={open}>Open</div>\n'), ["clickable-div"]);
+ assert.deepStrictEqual(rulesFor('<div role="none" tabIndex={0} onClick={open}>Open</div>\n'), ["clickable-div"]);
+ assert.deepStrictEqual(rulesFor('<li role="listitem" tabIndex={0} onClick={open}>Open</li>\n'), ["clickable-div"]);
+ assert.deepStrictEqual(rulesFor('<img role="img" tabIndex={0} onClick={open} alt="Open" />\n'), ["clickable-div"]);
+ // Interactive role + tabIndex stays quiet across JSX, Vue, and Svelte.
+ assert.deepStrictEqual(rulesFor('<div role="button" tabIndex={0} onClick={open}>Open</div>\n'), []);
+ assert.deepStrictEqual(rulesFor('<div role="button" :tabindex="0" @click="open">Open</div>\n', "A.vue"), []);
+ assert.deepStrictEqual(rulesFor('<div role="button" tabindex="0" on:click={open}>Open</div>\n', "A.svelte"), []);
+});
+
test("focus-removed flags a bare focus:outline-none, not one with a ring replacement", () => {
const rulesFor = (src) => scanSource(src, "a.tsx").map((f) => f.rule);
assert.deepStrictEqual(rulesFor('<input className="focus:outline-none" />\n'), ["focus-removed"]);