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.

Flag TDesign t-card and TCard nesting in nested-cards (#350)

Jay5b8fd8ca70f3
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 5cc51aa..b2f5986 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -417,18 +417,111 @@ function lineAt(starts, index) {
// Index of the ">" that closes the tag, skipping any "{...}" attribute expression so an arrow
// function (=>) or a comparison inside it does not end the tag early. -1 when nothing closes it.
-function tagEnd(text, from) {
- const end = walkTag(text, from);
+function tagEnd(text, from, onExpression) {
+ const end = walkTag(text, from, undefined, onExpression);
return end === -1 ? text.indexOf(">", from) : end;
}
+// Find a nested element while skipping attribute values and stopping at the
+// outer element's matching close tag.
+function containsNestedTag(text, from, outerTag, tagNames, rangeEnd = text.length, inExpression = false) {
+ const expected = new Set(tagNames);
+ const anyTag = /^<(\/?)\s*([A-Za-z][\w:.-]*)(?=[\s/>])/;
+ let depth = 1;
+ let offset = from;
+ while (offset < rangeEnd) {
+ if (inExpression) {
+ const skipped = skipExpressionLiteral(text, offset, rangeEnd);
+ if (skipped !== offset) {
+ offset = skipped;
+ continue;
+ }
+ }
+ const start = text.indexOf("<", offset);
+ if (start === -1 || start >= rangeEnd) return false;
+ const remaining = text.slice(start);
+ const tag = anyTag.exec(remaining);
+ if (!tag) {
+ offset = start + 1;
+ continue;
+ }
+ const expressions = [];
+ const end = tagEnd(text, start + tag[0].length, (open, close) => expressions.push([open, close]));
+ if (end === -1 || end >= rangeEnd) {
+ offset = start + 1;
+ continue;
+ }
+ // A comparison such as `count < limit && <Card />` can look like an
+ // opening tag until the later JSX element. Reject that candidate so the
+ // scanner can continue at the real `<Card />`.
+ if (hasUnexpectedOpenAngle(text, start + tag[0].length, end, expressions)) {
+ offset = start + 1;
+ continue;
+ }
+ for (const [open, close] of expressions) {
+ if (containsNestedTag(text, open, outerTag, tagNames, close, true)) return true;
+ }
+ const closing = tag[1] === "/";
+ const name = tag[2];
+ const selfClosing = /\/\s*$/.test(text.slice(start + tag[0].length, end));
+ if (!closing && expected.has(name)) return true;
+ if (name === outerTag) {
+ if (closing && --depth === 0) return false;
+ if (!closing && !selfClosing) depth++;
+ }
+ offset = end + 1;
+ }
+ return false;
+}
+
+function hasUnexpectedOpenAngle(text, from, to, expressions) {
+ let expressionIndex = 0;
+ let quote = null;
+ for (let i = from; i < to; i++) {
+ const expression = expressions[expressionIndex];
+ if (expression && i === expression[0]) {
+ i = expression[1];
+ expressionIndex++;
+ continue;
+ }
+ const c = text[i];
+ if (quote) {
+ if (c === "\\") i++;
+ else if (c === quote) quote = null;
+ } else if (c === "'" || c === '"' || c === "`") quote = c;
+ else if (c === "<") return true;
+ }
+ return false;
+}
+
+function skipExpressionLiteral(text, at, end) {
+ const c = text[at];
+ if (c === "'" || c === '"' || c === "`") {
+ for (let i = at + 1; i < end; i++) {
+ if (text[i] === "\\") i++;
+ else if (text[i] === c) return i + 1;
+ }
+ return end;
+ }
+ if (c === "/" && text[at + 1] === "/") {
+ const newline = text.indexOf("\n", at + 2);
+ return newline === -1 || newline >= end ? end : newline + 1;
+ }
+ if (c === "/" && text[at + 1] === "*") {
+ const close = text.indexOf("*/", at + 2);
+ return close === -1 || close >= end ? end : close + 2;
+ }
+ return at;
+}
+
// Walks an opening tag from `from` and returns the index of its closing ">", or -1. A > inside a
// quoted attribute value (title="a > b") does not end the tag, and a brace inside a string or a
// regex literal in an expression (onLoad={() => f("}")}) does not close it. `own(i)` is called
// for each character outside {...} expressions: the element's own attributes.
-function walkTag(text, from, own) {
+function walkTag(text, from, own, onExpression) {
let depth = 0;
let quote = null;
+ let expressionStart = -1;
for (let i = from; i < text.length; i++) {
const c = text[i];
if (own && depth === 0) own(i);
@@ -437,8 +530,17 @@ function walkTag(text, from, own) {
else if (c === quote) quote = null;
} else if (c === '"' || c === "'" || (depth > 0 && c === "`")) quote = c;
else if (c === "/" && depth > 0 && regexInExpression(text, i)) i = skipRegex(text, i) - 1;
- else if (c === "{") depth++;
- else if (c === "}" && depth > 0) depth--;
+ else if (c === "{") {
+ if (depth === 0) expressionStart = i + 1;
+ depth++;
+ }
+ else if (c === "}" && depth > 0) {
+ depth--;
+ if (depth === 0 && expressionStart !== -1) {
+ onExpression?.(expressionStart, i);
+ expressionStart = -1;
+ }
+ }
else if (c === ">" && depth === 0) return i;
}
return -1;
@@ -994,16 +1096,15 @@ const rules = [
// So does PrimeNG's p-card (#280).
// NG-ZORRO's nz-card nests the same way (#326).
// Shoelace's sl-card web component too (#342).
+ // TDesign's t-card / TCard nest the same way (#343).
// Chakra UI v3's Card.Root is the card itself; its Card.Header/Body/Footer parts are not (#305).
- for (const m of matches(/<(Card\.Root|Card|IonCard|ion-card|VCard|v-card|mat-card|QCard|q-card|ElCard|el-card|NCard|n-card|ACard|a-card|p-card|nz-card|sl-card)(?=[\s/>])/, code)) {
+ for (const m of matches(/<(Card\.Root|Card|IonCard|ion-card|VCard|v-card|mat-card|QCard|q-card|ElCard|el-card|NCard|n-card|ACard|a-card|p-card|nz-card|sl-card|TCard|t-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}>`, end + 1);
- if (close === -1) continue;
- const inner = code.slice(end + 1, close);
- if (new RegExp(`<${tag.replace(/\./g, "\\.")}(?=[\\s/>])`).test(inner)) hits.push(m.index);
+ const innerTags = tag === "t-card" || tag === "TCard" ? ["t-card", "TCard"] : [tag];
+ if (containsNestedTag(code, end + 1, tag, innerTags)) hits.push(m.index);
}
} else {
const card = /\b(?:Elevated|Outlined|Filled)?Card\s*[({]/;
diff --git a/test/detect.test.js b/test/detect.test.js
index dcce1f5..2ac2772 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -1541,6 +1541,33 @@ test("nested-cards flags NG-ZORRO nz-card nesting, not its card parts (issue #32
assert.deepStrictEqual(cardLines("<mat-card>\n <mat-card>Pro plan</mat-card>\n</mat-card>\n"), [[1, "noticeable"]]);
});
+test("nested-cards flags TDesign t-card and TCard nesting, not sibling cards or other t- tags (issue #343)", () => {
+ const cardLines = (src, file = "Plans.vue") => scanSource(src, file, { kind: "web" }).filter((f) => f.rule === "nested-cards").map((f) => [f.line, f.severity]);
+ // Issue #343 repro:
+ const repro = [
+ "<template>",
+ ' <t-card title="Plans">',
+ ' <t-card title="Pro">$9</t-card>',
+ " </t-card>",
+ "</template>",
+ "",
+ ].join("\n");
+ assert.deepStrictEqual(cardLines(repro), [[2, "noticeable"]]);
+
+ // PascalCase TCard nested inside TCard
+ assert.deepStrictEqual(cardLines("<TCard>\n <TCard>Pro plan</TCard>\n</TCard>\n", "Plans.tsx"), [[1, "noticeable"]]);
+
+ // TDesign exposes the same card as both kebab-case and PascalCase.
+ assert.deepStrictEqual(cardLines("<t-card>\n <TCard />\n</t-card>\n"), [[1, "noticeable"]]);
+
+ // Sibling cards stay quiet
+ assert.deepStrictEqual(cardLines('<t-card title="Pro">$9</t-card>\n<t-card title="Team">$29</t-card>\n'), []);
+ assert.deepStrictEqual(cardLines('<t-card title="Pro">$9</t-card >\n<t-card title="Team">$29</t-card>\n'), []);
+
+ // A single card with other TDesign tags inside stays quiet
+ assert.deepStrictEqual(cardLines('<t-card title="Pro">\n <t-tag>New</t-tag>\n <t-button>Buy</t-button>\n</t-card>\n'), []);
+});
+
test("nested-cards flags Shoelace sl-card nesting, not other sl- tags inside one card (issue #342)", () => {
const cardLines = (src, file = "plans.html") => scanSource(src, file, { kind: "web" }).filter((f) => f.rule === "nested-cards").map((f) => [f.line, f.severity]);
// Issue #342 repro:
@@ -1568,6 +1595,17 @@ test("nested-cards reads opening tags with tagEnd so arrows or > in props do not
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"]);
+ assert.deepStrictEqual(rulesFor('<Card title="<Card>Example</Card>">Only one card</Card>'), []);
+ assert.deepStrictEqual(rulesFor('<Card><span title="<Card>Example</Card>">Text</span></Card>'), []);
+ assert.deepStrictEqual(rulesFor('<Card><span title="<Card>Example</Card>" /><Card /></Card>'), ["nested-cards"]);
+ // JSX comparisons before a real nested element must not consume that element
+ // as part of a false opening-tag candidate.
+ assert.deepStrictEqual(rulesFor("<Card>{count < limit && <Card />}</Card>"), ["nested-cards"]);
+ assert.deepStrictEqual(rulesFor("<Card>{count<limit&&<Card/>}</Card>"), ["nested-cards"]);
+ // JSX inside brace-valued attributes is rendered as a child and counts;
+ // text that merely looks like JSX inside an expression string does not.
+ assert.deepStrictEqual(rulesFor("<Card><div children={<Card />} /></Card>"), ["nested-cards"]);
+ assert.deepStrictEqual(rulesFor('<Card><div children={"<Card />"} /></Card>'), []);
});
test("img-no-alt accepts Angular [alt] bindings and Svelte {alt} shorthand", () => {