@noctcore/eslint-plugin-contracts 0.3.0 → 0.4.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/dist/index.cjs CHANGED
@@ -47,13 +47,14 @@ var recommended = {
47
47
  "noctcore-contracts/restrict-throw-to-taxonomy": "error",
48
48
  "noctcore-contracts/schema-enum-field-consistency": "error",
49
49
  "noctcore-contracts/fetch-must-check-ok": "error",
50
- // Config-required / heuristic rules ship inert. `require-registered-keys` and
51
- // `env-var-schema-parity` do nothing until their `sinks` / `schema` options are
52
- // set; `require-schema-parse-at-boundary` is a conservative syntactic slice of a
50
+ // Config-required / heuristic rules ship inert. `require-registered-keys`,
51
+ // `env-var-schema-parity` and `translation-key-exists` do nothing until their
52
+ // `sinks` / `schema` / `catalogs` options are set; `require-schema-parse-at-boundary` is a conservative syntactic slice of a
53
53
  // type-aware concern. Enable them explicitly once configured for your project.
54
54
  "noctcore-contracts/require-registered-keys": "off",
55
55
  "noctcore-contracts/env-var-schema-parity": "off",
56
- "noctcore-contracts/require-schema-parse-at-boundary": "off"
56
+ "noctcore-contracts/require-schema-parse-at-boundary": "off",
57
+ "noctcore-contracts/translation-key-exists": "off"
57
58
  };
58
59
 
59
60
  // src/rules/env-var-schema-parity.ts
