@starci/eslint-canon-be 3.8.20 → 3.9.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 (164) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/package.json +1 -1
  3. package/runtime/bundle.json +192 -0
  4. package/runtime/engine/by-code-unit.mjs +6 -6
  5. package/runtime/engine/model-config.mjs +98 -76
  6. package/runtime/engine/orca-config.mjs +22 -17
  7. package/runtime/engine/release-config.mjs +22 -18
  8. package/runtime/engine/removed-vocabulary.mjs +21 -20
  9. package/runtime/engine/resources-config.mjs +42 -35
  10. package/runtime/engine/runtime-root.mjs +11 -13
  11. package/runtime/engine/secrets.mjs +170 -102
  12. package/runtime/engine/sonar-config.mjs +16 -13
  13. package/runtime/engine/temp-root.mjs +3 -9
  14. package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
  15. package/runtime/knowledge/hfs/rules.yaml +6 -6
  16. package/runtime/knowledge/hfs/slots.yaml +2 -2
  17. package/runtime/knowledge/sonar-gate.yaml +1 -1
  18. package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
  19. package/runtime/scripts/api/git/cat-file.mjs +0 -1
  20. package/runtime/scripts/api/git/config-get.mjs +1 -2
  21. package/runtime/scripts/api/git/diff.mjs +0 -1
  22. package/runtime/scripts/api/git/lib.mjs +32 -32
  23. package/runtime/scripts/api/git/ls-files.mjs +0 -1
  24. package/runtime/scripts/api/git/ls-tree.mjs +0 -1
  25. package/runtime/scripts/api/git/merge-base.mjs +2 -3
  26. package/runtime/scripts/api/git/remote.mjs +0 -1
  27. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
  28. package/runtime/scripts/api/git/show.mjs +0 -1
  29. package/runtime/scripts/hfs/allows.mjs +37 -38
  30. package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
  31. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
  32. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
  33. package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
  34. package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
  35. package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
  36. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
  37. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
  38. package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
  39. package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
  40. package/runtime/scripts/hfs/architecture/config.mjs +302 -268
  41. package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
  42. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
  43. package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
  44. package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
  45. package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
  46. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
  47. package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
  48. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
  49. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
  50. package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
  51. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
  52. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
  53. package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
  54. package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
  55. package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
  56. package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
  57. package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
  58. package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
  59. package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
  60. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
  61. package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
  62. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
  63. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
  64. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
  65. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
  66. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
  67. package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
  68. package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
  69. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
  70. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
  71. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
  72. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
  73. package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
  74. package/runtime/scripts/hfs/architecture/index.mjs +272 -272
  75. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
  76. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
  77. package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
  78. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
  79. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
  80. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
  81. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
  82. package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
  83. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
  84. package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
  85. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
  86. package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
  87. package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
  88. package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
  89. package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
  90. package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
  91. package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
  92. package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
  93. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
  94. package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
  95. package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
  96. package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
  97. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
  98. package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
  99. package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
  100. package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
  101. package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
  102. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
  103. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
  104. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
  105. package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
  106. package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
  107. package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
  108. package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
  109. package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
  110. package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
  111. package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
  112. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
  113. package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
  114. package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
  115. package/runtime/scripts/hfs/edition-slots.mjs +43 -40
  116. package/runtime/scripts/hfs/linear-text.mjs +20 -20
  117. package/runtime/scripts/hfs/param-names.mjs +6 -11
  118. package/runtime/scripts/hfs/path-findings.mjs +120 -100
  119. package/runtime/scripts/hfs/repo-identity.mjs +55 -42
  120. package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
  121. package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
  122. package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
  123. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
  124. package/runtime/scripts/hfs/rules/read.mjs +19 -15
  125. package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
  126. package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
  127. package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
  128. package/runtime/scripts/hfs/slot-classify.mjs +42 -38
  129. package/runtime/scripts/hfs/slot-errors.mjs +8 -7
  130. package/runtime/scripts/hfs/slot-imports.mjs +70 -63
  131. package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
  132. package/runtime/scripts/hfs/slot-match.mjs +122 -111
  133. package/runtime/scripts/hfs/slot-path.mjs +0 -2
  134. package/runtime/scripts/hfs/slot-required.mjs +65 -59
  135. package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
  136. package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
  137. package/runtime/scripts/hfs/slots.mjs +175 -152
  138. package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
  139. package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
  140. package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
  141. package/runtime/scripts/hfs/view.mjs +38 -40
  142. package/runtime/scripts/lib/dockerfile.mjs +131 -114
  143. package/runtime/scripts/lib/env.mjs +3 -6
  144. package/runtime/scripts/lib/event-contract.mjs +73 -78
  145. package/runtime/scripts/lib/git.mjs +15 -16
  146. package/runtime/scripts/lib/glob.mjs +23 -14
  147. package/runtime/scripts/lib/grammar-lint.mjs +33 -37
  148. package/runtime/scripts/lib/i18n.mjs +32 -35
  149. package/runtime/scripts/lib/in-order.mjs +57 -38
  150. package/runtime/scripts/lib/json-clone.mjs +45 -36
  151. package/runtime/scripts/lib/json.mjs +26 -14
  152. package/runtime/scripts/lib/language.mjs +133 -129
  153. package/runtime/scripts/lib/list.mjs +9 -14
  154. package/runtime/scripts/lib/mutation-fence.mjs +10 -5
  155. package/runtime/scripts/lib/path-key.mjs +53 -38
  156. package/runtime/scripts/lib/read-text.mjs +30 -17
  157. package/runtime/scripts/lib/regex.mjs +1 -2
  158. package/runtime/scripts/lib/same-text.mjs +0 -1
  159. package/runtime/scripts/lib/sops-envelope.mjs +148 -120
  160. package/runtime/scripts/lib/stack-services.mjs +128 -129
  161. package/runtime/scripts/lib/ts-analysis.mjs +28 -26
  162. package/runtime/scripts/lib/ts-ast.mjs +36 -42
  163. package/runtime/scripts/lib/walk.mjs +33 -27
  164. package/runtime/scripts/lib/yaml-cached.mjs +4 -4
