@tonycodes-ui/mortar 0.24.0 → 0.25.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
@@ -55,6 +55,10 @@ export const RULES = {
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
56
  "invalid-phrasing":
57
57
  "A block HTML element in a phrasing-only slot that Mortar renders inside a <p>.",
58
+ "input-static-action":
59
+ "Static text passed to Input.action; use prefix or suffix instead.",
60
+ "input-absolute-adornment":
61
+ "An absolutely positioned decorative sibling of Input; use leadingIcon or trailingIcon.",
58
62
  "limited-data": "The audit could not use the installed version's own data.",
59
63
  };
60
64
 
@@ -338,10 +342,15 @@ function jsxOpening(src, i) {
338
342
 
339
343
  /**
340
344
  * The attributes of a JSX opening tag whose name ends at `i`: name, offset and,
341
- * for string literals (`a="x"` or `a={"x"}`), the value.
345
+ * for string literals (`a="x"` or `a={"x"}`), the value. Brace values also get
346
+ * `expression: { start, end }` covering the inner text. The returned array has
347
+ * `end` (index after the opening tag) and `selfClosing`.
342
348
  */
343
349
  export function jsxAttributes(src, i) {
344
- return jsxOpening(src, i).attrs;
350
+ const { attrs, end, selfClosing } = jsxOpening(src, i);
351
+ attrs.end = end;
352
+ attrs.selfClosing = selfClosing;
353
+ return attrs;
345
354
  }
346
355
 
347
356
  /**
@@ -469,6 +478,113 @@ function phrasingSlotMap(data) {
469
478
  return map;
470
479
  }
471
480
 
481
+ /** End index of the JSX element that opens with `tagName` at `afterName`. */
482
+ function jsxElementEnd(src, tagName, afterName, opening) {
483
+ if (opening.selfClosing) return opening.end;
484
+ const body = jsxChildrenRange(src, tagName, afterName);
485
+ if (!body) return opening.end;
486
+ const close = src
487
+ .slice(body.end)
488
+ .match(new RegExp(`^</${escapeRegExp(tagName)}\\s*>`));
489
+ return body.end + (close ? close[0].length : 0);
490
+ }
491
+
492
+ /**
493
+ * True when an `action={…}` expression is obvious static end content (a string
494
+ * or a noninteractive span/fragment), not an attached control.
495
+ */
496
+ export function isStaticInputAction(expression) {
497
+ const inner = expression.trim();
498
+ if (!inner) return false;
499
+ if (/^(["'`])([^"'`$\\]*)\1$/.test(inner)) return true;
500
+ if (/\bon[A-Z]\w*\s*=/.test(inner) || /\bhref\s*=/.test(inner)) return false;
501
+ if (/<\/?(?:button|a|input|select|textarea)\b/i.test(inner)) return false;
502
+ if (/<[A-Z][\w$]*\b/.test(inner)) return false;
503
+ if (/^<>\s*[^<{]+?\s*<\/>$/.test(inner)) return true;
504
+ if (/^<\s*span\b[^>]*>\s*[^<{]+?\s*<\/\s*span\s*>$/i.test(inner)) return true;
505
+ return false;
506
+ }
507
+
508
+ /**
509
+ * Absolutely positioned decorative siblings near a Mortar Input, typically
510
+ * left over from wrapping an unlabeled native input. Bounded: string
511
+ * `className` literals only, a nearby `relative` wrapper, and only the
512
+ * immediate sibling before or after this Input (so later Inputs in the same
513
+ * file are not flagged for an earlier icon).
514
+ */
515
+ export function absoluteAdornmentsBesideInput(src, inputStart, inputEnd) {
516
+ const windowStart = Math.max(0, inputStart - 1000);
517
+ const windowEnd = Math.min(src.length, inputEnd + 1000);
518
+ const before = src.slice(windowStart, inputStart);
519
+ const after = src.slice(inputEnd, windowEnd);
520
+ const hasRelative = (slice) =>
521
+ /\bclass(?:Name)?\s*=\s*(?:\{\s*)?(["'`])([^"'`]*\brelative\b[^"'`]*)\1/.test(
522
+ slice,
523
+ );
524
+ if (!hasRelative(before)) return [];
525
+ const hits = [];
526
+ const open =
527
+ /<(svg|img|span|i|div|em)\b(?=[^>]*\bclass(?:Name)?\s*=\s*(?:\{\s*)?(["'`])([^"'`]*\babsolute\b[^"'`]*)\2)[^>]*>/i;
528
+ const sideFrom = (cls, fallback) =>
529
+ /\b(?:right-|end-|inset-e|inset-inline-end)/.test(cls)
530
+ ? "trailing"
531
+ : /\b(?:left-|start-|inset-s|inset-inline-start)/.test(cls)
532
+ ? "leading"
533
+ : fallback;
534
+ const decorative = (tag, openText) => {
535
+ if (/\bon[A-Z]\w*\s*=/.test(openText) || /\bhref\s*=/.test(openText))
536
+ return false;
537
+ return (
538
+ tag === "svg" ||
539
+ tag === "img" ||
540
+ /\baria-hidden\b/.test(openText) ||
541
+ tag === "span" ||
542
+ tag === "i" ||
543
+ tag === "em" ||
544
+ tag === "div"
545
+ );
546
+ };
547
+ // Immediate previous sibling: the last absolute tag in `before`, with only
548
+ // whitespace (and its own closing) between it and the Input.
549
+ {
550
+ let last;
551
+ const re = new RegExp(open.source, "gi");
552
+ let m;
553
+ while ((m = re.exec(before))) last = m;
554
+ if (last) {
555
+ const tag = last[1].toLowerCase();
556
+ const cls = last[3];
557
+ const openText = last[0];
558
+ const from = last.index;
559
+ let afterOpen = from + openText.length;
560
+ if (!/\/>\s*$/.test(openText)) {
561
+ const close = before
562
+ .slice(afterOpen)
563
+ .match(new RegExp(`^[\\s\\S]*?</${tag}\\s*>`, "i"));
564
+ if (close) afterOpen += close[0].length;
565
+ }
566
+ if (/^\s*$/.test(before.slice(afterOpen)) && decorative(tag, openText))
567
+ hits.push({
568
+ index: windowStart + from,
569
+ side: sideFrom(cls, "leading"),
570
+ });
571
+ }
572
+ }
573
+ // Immediate next sibling: the first absolute tag in `after`, with only
574
+ // whitespace between the Input and it.
575
+ {
576
+ const m = after.match(new RegExp("^\\s*" + open.source, "i"));
577
+ if (m && decorative(m[1].toLowerCase(), m[0].trimStart() || m[0])) {
578
+ const absAt = m[0].indexOf("<");
579
+ hits.push({
580
+ index: inputEnd + absAt,
581
+ side: sideFrom(m[3], "trailing"),
582
+ });
583
+ }
584
+ }
585
+ return hits;
586
+ }
587
+
472
588
  /**
473
589
  * The text with string and template-literal contents blanked, so HTML inside
474
590
  * strings (an email template, say) is not read as JSX. Lengths and newlines
@@ -1275,6 +1391,47 @@ export async function auditApp(path = ".", { data: bundled } = {}) {
1275
1391
  guess ? `Use ${name}="${guess}".` : undefined,
1276
1392
  );
1277
1393
  }
1394
+ // Input adornments: static text in `action`, or absolute decorative
1395
+ // siblings left over from migrating an unlabeled native input.
1396
+ if (exportName === "Input") {
1397
+ const action = attrs.find((a) => a.name === "action");
1398
+ const staticAction =
1399
+ action &&
1400
+ (action.value !== undefined
1401
+ ? true
1402
+ : action.expression
1403
+ ? isStaticInputAction(
1404
+ src.slice(action.expression.start, action.expression.end),
1405
+ )
1406
+ : false);
1407
+ if (staticAction)
1408
+ add(
1409
+ "input-static-action",
1410
+ "warning",
1411
+ file,
1412
+ line(action.index),
1413
+ `<Input> action holds static text; action is for an attached control such as a Button.`,
1414
+ "Use `suffix` (or `prefix`) for end/start text adornments such as `%` or units.",
1415
+ );
1416
+ const elementEnd = jsxElementEnd(src, m[1], afterName, opening);
1417
+ // Use comment-blanked source (not string-blanked): className values
1418
+ // must stay readable so absolute/relative tokens are visible.
1419
+ for (const hit of absoluteAdornmentsBesideInput(
1420
+ src,
1421
+ m.index,
1422
+ elementEnd,
1423
+ )) {
1424
+ const prop = hit.side === "trailing" ? "trailingIcon" : "leadingIcon";
1425
+ add(
1426
+ "input-absolute-adornment",
1427
+ "warning",
1428
+ file,
1429
+ line(hit.index),
1430
+ `Absolutely positioned decorative sibling next to <Input>; it centres against the whole field (including the label).`,
1431
+ `Pass the icon as \`${prop}\` so it sits inside the text box.`,
1432
+ );
1433
+ }
1434
+ }
1278
1435
  // JSX body children on a control with no children slot (e.g. Switch).
1279
1436
  if (
1280
1437
  !opening.selfClosing &&
@@ -2,6 +2,11 @@
2
2
  // CHANGELOG. The audit uses them to suggest the current name for an import, a
3
3
  // prop or a value. tests/cli.test.mjs checks every replacement exists in the
4
4
  // catalogue and every removed prop is gone.
5
+ //
6
+ // Input adornments (audit rules input-static-action / input-absolute-adornment):
7
+ // when migrating an unlabeled native <input> to Mortar's labeled Input, use
8
+ // prefix/suffix for static text and leadingIcon/trailingIcon for decorative
9
+ // icons. Do not keep absolute siblings or put static text in action.
5
10
 
6
11
  export const REMOVED_EXPORTS = {
7
12
  MarketingTone: {