@tonycodes-ui/mortar 0.22.1 → 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/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. Known limits: props passed through a
8
- // spread ({...props}) are not checked; components re-exported or wrapped under
9
- // another name are not followed; JSX in .vue/.svelte/.astro/.html files is not
10
- // read; dynamic class names built at runtime are not resolved.
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";
@@ -219,6 +221,21 @@ export function blankComments(text, { css = false } = {}) {
219
221
  return out.join("");
220
222
  }
221
223
 
224
+ /**
225
+ * True when `src[i]` starts a JS string or template literal. An apostrophe in
226
+ * JSX text (`Don't`) does not: it must follow an operator, bracket, or a
227
+ * keyword such as `return` (same rule as blankStrings).
228
+ */
229
+ function quoteStarts(src, i) {
230
+ const q = src[i];
231
+ if (q !== '"' && q !== "'" && q !== "`") return false;
232
+ if (q === "`") return true;
233
+ const before = src.slice(0, i).trimEnd();
234
+ return /(?:^|=>|[=(,:?&|[{}+;!]|\b(?:return|case|throw|from|import))$/.test(
235
+ before,
236
+ );
237
+ }
238
+
222
239
  /** Index of the character after the `{…}` group starting at `i`. */
223
240
  function skipBraces(src, i) {
224
241
  let depth = 0;
@@ -229,7 +246,7 @@ function skipBraces(src, i) {
229
246
  else if (c === "}") {
230
247
  depth--;
231
248
  if (depth === 0) return i + 1;
232
- } else if (c === '"' || c === "'") {
249
+ } else if ((c === '"' || c === "'") && quoteStarts(src, i)) {
233
250
  i++;
234
251
  while (i < n && src[i] !== c && src[i] !== "\n") {
235
252
  if (src[i] === "\\") i++;
@@ -253,15 +270,18 @@ function skipBraces(src, i) {
253
270
  const NAME = /[A-Za-z_$][\w$]*(?:[-:.][\w$]+)*/y;
254
271
 
255
272
  /**
256
- * The attributes of a JSX opening tag whose name ends at `i`: name, offset and,
257
- * for string literals (`a="x"` or `a={"x"}`), the value.
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.
258
276
  */
259
- export function jsxAttributes(src, i) {
277
+ function jsxOpening(src, i) {
260
278
  const attrs = [];
261
279
  const n = src.length;
262
280
  while (i < n) {
263
281
  while (i < n && /\s/.test(src[i])) i++;
264
- if (src[i] === ">" || (src[i] === "/" && src[i + 1] === ">")) break;
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 };
265
285
  if (src[i] === "{") {
266
286
  i = skipBraces(src, i);
267
287
  continue;
@@ -291,7 +311,45 @@ export function jsxAttributes(src, i) {
291
311
  }
292
312
  attrs.push(attr);
293
313
  }
294
- 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;
295
353
  }
296
354
 
297
355
  /**
@@ -309,16 +367,6 @@ export function jsxAttributes(src, i) {
309
367
  export function blankStrings(src) {
310
368
  const out = src.split("");
311
369
  const n = src.length;
312
- // A quote in JSX text (Don't) is not a string.
313
- const starts = (i) => {
314
- const q = src[i];
315
- if (q !== '"' && q !== "'" && q !== "`") return false;
316
- if (q === "`") return true;
317
- const before = src.slice(0, i).trimEnd();
318
- return /(?:^|=>|[=(,:?&|[{}+;!]|\b(?:return|case|throw|from|import))$/.test(
319
- before,
320
- );
321
- };
322
370
  const blank = (from, to) => {
323
371
  for (let k = from; k < Math.min(to, n); k++)
324
372
  if (out[k] !== "\n") out[k] = " ";
@@ -358,7 +406,7 @@ export function blankStrings(src) {
358
406
  if (expr && c === "{") depth++;
359
407
  else if (expr && c === "}") {
360
408
  if (--depth === 0) return i;
361
- } else if ((c === '"' || c === "'" || c === "`") && starts(i)) {
409
+ } else if ((c === '"' || c === "'" || c === "`") && quoteStarts(src, i)) {
362
410
  i = blankOne(i, end);
363
411
  if (i < end) i++;
364
412
  continue;
@@ -1027,7 +1075,8 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1027
1075
  const propTypes = component.contract.propTypes;
1028
1076
  if (!propTypes) continue;
1029
1077
  const accepts = data.accepts(component);
1030
- for (const attr of jsxAttributes(src, m.index + m[0].length)) {
1078
+ const opening = jsxOpening(src, m.index + m[0].length);
1079
+ for (const attr of opening.attrs) {
1031
1080
  const name = attr.name;
1032
1081
  if (name === "key" || /[-:]/.test(name)) continue;
1033
1082
  if (!accepts.has(name)) {
@@ -1071,6 +1120,26 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1071
1120
  guess ? `Use ${name}="${guess}".` : undefined,
1072
1121
  );
1073
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
+ }
1074
1143
  }
1075
1144
  }
1076
1145