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.

feat(detect): flag inputs that use a placeholder as their only label (#105)

Nguyễn Tuấn Anhb2dbe1167267
diff --git a/skills/unslop-anything/reference/web.md b/skills/unslop-anything/reference/web.md
index d4a3f3d..74c7485 100644
--- a/skills/unslop-anything/reference/web.md
+++ b/skills/unslop-anything/reference/web.md
@@ -25,7 +25,7 @@ These break the web for real people. They are Critical or Noticeable, whatever t
- **Contrast** 4.5:1 for body text and 3:1 for large text and meaningful icons, in light and dark.
- **Reduced motion.** Every animation has a `prefers-reduced-motion: reduce` fallback. Content is visible without it [`reduced-motion`].
- **No horizontal scroll** at 375 wide, and the layout holds at 1920: the content width is capped and centered, the gutters grow.
-- **Forms** have labels (not placeholders as labels), the right `type` and `autocomplete`, errors next to the field in words, and the entered data kept after an error.
+- **Forms** have labels (not placeholders as labels), the right `type` and `autocomplete`, errors next to the field in words, and the entered data kept after an error [`placeholder-label`].
- **Nothing invented.** No customer counts, logo walls, testimonials, ratings, or case studies the user did not supply [`unverified-claim`, `invented-stat`].
## Web tells
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 4610529..7809eed 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -916,6 +916,48 @@ const rules = [
return hits;
},
},
+ {
+ id: "placeholder-label",
+ family: "accessibility",
+ severity: "noticeable",
+ stacks: ["js", "web"],
+ message: "Input uses a placeholder as its only label.",
+ fix: "Give the field a real label: wrap it in <label>, point a <label htmlFor> at its id, or add aria-label. Placeholders disappear while typing and are not read as labels.",
+ find({ code }) {
+ // Ids a <label for/htmlFor> in this file points to (static strings only).
+ const labelledBy = new Set();
+ const attrValue = (v) => v.replace(/^[{"'\s]+|[}"'\s]+$/g, "");
+ for (const m of matches(/<label\b[^<>]*?\s:?(?:htmlFor|for)\s*=\s*("[^"]+"|'[^']+'|\{[^{}]+\})/, code)) {
+ labelledBy.add(attrValue(m[1]));
+ }
+ // Ranges wrapped in a <label>...</label> pair.
+ const wrapped = [];
+ for (const m of matches(/<label\b[^<>]*>/, code)) {
+ const tag = m[0];
+ if (/\/\s*>$/.test(tag)) continue;
+ const close = code.indexOf("</label>", m.index + tag.length);
+ if (close !== -1) wrapped.push([m.index, close]);
+ }
+ const hits = [];
+ for (const m of matches(/<(?:input|textarea)(?=[\s/>])/, code)) {
+ const end = tagEnd(code, m.index);
+ const tag = code.slice(m.index, end === -1 ? undefined : end);
+ if (!/[\s:]placeholder\s*=/.test(tag)) continue;
+ if (/[\s:]aria-label\s*=|[\s:]aria-labelledby\s*=/.test(tag)) continue;
+ const type = /[\s:]type\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\s*"([^"]*)"\s*\}|\{\s*'([^']*)'\s*\})/.exec(tag);
+ const typeVal = (type?.[1] ?? type?.[2] ?? type?.[3] ?? type?.[4] ?? "").toLowerCase();
+ // Hidden and submit inputs need no label; a search input with
+ // aria-label is already exempt above.
+ if (typeVal === "hidden" || typeVal === "submit") continue;
+ if (/\{\s*\.\.\./.test(tag)) continue;
+ const id = /[\s:]id\s*=\s*("[^"]+"|'[^']+'|\{[^{}]+\})/.exec(tag);
+ if (id && labelledBy.has(attrValue(id[1]))) continue;
+ if (wrapped.some(([start, close]) => m.index > start && m.index < close)) continue;
+ hits.push(m.index);
+ }
+ return hits;
+ },
+ },
{
id: "zoom-disabled",
family: "accessibility",
diff --git a/test/detect.test.js b/test/detect.test.js
index a5ade65..d415fe5 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -579,6 +579,7 @@ test("web slop is caught", () => {
for (const id of [
"slop-gradient", "gradient-text", "tracked-caps-label", "unverified-claim", "img-no-alt", "decorative-pulse",
"clickable-div", "transition-all", "hover-scale", "icon-tile", "focus-removed", "zoom-disabled", "reduced-motion",
+ "placeholder-label",
]) assert.ok(found.has(id), `missing ${id}`);
});
@@ -720,6 +721,29 @@ test("img-no-alt accepts Angular [alt] bindings and Svelte {alt} shorthand", ()
assert.deepStrictEqual(rulesFor("<img {src} />\n", "A.svelte"), ["img-no-alt"]);
});
+test("placeholder-label flags inputs that use a placeholder as their only label", () => {
+ const rulesFor = (src) => scanSource(src, "a.tsx").map((f) => f.rule);
+ // The issue's repro.
+ assert.deepStrictEqual(rulesFor('<input type="email" placeholder="Email" />\n'), ["placeholder-label"]);
+ assert.deepStrictEqual(rulesFor('<textarea placeholder="Tell us more" />\n'), ["placeholder-label"]);
+ // A real label quiets it: aria-label, aria-labelledby, <label for>, wrapping <label>.
+ assert.deepStrictEqual(rulesFor('<input type="email" placeholder="Email" aria-label="Email" />\n'), []);
+ assert.deepStrictEqual(rulesFor('<input placeholder="Email" aria-labelledby="email-label" />\n'), []);
+ assert.deepStrictEqual(rulesFor('<label htmlFor="email">Email</label>\n<input id="email" placeholder="Email" />\n'), []);
+ assert.deepStrictEqual(rulesFor('<label htmlFor={id}>Email</label>\n<input id={id} placeholder="Email" />\n'), []);
+ assert.deepStrictEqual(scanSource('<label :for="fieldId">Email</label>\n<input :id="fieldId" placeholder="Email">\n', "a.vue"), []);
+ assert.deepStrictEqual(rulesFor('<label>Email <input placeholder="Email" /></label>\n'), []);
+ // A label pointing at a different id does not save the field.
+ assert.deepStrictEqual(rulesFor('<label htmlFor="name">Name</label>\n<input id="email" placeholder="Email" />\n'), ["placeholder-label"]);
+ // No placeholder, no finding; hidden and submit inputs need no label.
+ assert.deepStrictEqual(rulesFor('<input type="email" />\n'), []);
+ assert.deepStrictEqual(rulesFor('<input type="hidden" placeholder="token" />\n'), []);
+ assert.deepStrictEqual(rulesFor('<input type="submit" placeholder="Send" />\n'), []);
+ assert.deepStrictEqual(rulesFor('<input type="search" placeholder="Search" aria-label="Search" />\n'), []);
+ // An arrow function in an attribute does not end the tag early.
+ assert.deepStrictEqual(rulesFor('<input placeholder="Email" onChange={() => setEmail(v)} />\n'), ["placeholder-label"]);
+});
+
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"]);