@tonycodes-ui/mortar 0.23.0 → 0.24.0

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
@@ -53,6 +53,8 @@ export const RULES = {
53
53
  "raw-element":
54
54
  "A raw HTML element (such as <table> or <select>) that a Mortar component replaces, in an app that uses Mortar.",
55
55
  "legacy-package": `A dependency or import of the retired ${LEGACY_PKG}; install and import @tonycodes-ui/mortar instead (see the CHANGELOG migration note).`,
56
+ "invalid-phrasing":
57
+ "A block HTML element in a phrasing-only slot that Mortar renders inside a <p>.",
56
58
  "limited-data": "The audit could not use the installed version's own data.",
57
59
  };
58
60
 
@@ -322,6 +324,7 @@ function jsxOpening(src, i) {
322
324
  i = end + 1;
323
325
  } else if (q === "{") {
324
326
  const end = skipBraces(src, i);
327
+ attr.expression = { start: i + 1, end: end - 1 };
325
328
  const inner = src.slice(i + 1, end - 1).trim();
326
329
  const literal = inner.match(/^(["'`])([^"'`$\\]*)\1$/);
327
330
  if (literal) attr.value = literal[2];
@@ -371,6 +374,101 @@ export function jsxHasChildren(src, bodyStart, tagName) {
371
374
  return false;
372
375
  }
373
376
 
377
+ /** HTML elements that cannot nest inside a `<p>` (obvious block tags). */
378
+ export const PHRASING_BLOCK_TAGS = new Set([
379
+ "address",
380
+ "article",
381
+ "aside",
382
+ "blockquote",
383
+ "div",
384
+ "dl",
385
+ "dt",
386
+ "dd",
387
+ "fieldset",
388
+ "figcaption",
389
+ "figure",
390
+ "footer",
391
+ "form",
392
+ "h1",
393
+ "h2",
394
+ "h3",
395
+ "h4",
396
+ "h5",
397
+ "h6",
398
+ "header",
399
+ "hr",
400
+ "li",
401
+ "main",
402
+ "nav",
403
+ "ol",
404
+ "p",
405
+ "pre",
406
+ "section",
407
+ "table",
408
+ "ul",
409
+ ]);
410
+
411
+ const BLOCK_TAG = /<([a-z][a-z0-9]*)(?=[\s/>])/g;
412
+
413
+ /**
414
+ * Offsets of obvious block HTML tags in `src.slice(start, end)`.
415
+ * Strings should already be blanked so markup inside quotes is ignored.
416
+ */
417
+ export function blockTagsIn(src, start = 0, end = src.length) {
418
+ const hits = [];
419
+ BLOCK_TAG.lastIndex = start;
420
+ let m;
421
+ while ((m = BLOCK_TAG.exec(src)) && m.index < end) {
422
+ if (PHRASING_BLOCK_TAGS.has(m[1])) hits.push({ tag: m[1], index: m.index });
423
+ }
424
+ return hits;
425
+ }
426
+
427
+ /**
428
+ * The [start, end) range of JSX children for the element whose name is
429
+ * `tagName` and whose attributes start at `afterName`. Null when self-closing
430
+ * or the closing tag is missing.
431
+ */
432
+ export function jsxChildrenRange(src, tagName, afterName) {
433
+ const opening = jsxOpening(src, afterName);
434
+ if (opening.selfClosing) return null;
435
+ const start = opening.end;
436
+ const open = new RegExp(`<${escapeRegExp(tagName)}(?=[\\s/>])`, "g");
437
+ const close = new RegExp(`</${escapeRegExp(tagName)}\\s*>`, "g");
438
+ let depth = 1;
439
+ let i = start;
440
+ while (i < src.length && depth > 0) {
441
+ open.lastIndex = i;
442
+ close.lastIndex = i;
443
+ const nextOpen = open.exec(src);
444
+ const nextClose = close.exec(src);
445
+ if (!nextClose) return null;
446
+ if (nextOpen && nextOpen.index < nextClose.index) {
447
+ depth++;
448
+ i = nextOpen.index + nextOpen[0].length;
449
+ } else {
450
+ depth--;
451
+ if (depth === 0) return { start, end: nextClose.index };
452
+ i = nextClose.index + nextClose[0].length;
453
+ }
454
+ }
455
+ return null;
456
+ }
457
+
458
+ function escapeRegExp(s) {
459
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
460
+ }
461
+
462
+ /** Export name → phrasing-only prop names from each contract's `phrasingSlots`. */
463
+ function phrasingSlotMap(data) {
464
+ const map = new Map();
465
+ for (const c of data.components ?? []) {
466
+ const slots = c.contract?.phrasingSlots;
467
+ if (slots?.length) map.set(c.export, slots);
468
+ }
469
+ return map;
470
+ }
471
+
374
472
  /**
375
473
  * The text with string and template-literal contents blanked, so HTML inside
376
474
  * strings (an email template, say) is not read as JSX. Lengths and newlines
@@ -761,6 +859,13 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
761
859
  // tool's mapping; every component it names has existed since 0.1.
762
860
  let rawElements = replacements(data);
763
861
  if (!rawElements.size) rawElements = replacements(bundled);
862
+ // Phrasing-only slots (rendered inside a <p>). Older catalogues fall back
863
+ // to this tool's map so Banner/AuthLayout stay checked after upgrade of the
864
+ // CLI alone.
865
+ const bundledPhrasing = phrasingSlotMap(bundled);
866
+ const dataPhrasing = phrasingSlotMap(data);
867
+ const phrasingSlotsOf = (exportName) =>
868
+ dataPhrasing.get(exportName) ?? bundledPhrasing.get(exportName) ?? [];
764
869
  // Raw elements are collected from every JSX file and reported once the
765
870
  // whole app is known to use Mortar.
766
871
  const rawHits = [];
@@ -1097,6 +1202,8 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1097
1202
  });
1098
1203
 
1099
1204
  if (!JSX.has(ext)) continue;
1205
+ // Blank string contents so "<div>" inside a quote is not read as a tag.
1206
+ const jsx = blankStrings(src);
1100
1207
  // JSX opening tags of Mortar components.
1101
1208
  const tag = /<([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)?)(?=[\s/>])/g;
1102
1209
  for (const m of src.matchAll(tag)) {
@@ -1121,8 +1228,10 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1121
1228
  const propTypes = component.contract.propTypes;
1122
1229
  if (!propTypes) continue;
1123
1230
  const accepts = data.accepts(component);
1124
- const opening = jsxOpening(src, m.index + m[0].length);
1125
- for (const attr of opening.attrs) {
1231
+ const afterName = m.index + m[0].length;
1232
+ const opening = jsxOpening(src, afterName);
1233
+ const attrs = opening.attrs;
1234
+ for (const attr of attrs) {
1126
1235
  const name = attr.name;
1127
1236
  if (name === "key" || /[-:]/.test(name)) continue;
1128
1237
  if (!accepts.has(name)) {
@@ -1186,6 +1295,38 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1186
1295
  : `See the contract: mortar component ${exportName}.`,
1187
1296
  );
1188
1297
  }
1298
+ // Phrasing-only slots: block HTML children inside a <p> are invalid.
1299
+ const slots = phrasingSlotsOf(exportName);
1300
+ if (!slots.length) continue;
1301
+ const reportBlock = (slot, hit) =>
1302
+ add(
1303
+ "invalid-phrasing",
1304
+ "error",
1305
+ file,
1306
+ line(hit.index),
1307
+ `<${exportName}> ${slot} is phrasing-only (rendered inside a <p>); <${hit.tag}> is a block element.`,
1308
+ slot === "children"
1309
+ ? "Pass text, a link or emphasis. For a full section, use Section."
1310
+ : "Pass a string or inline markup (links, emphasis), not a nested <p> or other block element.",
1311
+ );
1312
+ for (const slot of slots) {
1313
+ const attr = attrs.find((a) => a.name === slot);
1314
+ if (attr?.expression) {
1315
+ for (const hit of blockTagsIn(
1316
+ jsx,
1317
+ attr.expression.start,
1318
+ attr.expression.end,
1319
+ ))
1320
+ reportBlock(slot, hit);
1321
+ continue;
1322
+ }
1323
+ if (attr) continue; // string literal or bare boolean — no JSX blocks
1324
+ if (slot !== "children" || opening.selfClosing) continue;
1325
+ const body = jsxChildrenRange(src, m[1], afterName);
1326
+ if (!body) continue;
1327
+ for (const hit of blockTagsIn(jsx, body.start, body.end))
1328
+ reportBlock(slot, hit);
1329
+ }
1189
1330
  }
1190
1331
  }
1191
1332