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): catch SwiftUI trailing-closure Button labels in vague-button (#185)

Onur Ugura90b8ffdc265
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index a9c293a..41a3ee0 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -1572,17 +1572,42 @@ const rules = [
}
// Native buttons: the label only counts inside a button call, so a
// bare Text("Submit") used as a heading or field label stays quiet.
- // Button("Submit") { save() } (Swift), Button(onClick = {}) {
- // Text("Submit") } (Compose trailing lambda), ElevatedButton(...,
+ // Button("Submit") { save() } or Button { save() } label: { Text("Submit") } (Swift),
+ // Button(onClick = {}) { Text("Submit") } (Compose trailing lambda), ElevatedButton(...,
// child: Text("Submit")) (Flutter). Only the first argument or a
- // Text(...) label counts, so a vague string in the action code does not.
- const label = /^\(\s*["'](?:Submit|Click here|Tap here)["']|\bText\(\s*["'](?:Submit|Click here|Tap here)["']/i;
+ // Text(...)/Label(...) label counts, so a vague string in the action code does not.
+ const isVagueText = (s) => /^(?:submit|click here|tap here)$/i.test(s);
+ const hasVagueLabel = (slice) => {
+ const first = /^\(\s*["']([^"']+)["']/.exec(slice);
+ if (first && isVagueText(first[1])) return true;
+ for (const t of matches(/\b(?:Text|Label)\(\s*["']([^"']+)["']/, slice)) {
+ if (isVagueText(t[1])) return true;
+ }
+ return false;
+ };
for (const m of matches(/\b(?:ElevatedButton|TextButton|OutlinedButton|FilledButton|Button)\s*\(/, code)) {
const open = m.index + m[0].length - 1;
let end = closeOf(code, open);
const lambda = /^\s*\{/.exec(code.slice(end));
if (lambda) end = closeOf(code, end + lambda[0].length - 1);
- if (label.test(code.slice(open, end))) hits.push(m.index);
+ const trailingLabel = /^\s*label\s*:\s*\{/.exec(code.slice(end));
+ if (trailingLabel) {
+ const labelOpen = end + trailingLabel[0].length - 1;
+ const labelEnd = closeOf(code, labelOpen);
+ if (hasVagueLabel(code.slice(labelOpen, labelEnd))) hits.push(m.index);
+ } else if (hasVagueLabel(code.slice(open, end))) {
+ hits.push(m.index);
+ }
+ }
+ // SwiftUI multiple-trailing-closure form: Button { save() } label: { Text("Submit") }.
+ for (const m of matches(/\bButton\s*\{/, code)) {
+ const actionOpen = m.index + m[0].length - 1;
+ const actionEnd = closeOf(code, actionOpen);
+ const trailingLabel = /^\s*label\s*:\s*\{/.exec(code.slice(actionEnd));
+ if (!trailingLabel) continue;
+ const labelOpen = actionEnd + trailingLabel[0].length - 1;
+ const labelEnd = closeOf(code, labelOpen);
+ if (hasVagueLabel(code.slice(labelOpen, labelEnd))) hits.push(m.index);
}
return hits;
},
diff --git a/test/detect.test.js b/test/detect.test.js
index 751e0b5..d6cddb5 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -486,6 +486,21 @@ test("vague-button catches Swift, Compose and Flutter buttons", () => {
assert.deepStrictEqual(scanSource('Text("Save address")', "a.kt"), []);
});
+test("vague-button catches SwiftUI trailing-closure Button { ... } label: { ... }", () => {
+ const lines = (src) => scanSource(src, "a.swift").filter((f) => f.rule === "vague-button").map((f) => f.line);
+ assert.deepStrictEqual(lines('Button {\n save()\n} label: {\n Text("Submit")\n}\n'), [1]);
+ assert.deepStrictEqual(lines('Button {\n save()\n} label: {\n HStack { Image(systemName: "arrow.right"); Text("Click here") }\n}\n'), [1]);
+ assert.deepStrictEqual(lines('Button {\n save()\n} label: {\n Label("Tap here", systemImage: "hand.tap")\n}\n'), [1]);
+ assert.deepStrictEqual(lines('Button(role: .destructive) {\n deleteAll()\n} label: {\n Text("Submit")\n}\n'), [1]);
+ // Specific labels stay quiet even when the action closure mentions a vague word.
+ assert.deepStrictEqual(lines('Button {\n analytics.track("Submit")\n let unused = Text("Submit")\n} label: {\n Text("Save address")\n}\n'), []);
+ assert.deepStrictEqual(lines('Button(role: .destructive) {\n log("Submit")\n} label: {\n Text("Delete account")\n}\n'), []);
+ // A type named Button without a trailing label closure is not a SwiftUI button call,
+ // and a lowercase helper like label("Submit") is not SwiftUI's Label view.
+ assert.deepStrictEqual(lines('struct Button {\n let title = Text("Submit")\n}\n'), []);
+ assert.deepStrictEqual(lines('Button("Save") { label("Submit") }\n'), []);
+});
+
test("vague-button reads the value of a submit or button input (issue #145)", () => {
const lines = (src, file = "form.html") => scanSource(src, file).filter((f) => f.rule === "vague-button").map((f) => f.line);
assert.deepStrictEqual(lines('<form>\n <input type="submit" value="Submit">\n</form>\n'), [2]);