@starci/eslint-canon-be 3.18.4 → 3.19.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.
Files changed (208) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/comments.js +1 -1
  3. package/dist/explicit-surface.js +1 -1
  4. package/dist/index.js +1 -1
  5. package/dist/jwt-alg.js +1 -1
  6. package/dist/lib/edition.d.ts +1 -1
  7. package/dist/lib/edition.js +1 -1
  8. package/dist/lib/hfs.d.ts +0 -6
  9. package/dist/lib/hfs.js +7 -5
  10. package/dist/lib/runtime-root.d.ts +6 -1
  11. package/dist/lib/runtime-root.js +8 -2
  12. package/dist/lib/subject-values.js +1 -1
  13. package/dist/module-layering.js +2 -2
  14. package/dist/project-graph.js +1 -1
  15. package/dist/third-party-owner.js +1 -1
  16. package/package.json +3 -1
  17. package/runtime/bundle.json +1 -190
  18. package/runtime/scripts/api/git/recorded-lines.mjs +5 -5
  19. package/runtime/scripts/hfs/project-graph.mjs +6 -6
  20. package/runtime/scripts/hfs/project-rule.mjs +3 -3
  21. package/runtime/config.example.yaml +0 -194
  22. package/runtime/engine/by-code-unit.mjs +0 -12
  23. package/runtime/engine/config.mjs +0 -461
  24. package/runtime/engine/invalid-config.mjs +0 -15
  25. package/runtime/engine/model-config.mjs +0 -112
  26. package/runtime/engine/orca-config.mjs +0 -34
  27. package/runtime/engine/plain-object.mjs +0 -5
  28. package/runtime/engine/release-config.mjs +0 -32
  29. package/runtime/engine/removed-vocabulary.mjs +0 -27
  30. package/runtime/engine/resources-config.mjs +0 -59
  31. package/runtime/engine/runtime-root.mjs +0 -138
  32. package/runtime/engine/secrets.mjs +0 -202
  33. package/runtime/engine/sonar-config.mjs +0 -24
  34. package/runtime/engine/temp-root.mjs +0 -26
  35. package/runtime/engine/yaml.mjs +0 -161
  36. package/runtime/knowledge/grammars/index.yaml +0 -114
  37. package/runtime/knowledge/hfs/canon-pins.yaml +0 -720
  38. package/runtime/knowledge/hfs/fe-stack.yaml +0 -272
  39. package/runtime/knowledge/hfs/rules.yaml +0 -6760
  40. package/runtime/knowledge/hfs/slots.yaml +0 -2783
  41. package/runtime/knowledge/patterns/fe/folder.yaml +0 -425
  42. package/runtime/knowledge/sonar-gate.yaml +0 -151
  43. package/runtime/modules/kernel/removed-vocabulary.yaml +0 -458
  44. package/runtime/modules/models/registry.yaml +0 -221
  45. package/runtime/modules/models/runtimes.yaml +0 -556
  46. package/runtime/modules/models/tiers.yaml +0 -66
  47. package/runtime/scripts/api/git/cat-file.mjs +0 -4
  48. package/runtime/scripts/api/git/config-get.mjs +0 -9
  49. package/runtime/scripts/api/git/diff.mjs +0 -4
  50. package/runtime/scripts/api/git/lib.mjs +0 -80
  51. package/runtime/scripts/api/git/ls-files.mjs +0 -5
  52. package/runtime/scripts/api/git/ls-tree.mjs +0 -4
  53. package/runtime/scripts/api/git/merge-base.mjs +0 -8
  54. package/runtime/scripts/api/git/remote.mjs +0 -4
  55. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -4
  56. package/runtime/scripts/api/git/show.mjs +0 -5
  57. package/runtime/scripts/api/git/tag.mjs +0 -4
  58. package/runtime/scripts/hfs/allows.mjs +0 -77
  59. package/runtime/scripts/hfs/architecture/ast-walks.mjs +0 -250
  60. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +0 -103
  61. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +0 -114
  62. package/runtime/scripts/hfs/architecture/backend.mjs +0 -462
  63. package/runtime/scripts/hfs/architecture/background-unowned.mjs +0 -254
  64. package/runtime/scripts/hfs/architecture/canon-clones.mjs +0 -291
  65. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +0 -224
  66. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +0 -102
  67. package/runtime/scripts/hfs/architecture/clones.mjs +0 -287
  68. package/runtime/scripts/hfs/architecture/component-tiers.mjs +0 -161
  69. package/runtime/scripts/hfs/architecture/config-unread.mjs +0 -16
  70. package/runtime/scripts/hfs/architecture/config.mjs +0 -407
  71. package/runtime/scripts/hfs/architecture/connection-map.mjs +0 -350
  72. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +0 -115
  73. package/runtime/scripts/hfs/architecture/context-coupling.mjs +0 -117
  74. package/runtime/scripts/hfs/architecture/context-map.mjs +0 -355
  75. package/runtime/scripts/hfs/architecture/context-owner.mjs +0 -102
  76. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +0 -65
  77. package/runtime/scripts/hfs/architecture/context-transaction.mjs +0 -75
  78. package/runtime/scripts/hfs/architecture/contract-bounds.mjs +0 -72
  79. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +0 -177
  80. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +0 -360
  81. package/runtime/scripts/hfs/architecture/contracts.mjs +0 -489
  82. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +0 -81
  83. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +0 -173
  84. package/runtime/scripts/hfs/architecture/dead-exports.mjs +0 -420
  85. package/runtime/scripts/hfs/architecture/default-deny.mjs +0 -175
  86. package/runtime/scripts/hfs/architecture/doc-language.mjs +0 -45
  87. package/runtime/scripts/hfs/architecture/entity-registry.mjs +0 -51
  88. package/runtime/scripts/hfs/architecture/entrypoint.mjs +0 -75
  89. package/runtime/scripts/hfs/architecture/error-codes.mjs +0 -54
  90. package/runtime/scripts/hfs/architecture/error-masked.mjs +0 -78
  91. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +0 -44
  92. package/runtime/scripts/hfs/architecture/feature-shape.mjs +0 -60
  93. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +0 -41
  94. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +0 -112
  95. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +0 -193
  96. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +0 -198
  97. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +0 -368
  98. package/runtime/scripts/hfs/architecture/frontend-world.mjs +0 -374
  99. package/runtime/scripts/hfs/architecture/frontend.mjs +0 -248
  100. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +0 -185
  101. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +0 -77
  102. package/runtime/scripts/hfs/architecture/hfs.mjs +0 -394
  103. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +0 -118
  104. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +0 -213
  105. package/runtime/scripts/hfs/architecture/identity.mjs +0 -121
  106. package/runtime/scripts/hfs/architecture/index.mjs +0 -402
  107. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +0 -69
  108. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +0 -231
  109. package/runtime/scripts/hfs/architecture/machine-ast.mjs +0 -232
  110. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +0 -32
  111. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +0 -237
  112. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +0 -126
  113. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +0 -192
  114. package/runtime/scripts/hfs/architecture/next-data-key.mjs +0 -374
  115. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +0 -26
  116. package/runtime/scripts/hfs/architecture/next-data.mjs +0 -425
  117. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +0 -55
  118. package/runtime/scripts/hfs/architecture/owners.mjs +0 -142
  119. package/runtime/scripts/hfs/architecture/package-shape.mjs +0 -92
  120. package/runtime/scripts/hfs/architecture/presentation.mjs +0 -173
  121. package/runtime/scripts/hfs/architecture/reachability.mjs +0 -330
  122. package/runtime/scripts/hfs/architecture/register-once.mjs +0 -212
  123. package/runtime/scripts/hfs/architecture/registration.mjs +0 -372
  124. package/runtime/scripts/hfs/architecture/render-graph.mjs +0 -449
  125. package/runtime/scripts/hfs/architecture/render-prop-across-server.mjs +0 -58
  126. package/runtime/scripts/hfs/architecture/required-files.mjs +0 -237
  127. package/runtime/scripts/hfs/architecture/root-packages.mjs +0 -78
  128. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +0 -133
  129. package/runtime/scripts/hfs/architecture/schema-owner.mjs +0 -288
  130. package/runtime/scripts/hfs/architecture/server-action.mjs +0 -78
  131. package/runtime/scripts/hfs/architecture/side-boundary.mjs +0 -13
  132. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +0 -320
  133. package/runtime/scripts/hfs/architecture/source-names.mjs +0 -393
  134. package/runtime/scripts/hfs/architecture/sql-keys.mjs +0 -344
  135. package/runtime/scripts/hfs/architecture/sql-owner.mjs +0 -291
  136. package/runtime/scripts/hfs/architecture/sql-returning.mjs +0 -76
  137. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +0 -117
  138. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +0 -587
  139. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +0 -42
  140. package/runtime/scripts/hfs/architecture/surface-nesting.mjs +0 -127
  141. package/runtime/scripts/hfs/architecture/surface.mjs +0 -88
  142. package/runtime/scripts/hfs/architecture/symbols.mjs +0 -273
  143. package/runtime/scripts/hfs/architecture/test-world-files.mjs +0 -263
  144. package/runtime/scripts/hfs/architecture/tiers.mjs +0 -256
  145. package/runtime/scripts/hfs/architecture/transport-owner.mjs +0 -248
  146. package/runtime/scripts/hfs/architecture/type-context.mjs +0 -335
  147. package/runtime/scripts/hfs/architecture/typescript.mjs +0 -477
  148. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +0 -165
  149. package/runtime/scripts/hfs/declaration-shape.mjs +0 -196
  150. package/runtime/scripts/hfs/declaration-slots.mjs +0 -89
  151. package/runtime/scripts/hfs/edition-slots.mjs +0 -80
  152. package/runtime/scripts/hfs/explicit-surface-rule.mjs +0 -59
  153. package/runtime/scripts/hfs/linear-text.mjs +0 -30
  154. package/runtime/scripts/hfs/manifest-shape.mjs +0 -474
  155. package/runtime/scripts/hfs/param-names.mjs +0 -13
  156. package/runtime/scripts/hfs/path-findings.mjs +0 -167
  157. package/runtime/scripts/hfs/repo-identity.mjs +0 -89
  158. package/runtime/scripts/hfs/rule-catalog.mjs +0 -458
  159. package/runtime/scripts/hfs/rule-params-shape.mjs +0 -103
  160. package/runtime/scripts/hfs/rules/database-constants.mjs +0 -25
  161. package/runtime/scripts/hfs/rules/database-migrations.mjs +0 -684
  162. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +0 -68
  163. package/runtime/scripts/hfs/rules/read.mjs +0 -45
  164. package/runtime/scripts/hfs/rules/secrets.mjs +0 -79
  165. package/runtime/scripts/hfs/rules/spec-placement.mjs +0 -28
  166. package/runtime/scripts/hfs/slot-app-view.mjs +0 -97
  167. package/runtime/scripts/hfs/slot-classify.mjs +0 -69
  168. package/runtime/scripts/hfs/slot-errors.mjs +0 -12
  169. package/runtime/scripts/hfs/slot-imports.mjs +0 -98
  170. package/runtime/scripts/hfs/slot-manifest-shape.mjs +0 -111
  171. package/runtime/scripts/hfs/slot-match.mjs +0 -164
  172. package/runtime/scripts/hfs/slot-path.mjs +0 -6
  173. package/runtime/scripts/hfs/slot-required.mjs +0 -95
  174. package/runtime/scripts/hfs/slot-semantic-problems.mjs +0 -175
  175. package/runtime/scripts/hfs/slot-side-problems.mjs +0 -32
  176. package/runtime/scripts/hfs/slots.mjs +0 -306
  177. package/runtime/scripts/hfs/test-world-slot.mjs +0 -5
  178. package/runtime/scripts/hfs/trailing-slashes.mjs +0 -8
  179. package/runtime/scripts/hfs/typescript-programs.mjs +0 -95
  180. package/runtime/scripts/hfs/view.mjs +0 -180
  181. package/runtime/scripts/lib/declaration-surface.mjs +0 -166
  182. package/runtime/scripts/lib/dockerfile.mjs +0 -164
  183. package/runtime/scripts/lib/env.mjs +0 -18
  184. package/runtime/scripts/lib/event-contract.mjs +0 -102
  185. package/runtime/scripts/lib/git-hooks-dir.mjs +0 -30
  186. package/runtime/scripts/lib/git.mjs +0 -35
  187. package/runtime/scripts/lib/glob.mjs +0 -32
  188. package/runtime/scripts/lib/grammar-lint.mjs +0 -57
  189. package/runtime/scripts/lib/i18n.mjs +0 -82
  190. package/runtime/scripts/lib/in-order.mjs +0 -90
  191. package/runtime/scripts/lib/json-clone.mjs +0 -70
  192. package/runtime/scripts/lib/json.mjs +0 -44
  193. package/runtime/scripts/lib/language.mjs +0 -233
  194. package/runtime/scripts/lib/list.mjs +0 -26
  195. package/runtime/scripts/lib/mutation-fence.mjs +0 -20
  196. package/runtime/scripts/lib/package-at.mjs +0 -62
  197. package/runtime/scripts/lib/path-key.mjs +0 -124
  198. package/runtime/scripts/lib/read-text.mjs +0 -47
  199. package/runtime/scripts/lib/regex.mjs +0 -3
  200. package/runtime/scripts/lib/rule-packs.mjs +0 -243
  201. package/runtime/scripts/lib/same-text.mjs +0 -4
  202. package/runtime/scripts/lib/secret-patterns.mjs +0 -230
  203. package/runtime/scripts/lib/sops-envelope.mjs +0 -212
  204. package/runtime/scripts/lib/stack-services.mjs +0 -191
  205. package/runtime/scripts/lib/ts-analysis.mjs +0 -39
  206. package/runtime/scripts/lib/ts-ast.mjs +0 -63
  207. package/runtime/scripts/lib/walk.mjs +0 -45
  208. package/runtime/scripts/lib/yaml-cached.mjs +0 -14
