@multiplatform.one/config 9.0.0 → 9.0.2

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.
Files changed (2) hide show
  1. package/design-lint.mjs +132 -63
  2. package/package.json +2 -2
package/design-lint.mjs CHANGED
@@ -264,10 +264,20 @@ const errorKey = /(?:^error|Error|^fail|Fail|Failure)\w*$/;
264
264
  // tooltip, alert banner or empty state is running text, titles included.
265
265
  // Text, View and the like pass the question to their parent; the label and
266
266
  // body text recipes are type sizes, not kinds, so they pass it on too.
267
- const labelTag =
268
- /^(?:a|button|summary|label|legend|th|option|optgroup|h[1-6]|H[1-6])$|(?:Button|Link|Anchor|Label|Title|Heading|Tab|Trigger|MenuItem|ItemText|ItemTitle)$|(?:Menu|Select|Dropdown|Command|Tabs)\w*\.Item$/;
269
- const textTag =
270
- /^(?:p|blockquote|figcaption|small|Paragraph)$|(?:Description|Hint|Helper|HelperText|Caption|Subtitle|Message)$|(?:^|\.)(?:Alert|Banner|Callout|Notice)(?:$|\.)|Toast|Tooltip|Empty|Error/;
267
+ const anyPattern = (...patterns) => ({
268
+ test: (text) => patterns.some((pattern) => pattern.test(text)),
269
+ });
270
+ const labelTag = anyPattern(
271
+ /^(?:a|button|summary|label|legend|th|option|optgroup|h[1-6]|H[1-6])$/,
272
+ /(?:Button|Link|Anchor|Label|Title|Heading|Tab|Trigger|MenuItem|ItemText|ItemTitle)$/,
273
+ /(?:Menu|Select|Dropdown|Command|Tabs)\w*\.Item$/,
274
+ );
275
+ const textTag = anyPattern(
276
+ /^(?:p|blockquote|figcaption|small|Paragraph)$/,
277
+ /(?:Description|Hint|Helper|HelperText|Caption|Subtitle|Message)$/,
278
+ /(?:^|\.)(?:Alert|Banner|Callout|Notice)(?:$|\.)/,
279
+ /Toast|Tooltip|Empty|Error/,
280
+ );
271
281
  const labelRole =
272
282
  /^(?:button|link|tab|menuitem|menuitemcheckbox|menuitemradio|option|heading|header|columnheader|rowheader|treeitem)$/;
273
283
  // The ruling names no case for a checkbox, radio or switch label, and Apple
