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): only flag gradient brush inside text styles (#94)

Nguyễn Tuấn Anh74433413fc0a
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 56131d8..52b19cb 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -218,6 +218,25 @@ function* matches(re, text) {
}
}
+// True when the `brush =` at `index` sits inside a TextStyle(...) or
+// SpanStyle(...) call: walk backwards matching parens until the innermost
+// still-open `(` is found and check the callee name in front of it.
+function isTextBrush(code, index) {
+ let depth = 0;
+ for (let i = index - 1; i >= 0; i--) {
+ const c = code[i];
+ if (c === ")") depth++;
+ else if (c === "(") {
+ if (depth > 0) depth--;
+ else {
+ const name = /(?:(?:^|[^\w.])(?:TextStyle|SpanStyle)|\.copy)\s*$/.exec(code.slice(0, i));
+ return Boolean(name);
+ }
+ }
+ }
+ return false;
+}
+
// Index just past the bracket that closes the one at `open`.
function closeOf(text, open) {
const pairs = { "(": ")", "{": "}", "[": "]" };
@@ -363,8 +382,16 @@ const rules = [
message: "Gradient-filled text.",
fix: "Emphasize with weight or size from the type scale, in a solid theme color.",
find({ code }) {
- const re = /<MaskedView|\bShaderMask\s*\(|\.foregroundStyle\(\s*(?:LinearGradient|\.linearGradient)|\.foregroundStyle\([^)]*Gradient|brush\s*=\s*Brush\.\w+Gradient|background-clip:\s*text|-webkit-background-clip:\s*text|\bbg-clip-text\b/;
- return [...matches(re, code)].map((m) => m.index);
+ const hits = [];
+ const re = /<MaskedView|\bShaderMask\s*\(|\.foregroundStyle\(\s*(?:LinearGradient|\.linearGradient)|\.foregroundStyle\([^)]*Gradient|background-clip:\s*text|-webkit-background-clip:\s*text|\bbg-clip-text\b/;
+ for (const m of matches(re, code)) hits.push(m.index);
+ // Compose: only a brush passed to TextStyle/SpanStyle is gradient text.
+ // Modifier.background(brush = ...) and Modifier.border(..., brush = ...)
+ // are plain gradient surfaces, not text, so they stay with slop-gradient.
+ for (const m of matches(/brush\s*=\s*Brush\.\w+Gradient/, code)) {
+ if (isTextBrush(code, m.index)) hits.push(m.index);
+ }
+ return hits;
},
},
{
diff --git a/test/detect.test.js b/test/detect.test.js
index 52a3a53..2ff86a7 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -316,6 +316,21 @@ test("slop-gradient reads only the gradient's own colors (issue #67)", () => {
for (const [src, file, lines] of spread) assert.deepStrictEqual(gradientLines(src, file), lines, src);
});
+test("gradient-text flags text brushes only, not gradient backgrounds (issue #66)", () => {
+ // Issue #66: a neutral gradient background has no text in it.
+ const background = "Box(Modifier.background(brush = Brush.verticalGradient(listOf(Color.White, Color.LightGray))))\n";
+ assert.deepStrictEqual(scanSource(background, "a.kt"), []);
+ const border = "Box(Modifier.border(2.dp, brush = Brush.linearGradient(listOf(Color.White, Color.LightGray))))\n";
+ assert.deepStrictEqual(scanSource(border, "a.kt"), []);
+ // A brush passed to text still flags.
+ const textStyle = "Text(\"Hello\", style = TextStyle(brush = Brush.linearGradient(listOf(Color.Red, Color.Blue))))\n";
+ assert.deepStrictEqual(scanSource(textStyle, "a.kt").map((f) => f.rule), ["gradient-text"]);
+ const spanStyle = "buildAnnotatedString { withStyle(SpanStyle(brush = Brush.linearGradient(listOf(Color.Red, Color.Blue)))) { append(\"Hi\") } }\n";
+ assert.deepStrictEqual(scanSource(spanStyle, "a.kt").map((f) => f.rule), ["gradient-text"]);
+ const copied = "Text(\"Hi\", style = MaterialTheme.typography.headlineLarge.copy(brush = Brush.linearGradient(listOf(Color.Red, Color.Blue))))\n";
+ assert.deepStrictEqual(scanSource(copied, "a.kt").map((f) => f.rule), ["gradient-text"]);
+});
+
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';