@@ -43,9 +43,7 @@ import { globExpression } from '../../lib/glob.mjs';
43
43
  import { stagedConsumerSources } from './staged-consumers.mjs';
44
44
  import { bindingsOf } from './dead-exports-bindings.mjs';
45
45
  import { operationContractRoots } from './operation-contract-roots.mjs';
46
-
47
46
  export const DEAD_EXPORT_RULE_IDS = ['HFS_UNUSED_EXPORT', 'HFS_UNUSED_FILE'];
48
-
49
47
  const PACKAGE_ENTRIES = ['src/index.ts', 'src/index.tsx', 'index.ts', 'index.tsx'];
50
48
  const OWNER_ENTRIES = ['index.ts', 'index.tsx'];
51
49
  const CHAIN_DEPTH = 6;
@@ -53,310 +51,370 @@ const CHAIN_DEPTH = 6;
53
51
  const UNJUDGED_TIERS = new Set(['none', 'e2e', 'fixtures']);
54
52
  const CONFIG_EXTENSIONS = ['', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '/index.ts', '/index.tsx', '/index.js'];
55
53
  const CONFIG_STRING = /['"`](\.{1,2}\/[^'"`\s]+)['"`]/g;
56
-
57
54
  /** The public entry file (repository-relative) of an owner unit, or null when the owner has none in the graph. */
58
55
  export function entryOf(graph, config, key, owner) {
59
- const declared = config.owners?.find(item => item.id === key)?.entry;
60
- if (declared && graph.files.has(declared)) return declared;
61
- for (const candidate of owner.tier === 'package' ? PACKAGE_ENTRIES : OWNER_ENTRIES) {
62
- const rel = owner.root ? `${owner.root}/${candidate}` : candidate;
63
- if (graph.files.has(rel)) return rel;
64
- }
65
- return null;
56
+ const declared = config.owners?.find(item => item.id === key)?.entry;
57
+ if (declared && graph.files.has(declared))
58
+ return declared;
59
+ for (const candidate of owner.tier === 'package' ? PACKAGE_ENTRIES : OWNER_ENTRIES) {
60
+ const rel = owner.root ? `${owner.root}/${candidate}` : candidate;
61
+ if (graph.files.has(rel))
62
+ return rel;
63
+ }
64
+ return null;
66
65
  }
67
-
68
66
  /** The additional public entry files (repository-relative, in the graph) the slot manifest declares for the owner (slot field `entries`, on the slot that holds its entry file). */
69
67
  function extraEntries(graph, entry) {
70
- const found = graph.resolver.classifyPath(entry);
71
- const names = found.slot ? graph.resolver.slot(found.slot)?.entries ?? [] : [];
72
- // an entry holding `*` (one file per operation) names every graph file it matches
73
- const matches = name => [...graph.files.keys()].filter(rel => globExpression(`${found.root}/${name}`).test(rel));
74
- return names.flatMap(name => (name.includes('*') ? matches(name) : [`${found.root}/${name}`].filter(rel => graph.files.has(rel))));
68
+ const found = graph.resolver.classifyPath(entry);
69
+ const names = found.slot ? graph.resolver.slot(found.slot)?.entries ?? [] : [];
70
+ // an entry holding `*` (one file per operation) names every graph file it matches
71
+ const matches = (name) => [...graph.files.keys()].filter(rel => globExpression(`${found.root}/${name}`).test(rel));
72
+ return names.flatMap((name) => (name.includes('*') ? matches(name) : [`${found.root}/${name}`].filter(rel => graph.files.has(rel))));
75
73
  }
76
-
77
74
  const addExportName = (found, sourceFile, name, node) => {
78
- if (found.has(name)) return;
79
- found.set(name, sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1);
75
+ if (found.has(name))
76
+ return;
77
+ found.set(name, sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1);
80
78
  };
81
-
82
79
  const addBindingNames = (ts, found, sourceFile, name, node) => {
83
- if (name.kind === ts.SyntaxKind.Identifier) addExportName(found, sourceFile, name.text, node);
84
- else for (const element of name.elements) if (element.kind === ts.SyntaxKind.BindingElement) addBindingNames(ts, found, sourceFile, element.name, node);
80
+ if (name.kind === ts.SyntaxKind.Identifier)
81
+ addExportName(found, sourceFile, name.text, node);
82
+ else
83
+ for (const element of name.elements)
84
+ if (element.kind === ts.SyntaxKind.BindingElement)
85
+ addBindingNames(ts, found, sourceFile, element.name, node);
85
86
  };
86
-
87
87
  const addExportDeclarationNames = (ts, statement, add) => {
88
- if (statement.exportClause?.kind === ts.SyntaxKind.NamedExports) {
89
- for (const element of statement.exportClause.elements) add(element.name.text, element);
90
- } else if (statement.exportClause?.kind === ts.SyntaxKind.NamespaceExport) add(statement.exportClause.name.text, statement);
88
+ if (statement.exportClause?.kind === ts.SyntaxKind.NamedExports) {
89
+ for (const element of statement.exportClause.elements)
90
+ add(element.name.text, element);
91
+ }
92
+ else if (statement.exportClause?.kind === ts.SyntaxKind.NamespaceExport)
93
+ add(statement.exportClause.name.text, statement);
91
94
  };
92
-
93
95
  const addStatementNames = (ts, found, sourceFile, statement) => {
94
- const kind = ts.SyntaxKind;
95
- const add = (name, node) => addExportName(found, sourceFile, name, node);
96
- if (statement.kind === kind.ExportDeclaration) return addExportDeclarationNames(ts, statement, add);
97
- if (statement.kind === kind.ExportAssignment) return add('default', statement);
98
- const modifiers = statement.modifiers ?? [];
99
- if (!modifiers.some(modifier => modifier.kind === kind.ExportKeyword)) return undefined;
100
- if (modifiers.some(modifier => modifier.kind === kind.DefaultKeyword)) return add('default', statement);
101
- if (statement.kind === kind.VariableStatement) {
102
- for (const declaration of statement.declarationList.declarations) addBindingNames(ts, found, sourceFile, declaration.name, declaration);
103
- return undefined;
104
- }
105
- return statement.name && statement.name.kind === kind.Identifier ? add(statement.name.text, statement) : undefined;
96
+ const kind = ts.SyntaxKind;
97
+ const add = (name, node) => addExportName(found, sourceFile, name, node);
98
+ if (ts.isExportDeclaration(statement))
99
+ return addExportDeclarationNames(ts, statement, add);
100
+ if (ts.isExportAssignment(statement))
101
+ return add('default', statement);
102
+ const declaring = statement; // reason: only declaration statements carry modifiers and a name; both are read as optional
103
+ const modifiers = declaring.modifiers ?? [];
104
+ if (!modifiers.some(modifier => modifier.kind === kind.ExportKeyword))
105
+ return undefined;
106
+ if (modifiers.some(modifier => modifier.kind === kind.DefaultKeyword))
107
+ return add('default', statement);
108
+ if (ts.isVariableStatement(statement)) {
109
+ for (const declaration of statement.declarationList.declarations)
110
+ addBindingNames(ts, found, sourceFile, declaration.name, declaration);
111
+ return undefined;
112
+ }
113
+ return declaring.name && ts.isIdentifier(declaring.name) ? add(declaring.name.text, statement) : undefined;
106
114
  };
107
-
108
115
  /** The exported names of an entry source file, each with the line of its declaration. */
109
116
  export function exportedNames(ts, sourceFile) {
110
- const found = new Map();
111
- for (const statement of sourceFile.statements) addStatementNames(ts, found, sourceFile, statement);
112
- return found;
117
+ const found = new Map();
118
+ for (const statement of sourceFile.statements)
119
+ addStatementNames(ts, found, sourceFile, statement);
120
+ return found;
113
121
  }
114
-
115
- const withoutSlash = value => value.replace(/\/$/, '');
116
-
122
+ const withoutSlash = (value) => value.replace(/\/$/, '');
117
123
  const addConfigTextRoots = (graph, directory, text, roots) => {
118
- for (const match of text.matchAll(CONFIG_STRING)) {
119
- const target = path.posix.join(directory, match[1]);
120
- const hit = CONFIG_EXTENSIONS.map(extension => `${target}${extension}`).find(candidate => graph.files.has(candidate));
121
- if (hit) roots.add(hit);
122
- }
124
+ for (const match of text.matchAll(CONFIG_STRING)) {
125
+ const target = path.posix.join(directory, match[1] ?? '');
126
+ const hit = CONFIG_EXTENSIONS.map(extension => `${target}${extension}`).find(candidate => graph.files.has(candidate));
127
+ if (hit)
128
+ roots.add(hit);
129
+ }
123
130
  };
124
-
125
131
  const addAppConfigRoots = (graph, config, app, roots) => {
126
- const directory = `apps/${app.name}`;
127
- let names = [];
128
- try { names = fs.readdirSync(path.join(config.root, ...directory.split('/'))); } catch { return; }
129
- for (const name of names) {
130
- if (graph.resolver.classifyPath(`${directory}/${name}`).slot !== 'fe.app.next' || !/\.[cm]?[jt]sx?$/.test(name)) continue;
131
- let text = '';
132
- try { text = fs.readFileSync(path.join(config.root, ...directory.split('/'), name), 'utf8'); } catch { continue; }
133
- addConfigTextRoots(graph, directory, text, roots);
134
- }
132
+ const directory = `apps/${app.name}`;
133
+ let names = [];
134
+ try {
135
+ names = fs.readdirSync(path.join(config.root, ...directory.split('/')));
136
+ }
137
+ catch {
138
+ return;
139
+ }
140
+ for (const name of names) {
141
+ if (graph.resolver.classifyPath(`${directory}/${name}`).slot !== 'fe.app.next' || !/\.[cm]?[jt]sx?$/.test(name))
142
+ continue;
143
+ let text = '';
144
+ try {
145
+ text = fs.readFileSync(path.join(config.root, ...directory.split('/'), name), 'utf8');
146
+ }
147
+ catch {
148
+ continue;
149
+ }
150
+ addConfigTextRoots(graph, directory, text, roots);
151
+ }
135
152
  };
136
-
137
153
  const addFrameworkConfigRoots = (graph, config, roots) => {
138
- for (const app of config.apps ?? []) addAppConfigRoots(graph, config, app, roots);
154
+ for (const app of config.apps ?? [])
155
+ addAppConfigRoots(graph, config, app, roots);
139
156
  };
140
-
141
157
  /** Roots are required app files, registered operation contracts, routes, package entries and config-named files. */
142
158
  function rootFiles(graph, config, context) {
143
- const roots = new Set();
144
- for (const [rel, node] of graph.files) {
145
- if (node.tier === 'route') { roots.add(rel); continue; }
146
- if (!node.owner || node.tier !== 'app') continue;
147
- const slot = graph.resolver.slot(node.owner.slot);
148
- const inside = rel.slice(withoutSlash(node.owner.root).length + 1);
149
- if ((slot?.requires ?? []).includes(inside)) roots.add(rel);
150
- }
151
- for (const [key, owner] of graph.ownerRoots) {
152
- if (owner.tier !== 'package') continue;
153
- const entry = entryOf(graph, config, key, owner);
154
- if (entry) roots.add(entry);
155
- }
156
- // A framework config of an app (slot fe.app.next) names sources by string, never by import.
157
- addFrameworkConfigRoots(graph, config, roots);
158
- for (const entry of operationContractRoots(context, graph, config)) roots.add(entry);
159
- return roots;
159
+ const roots = new Set();
160
+ for (const [rel, node] of graph.files) {
161
+ if (node.tier === 'route') {
162
+ roots.add(rel);
163
+ continue;
164
+ }
165
+ if (!node.owner || node.tier !== 'app')
166
+ continue;
167
+ const slot = graph.resolver.slot(node.owner.slot);
168
+ const inside = rel.slice(withoutSlash(node.owner.root).length + 1);
169
+ if ((slot?.requires ?? []).includes(inside))
170
+ roots.add(rel);
171
+ }
172
+ for (const [key, owner] of graph.ownerRoots) {
173
+ if (owner.tier !== 'package')
174
+ continue;
175
+ const entry = entryOf(graph, config, key, owner);
176
+ if (entry)
177
+ roots.add(entry);
178
+ }
179
+ // A framework config of an app (slot fe.app.next) names sources by string, never by import.
180
+ addFrameworkConfigRoots(graph, config, roots);
181
+ for (const entry of operationContractRoots(context, graph, config))
182
+ roots.add(entry);
183
+ return roots;
160
184
  }
161
-
162
185
  const unusedFileViolation = (rel, node) => ({
163
- ruleId: 'HFS_UNUSED_FILE',
164
- path: rel, line: 1, column: 1,
165
- slot: node.slot, owner: node.owner?.root ?? null,
166
- message: `${rel} is not reached from any root (an app main.ts or app.module.ts, a registered operation contract, a route file, a package entry): nothing consumes it. Delete the file, or import it where it is used (a spec alone does not count).`,
186
+ ruleId: 'HFS_UNUSED_FILE',
187
+ path: rel, line: 1, column: 1,
188
+ slot: node.slot, owner: node.owner?.root ?? null,
189
+ message: `${rel} is not reached from any root (an app main.ts or app.module.ts, a registered operation contract, a route file, a package entry): nothing consumes it. Delete the file, or import it where it is used (a spec alone does not count).`,
167
190
  });
168
-
169
191
  /** HFS_UNUSED_FILE: production files no root reaches through runtime or type-only imports and re-exports. */
170
192
  function deadFiles(graph, config, context) {
171
- const roots = rootFiles(graph, config, context);
172
- const forward = Map.groupBy(graph.edges, edge => edge.from);
173
- const reached = new Set(roots);
174
- const queue = [...roots];
175
- while (queue.length) for (const { to } of forward.get(queue.shift()) ?? []) if (!reached.has(to)) { reached.add(to); queue.push(to); }
176
- const judgedFiles = [...graph.files]
177
- .sort(([a], [b]) => a.localeCompare(b))
178
- .filter(([, node]) => !(node.tier === null || UNJUDGED_TIERS.has(node.tier) || node.tier === 'route'));
179
- const violations = judgedFiles.filter(([rel]) => !reached.has(rel)).map(([rel, node]) => unusedFileViolation(rel, node));
180
- return { violations, judged: judgedFiles.length, roots: roots.size };
193
+ const roots = rootFiles(graph, config, context);
194
+ const forward = edgeIndex(graph.edges, edge => edge.from);
195
+ const reached = new Set(roots);
196
+ const queue = [...roots];
197
+ while (queue.length) {
198
+ const current = queue.shift();
199
+ if (current === undefined)
200
+ break;
201
+ for (const { to } of forward.get(current) ?? [])
202
+ if (!reached.has(to)) {
203
+ reached.add(to);
204
+ queue.push(to);
205
+ }
206
+ }
207
+ const judgedFiles = [...graph.files]
208
+ .sort(([a], [b]) => a.localeCompare(b))
209
+ .filter(([, node]) => !(node.tier === null || UNJUDGED_TIERS.has(node.tier) || node.tier === 'route'));
210
+ const violations = judgedFiles.filter(([rel]) => !reached.has(rel)).map(([rel, node]) => unusedFileViolation(rel, node));
211
+ return { violations, judged: judgedFiles.length, roots: roots.size };
181
212
  }
182
-
183
213
  /** The unit spec of a webhook, gateway, subscription or SSE controller door: a door has no service of its own, so its spec is the only unit spec of its kind. */
184
214
  const DOOR_SPEC = /^src\/features\/(?:webhooks|realtime)\/.+\.(?:webhook|gateway|subscription|controller)\.spec\.ts$/u;
185
215
  const BUILDER_CONSUMER = /^src\/tests\/fixtures\/builders\/.+\.builder\.ts$/u;
186
216
  /** The test world slots (knowledge/hfs/slots.yaml): their files wire the real capabilities of the e2e run. */
187
217
  const WORLD_SLOTS = new Set(['be.tests.world', 'be.tests.world.kit']);
188
-
189
218
  /** Whether `rel` is a test file that counts as a consumer of any owner: a unit role spec, a fixture builder, a world file. */
190
219
  function testConsumer(graph, rel) {
191
- const params = graph.resolver.ruleParams();
192
- const specs = [...(params.unitRoles ?? []).map(role => role.spec), ...(params.logicRoles ?? []).map(role => `${role}.spec`)];
193
- if (/^(?:src|apps)\//u.test(rel) && specs.some(spec => rel.endsWith(`.${spec}.ts`))) return true;
194
- if (DOOR_SPEC.test(rel)) return true;
195
- if (BUILDER_CONSUMER.test(rel)) return true;
196
- const classified = graph.resolver.classifyPath(rel);
197
- return WORLD_SLOTS.has(classified.slot) && classified.status !== 'forbidden' && /\.tsx?$/u.test(rel);
220
+ const params = graph.resolver.ruleParams() ?? {};
221
+ const specs = [...(params.unitRoles ?? []).map((role) => role.spec), ...(params.logicRoles ?? []).map((role) => `${role}.spec`)];
222
+ if (/^(?:src|apps)\//u.test(rel) && specs.some(spec => rel.endsWith(`.${spec}.ts`)))
223
+ return true;
224
+ if (DOOR_SPEC.test(rel))
225
+ return true;
226
+ if (BUILDER_CONSUMER.test(rel))
227
+ return true;
228
+ const classified = graph.resolver.classifyPath(rel);
229
+ return classified.slot !== undefined && WORLD_SLOTS.has(classified.slot) && classified.status !== 'forbidden' && /\.tsx?$/u.test(rel);
198
230
  }
199
231
  /** The slot of integration specs and the slot of the integrations they pair with by folder (knowledge/hfs/slots.yaml). */
200
232
  const INTEGRATION_SPEC_SLOT = 'be.tests.integration';
201
233
  const INTEGRATION_SLOT = 'be.integrations';
202
-
203
234
  /** The provider an integration spec pairs with (its `<capability>` folder), or null when `rel` is no integration spec. */
204
235
  function integrationSpecProvider(graph, rel) {
205
- const classified = graph.resolver.classifyPath(rel);
206
- return classified.slot === INTEGRATION_SPEC_SLOT && classified.status !== 'forbidden' ? classified.bindings?.capability ?? null : null;
236
+ const classified = graph.resolver.classifyPath(rel);
237
+ return classified.slot === INTEGRATION_SPEC_SLOT && classified.status !== 'forbidden' ? classified.bindings?.capability ?? null : null;
207
238
  }
208
-
209
239
  /** Whether `to` belongs to the integration of `provider` (slot be.integrations, its `<provider>` binding). */
210
240
  function inIntegrationOf(graph, to, provider) {
211
- const owner = graph.resolver.ownerOf(to);
212
- return owner?.slot === INTEGRATION_SLOT && owner.bindings?.provider === provider;
241
+ const owner = graph.resolver.ownerOf(to);
242
+ return owner?.slot === INTEGRATION_SLOT && owner.bindings?.provider === provider;
213
243
  }
214
-
215
244
  /** The pseudo edges of one test consumer file: its import/export declarations that resolve to graph files it may reach. */
216
245
  function consumerFileEdges({ ts, options, graph, config }, rel, provider, indexedText) {
217
- const abs = path.join(config.root, ...rel.split('/'));
218
- let text = indexedText;
219
- if (text === undefined) {
220
- try { text = fs.readFileSync(abs, 'utf8'); } catch { return []; }
221
- }
222
- const sourceFile = ts.createSourceFile(abs, text, ts.ScriptTarget.Latest, true);
223
- const edges = [];
224
- for (const statement of sourceFile.statements) {
225
- if (!(ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) || !statement.moduleSpecifier || !ts.isStringLiteralLike(statement.moduleSpecifier)) continue;
226
- const resolved = ts.resolveModuleName(statement.moduleSpecifier.text, abs, options, ts.sys).resolvedModule?.resolvedFileName;
227
- const to = resolved ? graph.abs(canonical(resolved)) : null;
228
- if (to && (provider === null || inIntegrationOf(graph, to, provider))) edges.push({ from: rel, to, edge: { declaration: statement }, reexport: false });
229
- }
230
- return edges;
246
+ const abs = path.join(config.root, ...rel.split('/'));
247
+ let text = indexedText;
248
+ if (text === undefined) {
249
+ try {
250
+ text = fs.readFileSync(abs, 'utf8');
251
+ }
252
+ catch {
253
+ return [];
254
+ }
255
+ }
256
+ const sourceFile = ts.createSourceFile(abs, text, ts.ScriptTarget.Latest, true);
257
+ const edges = [];
258
+ for (const statement of sourceFile.statements) {
259
+ if (!(ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) || !statement.moduleSpecifier || !ts.isStringLiteralLike(statement.moduleSpecifier))
260
+ continue;
261
+ const resolved = ts.resolveModuleName(statement.moduleSpecifier.text, abs, options, ts.sys).resolvedModule?.resolvedFileName;
262
+ const to = resolved ? graph.abs(canonical(resolved)) : null;
263
+ if (to && (provider === null || inIntegrationOf(graph, to, provider)))
264
+ edges.push({ from: rel, to, edge: { declaration: statement }, reexport: false });
265
+ }
266
+ return edges;
231
267
  }
232
-
233
268
  /**
234
269
  * Pseudo edges (read from disk and staged index sources, outside the production program) to graph files: from the unit role specs, the fixture builders and
235
270
  * the test world files to anything, and from an integration spec only to the integration of its own provider folder.
236
271
  */
237
272
  function testConsumerEdges({ context, graph, config }) {
238
- const resolution = { ts: context.ts, options: context.projects?.[0]?.options ?? {}, graph, config };
239
- // Live lint keeps tracked disk consumers; staged snapshots add the consumers present in the same commit.
240
- const staged = stagedConsumerSources(config.root, file => testConsumer(graph, file) || integrationSpecProvider(graph, file) !== null);
241
- const consumers = [...[...treeOf(config.root).files].map(file => ({ file })), ...staged]
242
- .map(source => ({ ...source, any: testConsumer(graph, source.file) }))
243
- .map(({ file, text, any }) => ({ file, text, provider: any ? null : integrationSpecProvider(graph, file), any }))
244
- .filter(({ any, provider }) => any || provider !== null)
245
- .sort((a, b) => a.file.localeCompare(b.file));
246
- return consumers.flatMap(({ file: rel, text, provider }) => consumerFileEdges(resolution, rel, provider, text));
273
+ const resolution = { ts: context.ts, options: context.projects?.[0]?.options ?? {}, graph, config };
274
+ // Live lint keeps tracked disk consumers; staged snapshots add the consumers present in the same commit.
275
+ const staged = stagedConsumerSources(config.root, file => testConsumer(graph, file) || integrationSpecProvider(graph, file) !== null);
276
+ const consumers = [...[...treeOf(config.root).files].map((file) => ({ file })), ...staged]
277
+ .map(source => ({ ...source, any: testConsumer(graph, source.file) }))
278
+ .map(({ file, text, any }) => ({ file, text, provider: any ? null : integrationSpecProvider(graph, file), any }))
279
+ .filter(({ any, provider }) => any || provider !== null)
280
+ .sort((a, b) => a.file.localeCompare(b.file));
281
+ return consumers.flatMap(({ file: rel, text, provider }) => consumerFileEdges(resolution, rel, provider, text));
247
282
  }
248
-
249
- const edgeIndex = edges => {
250
- const edgesTo = new Map();
251
- for (const edge of edges) {
252
- if (!edgesTo.has(edge.to)) edgesTo.set(edge.to, []);
253
- edgesTo.get(edge.to).push(edge);
254
- }
255
- return edgesTo;
283
+ /** The edges grouped by an end of the edge (`to` unless a selector says otherwise), in edge order. */
284
+ const edgeIndex = (edges, keyOf = edge => edge.to) => {
285
+ const grouped = new Map();
286
+ for (const edge of edges) {
287
+ const key = keyOf(edge);
288
+ let list = grouped.get(key);
289
+ if (!list) {
290
+ list = [];
291
+ grouped.set(key, list);
292
+ }
293
+ list.push(edge);
294
+ }
295
+ return grouped;
256
296
  };
257
-
258
297
  const unitOfFile = (graph, rel) => {
259
- const known = graph.unit(rel);
260
- if (known) return known;
261
- const classified = graph.resolver.classifyPath(rel);
262
- const owner = classified.slot ? graph.resolver.ownerOf(rel) : null;
263
- return owner ? `${owner.slot}:${owner.root}` : null;
298
+ const known = graph.unit(rel);
299
+ if (known)
300
+ return known;
301
+ const classified = graph.resolver.classifyPath(rel);
302
+ const owner = classified.slot ? graph.resolver.ownerOf(rel) : null;
303
+ return owner ? `${owner.slot}:${owner.root}` : null;
264
304
  };
265
-
266
305
  const consumeReexportedName = (context, result, edge, pair, depth, visiting) => {
267
- if (pair.exported === null) { result.names.add(pair.imported); return; }
268
- // A re-export by name: the name is used when the re-exporting file's own export is used further on.
269
- const reexporter = edge.from;
270
- if (visiting.has(reexporter) || depth >= CHAIN_DEPTH) { result.names.add(pair.imported); return; }
271
- if (!(context.edgesTo.get(reexporter)?.length)) {
272
- // nobody imports the re-exporting file: a framework entry (route file) uses it, an unused owner entry does not
273
- const holder = context.graph.files.get(reexporter);
274
- if (!holder?.owner || context.graph.resolver.slot(holder.owner.slot)?.tier === 'app') result.names.add(pair.imported);
275
- return;
276
- }
277
- const next = consumedNames(context, reexporter, null, depth + 1, new Set([...visiting, reexporter]));
278
- if (next.all || next.names.has(pair.exported)) result.names.add(pair.imported);
306
+ if (pair.exported === null) {
307
+ result.names.add(pair.imported);
308
+ return;
309
+ }
310
+ // A re-export by name: the name is used when the re-exporting file's own export is used further on.
311
+ const reexporter = edge.from;
312
+ if (visiting.has(reexporter) || depth >= CHAIN_DEPTH) {
313
+ result.names.add(pair.imported);
314
+ return;
315
+ }
316
+ if (!(context.edgesTo.get(reexporter)?.length)) {
317
+ // nobody imports the re-exporting file: a framework entry (route file) uses it, an unused owner entry does not
318
+ const holder = context.graph.files.get(reexporter);
319
+ if (!holder?.owner || context.graph.resolver.slot(holder.owner.slot)?.tier === 'app')
320
+ result.names.add(pair.imported);
321
+ return;
322
+ }
323
+ const next = consumedNames(context, reexporter, null, depth + 1, new Set([...visiting, reexporter]));
324
+ if (next.all || next.names.has(pair.exported))
325
+ result.names.add(pair.imported);
279
326
  };
280
-
281
327
  /** What the importers of `file` take from it; edges from inside `ownerKey` are skipped when given. */
282
328
  const consumedNames = (context, file, ownerKey, depth, visiting) => {
283
- const result = { all: false, names: new Set() };
284
- for (const edge of context.edgesTo.get(file) ?? []) {
285
- if (ownerKey && unitOfFile(context.graph, edge.from) === ownerKey) continue;
286
- const bindings = bindingsOf(context, edge.edge.declaration);
287
- if (bindings.all) { result.all = true; continue; }
288
- for (const pair of bindings.pairs) consumeReexportedName(context, result, edge, pair, depth, visiting);
289
- }
290
- return result;
329
+ const result = { all: false, names: new Set() };
330
+ for (const edge of context.edgesTo.get(file) ?? []) {
331
+ if (ownerKey && unitOfFile(context.graph, edge.from) === ownerKey)
332
+ continue;
333
+ const bindings = bindingsOf(context, edge.edge.declaration);
334
+ if (bindings.all) {
335
+ result.all = true;
336
+ continue;
337
+ }
338
+ for (const pair of bindings.pairs)
339
+ consumeReexportedName(context, result, edge, pair, depth, visiting);
340
+ }
341
+ return result;
291
342
  };
292
-
293
343
  /** Adds to `used` the entry exports reached by a consumer outside the owner that imports a file the entry re-exports by name. */
294
344
  const addReexportedUses = (context, key, publicEntry, used) => {
295
- const { graph } = context;
296
- for (const edge of graph.edges) {
297
- if (edge.from !== publicEntry || !edge.reexport || graph.unit(edge.to) !== key) continue;
298
- const reexport = bindingsOf(context, edge.edge.declaration);
299
- const inner = consumedNames(context, edge.to, key, 0, new Set([publicEntry, edge.to]));
300
- if (reexport.all) continue;
301
- for (const pair of reexport.pairs) if (inner.all || inner.names.has(pair.imported)) used.names.add(pair.exported);
302
- }
345
+ const { graph } = context;
346
+ for (const edge of graph.edges) {
347
+ if (edge.from !== publicEntry || !edge.reexport || graph.unit(edge.to) !== key)
348
+ continue;
349
+ const reexport = bindingsOf(context, edge.edge.declaration);
350
+ const inner = consumedNames(context, edge.to, key, 0, new Set([publicEntry, edge.to]));
351
+ if (reexport.all)
352
+ continue;
353
+ for (const pair of reexport.pairs)
354
+ if ((inner.all || inner.names.has(pair.imported)) && pair.exported !== null)
355
+ used.names.add(pair.exported);
356
+ }
303
357
  };
304
-
305
358
  const unusedExportViolation = (publicEntry, name, line, owner) => ({
306
- ruleId: 'HFS_UNUSED_EXPORT',
307
- path: publicEntry, line, column: 1,
308
- name, owner: owner.root, slot: owner.slot,
309
- message: `${publicEntry} exports ${name}, but no production file outside ${owner.root || 'the repository root'} imports it; remove the export (only a unit role spec, a fixture builder, a test world file, or an integration spec of this integration's own provider folder counts besides production files).`,
359
+ ruleId: 'HFS_UNUSED_EXPORT',
360
+ path: publicEntry, line, column: 1,
361
+ name, owner: owner.root, slot: owner.slot,
362
+ message: `${publicEntry} exports ${name}, but no production file outside ${owner.root || 'the repository root'} imports it; remove the export (only a unit role spec, a fixture builder, a test world file, or an integration spec of this integration's own provider folder counts besides production files).`,
310
363
  });
