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): skip attribute expressions when finding the end of a tag (#19)

Ming97d26748285a
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index a1d3f6a..891f1b1 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -184,6 +184,19 @@ function lineAt(starts, index) {
return lo + 1;
}
+// Index of the ">" that closes the tag, skipping any "{...}" attribute expression so an arrow
+// function (=>) or a comparison inside it does not end the tag early. -1 when nothing closes it.
+function tagEnd(text, from) {
+ let depth = 0;
+ for (let i = from; i < text.length; i++) {
+ const c = text[i];
+ if (c === "{") depth++;
+ else if (c === "}" && depth > 0) depth--;
+ else if (c === ">" && depth === 0) return i;
+ }
+ return text.indexOf(">", from);
+}
+
function* matches(re, text) {
const g = new RegExp(re.source, re.flags.includes("g") ? re.flags : re.flags + "g");
let m;
@@ -666,8 +679,8 @@ const rules = [
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)) {
- const tagEnd = code.indexOf(">", m.index + m[0].length);
- const tag = code.slice(m.index, tagEnd === -1 ? undefined : tagEnd);
+ 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;
hits.push(m.index);
}
@@ -705,7 +718,7 @@ const rules = [
find({ code }) {
const hits = [];
for (const m of matches(/<img(?=[\s/>])/, code)) {
- const end = code.indexOf(">", m.index);
+ const end = tagEnd(code, m.index);
const tag = code.slice(m.index, end === -1 ? undefined : end);
if (!/[\s:]alt\s*=|\{\s*\.\.\./.test(tag)) hits.push(m.index);
}
diff --git a/test/detect.test.js b/test/detect.test.js
index 13a6772..357075e 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -244,6 +244,15 @@ test("the installer replaces a skill folder from before the rename", () => {
}
});
+test("an arrow function in an attribute does not end the tag for clickable-div and img-no-alt", () => {
+ const rulesFor = (src) => scanSource(src, "a.tsx").map((f) => f.rule);
+ assert.deepStrictEqual(rulesFor('<div onClick={() => go()} role="button" tabIndex={0}>x</div>\n'), []);
+ assert.deepStrictEqual(rulesFor('<img src={src} onError={() => setBroken(true)} alt="Avatar" />\n'), []);
+ // Still reported when the attribute really is missing.
+ assert.deepStrictEqual(rulesFor("<div onClick={() => go()}>x</div>\n"), ["clickable-div"]);
+ assert.deepStrictEqual(rulesFor("<img src={src} onError={() => setBroken(true)} />\n"), ["img-no-alt"]);
+});
+
test("the installer rejects an unknown flag before it copies anything", () => {
const fs = require("fs");
const os = require("os");