@@ -374,6 +384,27 @@ export const titleSmallWords = [
374
384
  export const titleParticles = ["by", "in", "off", "on", "out", "over", "up"];
375
385
  const smallWord = new Set(titleSmallWords);
376
386
  const particle = new Set(titleParticles);
387
+ function maskInterpolations(text) {
388
+ let masked = "";
389
+ let from = 0;
390
+ let close = -1;
391
+ for (let open = text.indexOf("{"); open !== -1; ) {
392
+ if (close <= open) close = text.indexOf("}", open + 1);
393
+ if (close === -1) break;
394
+ const next = text.indexOf("{", open + 1);
395
+ let end = -1;
396
+ if (next === open + 1 && text[close + 1] === "}") end = close + 2;
397
+ else if (next === -1 || next > close) end = close + 1;
398
+ if (end === -1) {
399
+ open = next;
400
+ continue;
401
+ }
402
+ masked += `${text.slice(from, open)} {x} `;
403
+ from = end;
404
+ open = text.indexOf("{", end);
405
+ }
406
+ return masked + text.slice(from);
407
+ }
377
408
  // The words the capitals rules judge, or undefined when the string is code
378
409
  // or has fewer than two real words (one word reads the same in both styles).
379
410
  // Interpolations, proper names, numbers, dotted names and words that start
@@ -382,14 +413,16 @@ const particle = new Set(titleParticles);
382
413
  // "e.g." quotes the user's own data and is not judged.
383
414
  export function caseWords(text, names = houseProperNames) {
384
415
  if (/^(?:e\.g\.|i\.e\.)/i.test(text.trim())) return undefined;
385
- let value = text.replace(/\{\{[^}]*\}\}|\{[^{}]*\}/g, " {x} ");
416
+ let value = maskInterpolations(text);
386
417
  for (const proper of names) value = value.replaceAll(proper, " {x} ");
387
418
  if (/[/=<>$_@#\\|{}[\]]/.test(value.replaceAll("{x}", ""))) return undefined;
388
419
  const words = value.split(/\s+/).flatMap((raw) => {
389
420
  if (raw === "{x}") return [{ neutral: true }];
390
- const word = raw.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}']+$/gu, "");
421
+ const word = raw
422
+ .replace(/^[^\p{L}\p{N}]+/u, "")
423
+ .replace(/(?<![^\p{L}\p{N}'])[^\p{L}\p{N}']+$/u, "");
391
424
  if (!/\p{L}/u.test(word)) return /\p{N}/u.test(word) ? [{ neutral: true }] : [];
392
- if (!/^[\p{Lu}\p{Ll}]/u.test(word) || /^\p{Ll}+\p{Lu}|[.(]/u.test(word))
425
+ if (!/^[\p{Lu}\p{Ll}]/u.test(word) || /^\p{Ll}+\p{Lu}/u.test(word) || /[.(]/u.test(word))
393
426
  return [{ neutral: true }];
394
427
  const lower = word.toLowerCase();
395
428
  return [
@@ -459,21 +492,34 @@ export function copyFindings(
459
492
  "copy-plural",
460
493
  `${quoted} hedges a plural; count through t()'s plural form (LC-202).`,
461
494
  ]);
495
+ return [
496
+ ...found,
497
+ ...caseFindings(value, quoted, { error, kind, joined, properNames }),
498
+ ...wordFindings(value, quoted, { error, bannedWords }),
499
+ ];
500
+ }
501
+ function caseFindings(value, quoted, { error, kind, joined, properNames }) {
462
502
  const words = caseWords(
463
503
  `${joined.before ? "{x} " : ""}${value}${joined.after ? " {x}" : ""}`,
464
504
  properNames ?? houseProperNames,
465
505
  );
506
+ if (!words) return [];
466
507
  const role = kind ?? (error ? "text" : undefined);
467
- if (words && role === "label" && !sentenceLike(value) && !isTitleStyle(words))
508
+ const found = [];
509
+ if (role === "label" && !sentenceLike(value) && !isTitleStyle(words))
468
510
  found.push([
469
511
  "copy-title-style",
470
512
  `${quoted} needs title-style capitals; it labels a control or titles a surface (LC-202).`,
471
513
  ]);
472
- if (words && role === "text" && readsAsTitleCase(words))
514
+ if (role === "text" && readsAsTitleCase(words))
473
515
  found.push([
474
516
  "copy-title-case",
475
517
  `${quoted} is Title Case; running text is sentence case (LC-202).`,
476
518
  ]);
519
+ return found;
520
+ }
521
+ function wordFindings(value, quoted, { error, bannedWords }) {
522
+ const found = [];
477
523
  const word = value.match(bannedPattern(bannedWords ?? houseBannedWords) ?? /$^/)?.[0];
478
524
  if (word)
479
525
  found.push([
@@ -494,11 +540,24 @@ export function copyFindings(
494
540
  found.push(["zoom-button", `${quoted} is a zoom button; zoom is a gesture (LC-217).`]);
495
541
  return found;
496
542
  }
543
+ function labelOrText(text, labelPattern, textPattern) {
544
+ if (labelPattern.test(text)) return "label";
545
+ if (textPattern.test(text)) return "text";
546
+ return undefined;
547
+ }
497
548
  // LC-202's capitals are English rules: a catalog under another locale is a
498
549
  // translation and keeps its language's own capitals.
499
- const catalogLocale =
500
- /(?:i18n|locales|localizations)[\\/](?:.*[\\/])?([a-z]{2})(?:[-_][A-Za-z]{2,4})?(?:[\\/]|\.(?:json|[cm]?[jt]s)$)/;
501
- const translation = (filename) => (catalogLocale.exec(filename)?.[1] ?? "en") !== "en";
550
+ const catalogRoot = /(?:i18n|locales|localizations)[\\/]/g;
551
+ const catalogLocaleTail =
552
+ /^(?:.*[\\/])?([a-z]{2})(?:[-_][A-Za-z]{2,4})?(?:[\\/]|\.(?:json|[cm]?[jt]s)$)/;
553
+ function catalogLocale(filename) {
554
+ for (const root of filename.matchAll(catalogRoot)) {
555
+ const locale = filename.slice(root.index + root[0].length).match(catalogLocaleTail);
556
+ if (locale) return locale[1];
557
+ }
558
+ return undefined;
559
+ }
560
+ const translation = (filename) => (catalogLocale(filename) ?? "en") !== "en";
502
561
  // A string catalog: a file in an i18n, locales or localizations directory,
503
562
  // directly or under a locale code.
504
563
  const catalogFile =
@@ -945,24 +1004,25 @@ export function inspectDesign(source, filename, projectRoot = process.cwd(), opt
945
1004
  if (tCall(parent) && parent.arguments[0] === child && /\s/.test(node.getText(file))) return {};
946
1005
  return undefined;
947
1006
  }
1007
+ function roleKind(attr) {
1008
+ if (
1009
+ ts.isJsxAttribute(attr) &&
1010
+ /^(?:role|accessibilityRole)$/.test(name(attr.name)) &&
1011
+ attr.initializer &&
1012
+ ts.isStringLiteral(attr.initializer)
1013
+ )
1014
+ return labelOrText(attr.initializer.text, labelRole, textRole);
1015
+ if (ts.isJsxSpreadAttribute(attr) && ts.isPropertyAccessExpression(attr.expression))
1016
+ return labelOrText(attr.expression.name.text, labelSpread, textSpread);
1017
+ return undefined;
1018
+ }
948
1019
  // LC-202 capitals: a copy string's kind comes from where it sits. "label"
949
1020
  // names a control or titles a surface, "text" is running text, "data" is
950
1021
  // a Frappe DocField's own label, and undefined means syntax can't tell.
951
1022
  function tagKind(element) {
952
1023
  for (const attr of element.attributes.properties) {
953
- if (
954
- ts.isJsxAttribute(attr) &&
955
- /^(?:role|accessibilityRole)$/.test(name(attr.name)) &&
956
- attr.initializer &&
957
- ts.isStringLiteral(attr.initializer)
958
- ) {
959
- if (labelRole.test(attr.initializer.text)) return "label";
960
- if (textRole.test(attr.initializer.text)) return "text";
961
- }
962
- if (ts.isJsxSpreadAttribute(attr) && ts.isPropertyAccessExpression(attr.expression)) {
963
- if (labelSpread.test(attr.expression.name.text)) return "label";
964
- if (textSpread.test(attr.expression.name.text)) return "text";
965
- }
1024
+ const kind = roleKind(attr);
1025
+ if (kind) return kind;
966
1026
  }
967
1027
  const tag = tagText(element.tagName);
968
1028
  if (textTag.test(tag)) return "text";
@@ -998,53 +1058,62 @@ export function inspectDesign(source, filename, projectRoot = process.cwd(), opt
998
1058
  }
999
1059
  return undefined;
1000
1060
  }
1061
+ function joinsCopy(parent, child) {
1062
+ if (!ts.isBinaryExpression(parent)) return false;
1063
+ const operator = parent.operatorToken.kind;
1064
+ return (
1065
+ operator === ts.SyntaxKind.PlusToken ||
1066
+ operator === ts.SyntaxKind.QuestionQuestionToken ||
1067
+ operator === ts.SyntaxKind.BarBarToken ||
1068
+ (operator === ts.SyntaxKind.AmpersandAmpersandToken && parent.right === child)
1069
+ );
1070
+ }
1071
+ function passesCopy(parent, child) {
1072
+ return (
1073
+ ts.isParenthesizedExpression(parent) ||
1074
+ ts.isAsExpression(parent) ||
1075
+ ts.isSatisfiesExpression(parent) ||
1076
+ (ts.isConditionalExpression(parent) && parent.condition !== child) ||
1077
+ joinsCopy(parent, child) ||
1078
+ (ts.isArrowFunction(parent) && parent.body === child) ||
1079
+ (tCall(parent) && parent.arguments[0] === child) ||
1080
+ (ts.isJsxExpression(parent) &&
1081
+ !ts.isJsxElement(parent.parent) &&
1082
+ !ts.isJsxFragment(parent.parent))
1083
+ );
1084
+ }
1085
+ function propertyKind(parent, host) {
1086
+ const key = name(parent.name);
1087
+ if (catalog)
1088
+ return translation(filename) ? "translation" : catalogKind(keyPath(parent).reverse());
1089
+ if (
1090
+ parent.parent.properties.some(
1091
+ (prop) => prop.name && /^(?:fieldname|fieldtype)$/.test(name(prop.name)),
1092
+ )
1093
+ )
1094
+ return "data";
1095
+ if (ts.isCallExpression(host) && /toast|notif|snack/i.test(host.expression.getText(file)))
1096
+ return "text";
1097
+ if (
1098
+ /^(?:label|title|heading)$/.test(key) &&
1099
+ parent.parent.properties.some((prop) => prop.name && runningSibling.test(name(prop.name)))
1100
+ )
1101
+ return undefined;
1102
+ return attributeKind(key);
1103
+ }
1001
1104
  function copyKind(node) {
1002
1105
  if (ts.isJsxText(node)) return elementKind(node.parent);
1003
1106
  for (let child = node, parent = node.parent; parent; child = parent, parent = parent.parent) {
1004
- if (
1005
- ts.isParenthesizedExpression(parent) ||
1006
- ts.isAsExpression(parent) ||
1007
- ts.isSatisfiesExpression(parent) ||
1008
- (ts.isConditionalExpression(parent) && parent.condition !== child) ||
1009
- (ts.isBinaryExpression(parent) &&
1010
- (parent.operatorToken.kind === ts.SyntaxKind.PlusToken ||
1011
- parent.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken ||
1012
- parent.operatorToken.kind === ts.SyntaxKind.BarBarToken ||
1013
- (parent.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken &&
1014
- parent.right === child))) ||
1015
- (ts.isArrowFunction(parent) && parent.body === child) ||
1016
- (tCall(parent) && parent.arguments[0] === child)
1017
- )
1018
- continue;
1019
- if (ts.isJsxExpression(parent)) {
1020
- if (ts.isJsxElement(parent.parent) || ts.isJsxFragment(parent.parent))
1021
- return elementKind(parent.parent);
1022
- continue;
1023
- }
1107
+ if (passesCopy(parent, child)) continue;
1108
+ if (ts.isJsxExpression(parent)) return elementKind(parent.parent);
1024
1109
  if (ts.isJsxAttribute(parent)) return attributeKind(name(parent.name), parent.parent.parent);
1025
1110
  if (!ts.isPropertyAssignment(parent) || parent.initializer !== child) return undefined;
1026
- const key = name(parent.name);
1027
1111
  const host = parent.parent.parent;
1028
- if (key === "defaultValue" && tCall(host)) {
1112
+ if (name(parent.name) === "defaultValue" && tCall(host)) {
1029
1113
  parent = host;
1030
1114
  continue;
1031
1115
  }
1032
- if (catalog)
1033
- return translation(filename) ? "translation" : catalogKind(keyPath(parent).reverse());
1034
- if (
1035
- parent.parent.properties.some(
1036
- (prop) => prop.name && /^(?:fieldname|fieldtype)$/.test(name(prop.name)),
1037
- )
1038
- )
1039
- return "data";
1040
- if (ts.isCallExpression(host) && /toast|notif|snack/i.test(host.expression.getText(file)))
1041
- return "text";
1042
- if (
1043
- /^(?:label|title|heading)$/.test(key) &&
1044
- parent.parent.properties.some((prop) => prop.name && runningSibling.test(name(prop.name)))
1045
- )
1046
- return undefined;
1047
- return attributeKind(key);
1116
+ return propertyKind(parent, host);
1048
1117
  }
1049
1118
  return undefined;
1050
1119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@multiplatform.one/config",
3
- "version": "9.0.0",
3
+ "version": "9.0.2",
4
4
  "description": "Shared build and test configuration presets for multiplatform.one",
5
5
  "keywords": [
6
6
  "config",
@@ -122,7 +122,7 @@
122
122
  "@babel/parser": "^7.29.9",
123
123
  "@babel/plugin-transform-block-scoping": "^7.29.7",
124
124
  "@babel/traverse": "^7.29.10",
125
- "@multiplatform.one/utils": "^9.0.0",
125
+ "@multiplatform.one/utils": "^9.0.2",
126
126
  "@tamagui/vite-plugin": "2.7.6",
127
127
  "@vitejs/plugin-react": "^6.1.2",
128
128
  "dotenv": "^17.4.2",