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): ignore alt inside quoted attribute values in img-no-alt (#277)

Papito084ee2a025fe3b0
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 59b725d..515e2da 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -1461,7 +1461,7 @@ const rules = [
const tag = code.slice(m.index, end === -1 ? undefined : end);
if ((tag.startsWith("<Image") || tag.startsWith("<Picture")) && !/\ssrc\s*=/.test(tag)) continue;
const hasAlt = /[\s:\[.]alt\]?\s*=|\{\s*alt\s*\}|\{\s*\.\.\./.test(tag)
- || (stack === "web" && /\salt(?=[\s/>]|$)/.test(tag));
+ || (stack === "web" && /\salt(?=[\s/>]|$)/.test(tag.replace(/=\s*(?:"[^"]*"|'[^']*')/g, '=""')));
if (!hasAlt) hits.push(m.index);
}
return hits;
diff --git a/test/detect.test.js b/test/detect.test.js
index 2f84723..5fd9229 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -1457,6 +1457,26 @@ test("img-no-alt accepts Angular [alt] bindings and Svelte {alt} shorthand", ()
assert.deepStrictEqual(rulesFor("<img {src} />\n", "A.svelte"), ["img-no-alt"]);
});
+test("img-no-alt ignores the word alt inside other attribute values (issue #262)", () => {
+ const rulesFor = (src, file) => scanSource(src, file, { kind: "web" }).map((f) => f.rule);
+ // Issue #262 repro:
+ assert.deepStrictEqual(rulesFor('<img src="/hero.png" class="card alt wide" />\n', "Gallery.html"), ["img-no-alt"]);
+ assert.deepStrictEqual(rulesFor('<img src="/team.png" title="Use alt text" />\n', "Gallery.html"), ["img-no-alt"]);
+ assert.deepStrictEqual(rulesFor('<img src="/team.png" title=\'Use alt text\' />\n', "Gallery.html"), ["img-no-alt"]);
+ // Quiet cases: real bare alt, alt="", alt="Team", bound :alt, [alt], {alt}
+ assert.deepStrictEqual(rulesFor('<img src="/a.png" alt>\n', "Gallery.html"), []);
+ assert.deepStrictEqual(rulesFor('<img src="/a.png" alt="" />\n', "Gallery.html"), []);
+ assert.deepStrictEqual(rulesFor('<img src="/a.png" alt="Team" />\n', "Gallery.html"), []);
+ assert.deepStrictEqual(rulesFor('<img :src="u" :alt="n" />\n', "Gallery.vue"), []);
+ assert.deepStrictEqual(rulesFor('<img [src]="u" [alt]="n" />\n', "Gallery.component.html"), []);
+ assert.deepStrictEqual(rulesFor('<img {src} {alt} />\n', "Gallery.svelte"), []);
+ // A real alt alongside class="card alt wide" stays quiet
+ assert.deepStrictEqual(rulesFor('<img src="/hero.png" class="card alt wide" alt="Hero" />\n', "Gallery.html"), []);
+ assert.deepStrictEqual(rulesFor('<img src="/hero.png" alt class="card alt wide" />\n', "Gallery.html"), []);
+ assert.deepStrictEqual(rulesFor('<img src="/hero.png" class = "card alt wide" />\n', "Gallery.html"), ["img-no-alt"]);
+ assert.deepStrictEqual(rulesFor("<img src={src} title={`Bob's photo`} alt class='thumb' />\n", "Thumb.svelte"), []);
+});
+
test("img-no-alt flags Nuxt Image's NuxtImg and NuxtPicture without alt, also kebab-case", () => {
const altLines = (src) => scanSource(src, "Hero.vue", { kind: "web" }).filter((f) => f.rule === "img-no-alt").map((f) => f.line);
// Issue #203: the repro.