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.
JavaScript accessibilityagent-skillsagent-skills-toolsagentsagentskillsai-slopclaudeclaude-codeclaude-skillsexpoflutterfrontendionicjetpack-composemobilereact-nativeswiftuiuiuxvibe-coding
fix(detect): read only a gradient's own colors in slop-gradient (#83)
Dipesh Sapkota4aaa5c48811c
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.jsindex 7bc108c..ac03fc5 100644--- a/skills/unslop-anything/scripts/detect.js+++ b/skills/unslop-anything/scripts/detect.js@@ -234,6 +234,99 @@ function closeOf(text, open) {return text.length;}+// The part of `rest` (the text after a gradient's constructor) during which+// `variable` still holds that gradient: up to the end of the enclosing block,+// or up to the variable's next assignment. A declaration of the same name in a+// nested block (a helper's own `const g`) shadows it and does not end it.+function bindingRegion(rest, variable) {+ let depth = 0;+ let blockEnd = rest.length;+ for (let i = 0; i < rest.length; i++) {+ if (rest[i] === "{") depth++;+ else if (rest[i] === "}" && --depth < 0) { blockEnd = i; break; }+ }+ const region = rest.slice(0, blockEnd);+ const write = new RegExp(`(?<![\\w$.])(?:(?:const|let|var|val)\\s+)?${variable}\\s*=(?!=)`);+ for (const m of matches(write, region)) {+ const declaredInNestedBlock =+ /^(?:const|let|var|val)\s/.test(m[0]) && blockDepth(region, m.index) > 0;+ if (!declaredInNestedBlock) return region.slice(0, m.index);+ }+ return region;+}++function blockDepth(text, index) {+ let depth = 0;+ for (let i = 0; i < index; i++) {+ if (text[i] === "{") depth++;+ else if (text[i] === "}") depth--;+ }+ return depth;+}++const COLOR_SIGN =/#[0-9a-fA-F]{3,8}\b|0x[0-9a-fA-F]{6,8}\b|\b(?:rgba?|hsla?|oklch|oklab)\(|\bColors?\.\w|\.(?:purple|indigo|pink|blue|red|green|orange|yellow|white|black|gray|grey)\b/;++// The text that holds the colors of the gradient named at `start`, so a color+// after it (the next CSS rule, a child element's style, the next statement)+// is never read as one of its stops (issue #67). Empty when the name is not a+// gradient definition, e.g. a type like GradientDrawable.Orientation.+function gradientScope(text, start, name) {+ // An element: <LinearGradient colors={...}>, <gradient android:startColor=.../>,+ // or an SVG <linearGradient> whose colors sit on its <stop> children. Other+ // children (an expo-linear-gradient's <Text>) are content, not stops.+ if (name.startsWith("<") || text[start - 1] === "<") {+ const end = tagEnd(text, start);+ if (end === -1) return text.slice(start, text.indexOf("\n", start) >>> 0);+ let scope = text.slice(start, end + 1);+ if (text[end - 1] !== "/") {+ const close = text.indexOf(`</${name.replace(/^</, "")}`, end);+ const body = text.slice(end + 1, close === -1 ? text.length : close);+ // tagEnd() skips "{...}", so a ">" in a JSX expression (offset={i > 0 ...})+ // does not cut the tag before its stopColor.+ for (const stop of matches(/<stop\b/i, body)) {+ const stopEnd = tagEnd(body, stop.index);+ scope += body.slice(stop.index, stopEnd === -1 ? body.length : stopEnd + 1);+ }+ }+ return scope;+ }+ // A call: linear-gradient(...), LinearGradient(colors: ...), Brush.linearGradient(...).+ const paren = start + name.length + (text.slice(start + name.length).match(/^\s*/)?.[0].length ?? 0);+ if (text[paren] !== "(") return "";+ const end = closeOf(text, paren);+ let scope = text.slice(start, end);+ if (COLOR_SIGN.test(scope.slice(name.length))) return scope;+ // A constructor that takes no colors (canvas createLinearGradient, Android+ // GradientDrawable()): they come in a Kotlin .apply { } block right after,+ // or in calls on the variable it is assigned to, for as long as that+ // variable holds it (until it is assigned again).+ const apply = /^\s*\.apply\s*\{/.exec(text.slice(end));+ if (apply) scope += text.slice(end, closeOf(text, end + apply[0].length - 1));+ // The assignment may sit on the line above: "const g =\n ctx.create...".+ const assigned = /([A-Za-z_$][\w$]*)\s*=\s*(?:new\s+)?[\w$.]*$/.exec(text.slice(Math.max(0, start - 200), start));+ if (assigned) {+ // Identifier characters only, so "$" is the one character to escape. The+ // lookbehind is the identifier boundary: \b fails before "$g", and a+ // preceding "." would make it another object's member (other.g).+ const variable = assigned[1].replace(/\$/g, "\\$");+ const region = bindingRegion(text.slice(end), variable);+ for (const call of matches(new RegExp(`(?<![\\w$.])${variable}\\.(?:addColorStop|setColors)\\s*\\(|(?<![\\w$.])${variable}\\.colors\\s*=(?!=)`), region)) {+ const after = call.index + call[0].length;+ let tail;+ if (call[0].endsWith("(")) {+ tail = closeOf(region, after - 1);+ } else {+ // `.colors = intArrayOf(...)` may span lines: read the value through+ // its closing bracket, or to the end of the line for a plain value.+ const at = after + /^\s*(?:new\s+)?[\w$.]*\s*/.exec(region.slice(after))[0].length;+ tail = "([{".includes(region[at]) ? closeOf(region, at) : region.indexOf("\n", call.index) >>> 0;+ }+ scope += region.slice(call.index, tail);+ }+ }+ return scope;+}+// ---------- rules ----------// Each rule gets ctx { code, stack, file } and returns indexes into code.@@ -251,8 +344,9 @@ const rules = [for (const m of matches(/(?<!<\/)(?:LinearGradient|linearGradient|RadialGradient|radialGradient|AngularGradient|Brush\.(?:linear|radial|horizontal|vertical|sweep)Gradient|linear-gradient|radial-gradient|GradientDrawable|<gradient\b)/, code)) {const lineStart = code.lastIndexOf("\n", m.index) + 1;if (/^\s*(?:import|export\s+\{|from)\b/.test(code.slice(lineStart, m.index))) continue;- const snippet = code.slice(m.index, m.index + 400);- if (hexColors(snippet, stack).some(isSlopHue) || SLOP_COLOR_NAME.test(snippet.slice(0, 250))) hits.push(m.index);+ // Only the gradient's own stops count, not whatever follows it.+ const stops = gradientScope(code, m.index, m[0]);+ if (hexColors(stops, stack).some(isSlopHue) || SLOP_COLOR_NAME.test(stops)) hits.push(m.index);}// Tailwind: bg-gradient-to-r from-violet-600 to-fuchsia-500for (const m of matches(/["'`][^"'`\n]*\bbg-(?:gradient|linear|radial|conic)-[^"'`\n]*["'`]/, code)) {diff --git a/test/detect.test.js b/test/detect.test.jsindex 6c96332..432ac66 100644--- a/test/detect.test.js+++ b/test/detect.test.js@@ -261,6 +261,57 @@ test("slop-gradient reads Android #AARRGGBB colors", () => {assert.deepStrictEqual(scanSource(scrim, "a.css"), []);});+test("slop-gradient reads only the gradient's own colors (issue #67)", () => {+ const gradientLines = (src, file) =>+ scanSource(src, file).filter((f) => f.rule === "slop-gradient").map((f) => f.line);+ // A purple that is not one of the stops: the next rule, a child's style, the+ // next statement, another prop. None of these gradients is purple.+ const notStops = [+ [".hero { background: linear-gradient(#ffffff, #f4f4f5); }\n.btn { color: #7c3aed; }\n", "a.css"],+ ['<LinearGradient colors={["#ffffff", "#f4f4f5"]}>\n <Text style={{ color: "#7c3aed" }}>Hi</Text>\n</LinearGradient>\n', "a.tsx"],+ ['<div style={{ background: "linear-gradient(#fff, #f4f4f5)", color: "#7c3aed" }} />\n', "a.tsx"],+ ["Rectangle().fill(LinearGradient(colors: [.white, .gray], startPoint: .top, endPoint: .bottom))\n .foregroundStyle(.purple)\n", "a.swift"],+ ["val bg = Brush.verticalGradient(listOf(Color.White, Color.LightGray))\nval accent = Color(0xFF7C3AED)\n", "a.kt"],+ ['const g = ctx.createLinearGradient(0, 0, 200, 0);\ng.addColorStop(0, "#ffffff");\ng.addColorStop(1, "#f4f4f5");\nctx.strokeStyle = "#7c3aed";\n', "a.js"],+ ];+ for (const [src, file] of notStops) assert.deepStrictEqual(gradientLines(src, file), [], src);+ // Purple stops wherever a gradient keeps them stay flagged: in the call, the+ // element's props, SVG <stop> children, or the canvas and Android calls that+ // follow a constructor taking no colors.+ const stops = [+ ['<svg><linearGradient id="g"><stop offset="0" stop-color="#7c3aed"/><stop offset="1" stop-color="#ec4899"/></linearGradient></svg>\n', "a.html"],+ ['<LinearGradient id="g" x1="0" y1="0" x2="1" y2="0">\n <Stop offset="0" stopColor="#7c3aed" />\n</LinearGradient>\n', "a.tsx"],+ ['const g = ctx.createLinearGradient(0, 0, 200, 0);\ng.addColorStop(0, "#7c3aed");\ng.addColorStop(1, "#ec4899");\n', "a.js"],+ ["val d = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, intArrayOf(0xFF7C3AED.toInt(), 0xFFEC4899.toInt()))\n", "a.kt"],+ ["val d = GradientDrawable().apply {\n colors = intArrayOf(0xFF7C3AED.toInt(), 0xFFEC4899.toInt())\n}\n", "a.kt"],+ ["decoration: BoxDecoration(gradient: LinearGradient(colors: [Colors.deepPurple, Colors.pink])),\n", "a.dart"],+ ['<div className="bg-[linear-gradient(90deg,#7c3aed,#ec4899)]" />\n', "a.tsx"],+ ];+ for (const [src, file] of stops) assert.deepStrictEqual(gradientLines(src, file), [1], src);+ // Stops that need the whole expression or the variable's whole lifetime.+ const spread = [+ // A ">" inside a JSX expression does not end the <Stop> tag.+ ['<LinearGradient id="g">\n <Stop offset={i > 0 ? "0" : "1"} stopColor="#7c3aed" />\n</LinearGradient>\n', "a.tsx", [1]],+ // The assignment and the constructor on different lines.+ ['const g =\n ctx.createLinearGradient(0, 0, 1, 0);\ng.addColorStop(0, "#7c3aed");\n', "a.js", [2]],+ // Another gradient built before this one's stops are added.+ ['const a = ctx.createLinearGradient(0, 0, 1, 0);\nconst b = ctx.createLinearGradient(0, 0, 1, 0);\na.addColorStop(0, "#7c3aed");\nb.addColorStop(0, "#ffffff");\n', "a.js", [1]],+ // A colors assignment that spans lines.+ ["val d = GradientDrawable()\nd.colors = intArrayOf(\n 0xFF7C3AED.toInt()\n)\n", "a.kt", [1]],+ // A reassigned variable: each gradient keeps only its own stops.+ ['let g = ctx.createLinearGradient(0, 0, 1, 0);\ng.addColorStop(0, "#ffffff");\ng = ctx.createLinearGradient(0, 0, 1, 0);\ng.addColorStop(0, "#7c3aed");\n', "a.js", [3]],+ // A declaration of the same name in a nested block shadows the variable;+ // it does not end it.+ ['const g = ctx.createLinearGradient(0, 0, 1, 0);\nfunction helper() {\n const g = null;\n}\ng.addColorStop(0, "#7c3aed");\n', "a.js", [1]],+ // The variable ends with its block: a sibling function's own g is another gradient.+ ['function a() {\n const g = ctx.createLinearGradient(0, 0, 1, 0);\n g.addColorStop(0, "#ffffff");\n}\nfunction b() {\n const g = ctx.createLinearGradient(0, 0, 1, 0);\n g.addColorStop(0, "#7c3aed");\n}\n', "a.js", [6]],+ // A "$" identifier, and a member of another object that shares the name.+ ['const $g = ctx.createLinearGradient(0, 0, 1, 0);\n$g.addColorStop(0, "#7c3aed");\n', "a.js", [1]],+ ['const g = ctx.createLinearGradient(0, 0, 1, 0);\ng.addColorStop(0, "#ffffff");\nother.g.addColorStop(0, "#7c3aed");\n', "a.js", []],+ ];+ for (const [src, file, lines] of spread) assert.deepStrictEqual(gradientLines(src, file), lines, src);+});+test("0xRRGGBB colors are read alongside 0xFFRRGGBB", () => {// Issue #42: the common Swift form Color(hex: 0x7C3AED) was missed.const shadow = 'Text("x").shadow(color: Color(hex: 0x7C3AED), radius: 20)\n';