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): use tagEnd when parsing JSX card tags in nested-cards (#183)

Onur Ugurc791fac45af0
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index a7002b0..4f39e10 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -959,12 +959,14 @@ const rules = [
find({ code, stack }) {
const hits = [];
if (stack === "js" || stack === "web") {
- for (const m of matches(/<(Card|ion-card)(?=[\s/>])[^>]*?(\/?)>/, code)) {
- if (m[2] === "/") continue;
+ for (const m of matches(/<(Card|ion-card)(?=[\s/>])/, code)) {
+ const end = tagEnd(code, m.index + m[0].length);
+ if (end === -1) continue;
+ if (/\/\s*$/.test(code.slice(m.index + m[0].length, end))) continue;
const tag = m[1];
- const close = code.indexOf(`</${tag}>`, m.index);
+ const close = code.indexOf(`</${tag}>`, end + 1);
if (close === -1) continue;
- const inner = code.slice(m.index + m[0].length, close);
+ const inner = code.slice(end + 1, close);
if (new RegExp(`<${tag}(?=[\\s/>])`).test(inner)) hits.push(m.index);
}
} else {
diff --git a/test/detect.test.js b/test/detect.test.js
index 77e3608..b427418 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -1156,6 +1156,18 @@ test("nested-cards ignores dotted subcomponents and ion-card parts, still flags
assert.deepStrictEqual(rulesFor("<ion-card><ion-card></ion-card></ion-card>", "a.html"), ["nested-cards"]);
});
+test("nested-cards reads opening tags with tagEnd so arrows or > in props do not misread sibling cards", () => {
+ const rulesFor = (src, file = "a.tsx") => scanSource(src, file).filter((f) => f.rule === "nested-cards").map((f) => f.rule);
+ assert.deepStrictEqual(rulesFor("<Card onClick={() => open()} />\n<Card><p>Second</p></Card>\n"), []);
+ assert.deepStrictEqual(rulesFor('<Card title="a > b" />\n<Card><p>Second</p></Card>\n'), []);
+ assert.deepStrictEqual(rulesFor("<Card disabled={count > 0} />\n<Card><p>Second</p></Card>\n"), []);
+ // Real nested cards with arrows or nested JSX in props still flag.
+ assert.deepStrictEqual(rulesFor("<Card onClick={() => open()}>\n <Card><p>Inner</p></Card>\n</Card>\n"), ["nested-cards"]);
+ assert.deepStrictEqual(rulesFor("<Card onClick={() => open()}>\n <Card />\n</Card>\n"), ["nested-cards"]);
+ assert.deepStrictEqual(rulesFor("<Card header={<span />}>\n <Card><p>Inner</p></Card>\n</Card>\n"), ["nested-cards"]);
+ assert.deepStrictEqual(rulesFor('<ion-card title="a > b"><ion-card></ion-card></ion-card>\n', "a.html"), ["nested-cards"]);
+});
+
test("img-no-alt accepts Angular [alt] bindings and Svelte {alt} shorthand", () => {
const rulesFor = (src, file) => scanSource(src, file).map((f) => f.rule);
assert.deepStrictEqual(rulesFor('<img [src]="u.avatar" [alt]="u.name">\n', "a.component.html"), []);