@tonycodes-ui/mortar 0.22.2 → 0.22.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -6
- package/catalog.json +355 -355
- package/cli/audit.mjs +74 -10
- package/dist/agent-data.json +1 -1
- package/dist/auth.d.ts +3 -1
- package/dist/auth.js +2 -1
- package/dist/primitives.d.ts +9 -3
- package/dist/primitives.js +6 -3
- package/docs/agents.md +1 -1
- package/package.json +1 -1
- package/src/auth.tsx +5 -1
- package/src/primitives.tsx +17 -3
package/cli/audit.mjs
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
// The scanner is deliberately lightweight (no TypeScript or Babel at runtime):
|
|
5
5
|
// comments are blanked, imports from the package are read with patterns, and
|
|
6
6
|
// JSX opening tags of imported Mortar components are tokenised to read their
|
|
7
|
-
// attribute names and literal values.
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
7
|
+
// attribute names and literal values. Components whose contract has no children
|
|
8
|
+
// slot are also checked for JSX body children between the tags. Known limits:
|
|
9
|
+
// props passed through a spread ({...props}) are not checked; components
|
|
10
|
+
// re-exported or wrapped under another name are not followed; JSX in
|
|
11
|
+
// .vue/.svelte/.astro/.html files is not read; dynamic class names built at
|
|
12
|
+
// runtime are not resolved.
|
|
11
13
|
import { readFileSync, readdirSync, statSync, existsSync } from "node:fs";
|
|
12
14
|
import { resolve, join, relative, dirname, extname, sep } from "node:path";
|
|
13
15
|
import { createHash } from "node:crypto";
|
|
@@ -268,15 +270,18 @@ function skipBraces(src, i) {
|
|
|
268
270
|
const NAME = /[A-Za-z_$][\w$]*(?:[-:.][\w$]+)*/y;
|
|
269
271
|
|
|
270
272
|
/**
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
+
* Walk from the end of a JSX tag name to the `>` or `/>` that closes the
|
|
274
|
+
* opening tag. Returns the attributes, the index after the delimiter, and
|
|
275
|
+
* whether the tag was self-closing.
|
|
273
276
|
*/
|
|
274
|
-
|
|
277
|
+
function jsxOpening(src, i) {
|
|
275
278
|
const attrs = [];
|
|
276
279
|
const n = src.length;
|
|
277
280
|
while (i < n) {
|
|
278
281
|
while (i < n && /\s/.test(src[i])) i++;
|
|
279
|
-
if (src[i] === "
|
|
282
|
+
if (src[i] === "/" && src[i + 1] === ">")
|
|
283
|
+
return { attrs, end: i + 2, selfClosing: true };
|
|
284
|
+
if (src[i] === ">") return { attrs, end: i + 1, selfClosing: false };
|
|
280
285
|
if (src[i] === "{") {
|
|
281
286
|
i = skipBraces(src, i);
|
|
282
287
|
continue;
|
|
@@ -306,7 +311,45 @@ export function jsxAttributes(src, i) {
|
|
|
306
311
|
}
|
|
307
312
|
attrs.push(attr);
|
|
308
313
|
}
|
|
309
|
-
return attrs;
|
|
314
|
+
return { attrs, end: i, selfClosing: false };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* The attributes of a JSX opening tag whose name ends at `i`: name, offset and,
|
|
319
|
+
* for string literals (`a="x"` or `a={"x"}`), the value.
|
|
320
|
+
*/
|
|
321
|
+
export function jsxAttributes(src, i) {
|
|
322
|
+
return jsxOpening(src, i).attrs;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* True when the JSX element whose body starts at `bodyStart` (just after `>`)
|
|
327
|
+
* has non-whitespace children before its matching close tag. Nested elements
|
|
328
|
+
* with the same local name are counted so `<A><A/></A>` is not truncated.
|
|
329
|
+
*/
|
|
330
|
+
export function jsxHasChildren(src, bodyStart, tagName) {
|
|
331
|
+
const open = new RegExp(`<${tagName}(?=[\\s/>])`, "g");
|
|
332
|
+
const close = new RegExp(`</${tagName}\\s*>`, "g");
|
|
333
|
+
let depth = 1;
|
|
334
|
+
let i = bodyStart;
|
|
335
|
+
while (i < src.length && depth > 0) {
|
|
336
|
+
open.lastIndex = i;
|
|
337
|
+
close.lastIndex = i;
|
|
338
|
+
const nextOpen = open.exec(src);
|
|
339
|
+
const nextClose = close.exec(src);
|
|
340
|
+
if (!nextClose) return /\S/.test(src.slice(bodyStart));
|
|
341
|
+
if (nextOpen && nextOpen.index < nextClose.index) {
|
|
342
|
+
const nested = jsxOpening(src, nextOpen.index + nextOpen[0].length);
|
|
343
|
+
// Self-closing nested tags do not change depth.
|
|
344
|
+
if (!nested.selfClosing) depth++;
|
|
345
|
+
i = nested.end;
|
|
346
|
+
} else {
|
|
347
|
+
depth--;
|
|
348
|
+
if (depth === 0) return /\S/.test(src.slice(bodyStart, nextClose.index));
|
|
349
|
+
i = nextClose.index + nextClose[0].length;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return false;
|
|
310
353
|
}
|
|
311
354
|
|
|
312
355
|
/**
|
|
@@ -1032,7 +1075,8 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
|
|
|
1032
1075
|
const propTypes = component.contract.propTypes;
|
|
1033
1076
|
if (!propTypes) continue;
|
|
1034
1077
|
const accepts = data.accepts(component);
|
|
1035
|
-
|
|
1078
|
+
const opening = jsxOpening(src, m.index + m[0].length);
|
|
1079
|
+
for (const attr of opening.attrs) {
|
|
1036
1080
|
const name = attr.name;
|
|
1037
1081
|
if (name === "key" || /[-:]/.test(name)) continue;
|
|
1038
1082
|
if (!accepts.has(name)) {
|
|
@@ -1076,6 +1120,26 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
|
|
|
1076
1120
|
guess ? `Use ${name}="${guess}".` : undefined,
|
|
1077
1121
|
);
|
|
1078
1122
|
}
|
|
1123
|
+
// JSX body children on a control with no children slot (e.g. Switch).
|
|
1124
|
+
if (
|
|
1125
|
+
!opening.selfClosing &&
|
|
1126
|
+
!accepts.has("children") &&
|
|
1127
|
+
jsxHasChildren(src, opening.end, m[1])
|
|
1128
|
+
) {
|
|
1129
|
+
const guess = accepts.has("label")
|
|
1130
|
+
? "label"
|
|
1131
|
+
: closest("children", Object.keys(propTypes), 2);
|
|
1132
|
+
add(
|
|
1133
|
+
"unknown-prop",
|
|
1134
|
+
"error",
|
|
1135
|
+
file,
|
|
1136
|
+
line(opening.end),
|
|
1137
|
+
`<${exportName}> has no prop "children".`,
|
|
1138
|
+
guess
|
|
1139
|
+
? `Use \`${guess}\` (${propTypes[guess]?.type ?? "see the contract"}).`
|
|
1140
|
+
: `See the contract: mortar component ${exportName}.`,
|
|
1141
|
+
);
|
|
1142
|
+
}
|
|
1079
1143
|
}
|
|
1080
1144
|
}
|
|
1081
1145
|
|