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(detect): flag href-less anchors with onClick as clickable-div (#90)

Nguyễn Tuấn Anhac0330c9c513
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 801275c..142bd19 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -799,10 +799,17 @@ const rules = [
fix: "Use a <button> for actions and an <a href> for navigation. They come with focus, Enter and Space, and the right role.",
find({ code }) {
const hits = [];
- for (const m of matches(/<(?:div|span|li|td|img)\b[^<>]*?\s(?:onClick|@click|v-on:click|on:click)\s*=/, code)) {
+ 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 (/\brole\s*=|\btab[iI]ndex\s*=/.test(tag)) continue;
+ 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;
hits.push(m.index);
}
return hits;
diff --git a/test/detect.test.js b/test/detect.test.js
index 6ddb527..4c22532 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -494,6 +494,24 @@ test("img-no-alt flags Next.js <Image> without alt, and ignores <ImageGallery>",
assert.deepStrictEqual(rulesFor("<Image source={logo} accessibilityLabel=\"Logo\" />\n"), []);
});
+test("clickable-div flags a href-less anchor with onClick", () => {
+ const findingsFor = (src) => scanSource(src, "a.tsx");
+ const src = [
+ "<a onClick={go}>Go</a>",
+ "<span onClick={go}>Go</span>",
+ '<a href="/docs" onClick={track}>Docs</a>',
+ "",
+ ].join("\n");
+ const flagged = findingsFor(src).filter((f) => f.rule === "clickable-div").map((f) => f.line);
+ assert.deepStrictEqual(flagged, [1, 2]);
+ assert.deepStrictEqual(scanSource('<a :href="url" @click="track">Docs</a>\n', "a.vue"), []);
+ // A button role with a tabIndex is the one escape hatch for anchors...
+ assert.deepStrictEqual(findingsFor('<a onClick={go} role="button" tabIndex={0}>Go</a>\n'), []);
+ // ...but role alone or tabIndex alone does not excuse it.
+ assert.deepStrictEqual(findingsFor('<a onClick={go} role="button">Go</a>\n').map((f) => f.rule), ["clickable-div"]);
+ assert.deepStrictEqual(findingsFor('<a onClick={go} tabIndex={0}>Go</a>\n').map((f) => f.rule), ["clickable-div"]);
+});
+
test("unverified-claim allows one adjective between the number and the noun", () => {
const rulesFor = (src) => scanSource(src, "a.tsx").map((f) => f.rule);
assert.deepStrictEqual(rulesFor("<p>Join 10,000+ customers</p>\n"), ["unverified-claim"]);