311
-
312
364
  /** Adds to `used` what another declared entry of the same owner imports from `publicEntry`: that entry's signature makes it public. */
313
365
  const addSiblingEntryUses = (context, publicEntry, declared, used) => {
314
- for (const edge of context.edgesTo.get(publicEntry) ?? []) {
315
- if (edge.reexport || edge.from === publicEntry || !declared.has(edge.from)) continue;
316
- const bindings = bindingsOf(context, edge.edge.declaration);
317
- if (bindings.all) used.all = true;
318
- for (const pair of bindings.pairs) used.names.add(pair.imported);
319
- }
366
+ for (const edge of context.edgesTo.get(publicEntry) ?? []) {
367
+ if (edge.reexport || edge.from === publicEntry || !declared.has(edge.from))
368
+ continue;
369
+ const bindings = bindingsOf(context, edge.edge.declaration);
370
+ if (bindings.all)
371
+ used.all = true;
372
+ for (const pair of bindings.pairs)
373
+ used.names.add(pair.imported);
374
+ }
320
375
  };
321
-
322
376
  /** The unused-export violations and the export count of one public entry of an owner (`declared`: its slot's extra entries). */
323
377
  const publicEntryExports = (context, key, owner, publicEntry, declared) => {
324
- const names = exportedNames(context.ts, context.graph.files.get(publicEntry).sourceFile);
325
- const used = consumedNames(context, publicEntry, key, 0, new Set([publicEntry]));
326
- addReexportedUses(context, key, publicEntry, used);
327
- if (declared.has(publicEntry)) addSiblingEntryUses(context, publicEntry, declared, used);
328
- const violations = [...names]
329
- .filter(([name]) => !(used.all || used.names.has(name)))
330
- .map(([name, line]) => unusedExportViolation(publicEntry, name, line, owner));
331
- return { violations, exports: names.size };
378
+ const entryFile = context.graph.files.get(publicEntry);
379
+ if (!entryFile)
380
+ return { violations: [], exports: 0 };
381
+ const names = exportedNames(context.ts, entryFile.sourceFile);
382
+ const used = consumedNames(context, publicEntry, key, 0, new Set([publicEntry]));
383
+ addReexportedUses(context, key, publicEntry, used);
384
+ if (declared.has(publicEntry))
385
+ addSiblingEntryUses(context, publicEntry, declared, used);
386
+ const violations = [...names]
387
+ .filter(([name]) => !(used.all || used.names.has(name)))
388
+ .map(([name, line]) => unusedExportViolation(publicEntry, name, line, owner));
389
+ return { violations, exports: names.size };
332
390
  };
