@tonycodes-ui/mortar 0.22.2 → 0.23.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
@@ -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";
@@ -19,6 +21,10 @@ import {
19
21
  RENAMED_VALUES,
20
22
  } from "./migrations.mjs";
21
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
+
22
28
  export const RULES = {
23
29
  "not-installed": "@tonycodes-ui/mortar is imported but not installed.",
24
30
  "not-declared": "@tonycodes-ui/mortar is imported but not in package.json.",
@@ -46,6 +52,7 @@ export const RULES = {
46
52
  "local-copy": "A local copy of Mortar source.",
47
53
  "raw-element":
48
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).`,
49
56
  "limited-data": "The audit could not use the installed version's own data.",
50
57
  };
51
58
 
@@ -82,6 +89,20 @@ const CODE = new Set([
82
89
  const JSX = new Set([".js", ".jsx", ".tsx", ".mjs"]);
83
90
  const STYLE = new Set([".css", ".scss", ".sass", ".less", ".pcss", ".postcss"]);
84
91
  const PKG = "@tonycodes-ui/mortar";
92
+ const LEGACY_ESCAPE = LEGACY_PKG.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
93
+ const LEGACY_IMPORT = new RegExp(
94
+ String.raw`\b(import|export)\s+(type\s+)?((?:(?!\bfrom\b)[^;])*?)\s*\bfrom\s*(["'])(${LEGACY_ESCAPE}(?:\/[^"']*)?)\4`,
95
+ "g",
96
+ );
97
+ const LEGACY_SIDE_EFFECT = new RegExp(
98
+ String.raw`\bimport\s*(["'])(${LEGACY_ESCAPE}(?:\/[^"']*)?)\1`,
99
+ "g",
100
+ );
101
+ const LEGACY_CSS_IMPORT = new RegExp(
102
+ String.raw`@import\s*(["'])(${LEGACY_ESCAPE}(?:\/[^"']*)?)\1`,
103
+ "g",
104
+ );
105
+ const LEGACY_FIX = `Install and import "${PKG}" instead of "${LEGACY_PKG}". See the CHANGELOG migration note.`;
85
106
  const MAX_FILES = 20000;
86
107
  const MAX_BYTES = 1_000_000;
87
108
 
@@ -268,15 +289,18 @@ function skipBraces(src, i) {
268
289
  const NAME = /[A-Za-z_$][\w$]*(?:[-:.][\w$]+)*/y;
269
290
 
270
291
  /**
271
- * The attributes of a JSX opening tag whose name ends at `i`: name, offset and,
272
- * for string literals (`a="x"` or `a={"x"}`), the value.
292
+ * Walk from the end of a JSX tag name to the `>` or `/>` that closes the
293
+ * opening tag. Returns the attributes, the index after the delimiter, and
294
+ * whether the tag was self-closing.
273
295
  */
274
- export function jsxAttributes(src, i) {
296
+ function jsxOpening(src, i) {
275
297
  const attrs = [];
276
298
  const n = src.length;
277
299
  while (i < n) {
278
300
  while (i < n && /\s/.test(src[i])) i++;
279
- if (src[i] === ">" || (src[i] === "/" && src[i + 1] === ">")) break;
301
+ if (src[i] === "/" && src[i + 1] === ">")
302
+ return { attrs, end: i + 2, selfClosing: true };
303
+ if (src[i] === ">") return { attrs, end: i + 1, selfClosing: false };
280
304
  if (src[i] === "{") {
281
305
  i = skipBraces(src, i);
282
306
  continue;
@@ -306,7 +330,45 @@ export function jsxAttributes(src, i) {
306
330
  }
307
331
  attrs.push(attr);
308
332
  }
309
- return attrs;
333
+ return { attrs, end: i, selfClosing: false };
334
+ }
335
+
336
+ /**
337
+ * The attributes of a JSX opening tag whose name ends at `i`: name, offset and,
338
+ * for string literals (`a="x"` or `a={"x"}`), the value.
339
+ */
340
+ export function jsxAttributes(src, i) {
341
+ return jsxOpening(src, i).attrs;
342
+ }
343
+
344
+ /**
345
+ * True when the JSX element whose body starts at `bodyStart` (just after `>`)
346
+ * has non-whitespace children before its matching close tag. Nested elements
347
+ * with the same local name are counted so `<A><A/></A>` is not truncated.
348
+ */
349
+ export function jsxHasChildren(src, bodyStart, tagName) {
350
+ const open = new RegExp(`<${tagName}(?=[\\s/>])`, "g");
351
+ const close = new RegExp(`</${tagName}\\s*>`, "g");
352
+ let depth = 1;
353
+ let i = bodyStart;
354
+ while (i < src.length && depth > 0) {
355
+ open.lastIndex = i;
356
+ close.lastIndex = i;
357
+ const nextOpen = open.exec(src);
358
+ const nextClose = close.exec(src);
359
+ if (!nextClose) return /\S/.test(src.slice(bodyStart));
360
+ if (nextOpen && nextOpen.index < nextClose.index) {
361
+ const nested = jsxOpening(src, nextOpen.index + nextOpen[0].length);
362
+ // Self-closing nested tags do not change depth.
363
+ if (!nested.selfClosing) depth++;
364
+ i = nested.end;
365
+ } else {
366
+ depth--;
367
+ if (depth === 0) return /\S/.test(src.slice(bodyStart, nextClose.index));
368
+ i = nextClose.index + nextClose[0].length;
369
+ }
370
+ }
371
+ return false;
310
372
  }
311
373
 
312
374
  /**
@@ -752,6 +814,15 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
752
814
  '@import "tailwindcss"; first, then @import "@tonycodes-ui/mortar/styles.css";',
753
815
  );
754
816
  }
817
+ for (const m of css.matchAll(LEGACY_CSS_IMPORT))
818
+ add(
819
+ "legacy-package",
820
+ "error",
821
+ file,
822
+ line(m.index),
823
+ `Imports the retired "${m[2]}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
824
+ LEGACY_FIX,
825
+ );
755
826
  // Selectors: the text before each `{` that is not an at-rule.
756
827
  for (const m of css.matchAll(/([^{};]+)\{/g)) {
757
828
  const selector = m[1].trim();
@@ -779,6 +850,24 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
779
850
  }
780
851
 
781
852
  const src = blankComments(text);
853
+ for (const m of src.matchAll(LEGACY_IMPORT))
854
+ add(
855
+ "legacy-package",
856
+ "error",
857
+ file,
858
+ line(m.index),
859
+ `Imports the retired "${m[5]}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
860
+ LEGACY_FIX,
861
+ );
862
+ for (const m of src.matchAll(LEGACY_SIDE_EFFECT))
863
+ add(
864
+ "legacy-package",
865
+ "error",
866
+ file,
867
+ line(m.index),
868
+ `Imports the retired "${m[2]}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
869
+ LEGACY_FIX,
870
+ );
782
871
  // Email markup is tables by design, and has its own components.
783
872
  const emailFile = EMAIL_FILE.test(src);
784
873
  if (JSX.has(ext) && !emailFile) {
@@ -1032,7 +1121,8 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1032
1121
  const propTypes = component.contract.propTypes;
1033
1122
  if (!propTypes) continue;
1034
1123
  const accepts = data.accepts(component);
1035
- for (const attr of jsxAttributes(src, m.index + m[0].length)) {
1124
+ const opening = jsxOpening(src, m.index + m[0].length);
1125
+ for (const attr of opening.attrs) {
1036
1126
  const name = attr.name;
1037
1127
  if (name === "key" || /[-:]/.test(name)) continue;
1038
1128
  if (!accepts.has(name)) {
@@ -1076,6 +1166,26 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1076
1166
  guess ? `Use ${name}="${guess}".` : undefined,
1077
1167
  );
1078
1168
  }
1169
+ // JSX body children on a control with no children slot (e.g. Switch).
1170
+ if (
1171
+ !opening.selfClosing &&
1172
+ !accepts.has("children") &&
1173
+ jsxHasChildren(src, opening.end, m[1])
1174
+ ) {
1175
+ const guess = accepts.has("label")
1176
+ ? "label"
1177
+ : closest("children", Object.keys(propTypes), 2);
1178
+ add(
1179
+ "unknown-prop",
1180
+ "error",
1181
+ file,
1182
+ line(opening.end),
1183
+ `<${exportName}> has no prop "children".`,
1184
+ guess
1185
+ ? `Use \`${guess}\` (${propTypes[guess]?.type ?? "see the contract"}).`
1186
+ : `See the contract: mortar component ${exportName}.`,
1187
+ );
1188
+ }
1079
1189
  }
1080
1190
  }
1081
1191
 
@@ -1104,6 +1214,19 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1104
1214
  pkgJson?.dependencies?.[PKG] ??
1105
1215
  pkgJson?.devDependencies?.[PKG] ??
1106
1216
  pkgJson?.peerDependencies?.[PKG];
1217
+ const legacyRange =
1218
+ pkgJson?.dependencies?.[LEGACY_PKG] ??
1219
+ pkgJson?.devDependencies?.[LEGACY_PKG] ??
1220
+ pkgJson?.peerDependencies?.[LEGACY_PKG];
1221
+ if (legacyRange)
1222
+ add(
1223
+ "legacy-package",
1224
+ "error",
1225
+ pkgFile,
1226
+ 1,
1227
+ `Depends on the retired "${LEGACY_PKG}"; install and import "${PKG}" instead (see the CHANGELOG migration note).`,
1228
+ LEGACY_FIX,
1229
+ );
1107
1230
  const reactEmail = "@react-email/components";
1108
1231
  if (
1109
1232
  usesEmail &&