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): recognize multi-line background receivers in safe-area-off (#182)

Onur Ugurc1697c1411d3
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 801b0b9..a7002b0 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -645,6 +645,136 @@ function macOnlySwift(code) {
return appKit;
}
+function matchOpenBackward(code, closeIdx, openCh, closeCh) {
+ let depth = 0;
+ for (let i = closeIdx; i >= 0; i--) {
+ const c = code[i];
+ if (c === '"') {
+ i--;
+ while (i >= 0) {
+ if (code[i] === '"') {
+ let slashes = 0;
+ while (i - 1 - slashes >= 0 && code[i - 1 - slashes] === "\\") slashes++;
+ if (slashes % 2 === 0) break;
+ }
+ i--;
+ }
+ continue;
+ }
+ if (c === closeCh) depth++;
+ else if (c === openCh && --depth === 0) return i;
+ }
+ return -1;
+}
+
+// True when the `.ignoresSafeArea` / `.edgesIgnoringSafeArea` at `index` sits
+// on a background/decorative view (even across a multi-line call or modifier
+// chain) or directly inside a `.background(...)` / `.background { ... }`.
+function isBackgroundSafeArea(code, index) {
+ const BG_CONSTRUCTOR = /^(?:Color|\w*Image|Rectangle|Divider|\w*Gradient)$/;
+ // 1) Directly inside a still-open `.background(` or `.background {`.
+ const limit = Math.max(0, index - 4000);
+ let pDepth = 0;
+ let bDepth = 0;
+ for (let i = index - 1; i >= limit; i--) {
+ const c = code[i];
+ if (c === '"') {
+ i--;
+ while (i >= limit) {
+ if (code[i] === '"') {
+ let slashes = 0;
+ while (i - 1 - slashes >= 0 && code[i - 1 - slashes] === "\\") slashes++;
+ if (slashes % 2 === 0) break;
+ }
+ i--;
+ }
+ continue;
+ }
+ if (c === ")") pDepth++;
+ else if (c === "(") {
+ if (pDepth > 0) pDepth--;
+ else {
+ if (/\.background\s*$/.test(code.slice(Math.max(0, i - 32), i))) return true;
+ break;
+ }
+ } else if (c === "}") bDepth++;
+ else if (c === "{") {
+ if (bDepth > 0) bDepth--;
+ else {
+ let head = i - 1;
+ while (head >= 0 && /\s/.test(code[head])) head--;
+ if (head >= 0 && code[head] === ")") {
+ const open = matchOpenBackward(code, head, "(", ")");
+ if (open !== -1) head = open - 1;
+ }
+ if (/\.background\s*$/.test(code.slice(Math.max(0, head - 31), head + 1))) return true;
+ break;
+ }
+ }
+ }
+ // 2) Walk backwards through the receiver's modifier chain to its root view.
+ let i = index - 1;
+ while (i >= 0) {
+ while (i >= 0 && /[\s?!]/.test(code[i])) i--;
+ if (i < 0) return false;
+ if (code[i] === ")") {
+ const open = matchOpenBackward(code, i, "(", ")");
+ if (open === -1) return false;
+ let end = open - 1;
+ while (end >= 0 && /\s/.test(code[end])) end--;
+ let start = end;
+ while (start >= 0 && /\w/.test(code[start])) start--;
+ const name = code.slice(start + 1, end + 1);
+ if (BG_CONSTRUCTOR.test(name)) return true;
+ let dot = start;
+ while (dot >= 0 && /\s/.test(code[dot])) dot--;
+ if (dot >= 0 && code[dot] === ".") {
+ i = dot - 1;
+ continue;
+ }
+ return /background|Gradient/.test(name);
+ }
+ if (code[i] === "}") {
+ const open = matchOpenBackward(code, i, "{", "}");
+ if (open === -1) return false;
+ let head = open - 1;
+ while (head >= 0 && /\s/.test(code[head])) head--;
+ if (head >= 0 && code[head] === ")") {
+ const pOpen = matchOpenBackward(code, head, "(", ")");
+ if (pOpen === -1) return false;
+ head = pOpen - 1;
+ while (head >= 0 && /\s/.test(code[head])) head--;
+ }
+ let start = head;
+ while (start >= 0 && /\w/.test(code[start])) start--;
+ let dot = start;
+ while (dot >= 0 && /\s/.test(code[dot])) dot--;
+ if (dot >= 0 && code[dot] === ".") {
+ i = dot - 1;
+ continue;
+ }
+ return false;
+ }
+ if (/\w/.test(code[i])) {
+ let cur = i;
+ while (cur >= 0 && /\w/.test(code[cur])) {
+ let start = cur;
+ while (start >= 0 && /\w/.test(code[start])) start--;
+ const name = code.slice(start + 1, cur + 1);
+ if (BG_CONSTRUCTOR.test(name) || /background|Gradient/.test(name)) return true;
+ let dot = start;
+ while (dot >= 0 && /\s/.test(code[dot])) dot--;
+ if (dot < 0 || code[dot] !== ".") break;
+ cur = dot - 1;
+ while (cur >= 0 && /[\s?!]/.test(code[cur])) cur--;
+ }
+ return false;
+ }
+ return false;
+ }
+ return false;
+}
+
function glowLayers(list) {
const layers = [];
let depth = 0;
@@ -1050,8 +1180,7 @@ const rules = [
find({ code }) {
const hits = [];
for (const m of matches(/\.ignoresSafeArea\(\s*(?:\.(?:all|container)\s*)?(?:,?\s*edges:\s*\.all\s*)?\)|\.edgesIgnoringSafeArea\(\s*\.all\s*\)/, code)) {
- const lineStart = code.lastIndexOf("\n", m.index) + 1;
- if (/background|Color\(|Color\.|Image\(|Gradient|Rectangle\(\)|Divider\(\)/.test(code.slice(lineStart, m.index))) continue;
+ if (isBackgroundSafeArea(code, m.index)) continue;
hits.push(m.index);
}
return hits;
diff --git a/test/detect.test.js b/test/detect.test.js
index 821e4fb..77e3608 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -90,6 +90,35 @@ test("safe-area-off flags .ignoresSafeArea(.all) forms, not backgrounds", () =>
assert.deepStrictEqual(scanSource("VStack { }.ignoresSafeArea(edges: .top)", "a.swift"), []);
});
+test("safe-area-off skips multi-line background receivers without hiding container views", () => {
+ const lines = (src) => scanSource(src, "a.swift").filter((f) => f.rule === "safe-area-off").map((f) => f.line);
+ const quiet = [
+ "ZStack {\n Color.black\n .ignoresSafeArea()\n}\n",
+ "Color.black\n .opacity(0.8)\n .ignoresSafeArea()\n",
+ "Color.black\n .edgesIgnoringSafeArea(.all)\n",
+ "LinearGradient(\n colors: [.black, .clear],\n startPoint: .top,\n endPoint: .bottom\n)\n .ignoresSafeArea()\n",
+ "VStack {\n Text(\"Hi\")\n}\n.background(LinearGradient(colors: [.white, .gray],\n startPoint: .center, endPoint: .bottom).ignoresSafeArea())\n",
+ "VStack {\n Text(\"Hi\")\n}\n.background {\n backdrop\n .ignoresSafeArea()\n}\n",
+ "Image(\"hero\")\n .resizable()\n .scaledToFill()\n .ignoresSafeArea()\n",
+ "Rectangle()\n .fill(.black)\n .ignoresSafeArea()\n",
+ "SwiftUI.Color.black\n .ignoresSafeArea()\n",
+ "AsyncImage(url: url)\n .ignoresSafeArea()\n",
+ "KFImage(url)\n .resizable()\n .ignoresSafeArea()\n",
+ "Color.theme.accent\n .ignoresSafeArea()\n",
+ "Color.brand.primary\n .opacity(0.5)\n .ignoresSafeArea()\n",
+ "heroGradient\n .ignoresSafeArea()\n",
+ "backgroundLayer()\n .ignoresSafeArea()\n",
+ "backgroundImage?.resizable()\n .ignoresSafeArea()\n",
+ ];
+ for (const src of quiet) assert.deepStrictEqual(lines(src), [], src);
+ // Real container views still flag, even when they contain a Color or follow a .background modifier.
+ assert.deepStrictEqual(lines("VStack {\n Color.red\n Text(\"Content\")\n}\n.ignoresSafeArea()\n"), [5]);
+ assert.deepStrictEqual(lines("VStack {\n Text(\"Content\")\n}\n.background(Color.white)\n.ignoresSafeArea()\n"), [5]);
+ assert.deepStrictEqual(lines("VStack {\n Text(\"Content\")\n}\n.background { Color.white }\n.ignoresSafeArea()\n"), [5]);
+ assert.deepStrictEqual(lines("Text(\"Hi\").foregroundStyle(Color.red).ignoresSafeArea()\n"), [1]);
+ assert.deepStrictEqual(lines("ZStack {\n Color.black\n .ignoresSafeArea()\n VStack {\n Text(\"Hi\")\n }\n .ignoresSafeArea()\n}\n"), [7]);
+});
+
test("fixed-text-size skips Swift files that build for macOS only", () => {
const rulesOf = (src) => scanSource(src, "a.swift").filter((f) => f.rule === "fixed-text-size").map((f) => f.line);
const text = 'Text("Hi").font(.system(size: 13))\n';