@@ -1,118 +0,0 @@
1
- import path from 'node:path';
2
- import { machineKit } from './machine-ast.mjs';
3
- import { treeOf } from './required-files.mjs';
4
- import { byCodeUnit } from '../../lib/list.mjs';
5
- /**
6
- * R56 `hooks-are-hooks` (FE_HOOKS_ARE_HOOKS), the repository half of the eslint rule `hooks-folder-holds-hooks-only`, which
7
- * judges one file at a time and leaves to the machine what needs the whole tree. Per domain folder of hooks
8
- * (slot fe.hooks, apps/<app>/src/hooks/<domain>):
9
- *
10
- * - at most one shared file, named `<domain>.shared.ts`: a second `*.shared.ts`, or one named for another domain, is a
11
- * second dumping ground for non-hook helpers;
12
- * - no helper declared twice: a top-level function or arrow-function constant that is not a hook and whose name is declared
13
- * in two files of the domain is a helper copied instead of shared; it lives once in `<domain>.shared.ts`.
14
- */
15
- export const HOOKS_ARE_HOOKS_RULE_IDS = ['FE_HOOKS_ARE_HOOKS'];
16
- const RULE = 'FE_HOOKS_ARE_HOOKS';
17
- const HOOKS_SLOT = 'fe.hooks';
18
- const HOOK_NAME = /^use[A-Z0-9]/u;
19
- const SHARED = /\.shared\.[cm]?tsx?$/u;
20
- const sharedFilesOf = (tree, resolver, domains) => {
21
- const shared = new Map(); // domain root -> [file]
22
- for (const rel of [...tree.files].sort(byCodeUnit)) {
23
- const classified = resolver.classifyPath(rel);
24
- if (classified.slot !== HOOKS_SLOT || !classified.root)
25
- continue;
26
- domains.add(classified.root);
27
- const base = path.posix.basename(rel);
28
- if (/\.spec\.[cm]?tsx?$/u.test(base) || !SHARED.test(base))
29
- continue;
30
- let list = shared.get(classified.root);
31
- if (!list) {
32
- list = [];
33
- shared.set(classified.root, list);
34
- }
35
- list.push({ rel, base });
36
- }
37
- return shared;
38
- };
39
- const reportSharedFileViolations = (shared, violations) => {
40
- for (const [root, list] of shared) {
41
- const domain = path.posix.basename(root);
42
- const at = (item, message) => violations.push({ ruleId: RULE, path: item.rel, line: 1, column: 1, domain, message });
43
- for (const item of list) {
44
- if (item.base.replace(/\.[cm]?tsx?$/u, '') !== `${domain}.shared` || path.posix.dirname(item.rel) !== root) {
45
- at(item, `${item.rel} is a shared file that is not ${root}/${domain}.shared.ts; a hooks domain has one shared file for its non-hook helpers, named after the domain. Move the helpers into it.`);
46
- }
47
- }
48
- for (const item of list.filter(entry => entry.base.replace(/\.[cm]?tsx?$/u, '') === `${domain}.shared` && path.posix.dirname(entry.rel) === root).slice(1)) {
49
- at(item, `${item.rel} is a second shared file of hooks domain ${domain}; a domain has exactly one, ${domain}.shared.ts. Merge the helpers into it.`);
50
- }
51
- }
52
- };
53
- const helperDeclarationsOf = (statement, ts) => {
54
- const found = [];
55
- if (ts.isFunctionDeclaration(statement) && statement.name)
56
- found.push([statement.name.text, statement.name]);
57
- if (ts.isVariableStatement(statement)) {
58
- for (const declaration of statement.declarationList.declarations) {
59
- const init = declaration.initializer;
60
- if (ts.isIdentifier(declaration.name) && init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init)))
61
- found.push([declaration.name.text, declaration.name]);
62
- }
63
- }
64
- return found;
65
- };
66
- const recordDeclaredHelper = (declared, root, name, file, node) => {
67
- if (HOOK_NAME.test(name))
68
- return;
69
- let byName = declared.get(root);
70
- if (!byName) {
71
- byName = new Map();
72
- declared.set(root, byName);
73
- }
74
- let list = byName.get(name);
75
- if (!list) {
76
- list = [];
77
- byName.set(name, list);
78
- }
79
- list.push({ file, node });
80
- };
81
- const declaredHelpersOf = (graph, resolver, ts) => {
82
- const declared = new Map(); // domain root -> name -> [{file, node}]
83
- for (const file of graph.files.values()) {
84
- if (file.slot !== HOOKS_SLOT)
85
- continue;
86
- const root = resolver.classifyPath(file.rel).root ?? ''; // a file in the hooks slot was placed by that slot, so its root exists
87
- for (const statement of file.sourceFile.statements) {
88
- for (const [name, node] of helperDeclarationsOf(statement, ts))
89
- recordDeclaredHelper(declared, root, name, file, node);
90
- }
91
- }
92
- return declared;
93
- };
94
- const reportDuplicateHelpers = (declared, violations, kit) => {
95
- for (const [root, byName] of declared) {
96
- const domain = path.posix.basename(root);
97
- for (const [name, list] of byName) {
98
- if (list.length < 2)
99
- continue;
100
- for (const item of list.slice(1)) {
101
- const others = list.map(entry => entry.file.rel).filter(rel => rel !== item.file.rel).sort(byCodeUnit);
102
- violations.push({ ruleId: RULE, path: item.file.rel, ...kit.at(item.file.rel, item.file.sourceFile, item.node), domain, helper: name,
103
- message: `${name} is declared again in hooks domain ${domain} (also in ${others.join(', ')}); a helper the hooks share is written once, in ${domain}.shared.ts.` });
104
- }
105
- }
106
- }
107
- };
108
- export function checkHooksAreHooks(input) {
109
- const { config, graph } = input;
110
- const kit = machineKit(input);
111
- const { ts, resolver } = kit;
112
- const violations = [];
113
- const domains = new Set();
114
- const shared = sharedFilesOf(treeOf(config.root), resolver, domains);
115
- reportSharedFileViolations(shared, violations);
116
- reportDuplicateHelpers(declaredHelpersOf(graph, resolver, ts), violations, kit);
117
- return { violations, coverage: { status: 'checked', domains: domains.size } };
118
- }
@@ -1,213 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { byCodeUnit } from '../../lib/list.mjs';
4
- import { escapeRegExp } from '../../lib/regex.mjs';
5
- /**
6
- * R106 `i18n-keys` (FE_I18N_KEYS). The catalogs of an app (`apps/<app>/src/modules/i18n/messages/<locale>.json`, slot fe.modules.i18n) and
7
- * the source that reads them agree, both ways. The key sets of the locales agree with each other (FE_I18N_CATALOG, `starci app check`); this
8
- * rule holds the source to the catalogs:
9
- *
10
- * - a key read with a literal is in every locale: `useTranslations("ns")` / `getTranslations("ns")` (also `{ namespace }`, `await`)
11
- * imported from `next-intl` or `next-intl/server`, then `t("key")`, `t.rich("key")`, `t.raw("key")`, `t.markup("key")` inside the app's
12
- * own source (both branches of a conditional key are read). A key no locale's catalog holds is FE_I18N_KEYS, once per call. A computed
13
- * key (a template with an interpolation, a variable) is not judged here; it counts as a read.
14
- * - a key nobody reads is dead: a leaf of the app's catalogs that no string in the app's or the shared packages' source can be reading.
15
- * A leaf counts as read when a string literal or a template pattern (each interpolation a wildcard within its dotted segment) equals a
16
- * tail of its dotted path (`"title"`, `"card.title"`, `"orders.card.title"` all read `orders.card.title`); the test is deliberately loose
17
- * so a key handed through props or a table of keys is never reported, and only a key that appears nowhere is.
18
- *
19
- * Reads the source of the program and the JSON catalogs on disk; the wording of a translation is the product owner's.
20
- */
21
- export const I18N_KEYS_RULE_IDS = ['FE_I18N_KEYS'];
22
- const RULE = 'FE_I18N_KEYS';
23
- const MODULES = new Set(['next-intl', 'next-intl/server']);
24
- const FACTORIES = new Set(['useTranslations', 'getTranslations']);
25
- const READERS = new Set(['rich', 'raw', 'markup']);
26
- const APP_SOURCE = /^apps\/([^/]+)\/src\//u;
27
- const leavesOf = (value, prefix = '') => (value !== null && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length
28
- ? Object.entries(value).flatMap(([key, child]) => leavesOf(child, prefix ? `${prefix}.${key}` : key))
29
- : [prefix]);
30
- const readCatalog = (root, rel) => {
31
- try {
32
- return new Set(leavesOf(JSON.parse(fs.readFileSync(path.join(root, rel), 'utf8'))).filter(Boolean));
33
- }
34
- catch {
35
- return null;
36
- }
37
- };
38
- /** True when the key `full` is a leaf of `leaves` or a namespace above one (an object read by `rich`, `raw` or a nested `t`). */
39
- const held = (leaves, full) => leaves.has(full) || [...leaves].some(leaf => leaf.startsWith(`${full}.`));
40
- // A pattern that is only wildcards (`${a}`, `${a}.${b}`) names nothing: it would read every key, so it reads none.
41
- const tailMatches = (pattern, leafSegments) => pattern.some(segment => segment !== '*' && segment !== '') && pattern.length <= leafSegments.length
42
- && pattern.every((segment, index) => {
43
- const leaf = leafSegments[leafSegments.length - pattern.length + index];
44
- return segment instanceof RegExp ? segment.test(leaf) : segment === '*' || segment === leaf;
45
- });
46
- function catalogsPerApp(config, graph) {
47
- const catalogs = new Map();
48
- const seen = new Set();
49
- for (const file of graph.files.values()) {
50
- const app = APP_SOURCE.exec(file.rel)?.[1];
51
- if (!app || seen.has(app))
52
- continue;
53
- seen.add(app);
54
- const dir = `apps/${app}/src/modules/i18n/messages/`;
55
- let names = [];
56
- try {
57
- names = fs.readdirSync(path.join(config.root, dir)).filter(name => name.endsWith('.json')).sort(byCodeUnit);
58
- }
59
- catch { /* the app has no catalog directory: FE_I18N_PLACEMENT's finding */ }
60
- if (names.length)
61
- catalogs.set(dir, names.map(name => ({ rel: `${dir}${name}`, locale: name.slice(0, -5) })));
62
- }
63
- return catalogs;
64
- }
65
- /** Each interpolation can fill part or all of a segment; the template's static fragments keep their literal spelling. */
66
- function templatePatternOf(node) {
67
- const pattern = [];
68
- for (const segment of [node.head.text, ...node.templateSpans.map(span => span.literal.text)].join('\u0000').split('.')) {
69
- if (!segment.includes('\u0000')) {
70
- pattern.push(segment);
71
- continue;
72
- }
73
- const parts = segment.split('\u0000');
74
- pattern.push(parts.every(part => part === '') ? '*' : new RegExp(`^${parts.map((part) => escapeRegExp(part)).join('.*')}$`, 'su'));
75
- }
76
- return pattern;
77
- }
78
- function patternsOf(ts, files) {
79
- const patterns = [];
80
- for (const file of files) {
81
- const visit = (node) => {
82
- if (ts.isStringLiteralLike(node))
83
- patterns.push(node.text.split('.'));
84
- else if (ts.isTemplateExpression(node))
85
- patterns.push(templatePatternOf(node));
86
- ts.forEachChild(node, visit);
87
- };
88
- visit(file.sourceFile);
89
- }
90
- return patterns;
91
- }
92
- function translationFactories(ts, file) {
93
- const factories = new Set();
94
- for (const statement of file.sourceFile.statements) {
95
- if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !MODULES.has(statement.moduleSpecifier.text))
96
- continue;
97
- const named = statement.importClause?.namedBindings;
98
- if (!named || !ts.isNamedImports(named))
99
- continue;
100
- for (const element of named.elements) {
101
- if (!element.isTypeOnly && FACTORIES.has((element.propertyName ?? element.name).text))
102
- factories.add(element.name.text);
103
- }
104
- }
105
- return factories;
106
- }
107
- function namespaceForCall(ts, file, init, factories) {
108
- let expression = init;
109
- if (ts.isAwaitExpression(expression))
110
- expression = expression.expression;
111
- if (!ts.isCallExpression(expression) || !ts.isIdentifier(expression.expression) || !factories.has(expression.expression.text))
112
- return null;
113
- const [first] = expression.arguments;
114
- if (first === undefined)
115
- return '';
116
- if (ts.isStringLiteralLike(first))
117
- return first.text;
118
- if (!ts.isObjectLiteralExpression(first))
119
- return null;
120
- const property = first.properties.find((candidate) => ts.isPropertyAssignment(candidate) && candidate.name.getText(file.sourceFile) === 'namespace');
121
- if (!property)
122
- return '';
123
- return ts.isStringLiteralLike(property.initializer) ? property.initializer.text : null;
124
- }
125
- function translatorNamespaces(ts, file, factories) {
126
- const namespaces = new Map();
127
- const collect = (node) => {
128
- if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
129
- const namespace = namespaceForCall(ts, file, node.initializer, factories);
130
- if (namespace !== null)
131
- namespaces.set(node, namespace);
132
- }
133
- ts.forEachChild(node, collect);
134
- };
135
- collect(file.sourceFile);
136
- return namespaces;
137
- }
138
- function keysOf(ts, argument) {
139
- if (ts.isConditionalExpression(argument))
140
- return [...keysOf(ts, argument.whenTrue), ...keysOf(ts, argument.whenFalse)];
141
- if (ts.isParenthesizedExpression(argument) || ts.isAsExpression(argument) || ts.isSatisfiesExpression(argument))
142
- return keysOf(ts, argument.expression);
143
- return ts.isStringLiteralLike(argument) ? [argument] : [];
144
- }
145
- function reportCallKeys(reads, node) {
146
- const { ts, file, checker, namespaces, loaded, app, report } = reads;
147
- const callee = node.expression;
148
- let owner = ts.isIdentifier(callee) ? callee : null;
149
- if (ts.isPropertyAccessExpression(callee) && READERS.has(callee.name.text) && ts.isIdentifier(callee.expression))
150
- owner = callee.expression;
151
- const [first] = node.arguments;
152
- const declaration = owner ? checker.getSymbolAtLocation(owner)?.valueDeclaration : null;
153
- if (!owner || first === undefined || !declaration || !namespaces.has(declaration))
154
- return;
155
- for (const literal of keysOf(ts, first)) {
156
- const namespace = namespaces.get(declaration);
157
- const full = namespace ? `${namespace}.${literal.text}` : literal.text;
158
- const lacking = loaded.filter(catalog => !held(catalog.leaves, full)).map(catalog => catalog.locale);
159
- if (lacking.length)
160
- report(file, literal, `${file.rel} reads "${full}" and ${lacking.join(', ')} ${lacking.length === 1 ? 'holds' : 'hold'} no such key; add it to every catalog of ${app} or read a key that exists`, { key: full, locales: lacking });
161
- }
162
- }
163
- function reportTranslationReads(ts, file, checker, namespaces, loaded, app, report) {
164
- const reads = { ts, file, checker, namespaces, loaded, app, report };
165
- const read = (node) => {
166
- if (ts.isCallExpression(node))
167
- reportCallKeys(reads, node);
168
- ts.forEachChild(node, read);
169
- };
170
- read(file.sourceFile);
171
- }
172
- function reportUnusedCatalogKeys(primary, loaded, patterns, app, violations) {
173
- const every = new Set(loaded.flatMap(catalog => [...catalog.leaves]));
174
- for (const leaf of [...every].sort(byCodeUnit)) {
175
- const segments = leaf.split('.');
176
- if (patterns.some(pattern => tailMatches(pattern, segments)))
177
- continue;
178
- violations.push({ ruleId: RULE, path: primary.rel, line: 1, column: 1, message: `${primary.rel} holds the key "${leaf}" and no source of ${app} or the shared packages reads it; delete the key from every catalog`, key: leaf });
179
- }
180
- }
181
- export function checkI18nKeys({ config, graph, context }) {
182
- const ts = context.ts;
183
- const violations = [];
184
- const report = (file, node, message, extra = {}) => {
185
- const at = file.sourceFile.getLineAndCharacterOfPosition(node.getStart(file.sourceFile));
186
- violations.push({ ruleId: RULE, path: file.rel, line: at.line + 1, column: at.character + 1, message, ...extra });
187
- };
188
- const catalogsByApp = catalogsPerApp(config, graph);
189
- let apps = 0;
190
- for (const [dir, catalogs] of catalogsByApp) {
191
- const app = /^apps\/([^/]+)\//u.exec(dir)?.[1];
192
- const loaded = catalogs.map(catalog => ({ ...catalog, leaves: readCatalog(config.root, catalog.rel) })).filter((catalog) => Boolean(catalog.leaves));
193
- if (!app || !loaded.length)
194
- continue;
195
- apps += 1;
196
- const own = [...graph.files.values()].filter(file => file.rel.startsWith(`apps/${app}/src/`));
197
- const shared = [...graph.files.values()].filter(file => /^packages\/[^/]+\/src\//u.test(file.rel));
198
- const patterns = patternsOf(ts, [...own, ...shared]);
199
- for (const file of own) {
200
- const factories = translationFactories(ts, file);
201
- if (!factories.size)
202
- continue;
203
- const checker = context.checkerFor(file.sourceFile.fileName);
204
- const namespaces = translatorNamespaces(ts, file, factories);
205
- if (!namespaces.size)
206
- continue;
207
- reportTranslationReads(ts, file, checker, namespaces, loaded, app, report);
208
- }
209
- const [primary] = [...loaded].sort((a, b) => a.locale.localeCompare(b.locale));
210
- reportUnusedCatalogKeys(primary, loaded, patterns, app, violations);
211
- }
212
- return { violations, coverage: { status: 'checked', apps } };
213
- }
@@ -1,121 +0,0 @@
1
- // identity.mjs - the back-end identity machine. Under lite the one token verifier is @starci/auth, composed by be.integrations.passport:
2
- // no app file calls jose's jwtVerify, decodes a token unverified or reads authorization claims from a request body (R329), and the
3
- // passport integration imports @starci/auth/server. In both editions every jwtVerify call holds the JWT policy (R260, jwtPolicyProblems).
4
- import path from 'node:path';
5
- import { machineKit } from './machine-ast.mjs';
6
- import { jwtPolicyProblems, reportAt, unwrap } from './jwt-ast.mjs';
7
- import { accessName } from '../../lib/ts-ast.mjs';
8
- const PASSPORT_SLOT = 'be.integrations.passport';
9
- const PACKAGE_ENTRY = '@starci/auth/server';
10
- const BE_JWT_VERIFIED = 'BE_AUTH_JWT_OWN_JWKS';
11
- /** The rule ids the back-end identity machine reports. */
12
- export const IDENTITY_RULE_IDS = Object.freeze(['BE_AUTH_JWT_OWN_JWKS', 'BE_JWT_ALG_PINNED']);
13
- const JWT_MODULES = new Set(['jose', 'jsonwebtoken', 'jwt-decode']);
14
- const CLAIM_NAMES = new Set(['role', 'roles', 'membership', 'memberships']);
15
- const importedCall = (kit, checker, call) => {
16
- const direct = kit.importBinding(checker, call.expression);
17
- if (direct?.module === 'jwt-decode' && direct.name === 'default')
18
- return { module: direct.module, name: 'jwtDecode' };
19
- if (direct)
20
- return direct;
21
- if (!kit.ts.isPropertyAccessExpression(call.expression) || !kit.ts.isIdentifier(call.expression.expression))
22
- return null;
23
- const owner = kit.importBinding(checker, call.expression.expression);
24
- return owner?.name === 'default' ? { module: owner.module, name: call.expression.name.text } : null;
25
- };
26
- const requestLike = (ts, node) => {
27
- const current = unwrap(ts, node);
28
- if (ts.isIdentifier(current))
29
- return /^(?:req|request)$/iu.test(current.text);
30
- return (ts.isPropertyAccessExpression(current) || ts.isElementAccessExpression(current)) && accessName(ts, current) === 'request';
31
- };
32
- const fromRequestBody = (kit, checker, node, seen = new Set()) => {
33
- const current = unwrap(kit.ts, node);
34
- if (!current || seen.has(current))
35
- return false;
36
- seen.add(current);
37
- if (kit.ts.isPropertyAccessExpression(current) || kit.ts.isElementAccessExpression(current)) {
38
- if (accessName(kit.ts, current) === 'body' && requestLike(kit.ts, current.expression))
39
- return true;
40
- return fromRequestBody(kit, checker, current.expression, seen);
41
- }
42
- if (!kit.ts.isIdentifier(current))
43
- return false;
44
- return kit.declarationsOf(checker, current).some(declaration => kit.ts.isVariableDeclaration(declaration) && declaration.initializer
45
- && fromRequestBody(kit, checker, declaration.initializer, seen));
46
- };
47
- function inspectJwtCall(kit, checker, file, node, counters, violations) {
48
- const binding = importedCall(kit, checker, node);
49
- if (binding && JWT_MODULES.has(binding.module) && ['decode', 'decodeJwt', 'jwtDecode'].includes(binding.name)) {
50
- reportAt(violations, kit, BE_JWT_VERIFIED, file, node, `${binding.module}.${binding.name} decodes a token without proving its signature; verify it through integrations/passport.`, { module: binding.module, method: binding.name });
51
- }
52
- if (binding?.module === 'jose' && binding.name === 'jwtVerify') {
53
- counters.verifyCalls += 1;
54
- reportAt(violations, kit, BE_JWT_VERIFIED, file, node, 'jwtVerify is called in the app; @starci/auth verifies the own access tokens, reached through ' + PASSPORT_SLOT + '.');
55
- return true;
56
- }
57
- return false;
58
- }
59
- function inspectRequestClaims(kit, file, node, checker, violations) {
60
- const { ts } = kit;
61
- const claim = accessName(ts, node);
62
- if ((ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) && claim !== null && CLAIM_NAMES.has(claim)) {
63
- if (fromRequestBody(kit, checker, node.expression))
64
- reportAt(violations, kit, BE_JWT_VERIFIED, file, node, `${claim} is read from the request body; authorization claims come only from the verified token or the database.`, { claim });
65
- }
66
- if (ts.isVariableDeclaration(node) && ts.isObjectBindingPattern(node.name) && node.initializer && fromRequestBody(kit, checker, node.initializer)) {
67
- for (const element of node.name.elements) {
68
- const property = element.propertyName ?? element.name;
69
- if (ts.isIdentifier(property) && CLAIM_NAMES.has(property.text))
70
- reportAt(violations, kit, BE_JWT_VERIFIED, file, element, `${property.text} is destructured from the request body; authorization claims come only from the verified token or the database.`, { claim: property.text });
71
- }
72
- }
73
- }
74
- function inspectBackendNode(kit, checker, file, node, counters, violations) {
75
- const handled = kit.ts.isCallExpression(node) && inspectJwtCall(kit, checker, file, node, counters, violations);
76
- if (!handled)
77
- inspectRequestClaims(kit, file, node, checker, violations);
78
- }
79
- function checkOwnJwks(input) {
80
- const kit = machineKit(input);
81
- const violations = [];
82
- const integrations = [...input.graph.files.values()].filter((file) => file.slot === PASSPORT_SLOT);
83
- if (!integrations.length)
84
- return { violations, coverage: { status: 'not-applicable', integrationFiles: 0, verifyCalls: 0 } };
85
- const counters = { verifyCalls: 0 };
86
- for (const file of input.graph.files.values()) {
87
- const checker = kit.checkerOf(file.sourceFile);
88
- kit.walk(file.sourceFile, (node) => inspectBackendNode(kit, checker, file, node, counters, violations));
89
- }
90
- const composes = integrations.some((file) => file.sourceFile.statements.some((statement) => kit.ts.isImportDeclaration(statement)
91
- && kit.ts.isStringLiteral(statement.moduleSpecifier) && statement.moduleSpecifier.text === PACKAGE_ENTRY));
92
- const anchor = integrations.find((file) => path.posix.basename(file.rel) === 'index.ts') ?? integrations[0];
93
- if (!composes && anchor)
94
- reportAt(violations, kit, BE_JWT_VERIFIED, anchor, anchor.sourceFile, PASSPORT_SLOT + ' does not import ' + PACKAGE_ENTRY + ', the one verifier of the own access tokens.');
95
- return { violations, coverage: { status: 'checked', integrationFiles: integrations.length, verifyCalls: counters.verifyCalls, composesPackage: composes } };
96
- }
97
- /** R260 BE_JWT_ALG_PINNED: every jose jwtVerify call pins a literal secure algorithm list and a configured issuer, and binds the principal by a configured audience or the authenticated role. */
98
- export function checkJwtPolicy(input) {
99
- const kit = machineKit(input);
100
- const violations = [];
101
- let verifyCalls = 0;
102
- for (const file of input.graph.files.values()) {
103
- const checker = kit.checkerOf(file.sourceFile);
104
- kit.walk(file.sourceFile, (node) => {
105
- if (!kit.ts.isCallExpression(node) || !kit.isImportOf(checker, node.expression, 'jwtVerify', 'jose'))
106
- return;
107
- verifyCalls += 1;
108
- const problems = jwtPolicyProblems(kit.ts, checker, node);
109
- if (problems.length)
110
- reportAt(violations, kit, 'BE_JWT_ALG_PINNED', file, node, problems.join(' '));
111
- });
112
- }
113
- return { violations, coverage: { status: 'checked', verifyCalls } };
114
- }
115
- /** The back-end identity machine: the own-JWKS law where the passport integration exists (lite), the JWT policy everywhere. */
116
- export function checkBackendIdentity(input) {
117
- const slot = input.graph.resolver.slot(PASSPORT_SLOT);
118
- const own = slot && input.graph.resolver.slotEnabled(slot) ? checkOwnJwks(input) : { violations: [], coverage: { status: 'not-applicable', reason: 'no passport integration slot (full edition)' } };
119
- const policy = checkJwtPolicy(input);
120
- return { violations: [...own.violations, ...policy.violations], coverage: { status: 'checked', ownJwks: own.coverage, jwtPolicy: policy.coverage } };
121
- }