333
-
334
391
  const ownerExportFindings = (context, config) => {
335
- const { graph } = context;
336
- const violations = [];
337
- let owners = 0;
338
- let exports = 0;
339
- for (const [key, owner] of graph.ownerRoots) {
340
- if (owner.tier === 'app') continue;
341
- const entry = entryOf(graph, config, key, owner);
342
- if (!entry) continue;
343
- owners += 1;
344
- const declared = new Set(extraEntries(graph, entry));
345
- for (const publicEntry of [entry, ...declared]) {
346
- const found = publicEntryExports(context, key, owner, publicEntry, declared);
347
- exports += found.exports;
348
- violations.push(...found.violations);
392
+ const { graph } = context;
393
+ const violations = [];
394
+ let owners = 0;
395
+ let exports = 0;
396
+ for (const [key, owner] of graph.ownerRoots) {
397
+ if (owner.tier === 'app')
398
+ continue;
399
+ const entry = entryOf(graph, config, key, owner);
400
+ if (!entry)
401
+ continue;
402
+ owners += 1;
403
+ const declared = new Set(extraEntries(graph, entry));
404
+ for (const publicEntry of [entry, ...declared]) {
405
+ const found = publicEntryExports(context, key, owner, publicEntry, declared);
406
+ exports += found.exports;
407
+ violations.push(...found.violations);
408
+ }
349
409
  }
350
- }
351
- return { violations, owners, exports };
410
+ return { violations, owners, exports };
352
411
  };
353
-
354
412
  export function checkDeadExports({ context, graph, config }) {
355
- const edgesTo = edgeIndex([...graph.edges, ...testConsumerEdges({ context, graph, config })]);
356
- const analysis = { ...context, graph, edgesTo, namespaceCheckers: new WeakMap(), namespaceUses: new WeakMap() };
357
- const exports = ownerExportFindings(analysis, config);
358
- const files = deadFiles(graph, config, context);
359
- const dead = exports.violations.length;
360
- exports.violations.push(...files.violations);
361
- return { violations: exports.violations, coverage: { status: 'checked', owners: exports.owners, exports: exports.exports, dead, files: files.judged, roots: files.roots, unusedFiles: files.violations.length } };
413
+ const edgesTo = edgeIndex([...graph.edges, ...testConsumerEdges({ context, graph, config })]);
414
+ const analysis = { ...context, graph, edgesTo, namespaceCheckers: new WeakMap(), namespaceUses: new WeakMap() };
415
+ const exports = ownerExportFindings(analysis, config);
416
+ const files = deadFiles(graph, config, context);
417
+ const dead = exports.violations.length;
418
+ exports.violations.push(...files.violations);
419
+ return { violations: exports.violations, coverage: { status: 'checked', owners: exports.owners, exports: exports.exports, dead, files: files.judged, roots: files.roots, unusedFiles: files.violations.length } };
362
420
  }