@@ -594,8 +595,8 @@ var fetchMustCheckOkRule = createRule({
594
595
  }
595
596
  return {
596
597
  CallExpression(node) {
597
- const path2 = calleePath(node.callee);
598
- if (path2 === null || !fetchFunctions.has(path2)) {
598
+ const path3 = calleePath(node.callee);
599
+ if (path3 === null || !fetchFunctions.has(path3)) {
599
600
  return;
600
601
  }
601
602
  const parent = skipAwait(node.parent);
@@ -1094,11 +1095,11 @@ var requireRegisteredKeysRule = createRule({
1094
1095
  const registryHint = registry ? ` (import it from '${registry}')` : "";
1095
1096
  return {
1096
1097
  CallExpression(node) {
1097
- const path2 = calleePath2(node.callee);
1098
- if (path2 === null) {
1098
+ const path3 = calleePath2(node.callee);
1099
+ if (path3 === null) {
1099
1100
  return;
1100
1101
  }
1101
- const indexes = sinkMap.get(path2);
1102
+ const indexes = sinkMap.get(path3);
1102
1103
  if (indexes === void 0) {
1103
1104
  return;
1104
1105
  }
@@ -1108,7 +1109,7 @@ var requireRegisteredKeysRule = createRule({
1108
1109
  context.report({
1109
1110
  node: arg,
1110
1111
  messageId: "unregisteredKey",
1111
- data: { callee: path2, value: `'${arg.value}'`, registryHint }
1112
+ data: { callee: path3, value: `'${arg.value}'`, registryHint }
1112
1113
  });
1113
1114
  }
1114
1115
  }
@@ -1409,10 +1410,689 @@ var schemaEnumFieldConsistencyRule = createRule({
1409
1410
  }
1410
1411
  });
1411
1412
 
1413
+ // src/i18n/catalogs.ts
1414
+ var import_node_fs2 = require("fs");
1415
+ var import_node_path2 = __toESM(require("path"), 1);
1416
+ var NS_PLACEHOLDER = "{ns}";
1417
+ var SAFE_NAMESPACE = /^(?!\.{1,2}$)[^/\\\0]+$/u;
1418
+ var fileCache = /* @__PURE__ */ new Map();
1419
+ function readCatalogFile(absolute) {
1420
+ let mtimeMs;
1421
+ try {
1422
+ const stats = (0, import_node_fs2.statSync)(absolute);
1423
+ if (!stats.isFile()) return { kind: "missing" };
1424
+ mtimeMs = stats.mtimeMs;
1425
+ } catch {
1426
+ return { kind: "missing" };
1427
+ }
1428
+ const cached = fileCache.get(absolute);
1429
+ if (cached !== void 0 && cached.mtimeMs === mtimeMs) {
1430
+ return cached.value.ok ? { kind: "ok", entry: cached } : { kind: "invalid", reason: cached.value.reason };
1431
+ }
1432
+ let value;
1433
+ try {
1434
+ value = { ok: true, json: JSON.parse((0, import_node_fs2.readFileSync)(absolute, "utf8")) };
1435
+ } catch (error) {
1436
+ value = { ok: false, reason: error instanceof Error ? error.message : String(error) };
1437
+ }
1438
+ const entry = { mtimeMs, value, flattened: /* @__PURE__ */ new Map() };
1439
+ fileCache.set(absolute, entry);
1440
+ return value.ok ? { kind: "ok", entry } : { kind: "invalid", reason: value.reason };
1441
+ }
1442
+ function isRecord(value) {
1443
+ return value !== null && typeof value === "object";
1444
+ }
1445
+ function descend(json, keyPath) {
1446
+ if (keyPath === void 0 || keyPath === "") return json;
1447
+ let current = json;
1448
+ for (const segment of keyPath.split(".")) {
1449
+ if (!isRecord(current) || !Object.hasOwn(current, segment)) return void 0;
1450
+ current = current[segment];
1451
+ }
1452
+ return current;
1453
+ }
1454
+ function flatten(root, keySeparator, label) {
1455
+ const leaves = /* @__PURE__ */ new Set();
1456
+ const branches = /* @__PURE__ */ new Set();
1457
+ const visit = (value, prefix) => {
1458
+ if (!isRecord(value)) {
1459
+ leaves.add(prefix);
1460
+ return;
1461
+ }
1462
+ branches.add(prefix);
1463
+ if (keySeparator === false) return;
1464
+ for (const [key, child] of Object.entries(value)) {
1465
+ visit(child, `${prefix}${keySeparator}${key}`);
1466
+ }
1467
+ };
1468
+ for (const [key, child] of Object.entries(root)) {
1469
+ if (keySeparator === false) {
1470
+ (isRecord(child) ? branches : leaves).add(key);
1471
+ } else {
1472
+ visit(child, key);
1473
+ }
1474
+ }
1475
+ return { label, leaves, branches };
1476
+ }
1477
+ function loadSource(cwd, file, keyPath, keySeparator) {
1478
+ const absolute = import_node_path2.default.isAbsolute(file) ? file : import_node_path2.default.resolve(cwd, file);
1479
+ const read = readCatalogFile(absolute);
1480
+ if (read.kind === "missing") return { kind: "absent" };
1481
+ if (read.kind === "invalid") return { kind: "error", reason: `${file}: ${read.reason}` };
1482
+ const cacheKey = `${keyPath ?? ""}\0${keySeparator === false ? "" : keySeparator}`;
1483
+ const cached = read.entry.flattened.get(cacheKey);
1484
+ if (cached !== void 0) {
1485
+ return cached === null ? { kind: "absent" } : { kind: "ok", catalog: cached };
1486
+ }
1487
+ const subtree = read.entry.value.ok ? descend(read.entry.value.json, keyPath) : void 0;
1488
+ const label = keyPath ? `${file}#${keyPath}` : file;
1489
+ const catalog = isRecord(subtree) ? flatten(subtree, keySeparator, label) : null;
1490
+ read.entry.flattened.set(cacheKey, catalog);
1491
+ return catalog === null ? { kind: "absent" } : { kind: "ok", catalog };
1492
+ }
1493
+ function catalogsForNamespace(namespace, sources, settings) {
1494
+ const catalogs = [];
1495
+ const errors = [];
1496
+ for (const source of sources) {
1497
+ const templated = source.file.includes(NS_PLACEHOLDER) || (source.keyPath?.includes(NS_PLACEHOLDER) ?? false);
1498
+ if (templated) {
1499
+ if (!SAFE_NAMESPACE.test(namespace)) continue;
1500
+ const file = source.file.replaceAll(NS_PLACEHOLDER, namespace);
1501
+ const keyPath = source.keyPath?.replaceAll(NS_PLACEHOLDER, namespace);
1502
+ const load2 = loadSource(settings.cwd, file, keyPath, settings.keySeparator);
1503
+ if (load2.kind === "ok") catalogs.push(load2.catalog);
1504
+ else if (load2.kind === "error") errors.push(load2.reason);
1505
+ continue;
1506
+ }
1507
+ if ((source.namespace ?? settings.defaultNamespace) !== namespace) continue;
1508
+ const load = loadSource(settings.cwd, source.file, source.keyPath, settings.keySeparator);
1509
+ if (load.kind === "ok") catalogs.push(load.catalog);
1510
+ else if (load.kind === "error") errors.push(load.reason);
1511
+ else {
1512
+ const where = source.keyPath ? `${source.file}#${source.keyPath}` : source.file;
1513
+ errors.push(`${where}: not found or not a JSON object`);
1514
+ }
1515
+ }
1516
+ return { catalogs, errors };
1517
+ }
1518
+ var PLURAL_CATEGORIES = ["zero", "one", "two", "few", "many", "other"];
1519
+ function catalogHasKey(catalog, key, lookup) {
1520
+ if (catalog.leaves.has(key)) return true;
1521
+ if (lookup.returnObjects && catalog.branches.has(key)) return true;
1522
+ if (lookup.plural) {
1523
+ const sep = lookup.pluralSeparator;
1524
+ for (const category of PLURAL_CATEGORIES) {
1525
+ if (catalog.leaves.has(`${key}${sep}${category}`)) return true;
1526
+ if (catalog.leaves.has(`${key}${sep}ordinal${sep}${category}`)) return true;
1527
+ }
1528
+ }
1529
+ if (lookup.context) {
1530
+ const variant = `${key}${lookup.contextSeparator}`;
1531
+ for (const leaf of catalog.leaves) {
1532
+ if (leaf.startsWith(variant)) return true;
1533
+ }
1534
+ }
1535
+ return false;
1536
+ }
1537
+ function catalogHasPrefix(catalog, prefix) {
1538
+ for (const leaf of catalog.leaves) {
1539
+ if (leaf.startsWith(prefix)) return true;
1540
+ }
1541
+ for (const branch of catalog.branches) {
1542
+ if (branch.startsWith(prefix)) return true;
1543
+ }
1544
+ return false;
1545
+ }
1546
+
1547
+ // src/i18n/translationUsage.ts
1548
+ var import_utils11 = require("@typescript-eslint/utils");
1549
+ var UNRESOLVED = "unresolved";
1550
+ var MAX_DEPTH = 8;
1551
+ function unwrap(node) {
1552
+ let current = node;
1553
+ while (current.type === import_utils11.AST_NODE_TYPES.TSAsExpression || current.type === import_utils11.AST_NODE_TYPES.TSSatisfiesExpression || current.type === import_utils11.AST_NODE_TYPES.TSNonNullExpression) {
1554
+ current = current.expression;
1555
+ }
1556
+ return current;
1557
+ }
1558
+ function staticString(node) {
1559
+ const inner = unwrap(node);
1560
+ if (inner.type === import_utils11.AST_NODE_TYPES.Literal && typeof inner.value === "string") return inner.value;
1561
+ if (inner.type === import_utils11.AST_NODE_TYPES.TemplateLiteral && inner.expressions.length === 0) {
1562
+ return inner.quasis[0]?.value.cooked ?? null;
1563
+ }
1564
+ return null;
1565
+ }
1566
+ function propertyName2(property) {
1567
+ if (property.computed) return staticString(property.key);
1568
+ if (property.key.type === import_utils11.AST_NODE_TYPES.Identifier) return property.key.name;
1569
+ return staticString(property.key);
1570
+ }
1571
+ function targetIdentifier(node) {
1572
+ if (node.type === import_utils11.AST_NODE_TYPES.Identifier) return node;
1573
+ if (node.type === import_utils11.AST_NODE_TYPES.AssignmentPattern && node.left.type === import_utils11.AST_NODE_TYPES.Identifier) {
1574
+ return node.left;
1575
+ }
1576
+ return null;
1577
+ }
1578
+ function createTranslationVisitor(context, settings, onUsage) {
1579
+ const sourceCode = context.sourceCode;
1580
+ const defaultBinding = { namespaces: [settings.defaultNamespace], keyPrefix: null };
1581
+ function resolveVariable(identifier) {
1582
+ let scope = sourceCode.getScope(identifier);
1583
+ while (scope !== null) {
1584
+ const variable = scope.set.get(identifier.name);
1585
+ if (variable !== void 0) return variable;
1586
+ scope = scope.upper;
1587
+ }
1588
+ return null;
1589
+ }
1590
+ function typedStringLiteral(node) {
1591
+ const services = sourceCode.parserServices;
1592
+ const program = services?.program;
1593
+ const map = services?.esTreeNodeToTSNodeMap;
1594
+ if (!program || !map) return null;
1595
+ const type = program.getTypeChecker().getTypeAtLocation(map.get(node));
1596
+ return type.isStringLiteral() ? type.value : null;
1597
+ }
1598
+ function resolveNamespaces(node, depth = 0) {
1599
+ if (node === void 0) return defaultBinding.namespaces;
1600
+ const inner = unwrap(node);
1601
+ const literal = staticString(inner);
1602
+ if (literal !== null) return [literal];
1603
+ if (inner.type === import_utils11.AST_NODE_TYPES.Literal && inner.value === null) return defaultBinding.namespaces;
1604
+ if (inner.type === import_utils11.AST_NODE_TYPES.ArrayExpression) {
1605
+ const namespaces = [];
1606
+ for (const element of inner.elements) {
1607
+ if (element === null || element.type === import_utils11.AST_NODE_TYPES.SpreadElement) return UNRESOLVED;
1608
+ const value2 = staticString(element) ?? resolveIdentifierString(element, depth);
1609
+ if (value2 === null) return UNRESOLVED;
1610
+ namespaces.push(value2);
1611
+ }
1612
+ return namespaces.length > 0 ? namespaces : defaultBinding.namespaces;
1613
+ }
1614
+ if (inner.type === import_utils11.AST_NODE_TYPES.Identifier && inner.name === "undefined") return defaultBinding.namespaces;
1615
+ const value = resolveIdentifierString(inner, depth);
1616
+ return value === null ? UNRESOLVED : [value];
1617
+ }
1618
+ function resolveIdentifierString(node, depth) {
1619
+ if (node.type !== import_utils11.AST_NODE_TYPES.Identifier || depth > MAX_DEPTH) return null;
1620
+ if (Object.hasOwn(settings.namespaceIdentifiers, node.name)) {
1621
+ return settings.namespaceIdentifiers[node.name] ?? null;
1622
+ }
1623
+ const definition = resolveVariable(node)?.defs[0];
1624
+ if (definition?.type === "Variable" && definition.parent.kind === "const" && definition.node.id.type === import_utils11.AST_NODE_TYPES.Identifier && definition.node.init !== null) {
1625
+ const init = unwrap(definition.node.init);
1626
+ const literal = staticString(init);
1627
+ if (literal !== null) return literal;
1628
+ const chained = resolveIdentifierString(init, depth + 1);
1629
+ if (chained !== null) return chained;
1630
+ }
1631
+ return typedStringLiteral(node);
1632
+ }
1633
+ function isHookCall(node) {
1634
+ return node.type === import_utils11.AST_NODE_TYPES.CallExpression && node.callee.type === import_utils11.AST_NODE_TYPES.Identifier && settings.hooks.has(node.callee.name);
1635
+ }
1636
+ function isInstance(node) {
1637
+ return node.type === import_utils11.AST_NODE_TYPES.Identifier && settings.instances.has(node.name);
1638
+ }
1639
+ function staticPrefix(node) {
1640
+ if (node === void 0) return null;
1641
+ const inner = unwrap(node);
1642
+ if (inner.type === import_utils11.AST_NODE_TYPES.Identifier && inner.name === "undefined") return null;
1643
+ if (inner.type === import_utils11.AST_NODE_TYPES.Literal && inner.value === null) return null;
1644
+ return staticString(inner) ?? UNRESOLVED;
1645
+ }
1646
+ function bindingFromHook(call) {
1647
+ const [nsArg, optionsArg] = call.arguments;
1648
+ const namespaces = resolveNamespaces(nsArg);
1649
+ if (namespaces === UNRESOLVED) return UNRESOLVED;
1650
+ let keyPrefix = null;
1651
+ if (optionsArg !== void 0) {
1652
+ const options = unwrap(optionsArg);
1653
+ if (options.type !== import_utils11.AST_NODE_TYPES.ObjectExpression) return UNRESOLVED;
1654
+ for (const property of options.properties) {
1655
+ if (property.type !== import_utils11.AST_NODE_TYPES.Property) return UNRESOLVED;
1656
+ if (propertyName2(property) !== "keyPrefix") continue;
1657
+ const prefix = staticPrefix(property.value);
1658
+ if (prefix === UNRESOLVED) return UNRESOLVED;
1659
+ keyPrefix = prefix;
1660
+ }
1661
+ }
1662
+ return { namespaces, keyPrefix };
1663
+ }
1664
+ function bindingFromGetFixedT(call) {
1665
+ const [, nsArg, prefixArg] = call.arguments;
1666
+ const namespaces = resolveNamespaces(nsArg);
1667
+ if (namespaces === UNRESOLVED) return UNRESOLVED;
1668
+ const keyPrefix = staticPrefix(prefixArg);
1669
+ if (keyPrefix === UNRESOLVED) return UNRESOLVED;
1670
+ return { namespaces, keyPrefix };
1671
+ }
1672
+ function isGetFixedT(node) {
1673
+ return node.type === import_utils11.AST_NODE_TYPES.CallExpression && node.callee.type === import_utils11.AST_NODE_TYPES.MemberExpression && !node.callee.computed && node.callee.property.type === import_utils11.AST_NODE_TYPES.Identifier && node.callee.property.name === "getFixedT" && isInstance(node.callee.object);
1674
+ }
1675
+ function hookCallOf(identifier) {
1676
+ const definition = resolveVariable(identifier)?.defs[0];
1677
+ if (definition?.type !== "Variable" || definition.node.id.type !== import_utils11.AST_NODE_TYPES.Identifier) return null;
1678
+ const init = definition.node.init === null ? null : unwrap(definition.node.init);
1679
+ return init !== null && isHookCall(init) ? init : null;
1680
+ }
1681
+ function bindingOfTSource(object) {
1682
+ const inner = unwrap(object);
1683
+ if (isHookCall(inner)) return bindingFromHook(inner);
1684
+ if (inner.type === import_utils11.AST_NODE_TYPES.Identifier) {
1685
+ const hook = hookCallOf(inner);
1686
+ if (hook !== null) return bindingFromHook(hook);
1687
+ if (isInstance(inner)) return defaultBinding;
1688
+ }
1689
+ return null;
1690
+ }
1691
+ function bindingFromType(annotation) {
1692
+ const type = annotation?.typeAnnotation;
1693
+ if (type?.type !== import_utils11.AST_NODE_TYPES.TSTypeReference) return null;
1694
+ const name = type.typeName.type === import_utils11.AST_NODE_TYPES.Identifier ? type.typeName.name : type.typeName.type === import_utils11.AST_NODE_TYPES.TSQualifiedName ? type.typeName.right.name : null;
1695
+ if (name === null || !settings.typeNames.has(name)) return null;
1696
+ const [nsType, prefixType] = type.typeArguments?.params ?? [];
1697
+ const literalOf = (node) => node.type === import_utils11.AST_NODE_TYPES.TSLiteralType ? staticString(node.literal) : null;
1698
+ let namespaces = defaultBinding.namespaces;
1699
+ if (nsType !== void 0) {
1700
+ if (nsType.type === import_utils11.AST_NODE_TYPES.TSTupleType) {
1701
+ const values = nsType.elementTypes.map(literalOf);
1702
+ if (values.length === 0 || values.some((value) => value === null)) return UNRESOLVED;
1703
+ namespaces = values;
1704
+ } else {
1705
+ const value = literalOf(nsType);
1706
+ if (value === null) return UNRESOLVED;
1707
+ namespaces = [value];
1708
+ }
1709
+ }
1710
+ let keyPrefix = null;
1711
+ if (prefixType !== void 0) {
1712
+ keyPrefix = literalOf(prefixType);
1713
+ if (keyPrefix === null) return UNRESOLVED;
1714
+ }
1715
+ return { namespaces, keyPrefix };
1716
+ }
1717
+ function bindingFromDeclarator(declarator, name, depth) {
1718
+ if (declarator.init === null) return null;
1719
+ const init = unwrap(declarator.init);
1720
+ const id = declarator.id;
1721
+ if (id.type === import_utils11.AST_NODE_TYPES.Identifier) {
1722
+ if (isGetFixedT(init)) return bindingFromGetFixedT(init);
1723
+ if (init.type === import_utils11.AST_NODE_TYPES.MemberExpression && !init.computed && init.property.type === import_utils11.AST_NODE_TYPES.Identifier && init.property.name === "t") {
1724
+ return bindingOfTSource(init.object);
1725
+ }
1726
+ if (init.type === import_utils11.AST_NODE_TYPES.Identifier) return bindingOfIdentifier(init, depth + 1);
1727
+ return null;
1728
+ }
1729
+ if (id.type === import_utils11.AST_NODE_TYPES.ObjectPattern) {
1730
+ for (const property of id.properties) {
1731
+ if (property.type !== import_utils11.AST_NODE_TYPES.Property || targetIdentifier(property.value) !== name) continue;
1732
+ return propertyName2(property) === "t" ? bindingOfTSource(init) : null;
1733
+ }
1734
+ return null;
1735
+ }
1736
+ if (id.type === import_utils11.AST_NODE_TYPES.ArrayPattern) {
1737
+ const first = id.elements[0];
1738
+ if (first && targetIdentifier(first) === name && isHookCall(init)) return bindingFromHook(init);
1739
+ }
1740
+ return null;
1741
+ }
1742
+ function bindingOfIdentifier(identifier, depth = 0) {
1743
+ if (depth > MAX_DEPTH) return null;
1744
+ const variable = resolveVariable(identifier);
1745
+ if (variable === null) {
1746
+ return settings.functions.has(identifier.name) ? defaultBinding : null;
1747
+ }
1748
+ const definition = variable.defs[0];
1749
+ if (definition === void 0) return null;
1750
+ switch (definition.type) {
1751
+ case "ImportBinding": {
1752
+ const specifier = definition.node;
1753
+ if (specifier.type !== import_utils11.AST_NODE_TYPES.ImportSpecifier) return null;
1754
+ const imported = specifier.imported.type === import_utils11.AST_NODE_TYPES.Identifier ? specifier.imported.name : specifier.imported.value;
1755
+ return settings.functions.has(imported) ? defaultBinding : null;
1756
+ }
1757
+ case "Parameter": {
1758
+ const name = definition.name;
1759
+ if (name.type !== import_utils11.AST_NODE_TYPES.Identifier) return null;
1760
+ const typed = bindingFromType(name.typeAnnotation);
1761
+ if (typed !== null) return typed;
1762
+ return settings.functions.has(name.name) ? UNRESOLVED : null;
1763
+ }
1764
+ case "Variable":
1765
+ return definition.name.type === import_utils11.AST_NODE_TYPES.Identifier ? bindingFromDeclarator(definition.node, definition.name, depth) : null;
1766
+ default:
1767
+ return null;
1768
+ }
1769
+ }
1770
+ function bindingOfCallee(callee) {
1771
+ if (callee.type === import_utils11.AST_NODE_TYPES.Identifier) return bindingOfIdentifier(callee);
1772
+ if (callee.type === import_utils11.AST_NODE_TYPES.MemberExpression && !callee.computed && callee.property.type === import_utils11.AST_NODE_TYPES.Identifier && callee.property.name === "t") {
1773
+ return bindingOfTSource(callee.object);
1774
+ }
1775
+ return null;
1776
+ }
1777
+ function readCallOptions(node) {
1778
+ const none = { namespaces: null, plural: false, context: false, returnObjects: false };
1779
+ if (node === void 0) return none;
1780
+ const inner = unwrap(node);
1781
+ if (inner.type !== import_utils11.AST_NODE_TYPES.ObjectExpression) return UNRESOLVED;
1782
+ let namespaces = null;
1783
+ let plural = false;
1784
+ let context2 = false;
1785
+ let returnObjects = false;
1786
+ for (const property of inner.properties) {
1787
+ if (property.type !== import_utils11.AST_NODE_TYPES.Property) return UNRESOLVED;
1788
+ const name = propertyName2(property);
1789
+ if (name === null || name === "keyPrefix") return UNRESOLVED;
1790
+ if (name === "ns") {
1791
+ const resolved = resolveNamespaces(property.value);
1792
+ if (resolved === UNRESOLVED) return UNRESOLVED;
1793
+ namespaces = resolved;
1794
+ } else if (name === "count") {
1795
+ plural = true;
1796
+ } else if (name === "context") {
1797
+ context2 = true;
1798
+ } else if (name === "returnObjects") {
1799
+ const value = unwrap(property.value);
1800
+ returnObjects = !(value.type === import_utils11.AST_NODE_TYPES.Literal && value.value === false);
1801
+ }
1802
+ }
1803
+ return { namespaces, plural, context: context2, returnObjects };
1804
+ }
1805
+ function qualify(raw, binding, optionNamespaces) {
1806
+ const { nsSeparator, keySeparator } = settings;
1807
+ if (nsSeparator !== false && raw.includes(nsSeparator)) {
1808
+ if (binding.keyPrefix !== null) return null;
1809
+ const [head = "", ...rest] = raw.split(nsSeparator);
1810
+ if (head !== "" && rest.length > 0) {
1811
+ return { namespaces: [head], key: rest.join(keySeparator === false ? nsSeparator : keySeparator) };
1812
+ }
1813
+ }
1814
+ const namespaces = optionNamespaces ?? binding.namespaces;
1815
+ if (binding.keyPrefix === null || binding.keyPrefix === "") return { namespaces, key: raw };
1816
+ return { namespaces, key: `${binding.keyPrefix}${keySeparator === false ? "" : keySeparator}${raw}` };
1817
+ }
1818
+ function emit(node, keyNode, binding, options) {
1819
+ const inner = unwrap(keyNode);
1820
+ const raws = [];
1821
+ const single = staticString(inner);
1822
+ if (single !== null) {
1823
+ raws.push(single);
1824
+ } else if (inner.type === import_utils11.AST_NODE_TYPES.ArrayExpression && inner.elements.length > 0) {
1825
+ for (const element of inner.elements) {
1826
+ const value = element === null || element.type === import_utils11.AST_NODE_TYPES.SpreadElement ? null : staticString(element);
1827
+ if (value === null) {
1828
+ onUsage({ kind: "dynamic", node });
1829
+ return;
1830
+ }
1831
+ raws.push(value);
1832
+ }
1833
+ } else if (inner.type === import_utils11.AST_NODE_TYPES.TemplateLiteral) {
1834
+ const head = inner.quasis[0]?.value.cooked ?? "";
1835
+ const qualified = head === "" ? null : qualify(head, binding, options.namespaces);
1836
+ if (qualified === null) {
1837
+ onUsage({ kind: "dynamic", node });
1838
+ } else {
1839
+ onUsage({ kind: "prefix", node, namespaces: qualified.namespaces, prefix: qualified.key });
1840
+ }
1841
+ return;
1842
+ } else {
1843
+ onUsage({ kind: "dynamic", node });
1844
+ return;
1845
+ }
1846
+ let namespaces = null;
1847
+ const keys = [];
1848
+ for (const raw of raws) {
1849
+ const qualified = qualify(raw, binding, options.namespaces);
1850
+ if (qualified === null || raw === "") {
1851
+ onUsage({ kind: "unresolved", node });
1852
+ return;
1853
+ }
1854
+ if (namespaces !== null && namespaces.join("\0") !== qualified.namespaces.join("\0")) {
1855
+ onUsage({ kind: "unresolved", node });
1856
+ return;
1857
+ }
1858
+ namespaces = qualified.namespaces;
1859
+ keys.push(qualified.key);
1860
+ }
1861
+ onUsage({
1862
+ kind: "key",
1863
+ node,
1864
+ namespaces: namespaces ?? binding.namespaces,
1865
+ keys,
1866
+ plural: options.plural,
1867
+ context: options.context,
1868
+ returnObjects: options.returnObjects
1869
+ });
1870
+ }
1871
+ function jsxAttributeValue(attribute) {
1872
+ const value = attribute.value;
1873
+ if (value === null) return null;
1874
+ if (value.type === import_utils11.AST_NODE_TYPES.JSXExpressionContainer) {
1875
+ return value.expression.type === import_utils11.AST_NODE_TYPES.JSXEmptyExpression ? null : value.expression;
1876
+ }
1877
+ return value;
1878
+ }
1879
+ return {
1880
+ CallExpression(node) {
1881
+ const binding = bindingOfCallee(node.callee);
1882
+ if (binding === null) return;
1883
+ const [keyArg, secondArg, thirdArg] = node.arguments;
1884
+ if (keyArg === void 0) return;
1885
+ if (binding === UNRESOLVED) {
1886
+ onUsage({ kind: "unresolved", node: keyArg });
1887
+ return;
1888
+ }
1889
+ const optionsArg = secondArg !== void 0 && staticString(secondArg) !== null ? thirdArg : secondArg;
1890
+ const options = readCallOptions(optionsArg);
1891
+ if (options === UNRESOLVED) {
1892
+ onUsage({ kind: "unresolved", node: keyArg });
1893
+ return;
1894
+ }
1895
+ emit(keyArg, keyArg, binding, options);
1896
+ },
1897
+ JSXOpeningElement(node) {
1898
+ if (node.name.type !== import_utils11.AST_NODE_TYPES.JSXIdentifier || !settings.transComponents.has(node.name.name)) return;
1899
+ const attributes = /* @__PURE__ */ new Map();
1900
+ for (const attribute of node.attributes) {
1901
+ if (attribute.type === import_utils11.AST_NODE_TYPES.JSXSpreadAttribute) {
1902
+ onUsage({ kind: "unresolved", node });
1903
+ return;
1904
+ }
1905
+ if (attribute.name.type === import_utils11.AST_NODE_TYPES.JSXIdentifier) attributes.set(attribute.name.name, attribute);
1906
+ }
1907
+ const keyAttribute = attributes.get("i18nKey");
1908
+ const keyNode = keyAttribute === void 0 ? null : jsxAttributeValue(keyAttribute);
1909
+ if (keyNode === null) return;
1910
+ let binding = defaultBinding;
1911
+ const tAttribute = attributes.get("t");
1912
+ const tNode = tAttribute === void 0 ? null : jsxAttributeValue(tAttribute);
1913
+ if (tNode !== null) {
1914
+ binding = tNode.type === import_utils11.AST_NODE_TYPES.Identifier ? bindingOfIdentifier(tNode) : UNRESOLVED;
1915
+ }
1916
+ let namespaces = null;
1917
+ const nsAttribute = attributes.get("ns");
1918
+ const nsNode = nsAttribute === void 0 ? null : jsxAttributeValue(nsAttribute);
1919
+ if (nsNode !== null) {
1920
+ const resolved = resolveNamespaces(nsNode);
1921
+ if (resolved === UNRESOLVED) binding = UNRESOLVED;
1922
+ else namespaces = resolved;
1923
+ }
1924
+ if (binding === null || binding === UNRESOLVED) {
1925
+ onUsage({ kind: "unresolved", node: keyNode });
1926
+ return;
1927
+ }
1928
+ emit(keyNode, keyNode, binding, {
1929
+ namespaces,
1930
+ plural: attributes.has("count"),
1931
+ context: attributes.has("context"),
1932
+ returnObjects: false
1933
+ });
1934
+ }
1935
+ };
1936
+ }
1937
+
1938
+ // src/rules/translation-key-exists.ts
1939
+ var RULE_NAME11 = "translation-key-exists";
1940
+ var stringList = { type: "array", items: { type: "string", minLength: 1 }, uniqueItems: true };
1941
+ var separator = { oneOf: [{ type: "string", minLength: 1 }, { type: "boolean", enum: [false] }] };
1942
+ var optionSchema9 = {
1943
+ type: "object",
1944
+ additionalProperties: false,
1945
+ properties: {
1946
+ catalogs: {
1947
+ type: "array",
1948
+ items: {
1949
+ type: "object",
1950
+ additionalProperties: false,
1951
+ required: ["file"],
1952
+ properties: {
1953
+ file: { type: "string", minLength: 1 },
1954
+ namespace: { type: "string", minLength: 1 },
1955
+ keyPath: { type: "string", minLength: 1 }
1956
+ }
1957
+ }
1958
+ },
1959
+ defaultNamespace: { type: "string", minLength: 1 },
1960
+ fallbackNamespaces: stringList,
1961
+ hooks: stringList,
1962
+ instances: stringList,
1963
+ functions: stringList,
1964
+ typeNames: stringList,
1965
+ transComponents: stringList,
1966
+ namespaceIdentifiers: { type: "object", additionalProperties: { type: "string", minLength: 1 } },
1967
+ nsSeparator: separator,
1968
+ keySeparator: separator,
1969
+ pluralSeparator: { type: "string", minLength: 1 },
1970
+ contextSeparator: { type: "string", minLength: 1 },
1971
+ dynamicKeys: { type: "string", enum: ["ignore", "check-prefix"] }
1972
+ }
1973
+ };
1974
+ var TRANSLATION_DEFAULTS = {
1975
+ defaultNamespace: "translation",
1976
+ hooks: ["useTranslation"],
1977
+ instances: ["i18n", "i18next"],
1978
+ functions: ["t"],
1979
+ typeNames: ["TFunction"],
1980
+ transComponents: ["Trans"],
1981
+ nsSeparator: ":",
1982
+ keySeparator: ".",
1983
+ pluralSeparator: "_",
1984
+ contextSeparator: "_"
1985
+ };
1986
+ function translationSettingsOf(options) {
1987
+ return {
1988
+ hooks: new Set(options.hooks ?? TRANSLATION_DEFAULTS.hooks),
1989
+ instances: new Set(options.instances ?? TRANSLATION_DEFAULTS.instances),
1990
+ functions: new Set(options.functions ?? TRANSLATION_DEFAULTS.functions),
1991
+ typeNames: new Set(options.typeNames ?? TRANSLATION_DEFAULTS.typeNames),
1992
+ transComponents: new Set(options.transComponents ?? TRANSLATION_DEFAULTS.transComponents),
1993
+ namespaceIdentifiers: options.namespaceIdentifiers ?? {},
1994
+ defaultNamespace: options.defaultNamespace ?? TRANSLATION_DEFAULTS.defaultNamespace,
1995
+ nsSeparator: options.nsSeparator ?? TRANSLATION_DEFAULTS.nsSeparator,
1996
+ keySeparator: options.keySeparator ?? TRANSLATION_DEFAULTS.keySeparator
1997
+ };
1998
+ }
1999
+ var translationKeyExistsRule = createRule({
2000
+ name: RULE_NAME11,
2001
+ meta: {
2002
+ type: "problem",
2003
+ docs: {
2004
+ description: "Require every static i18next / react-i18next translation key (`t(...)`, `i18n.t(...)`, `<Trans i18nKey>`) to exist in the catalog of the namespace in scope."
2005
+ },
2006
+ schema: [optionSchema9],
2007
+ messages: {
2008
+ missingKey: "Translation key `{{key}}` does not exist in namespace `{{namespace}}` ({{catalogs}}). It renders as the raw key at runtime: fix the key or add it to the catalog.",
2009
+ missingKeyPrefix: "No key in namespace `{{namespace}}` ({{catalogs}}) starts with `{{prefix}}`, so this template key can never resolve.",
2010
+ unknownNamespace: "Namespace `{{namespace}}` has no catalog in the rule configuration. Fix the namespace name or add a `catalogs` entry for it.",
2011
+ catalogUnreadable: "Translation catalog could not be loaded: {{reason}}."
2012
+ }
2013
+ },
2014
+ defaultOptions: [{}],
2015
+ create(context, [options]) {
2016
+ const sources = options.catalogs ?? [];
2017
+ if (sources.length === 0) {
2018
+ return {};
2019
+ }
2020
+ const settings = translationSettingsOf(options);
2021
+ const fallbackNamespaces = options.fallbackNamespaces ?? [];
2022
+ const catalogSettings = {
2023
+ cwd: context.cwd,
2024
+ defaultNamespace: settings.defaultNamespace,
2025
+ keySeparator: settings.keySeparator
2026
+ };
2027
+ const lookupBase = {
2028
+ pluralSeparator: options.pluralSeparator ?? TRANSLATION_DEFAULTS.pluralSeparator,
2029
+ contextSeparator: options.contextSeparator ?? TRANSLATION_DEFAULTS.contextSeparator
2030
+ };
2031
+ const checkPrefix = options.dynamicKeys === "check-prefix";
2032
+ const resolved = /* @__PURE__ */ new Map();
2033
+ const reportedErrors = /* @__PURE__ */ new Set();
2034
+ function catalogsOf(namespace) {
2035
+ let entry = resolved.get(namespace);
2036
+ if (entry === void 0) {
2037
+ entry = catalogsForNamespace(namespace, sources, catalogSettings);
2038
+ resolved.set(namespace, entry);
2039
+ }
2040
+ return entry;
2041
+ }
2042
+ function searched(node, namespaces) {
2043
+ const catalogs = [];
2044
+ for (const namespace of [...namespaces, ...fallbackNamespaces]) {
2045
+ const entry = catalogsOf(namespace);
2046
+ for (const reason of entry.errors) {
2047
+ if (!reportedErrors.has(reason)) {
2048
+ reportedErrors.add(reason);
2049
+ context.report({ node, messageId: "catalogUnreadable", data: { reason } });
2050
+ }
2051
+ }
2052
+ if (entry.errors.length > 0) return null;
2053
+ catalogs.push(...entry.catalogs);
2054
+ }
2055
+ if (catalogs.length === 0) {
2056
+ context.report({ node, messageId: "unknownNamespace", data: { namespace: namespaces.join("`, `") } });
2057
+ return null;
2058
+ }
2059
+ return catalogs;
2060
+ }
2061
+ const labels = (catalogs) => catalogs.map((catalog) => catalog.label).join(", ");
2062
+ return createTranslationVisitor(context, settings, (usage) => {
2063
+ if (usage.kind === "key") {
2064
+ const catalogs = searched(usage.node, usage.namespaces);
2065
+ if (catalogs === null) return;
2066
+ const lookup = { ...lookupBase, plural: usage.plural, context: usage.context, returnObjects: usage.returnObjects };
2067
+ const found = usage.keys.some((key) => catalogs.some((catalog) => catalogHasKey(catalog, key, lookup)));
2068
+ if (!found) {
2069
+ context.report({
2070
+ node: usage.node,
2071
+ messageId: "missingKey",
2072
+ data: { key: usage.keys.join("` | `"), namespace: usage.namespaces.join("`, `"), catalogs: labels(catalogs) }
2073
+ });
2074
+ }
2075
+ return;
2076
+ }
2077
+ if (usage.kind === "prefix" && checkPrefix) {
2078
+ const catalogs = searched(usage.node, usage.namespaces);
2079
+ if (catalogs === null) return;
2080
+ if (!catalogs.some((catalog) => catalogHasPrefix(catalog, usage.prefix))) {
2081
+ context.report({
2082
+ node: usage.node,
2083
+ messageId: "missingKeyPrefix",
2084
+ data: { prefix: usage.prefix, namespace: usage.namespaces.join("`, `"), catalogs: labels(catalogs) }
2085
+ });
2086
+ }
2087
+ }
2088
+ });
2089
+ }
2090
+ });
2091
+
1412
2092
  // src/rules/wire-message-naming.ts
1413
- var RULE_NAME11 = "wire-message-naming";
2093
+ var RULE_NAME12 = "wire-message-naming";
1414
2094
  var DEFAULT_ROLE_SUFFIXES = ["Event", "Command", "Query"];
1415
- var optionSchema9 = {
2095
+ var optionSchema10 = {
1416
2096
  type: "object",
1417
2097
  additionalProperties: false,
1418
2098
  properties: {
@@ -1453,14 +2133,14 @@ function typeLiteralNode(obj) {
1453
2133
  return null;
1454
2134
  }
1455
2135
  var wireMessageNamingRule = createRule({
1456
- name: RULE_NAME11,
2136
+ name: RULE_NAME12,
1457
2137
  meta: {
1458
2138
  type: "problem",
1459
2139
  docs: {
1460
2140
  description: "A message-schema const ending in a role suffix (default Event/Command/Query) whose zod object declares `type: z.literal(...)` must set that literal to kebab-case(const name minus its role suffix)."
1461
2141
  },
1462
2142
  fixable: "code",
1463
- schema: [optionSchema9],
2143
+ schema: [optionSchema10],
1464
2144
  messages: {
1465
2145
  typeMismatch: "Wire `type` literal '{{actual}}' for `{{name}}` must be '{{expected}}' \u2014 kebab-case of the const name minus its role suffix."
1466
2146
  }
@@ -1496,11 +2176,11 @@ var wireMessageNamingRule = createRule({
1496
2176
  });
1497
2177
 
1498
2178
  // src/rules/zod-schema-naming.ts
1499
- var RULE_NAME12 = "zod-schema-naming";
2179
+ var RULE_NAME13 = "zod-schema-naming";
1500
2180
  var SCHEMA_NAME = /^[A-Z][A-Za-z0-9]*Schema$/;
1501
2181
  var SUFFIX = "Schema";
1502
2182
  var DEFAULT_ROLE_SUFFIXES2 = [];
1503
- var optionSchema10 = {
2183
+ var optionSchema11 = {
1504
2184
  type: "object",
1505
2185
  additionalProperties: false,
1506
2186
  properties: {
@@ -1533,13 +2213,13 @@ function rootIdentifierName(node) {
1533
2213
  return null;
1534
2214
  }
1535
2215
  var zodSchemaNamingRule = createRule({
1536
- name: RULE_NAME12,
2216
+ name: RULE_NAME13,
1537
2217
  meta: {
1538
2218
  type: "problem",
1539
2219
  docs: {
1540
2220
  description: "Every exported zod schema is a PascalCase const suffixed `Schema`, paired with a same-named inferred type (`export type Foo = z.infer<typeof FooSchema>`)."
1541
2221
  },
1542
- schema: [optionSchema10],
2222
+ schema: [optionSchema11],
1543
2223
  messages: {
1544
2224
  schemaNaming: "Exported zod schema `{{name}}` must be a PascalCase const ending in `Schema` (e.g. `FooSchema`).",
1545
2225
  missingType: "Schema `{{name}}` has no sibling `export type {{base}} = z.infer<typeof {{name}}>`. Export the inferred type instead of hand-authoring a duplicate."
@@ -1602,7 +2282,8 @@ var rules = {
1602
2282
  "env-var-schema-parity": envVarSchemaParityRule,
1603
2283
  "require-schema-parse-at-boundary": requireSchemaParseAtBoundaryRule,
1604
2284
  "schema-enum-field-consistency": schemaEnumFieldConsistencyRule,
1605
- "fetch-must-check-ok": fetchMustCheckOkRule
2285
+ "fetch-must-check-ok": fetchMustCheckOkRule,
2286
+ "translation-key-exists": translationKeyExistsRule
1606
2287
  };
1607
2288
 
1608
2289
  // src/index.ts