@tonycodes-ui/mortar 0.22.3 → 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
@@ -21,6 +21,10 @@ import {
21
21
  RENAMED_VALUES,
22
22
  } from "./migrations.mjs";
23
23
 
24
+ // Built from parts so tracked source does not contain the retired scope literal
25
+ // (tests/npm-package-name.test.mjs forbids it outside the migration allowlist).
26
+ const LEGACY_PKG = ["@", "tony-ui", "/", "mortar"].join("");
27
+
24
28
  export const RULES = {
25
29
  "not-installed": "@tonycodes-ui/mortar is imported but not installed.",
26
30
  "not-declared": "@tonycodes-ui/mortar is imported but not in package.json.",
@@ -48,6 +52,9 @@ export const RULES = {
48
52
  "local-copy": "A local copy of Mortar source.",
49
53
  "raw-element":
50
54
  "A raw HTML element (such as <table> or <select>) that a Mortar component replaces, in an app that uses Mortar.",
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>.",
51
58
  "limited-data": "The audit could not use the installed version's own data.",
52
59
  };
53
60
 
@@ -84,6 +91,20 @@ const CODE = new Set([
84
91
  const JSX = new Set([".js", ".jsx", ".tsx", ".mjs"]);
85
92
  const STYLE = new Set([".css", ".scss", ".sass", ".less", ".pcss", ".postcss"]);
86
93
  const PKG = "@tonycodes-ui/mortar";
94
+ const LEGACY_ESCAPE = LEGACY_PKG.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
95
+ const LEGACY_IMPORT = new RegExp(
96
+ String.raw`\b(import|export)\s+(type\s+)?((?:(?!\bfrom\b)[^;])*?)\s*\bfrom\s*(["'])(${LEGACY_ESCAPE}(?:\/[^"']*)?)\4`,
97
+ "g",
98
+ );
99
+ const LEGACY_SIDE_EFFECT = new RegExp(
100
+ String.raw`\bimport\s*(["'])(${LEGACY_ESCAPE}(?:\/[^"']*)?)\1`,
101
+ "g",
102
+ );
103
+ const LEGACY_CSS_IMPORT = new RegExp(
104
+ String.raw`@import\s*(["'])(${LEGACY_ESCAPE}(?:\/[^"']*)?)\1`,
105
+ "g",
106
+ );
107
+ const LEGACY_FIX = `Install and import "${PKG}" instead of "${LEGACY_PKG}". See the CHANGELOG migration note.`;
87
108
  const MAX_FILES = 20000;
88
109
  const MAX_BYTES = 1_000_000;
89
110
 
@@ -303,6 +324,7 @@ function jsxOpening(src, i) {
303
324
  i = end + 1;
304
325
  } else if (q === "{") {
305
326
  const end = skipBraces(src, i);
327
+ attr.expression = { start: i + 1, end: end - 1 };
306
328
  const inner = src.slice(i + 1, end - 1).trim();
307
329
  const literal = inner.match(/^(["'`])([^"'`$\\]*)\1$/);
308
330
  if (literal) attr.value = literal[2];
@@ -352,6 +374,101 @@ export function jsxHasChildren(src, bodyStart, tagName) {
352
374
  return false;
353
375
  }
354
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
+
355
472
  /**
356
473
  * The text with string and template-literal contents blanked, so HTML inside
357
474
  * strings (an email template, say) is not read as JSX. Lengths and newlines
@@ -742,6 +859,13 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
742
859
  // tool's mapping; every component it names has existed since 0.1.
743
860
  let rawElements = replacements(data);
744
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) ?? [];
745
869
  // Raw elements are collected from every JSX file and reported once the
746
870
  // whole app is known to use Mortar.
747
871
  const rawHits = [];
@@ -795,6 +919,15 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
795
919
  '@import "tailwindcss"; first, then @import "@tonycodes-ui/mortar/styles.css";',
796
920
  );
797
921
  }
922
+ for (const m of css.matchAll(LEGACY_CSS_IMPORT))
923
+ add(
924
+ "legacy-package",
925
+ "error",
926
+ file,
927
+ line(m.index),
928
+ `Imports the retired "${m[2]}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
929
+ LEGACY_FIX,
930
+ );
798
931
  // Selectors: the text before each `{` that is not an at-rule.
799
932
  for (const m of css.matchAll(/([^{};]+)\{/g)) {
800
933
  const selector = m[1].trim();
@@ -822,6 +955,24 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
822
955
  }
823
956
 
824
957
  const src = blankComments(text);
958
+ for (const m of src.matchAll(LEGACY_IMPORT))
959
+ add(
960
+ "legacy-package",
961
+ "error",
962
+ file,
963
+ line(m.index),
964
+ `Imports the retired "${m[5]}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
965
+ LEGACY_FIX,
966
+ );
967
+ for (const m of src.matchAll(LEGACY_SIDE_EFFECT))
968
+ add(
969
+ "legacy-package",
970
+ "error",
971
+ file,
972
+ line(m.index),
973
+ `Imports the retired "${m[2]}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
974
+ LEGACY_FIX,
975
+ );
825
976
  // Email markup is tables by design, and has its own components.
826
977
  const emailFile = EMAIL_FILE.test(src);
827
978
  if (JSX.has(ext) && !emailFile) {
@@ -1051,6 +1202,8 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1051
1202
  });
1052
1203
 
1053
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);
1054
1207
  // JSX opening tags of Mortar components.
1055
1208
  const tag = /<([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)?)(?=[\s/>])/g;
1056
1209
  for (const m of src.matchAll(tag)) {
@@ -1075,8 +1228,10 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1075
1228
  const propTypes = component.contract.propTypes;
1076
1229
  if (!propTypes) continue;
1077
1230
  const accepts = data.accepts(component);
1078
- const opening = jsxOpening(src, m.index + m[0].length);
1079
- 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) {
1080
1235
  const name = attr.name;
1081
1236
  if (name === "key" || /[-:]/.test(name)) continue;
1082
1237
  if (!accepts.has(name)) {
@@ -1140,6 +1295,38 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1140
1295
  : `See the contract: mortar component ${exportName}.`,
1141
1296
  );
1142
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
+ }
1143
1330
  }
1144
1331
  }
1145
1332
 
@@ -1168,6 +1355,19 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1168
1355
  pkgJson?.dependencies?.[PKG] ??
1169
1356
  pkgJson?.devDependencies?.[PKG] ??
1170
1357
  pkgJson?.peerDependencies?.[PKG];
1358
+ const legacyRange =
1359
+ pkgJson?.dependencies?.[LEGACY_PKG] ??
1360
+ pkgJson?.devDependencies?.[LEGACY_PKG] ??
1361
+ pkgJson?.peerDependencies?.[LEGACY_PKG];
1362
+ if (legacyRange)
1363
+ add(
1364
+ "legacy-package",
1365
+ "error",
1366
+ pkgFile,
1367
+ 1,
1368
+ `Depends on the retired "${LEGACY_PKG}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
1369
+ LEGACY_FIX,
1370
+ );
1171
1371
  const reactEmail = "@react-email/components";
1172
1372
  if (
1173
1373
  usesEmail &&