@starci/eslint-canon-be 3.18.4 → 3.19.1

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 (211) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +1 -1
  3. package/dist/comments.js +1 -1
  4. package/dist/explicit-surface.js +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/jwt-alg.js +1 -1
  7. package/dist/lib/edition.d.ts +1 -1
  8. package/dist/lib/edition.js +1 -1
  9. package/dist/lib/hfs-view.d.ts +5 -0
  10. package/dist/lib/hfs.d.ts +0 -6
  11. package/dist/lib/hfs.js +7 -5
  12. package/dist/lib/runtime-root.d.ts +6 -1
  13. package/dist/lib/runtime-root.js +8 -2
  14. package/dist/lib/subject-values.js +1 -1
  15. package/dist/module-layering.js +2 -2
  16. package/dist/project-graph.js +1 -1
  17. package/dist/schema-authority.js +66 -2
  18. package/dist/third-party-owner.js +1 -1
  19. package/package.json +3 -1
  20. package/runtime/bundle.json +1 -190
  21. package/runtime/scripts/api/git/recorded-lines.mjs +5 -5
  22. package/runtime/scripts/hfs/project-graph.mjs +6 -6
  23. package/runtime/scripts/hfs/project-rule.mjs +3 -3
  24. package/runtime/config.example.yaml +0 -194
  25. package/runtime/engine/by-code-unit.mjs +0 -12
  26. package/runtime/engine/config.mjs +0 -461
  27. package/runtime/engine/invalid-config.mjs +0 -15
  28. package/runtime/engine/model-config.mjs +0 -112
  29. package/runtime/engine/orca-config.mjs +0 -34
  30. package/runtime/engine/plain-object.mjs +0 -5
  31. package/runtime/engine/release-config.mjs +0 -32
  32. package/runtime/engine/removed-vocabulary.mjs +0 -27
  33. package/runtime/engine/resources-config.mjs +0 -59
  34. package/runtime/engine/runtime-root.mjs +0 -138
  35. package/runtime/engine/secrets.mjs +0 -202
  36. package/runtime/engine/sonar-config.mjs +0 -24
  37. package/runtime/engine/temp-root.mjs +0 -26
  38. package/runtime/engine/yaml.mjs +0 -161
  39. package/runtime/knowledge/grammars/index.yaml +0 -114
  40. package/runtime/knowledge/hfs/canon-pins.yaml +0 -720
  41. package/runtime/knowledge/hfs/fe-stack.yaml +0 -272
  42. package/runtime/knowledge/hfs/rules.yaml +0 -6760
  43. package/runtime/knowledge/hfs/slots.yaml +0 -2783
  44. package/runtime/knowledge/patterns/fe/folder.yaml +0 -425
  45. package/runtime/knowledge/sonar-gate.yaml +0 -151
  46. package/runtime/modules/kernel/removed-vocabulary.yaml +0 -458
  47. package/runtime/modules/models/registry.yaml +0 -221
  48. package/runtime/modules/models/runtimes.yaml +0 -556
  49. package/runtime/modules/models/tiers.yaml +0 -66
  50. package/runtime/scripts/api/git/cat-file.mjs +0 -4
  51. package/runtime/scripts/api/git/config-get.mjs +0 -9
  52. package/runtime/scripts/api/git/diff.mjs +0 -4
  53. package/runtime/scripts/api/git/lib.mjs +0 -80
  54. package/runtime/scripts/api/git/ls-files.mjs +0 -5
  55. package/runtime/scripts/api/git/ls-tree.mjs +0 -4
  56. package/runtime/scripts/api/git/merge-base.mjs +0 -8
  57. package/runtime/scripts/api/git/remote.mjs +0 -4
  58. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -4
  59. package/runtime/scripts/api/git/show.mjs +0 -5
  60. package/runtime/scripts/api/git/tag.mjs +0 -4
  61. package/runtime/scripts/hfs/allows.mjs +0 -77
  62. package/runtime/scripts/hfs/architecture/ast-walks.mjs +0 -250
  63. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +0 -103
  64. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +0 -114
  65. package/runtime/scripts/hfs/architecture/backend.mjs +0 -462
  66. package/runtime/scripts/hfs/architecture/background-unowned.mjs +0 -254
  67. package/runtime/scripts/hfs/architecture/canon-clones.mjs +0 -291
  68. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +0 -224
  69. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +0 -102
  70. package/runtime/scripts/hfs/architecture/clones.mjs +0 -287
  71. package/runtime/scripts/hfs/architecture/component-tiers.mjs +0 -161
  72. package/runtime/scripts/hfs/architecture/config-unread.mjs +0 -16
  73. package/runtime/scripts/hfs/architecture/config.mjs +0 -407
  74. package/runtime/scripts/hfs/architecture/connection-map.mjs +0 -350
  75. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +0 -115
  76. package/runtime/scripts/hfs/architecture/context-coupling.mjs +0 -117
  77. package/runtime/scripts/hfs/architecture/context-map.mjs +0 -355
  78. package/runtime/scripts/hfs/architecture/context-owner.mjs +0 -102
  79. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +0 -65
  80. package/runtime/scripts/hfs/architecture/context-transaction.mjs +0 -75
  81. package/runtime/scripts/hfs/architecture/contract-bounds.mjs +0 -72
  82. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +0 -177
  83. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +0 -360
  84. package/runtime/scripts/hfs/architecture/contracts.mjs +0 -489
  85. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +0 -81
  86. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +0 -173
  87. package/runtime/scripts/hfs/architecture/dead-exports.mjs +0 -420
  88. package/runtime/scripts/hfs/architecture/default-deny.mjs +0 -175
  89. package/runtime/scripts/hfs/architecture/doc-language.mjs +0 -45
  90. package/runtime/scripts/hfs/architecture/entity-registry.mjs +0 -51
  91. package/runtime/scripts/hfs/architecture/entrypoint.mjs +0 -75
  92. package/runtime/scripts/hfs/architecture/error-codes.mjs +0 -54
  93. package/runtime/scripts/hfs/architecture/error-masked.mjs +0 -78
  94. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +0 -44
  95. package/runtime/scripts/hfs/architecture/feature-shape.mjs +0 -60
  96. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +0 -41
  97. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +0 -112
  98. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +0 -193
  99. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +0 -198
  100. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +0 -368
  101. package/runtime/scripts/hfs/architecture/frontend-world.mjs +0 -374
  102. package/runtime/scripts/hfs/architecture/frontend.mjs +0 -248
  103. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +0 -185
  104. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +0 -77
  105. package/runtime/scripts/hfs/architecture/hfs.mjs +0 -394
  106. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +0 -118
  107. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +0 -213
  108. package/runtime/scripts/hfs/architecture/identity.mjs +0 -121
  109. package/runtime/scripts/hfs/architecture/index.mjs +0 -402
  110. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +0 -69
  111. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +0 -231
  112. package/runtime/scripts/hfs/architecture/machine-ast.mjs +0 -232
  113. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +0 -32
  114. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +0 -237
  115. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +0 -126
  116. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +0 -192
  117. package/runtime/scripts/hfs/architecture/next-data-key.mjs +0 -374
  118. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +0 -26
  119. package/runtime/scripts/hfs/architecture/next-data.mjs +0 -425
  120. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +0 -55
  121. package/runtime/scripts/hfs/architecture/owners.mjs +0 -142
  122. package/runtime/scripts/hfs/architecture/package-shape.mjs +0 -92
  123. package/runtime/scripts/hfs/architecture/presentation.mjs +0 -173
  124. package/runtime/scripts/hfs/architecture/reachability.mjs +0 -330
  125. package/runtime/scripts/hfs/architecture/register-once.mjs +0 -212
  126. package/runtime/scripts/hfs/architecture/registration.mjs +0 -372
  127. package/runtime/scripts/hfs/architecture/render-graph.mjs +0 -449
  128. package/runtime/scripts/hfs/architecture/render-prop-across-server.mjs +0 -58
  129. package/runtime/scripts/hfs/architecture/required-files.mjs +0 -237
  130. package/runtime/scripts/hfs/architecture/root-packages.mjs +0 -78
  131. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +0 -133
  132. package/runtime/scripts/hfs/architecture/schema-owner.mjs +0 -288
  133. package/runtime/scripts/hfs/architecture/server-action.mjs +0 -78
  134. package/runtime/scripts/hfs/architecture/side-boundary.mjs +0 -13
  135. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +0 -320
  136. package/runtime/scripts/hfs/architecture/source-names.mjs +0 -393
  137. package/runtime/scripts/hfs/architecture/sql-keys.mjs +0 -344
  138. package/runtime/scripts/hfs/architecture/sql-owner.mjs +0 -291
  139. package/runtime/scripts/hfs/architecture/sql-returning.mjs +0 -76
  140. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +0 -117
  141. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +0 -587
  142. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +0 -42
  143. package/runtime/scripts/hfs/architecture/surface-nesting.mjs +0 -127
  144. package/runtime/scripts/hfs/architecture/surface.mjs +0 -88
  145. package/runtime/scripts/hfs/architecture/symbols.mjs +0 -273
  146. package/runtime/scripts/hfs/architecture/test-world-files.mjs +0 -263
  147. package/runtime/scripts/hfs/architecture/tiers.mjs +0 -256
  148. package/runtime/scripts/hfs/architecture/transport-owner.mjs +0 -248
  149. package/runtime/scripts/hfs/architecture/type-context.mjs +0 -335
  150. package/runtime/scripts/hfs/architecture/typescript.mjs +0 -477
  151. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +0 -165
  152. package/runtime/scripts/hfs/declaration-shape.mjs +0 -196
  153. package/runtime/scripts/hfs/declaration-slots.mjs +0 -89
  154. package/runtime/scripts/hfs/edition-slots.mjs +0 -80
  155. package/runtime/scripts/hfs/explicit-surface-rule.mjs +0 -59
  156. package/runtime/scripts/hfs/linear-text.mjs +0 -30
  157. package/runtime/scripts/hfs/manifest-shape.mjs +0 -474
  158. package/runtime/scripts/hfs/param-names.mjs +0 -13
  159. package/runtime/scripts/hfs/path-findings.mjs +0 -167
  160. package/runtime/scripts/hfs/repo-identity.mjs +0 -89
  161. package/runtime/scripts/hfs/rule-catalog.mjs +0 -458
  162. package/runtime/scripts/hfs/rule-params-shape.mjs +0 -103
  163. package/runtime/scripts/hfs/rules/database-constants.mjs +0 -25
  164. package/runtime/scripts/hfs/rules/database-migrations.mjs +0 -684
  165. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +0 -68
  166. package/runtime/scripts/hfs/rules/read.mjs +0 -45
  167. package/runtime/scripts/hfs/rules/secrets.mjs +0 -79
  168. package/runtime/scripts/hfs/rules/spec-placement.mjs +0 -28
  169. package/runtime/scripts/hfs/slot-app-view.mjs +0 -97
  170. package/runtime/scripts/hfs/slot-classify.mjs +0 -69
  171. package/runtime/scripts/hfs/slot-errors.mjs +0 -12
  172. package/runtime/scripts/hfs/slot-imports.mjs +0 -98
  173. package/runtime/scripts/hfs/slot-manifest-shape.mjs +0 -111
  174. package/runtime/scripts/hfs/slot-match.mjs +0 -164
  175. package/runtime/scripts/hfs/slot-path.mjs +0 -6
  176. package/runtime/scripts/hfs/slot-required.mjs +0 -95
  177. package/runtime/scripts/hfs/slot-semantic-problems.mjs +0 -175
  178. package/runtime/scripts/hfs/slot-side-problems.mjs +0 -32
  179. package/runtime/scripts/hfs/slots.mjs +0 -306
  180. package/runtime/scripts/hfs/test-world-slot.mjs +0 -5
  181. package/runtime/scripts/hfs/trailing-slashes.mjs +0 -8
  182. package/runtime/scripts/hfs/typescript-programs.mjs +0 -95
  183. package/runtime/scripts/hfs/view.mjs +0 -180
  184. package/runtime/scripts/lib/declaration-surface.mjs +0 -166
  185. package/runtime/scripts/lib/dockerfile.mjs +0 -164
  186. package/runtime/scripts/lib/env.mjs +0 -18
  187. package/runtime/scripts/lib/event-contract.mjs +0 -102
  188. package/runtime/scripts/lib/git-hooks-dir.mjs +0 -30
  189. package/runtime/scripts/lib/git.mjs +0 -35
  190. package/runtime/scripts/lib/glob.mjs +0 -32
  191. package/runtime/scripts/lib/grammar-lint.mjs +0 -57
  192. package/runtime/scripts/lib/i18n.mjs +0 -82
  193. package/runtime/scripts/lib/in-order.mjs +0 -90
  194. package/runtime/scripts/lib/json-clone.mjs +0 -70
  195. package/runtime/scripts/lib/json.mjs +0 -44
  196. package/runtime/scripts/lib/language.mjs +0 -233
  197. package/runtime/scripts/lib/list.mjs +0 -26
  198. package/runtime/scripts/lib/mutation-fence.mjs +0 -20
  199. package/runtime/scripts/lib/package-at.mjs +0 -62
  200. package/runtime/scripts/lib/path-key.mjs +0 -124
  201. package/runtime/scripts/lib/read-text.mjs +0 -47
  202. package/runtime/scripts/lib/regex.mjs +0 -3
  203. package/runtime/scripts/lib/rule-packs.mjs +0 -243
  204. package/runtime/scripts/lib/same-text.mjs +0 -4
  205. package/runtime/scripts/lib/secret-patterns.mjs +0 -230
  206. package/runtime/scripts/lib/sops-envelope.mjs +0 -212
  207. package/runtime/scripts/lib/stack-services.mjs +0 -191
  208. package/runtime/scripts/lib/ts-analysis.mjs +0 -39
  209. package/runtime/scripts/lib/ts-ast.mjs +0 -63
  210. package/runtime/scripts/lib/walk.mjs +0 -45
  211. package/runtime/scripts/lib/yaml-cached.mjs +0 -14
@@ -1,254 +0,0 @@
1
- import path from 'node:path';
2
- import { machineKit } from './machine-ast.mjs';
3
- /**
4
- * R46 `background-unowned` (BE_BACKGROUND_UNOWNED, BE-CONVENTION 1.6 and 1.13). Background work is a transport that only
5
- * a worker or api app runs (an api app composes the message transport of its own service; a separate worker is opt-in):
6
- *
7
- * - a processor, consumer, scheduler, saga or event handler is composed by an app of kind `worker` or `api`: its file is
8
- * reachable, through runtime imports, from an app root declared in hfs.json. Context comes from its file role or framework
9
- * decorators, never a method name. Infrastructure restrictions on scheduler libraries remain R90 BE_INFRA_OWNER's concern;
10
- * - methods in those contexts, and methods they call transitively, are reachable from a composed background entry point:
11
- * its imports and the files of its own feature (command handlers are registered with the bus). Platform mechanisms are
12
- * exempt. Resolvers and controllers are user entry points and never background, even when called by a background method.
13
- *
14
- * Classification follows statically resolved calls; ownership follows runtime imports, re-exports and the entry point's feature.
15
- */
16
- export const BACKGROUND_UNOWNED_RULE_IDS = ['BE_BACKGROUND_UNOWNED'];
17
- const RULE = 'BE_BACKGROUND_UNOWNED';
18
- const BACKGROUND_ROLES = new Set(['processor', 'consumer', 'scheduler', 'saga', 'event-handler']);
19
- const USER_ROLES = new Set(['resolver', 'controller']);
20
- const CLASS_DECORATORS = new Map([
21
- ['@nestjs/bull:Processor', 'processor'], ['@nestjs/bullmq:Processor', 'processor'],
22
- ['@nestjs/cqrs:EventsHandler', 'event-handler'],
23
- ]);
24
- const METHOD_DECORATORS = new Map([
25
- ['@nestjs/cqrs:Saga', 'saga'], ['@nestjs/event-emitter:OnEvent', 'event-handler'],
26
- ...['Cron', 'Interval', 'Timeout'].map((name) => [`@nestjs/schedule:${name}`, 'scheduler']),
27
- ]);
28
- const USER_DECORATORS = new Map([['@nestjs/graphql:Resolver', 'resolver'], ['@nestjs/common:Controller', 'controller']]);
29
- const roleOf = (rel) => {
30
- const parts = path.posix.basename(rel).split('.');
31
- return parts.length >= 3 && parts.at(-1) === 'ts' ? parts.at(-2) ?? null : null;
32
- };
33
- const decoratedRole = (kit, checker, node, roles) => {
34
- for (const decorator of kit.decorators(node)) {
35
- const expression = decorator.expression;
36
- const callee = kit.ts.isCallExpression(expression) ? expression.expression : expression;
37
- const binding = kit.importBinding(checker, callee);
38
- const role = binding ? roles.get(`${binding.module}:${binding.name}`) : null;
39
- if (role)
40
- return role;
41
- }
42
- return null;
43
- };
44
- const adjacencyOf = (edges) => {
45
- const adjacency = new Map();
46
- for (const edge of edges) {
47
- if (!edge.runtime)
48
- continue;
49
- let targets = adjacency.get(edge.from);
50
- if (!targets) {
51
- targets = [];
52
- adjacency.set(edge.from, targets);
53
- }
54
- targets.push(edge.to);
55
- }
56
- return adjacency;
57
- };
58
- const closureOf = (adjacency, starts) => {
59
- const seen = new Set(starts);
60
- const queue = [...starts];
61
- while (queue.length) {
62
- const current = queue.shift();
63
- if (current === undefined)
64
- break;
65
- for (const next of adjacency.get(current) ?? []) {
66
- if (seen.has(next))
67
- continue;
68
- seen.add(next);
69
- queue.push(next);
70
- }
71
- }
72
- return seen;
73
- };
74
- const appRootsOf = (apps, graph) => {
75
- const rootOf = (app) => `apps/${app.name}/src/app.module.ts`;
76
- const workers = apps.filter(app => app.kind === 'worker').map(rootOf).filter(rel => graph.files.has(rel));
77
- const services = apps.filter(app => app.kind === 'api').map(rootOf).filter(rel => graph.files.has(rel));
78
- return { workers, composed: [...workers, ...services] };
79
- };
80
- const reportUncomposedFiles = (background, composed, workerRoots, report) => {
81
- for (const { file, role } of background) {
82
- if (file.tier === 'platform' || composed.has(file.rel))
83
- continue;
84
- report(file, file.sourceFile, `${file.rel} is a ${role} that no service app composes${workerRoots.length ? '' : ' (hfs.json declares no worker app)'}; its module must be imported by the root module of an app of kind worker or api.`, { role });
85
- }
86
- };
87
- const rootsOfRunningFeatures = (running, files) => {
88
- const roots = new Set();
89
- for (const { file } of running) {
90
- roots.add(file.rel);
91
- const owner = file.owner?.root;
92
- if (owner)
93
- for (const other of files.values())
94
- if (other.owner?.root === owner)
95
- roots.add(other.rel);
96
- }
97
- return roots;
98
- };
99
- const callableKey = (kit, node) => `${kit.graphPath(node)}:${node.getStart()}`;
100
- const isCallable = (ts, node) => ts.isFunctionLike(node) && 'body' in node && Boolean(node.body);
101
- const callableBody = (ts, declaration) => {
102
- const node = declaration.initializer ?? declaration; // reason: only variable-like declarations carry an initializer
103
- return isCallable(ts, node) ? node : null;
104
- };
105
- const methodMember = (ts, node) => {
106
- if (ts.isMethodDeclaration(node))
107
- return node;
108
- return ts.isPropertyDeclaration(node.parent) && node.parent.initializer === node ? node.parent : null;
109
- };
110
- const isUserClass = (kit, checker, node) => USER_ROLES.has(roleOf(kit.graphPath(node) ?? '') ?? '')
111
- || Boolean(decoratedRole(kit, checker, node, USER_DECORATORS));
112
- const seedClass = (scope, file, checker, node, role) => {
113
- scope.background.set(file.rel, { file, role });
114
- scope.contextFiles.add(file.rel);
115
- for (const symbol of checker.getTypeAtLocation(node).getProperties()) {
116
- for (const declaration of symbol.declarations ?? []) {
117
- const body = callableBody(scope.kit.ts, declaration);
118
- if (body)
119
- scope.inherited.push(body);
120
- }
121
- }
122
- };
123
- const classContext = (scope, file, checker, node, state) => {
124
- const user = state.user || isUserClass(scope.kit, checker, node);
125
- if (!state.nested && user)
126
- scope.userFiles.add(file.rel);
127
- let classRole = null;
128
- if (!state.nested && !user) {
129
- const fileRole = state.fileRole !== null && BACKGROUND_ROLES.has(state.fileRole) ? state.fileRole : null;
130
- classRole = decoratedRole(scope.kit, checker, node, CLASS_DECORATORS) ?? fileRole;
131
- }
132
- if (classRole)
133
- seedClass(scope, file, checker, node, classRole);
134
- return { ...state, user, classRole };
135
- };
136
- const recordCallable = (scope, file, checker, node, state) => {
137
- const { kit } = scope;
138
- const member = methodMember(kit.ts, node);
139
- const canSeed = member && !state.user && !state.nested;
140
- const role = canSeed ? decoratedRole(kit, checker, member, METHOD_DECORATORS) ?? state.classRole : null;
141
- const entry = { file, node, checker, user: state.user, member, name: member ? kit.propertyNameText(member.name) : null };
142
- scope.callables.set(callableKey(kit, node), entry);
143
- if (role) {
144
- scope.background.set(file.rel, { file, role });
145
- scope.contextFiles.add(file.rel);
146
- scope.seeds.push(entry);
147
- }
148
- };
149
- const indexCallables = (scope, file, checker, node, state) => {
150
- const { ts } = scope.kit;
151
- if (ts.isClassDeclaration(node) || ts.isClassExpression(node))
152
- state = classContext(scope, file, checker, node, state);
153
- const callable = ts.isFunctionLike(node);
154
- if (isCallable(ts, node))
155
- recordCallable(scope, file, checker, node, state);
156
- const childState = { ...state, nested: state.nested || callable };
157
- ts.forEachChild(node, (child) => indexCallables(scope, file, checker, child, childState));
158
- };
159
- /** Index callable bodies once; a resolver/controller is a barrier to background propagation. */
160
- const backgroundContextOf = (graph, kit) => {
161
- const callables = new Map();
162
- const background = new Map();
163
- const seeds = [];
164
- const inherited = [];
165
- const scope = { kit, callables, background, seeds, inherited, userFiles: new Set(), contextFiles: new Set() };
166
- for (const file of graph.files.values()) {
167
- const checker = kit.checkerOf(file.sourceFile);
168
- const fileRole = roleOf(file.rel);
169
- if (fileRole !== null && BACKGROUND_ROLES.has(fileRole))
170
- background.set(file.rel, { file, role: fileRole });
171
- indexCallables(scope, file, checker, file.sourceFile, { fileRole, classRole: null, user: fileRole !== null && USER_ROLES.has(fileRole), nested: false });
172
- }
173
- // Request context overrides a file suffix; mixed files can still hold a separate background class.
174
- for (const rel of scope.userFiles)
175
- if (!scope.contextFiles.has(rel))
176
- background.delete(rel);
177
- for (const body of inherited) {
178
- const entry = callables.get(callableKey(kit, body));
179
- if (entry)
180
- seeds.push(entry);
181
- }
182
- return { callables, background: [...background.values()], seeds };
183
- };
184
- const calledBodies = (kit, checker, call) => {
185
- const { ts } = kit;
186
- const expression = call.expression;
187
- if (ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression)) {
188
- const receiver = checker.getNonNullableType(checker.getTypeAtLocation(expression.expression));
189
- if (receiver.getSymbol()?.declarations?.some(declaration => isUserClass(kit, checker, declaration)))
190
- return [];
191
- }
192
- const target = ts.isPropertyAccessExpression(expression) ? expression.name : expression;
193
- const declarations = [...kit.declarationsOf(checker, target)];
194
- const signature = checker.getResolvedSignature(call)?.declaration;
195
- if (signature)
196
- declarations.push(signature);
197
- return declarations.map(declaration => callableBody(ts, declaration)).filter((body) => body !== null);
198
- };
199
- const checkBackgroundMethods = ({ kit, context, reachable, report }) => {
200
- const { callables, seeds } = context;
201
- const seen = new Set();
202
- const queue = [...seeds];
203
- let methods = 0;
204
- while (queue.length) {
205
- const entry = queue.pop();
206
- if (entry === undefined)
207
- break;
208
- const { file, node, checker, user, member, name } = entry;
209
- const key = callableKey(kit, node);
210
- if (user || seen.has(key))
211
- continue;
212
- seen.add(key);
213
- if (name && member && file.tier !== 'platform') {
214
- methods += 1;
215
- if (!reachable.has(file.rel))
216
- report(file, member.name, `${name} is background work that no background entry point composed by a worker or api app can reach. Compose the processor, consumer, scheduler, saga or event handler that runs it in a worker or api app.`, { method: name });
217
- }
218
- kit.walk(node.body, (inner) => {
219
- // Nested bodies run only through a resolved call or when passed as a callback.
220
- if (kit.ts.isFunctionLike(inner) || kit.ts.isClassDeclaration(inner) || kit.ts.isClassExpression(inner))
221
- return false;
222
- if (!kit.ts.isCallExpression(inner))
223
- return undefined;
224
- const callbacks = inner.arguments.filter((argument) => kit.ts.isArrowFunction(argument) || kit.ts.isFunctionExpression(argument));
225
- for (const body of [...calledBodies(kit, checker, inner), ...callbacks]) {
226
- const called = callables.get(callableKey(kit, body));
227
- if (called)
228
- queue.push(called);
229
- }
230
- return undefined;
231
- });
232
- }
233
- return methods;
234
- };
235
- export function checkBackgroundUnowned(input) {
236
- const { graph } = input;
237
- const kit = machineKit(input);
238
- const { resolver } = kit;
239
- const violations = [];
240
- const report = (file, node, message, extra = {}) => violations.push({ ruleId: RULE, path: file.rel, ...kit.at(file.rel, file.sourceFile, node), message, ...extra });
241
- const adjacency = adjacencyOf(graph.edges);
242
- const closure = (starts) => closureOf(adjacency, starts);
243
- const { workers: workerRoots, composed: appRoots } = appRootsOf(resolver.repo.apps, graph);
244
- const composed = closure(appRoots);
245
- const context = backgroundContextOf(graph, kit);
246
- const { background } = context;
247
- const running = background.filter(({ file }) => composed.has(file.rel));
248
- reportUncomposedFiles(background, composed, workerRoots, report);
249
- // Everything a composed background entry point can reach: its imports and its own feature's command handlers.
250
- const roots = rootsOfRunningFeatures(running, graph.files);
251
- const reachable = closure([...roots]);
252
- const methods = checkBackgroundMethods({ kit, context, reachable, report });
253
- return { violations, coverage: { status: 'checked', workers: workerRoots.length, jobsAndConsumers: background.length, composed: running.length, backgroundMethods: methods } };
254
- }
@@ -1,291 +0,0 @@
1
- import { nameText } from '../../lib/ts-ast.mjs';
2
- import { importsOf, unwrapExpression } from '../../lib/ts-analysis.mjs';
3
- import { nodeDecorators } from './ast-walks.mjs';
4
- import { canonJsxCloneNodes } from './canon-jsx-clones.mjs';
5
- const READ_METHODS = new Set(['find', 'findBy', 'findAndCount', 'findOne', 'findOneBy', 'findOneOrFail', 'findOneByOrFail']);
6
- function unwrap(ts, node) {
7
- return node === null ? null : unwrapExpression(ts, node, { awaitExpressions: true, typeAssertions: false });
8
- }
9
- const identifierText = (ts, name) => (name && ts.isIdentifier(name) ? name.text : undefined);
10
- const isIdentifierParameter = (ts, parameter) => ts.isIdentifier(parameter.name);
11
- const imported = (env, node, name, source) => {
12
- if (!node || !env.ts.isIdentifier(node))
13
- return false;
14
- const binding = env.imports.get(node.text);
15
- return binding?.name === name && binding.source === source;
16
- };
17
- const decoratorsHave = (env, node, name, source) => nodeDecorators(env.ts, node)
18
- .some(decorator => imported(env, env.ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression, name, source));
19
- const portType = (env, node, name, source) => Boolean(node && env.ts.isTypeReferenceNode(node) && imported(env, node.typeName, name, source));
20
- /** Data moves between shapes here; operators, arbitrary calls and executable callbacks remain compared. */
21
- function mapping(env, input, allowCall = () => false) {
22
- const { ts } = env;
23
- const node = unwrap(ts, input);
24
- if (!node)
25
- return false;
26
- if (ts.isIdentifier(node) || ts.isLiteralExpression(node) || [ts.SyntaxKind.NullKeyword, ts.SyntaxKind.TrueKeyword, ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.ThisKeyword].includes(node.kind))
27
- return true;
28
- if (ts.isPropertyAccessExpression(node))
29
- return mapping(env, node.expression, allowCall);
30
- if (ts.isObjectLiteralExpression(node))
31
- return node.properties.every(property => {
32
- if (ts.isShorthandPropertyAssignment(property))
33
- return !property.objectAssignmentInitializer;
34
- if (ts.isSpreadAssignment(property))
35
- return mapping(env, property.expression, allowCall);
36
- return ts.isPropertyAssignment(property) && nameText(ts, property.name) !== null && mapping(env, property.initializer, allowCall);
37
- });
38
- if (ts.isArrayLiteralExpression(node))
39
- return node.elements.every(item => mapping(env, ts.isSpreadElement(item) ? item.expression : item, allowCall));
40
- return ts.isCallExpression(node) && allowCall(node);
41
- }
42
- const parameterNames = (ts, method) => new Set(method.parameters.filter((parameter) => isIdentifierParameter(ts, parameter)).map(parameter => parameter.name.text));
43
- const propertyParameters = (ts, constructor) => {
44
- const parameters = constructor?.parameters ?? [];
45
- return new Set(parameters.filter((parameter) => !!parameter.modifiers?.length && isIdentifierParameter(ts, parameter)).map(parameter => parameter.name.text));
46
- };
47
- const inertParameters = (ts, constructor) => constructor.parameters.every(parameter => ts.isIdentifier(parameter.name) && !parameter.initializer);
48
- function receiverMember(ts, call) {
49
- if (!ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression))
50
- return null;
51
- const receiver = call.expression.expression;
52
- return ts.isPropertyAccessExpression(receiver) && receiver.expression.kind === ts.SyntaxKind.ThisKeyword ? receiver.name.text : null;
53
- }
54
- const statementCall = (ts, statement) => ((statement && (ts.isReturnStatement(statement) || ts.isExpressionStatement(statement))) ? unwrap(ts, statement.expression) : null);
55
- function delegation(env, _method, statement, members, allowCall = () => false) {
56
- const call = statementCall(env.ts, statement);
57
- return Boolean(call && env.ts.isCallExpression(call) && members.has(receiverMember(env.ts, call))
58
- && call.arguments.every(argument => mapping(env, argument, allowCall)));
59
- }
60
- function canonicalConstructor(env, constructor, logger = false) {
61
- if (!constructor?.body || !inertParameters(env.ts, constructor))
62
- return false;
63
- if (!logger)
64
- return constructor.body.statements.length === 0;
65
- const parameters = constructor.parameters.filter(parameter => decoratorsHave(env, parameter, 'InjectLogger', '@modules/platform/logging'));
66
- if (parameters.length !== 1 || constructor.body.statements.length !== 1)
67
- return false;
68
- const call = statementCall(env.ts, constructor.body.statements[0]);
69
- if (!call || !env.ts.isCallExpression(call))
70
- return false;
71
- const [argument] = call.arguments;
72
- return call.expression.kind === env.ts.SyntaxKind.SuperKeyword
73
- && call.arguments.length === 1 && argument !== undefined && env.ts.isIdentifier(argument) && argument.text === identifierText(env.ts, parameters[0]?.name);
74
- }
75
- function hasHeritage(env, declaration, name, source, kind) {
76
- return declaration.heritageClauses?.some(clause => clause.token === kind && clause.types.some(type => imported(env, type.expression, name, source))) ?? false;
77
- }
78
- function fenceStatement(env, statement, claims, job) {
79
- const call = statementCall(env.ts, statement);
80
- if (!call || !env.ts.isCallExpression(call) || receiverMember(env.ts, call) !== claims || !env.ts.isPropertyAccessExpression(call.expression)
81
- || call.expression.name.text !== 'advance' || call.arguments.length !== 1)
82
- return false;
83
- const options = unwrap(env.ts, call.arguments[0]);
84
- if (!options || !env.ts.isObjectLiteralExpression(options) || options.properties.length !== 3)
85
- return false;
86
- const properties = new Map(options.properties.filter(env.ts.isPropertyAssignment).map((property) => [nameText(env.ts, property.name), property.initializer]));
87
- const jobField = (node, field) => node !== undefined && env.ts.isPropertyAccessExpression(node) && env.ts.isIdentifier(node.expression) && node.expression.text === job && node.name.text === field;
88
- return jobField(properties.get('jobId'), 'jobId') && jobField(properties.get('expectedFencingToken'), 'fencingToken')
89
- && mapping(env, properties.get('step'));
90
- }
91
- function jobMethods(env, declaration, constructor, excluded) {
92
- const claims = constructor?.parameters.find(parameter => portType(env, parameter.type, 'JobClaims', '@modules/platform/jobs')
93
- && decoratorsHave(env, parameter, 'InjectJobClaims', '@modules/platform/jobs'));
94
- if (!claims || !env.ts.isIdentifier(claims.name))
95
- return;
96
- const claimsName = claims.name.text;
97
- const members = propertyParameters(env.ts, constructor);
98
- const runKey = (call) => receiverMember(env.ts, call) === claimsName && env.ts.isPropertyAccessExpression(call.expression) && call.expression.name.text === 'runKey'
99
- && call.arguments.length === 2 && call.arguments.every(argument => mapping(env, argument));
100
- for (const method of declaration.members) {
101
- if (!env.ts.isMethodDeclaration(method) || nameText(env.ts, method.name) !== 'run' || !method.body || method.parameters.length !== 1
102
- || !portType(env, method.parameters[0]?.type, 'ClaimedJob', '@modules/platform/jobs'))
103
- continue;
104
- const statements = method.body.statements;
105
- const job = [...parameterNames(env.ts, method)][0];
106
- const first = statements[0];
107
- if (!first || !fenceStatement(env, first, claimsName, job))
108
- continue;
109
- if (statements.length === 1 || (statements.length === 2 && delegation(env, method, statements[1], members, runKey)))
110
- excluded.add(method);
111
- else
112
- excluded.add(first);
113
- }
114
- if (canonicalConstructor(env, constructor))
115
- excluded.add(constructor);
116
- }
117
- function queueMethods(env, declaration, constructor, excluded) {
118
- const outbox = constructor?.parameters.find(parameter => portType(env, parameter.type, 'QueueOutbox', '@modules/platform/queue')
119
- && decoratorsHave(env, parameter, 'InjectQueueOutbox', '@modules/platform/queue'));
120
- if (!outbox || !env.ts.isIdentifier(outbox.name))
121
- return;
122
- for (const method of declaration.members) {
123
- if (!env.ts.isMethodDeclaration(method) || method.body?.statements.length !== 1)
124
- continue;
125
- const call = statementCall(env.ts, method.body.statements[0]);
126
- if (call && env.ts.isCallExpression(call) && receiverMember(env.ts, call) === outbox.name.text && env.ts.isPropertyAccessExpression(call.expression) && call.expression.name.text === 'write'
127
- && call.arguments.length === 3 && call.arguments.every(argument => mapping(env, argument)))
128
- excluded.add(method);
129
- }
130
- if (canonicalConstructor(env, constructor))
131
- excluded.add(constructor);
132
- }
133
- function rowMapper(env, call, row) {
134
- const { ts } = env;
135
- if (!call)
136
- return false;
137
- if (ts.isObjectLiteralExpression(unwrap(ts, call)))
138
- return mapping(env, call, iso => ts.isPropertyAccessExpression(iso.expression)
139
- && iso.expression.name.text === 'toISOString' && iso.arguments.length === 0 && mapping(env, iso.expression.expression));
140
- if (!ts.isCallExpression(call) || call.arguments.length !== 1)
141
- return false;
142
- const [argument] = call.arguments;
143
- if (!argument || !ts.isIdentifier(argument) || argument.text !== row || !ts.isIdentifier(call.expression))
144
- return false;
145
- const binding = env.imports.get(call.expression.text);
146
- return Boolean(binding && /\.rows(?:\.[cm]?ts)?$/.test(binding.source ?? '')) || env.mappers.has(call.expression.text);
147
- }
148
- function readResult(env, expression, row) {
149
- const { ts } = env;
150
- const node = unwrap(ts, expression);
151
- if (!node)
152
- return false;
153
- if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'map'
154
- && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === row && node.arguments.length === 1) {
155
- const mapper = unwrap(ts, node.arguments[0]);
156
- if (!mapper)
157
- return false;
158
- if (ts.isIdentifier(mapper))
159
- return env.mappers.has(mapper.text) || /\.rows(?:\.[cm]?ts)?$/.test(env.imports.get(mapper.text)?.source ?? '');
160
- return ts.isArrowFunction(mapper) && mapper.parameters.length === 1 && !ts.isBlock(mapper.body) && rowMapper(env, unwrap(ts, mapper.body), identifierText(ts, mapper.parameters[0]?.name));
161
- }
162
- if (!ts.isConditionalExpression(node))
163
- return rowMapper(env, node, row);
164
- const condition = unwrap(ts, node.condition);
165
- const simpleNull = ts.isBinaryExpression(condition) && [ts.SyntaxKind.EqualsEqualsEqualsToken, ts.SyntaxKind.ExclamationEqualsEqualsToken].includes(condition.operatorToken.kind)
166
- && ts.isIdentifier(condition.left) && condition.left.text === row && condition.right.kind === ts.SyntaxKind.NullKeyword;
167
- if (!(simpleNull || (ts.isIdentifier(condition) && condition.text === row)))
168
- return false;
169
- return [node.whenTrue, node.whenFalse].every(branch => unwrap(ts, branch).kind === ts.SyntaxKind.NullKeyword || rowMapper(env, unwrap(ts, branch), row));
170
- }
171
- function readCall(env, call, managers) {
172
- const { ts } = env;
173
- if (!call || !ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression) || !managers.has(receiverMember(ts, call)))
174
- return false;
175
- if (READ_METHODS.has(call.expression.name.text))
176
- return call.arguments.length <= 2 && call.arguments.every(argument => mapping(env, argument));
177
- const sql = call.arguments[0];
178
- return Boolean(call.expression.name.text === 'query' && sql && ts.isIdentifier(sql) && /^(?:FIND|LIST)_/.test(sql.text)
179
- && /\.sql(?:\.[cm]?ts)?$/.test(env.imports.get(sql.text)?.source ?? '') && call.arguments.every(argument => mapping(env, argument)));
180
- }
181
- function ownedRead(env, statement) {
182
- const { ts } = env;
183
- const call = statementCall(ts, statement);
184
- if (!call || !ts.isCallExpression(call) || !imported(env, call.expression, 'requireOwnedEntity', '@modules/platform/database') || call.arguments.length !== 5)
185
- return false;
186
- if (!call.arguments.slice(0, 4).every(argument => mapping(env, argument)))
187
- return false;
188
- const callback = unwrap(ts, call.arguments[4]);
189
- if (!callback || !ts.isArrowFunction(callback) || callback.parameters.length || !ts.isBlock(callback.body) || callback.body.statements.length !== 1)
190
- return false;
191
- const thrown = callback.body.statements[0];
192
- if (!thrown || !ts.isThrowStatement(thrown) || !thrown.expression || !ts.isNewExpression(thrown.expression) || thrown.expression.arguments?.length !== 1)
193
- return false;
194
- const options = thrown.expression.arguments[0];
195
- if (!options || !ts.isObjectLiteralExpression(options) || !mapping(env, options))
196
- return false;
197
- const code = options.properties.find((property) => ts.isPropertyAssignment(property) && nameText(ts, property.name) === 'code');
198
- return Boolean(code && ts.isPropertyAccessExpression(code.initializer) && code.initializer.name.text === 'NotFound');
199
- }
200
- function readMethods(env, declaration, constructor, excluded) {
201
- const managers = new Set((constructor?.parameters ?? []).filter(parameter => portType(env, parameter.type, 'EntityManager', 'typeorm')
202
- && nodeDecorators(env.ts, parameter).some(decorator => {
203
- const call = env.ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
204
- const binding = env.ts.isIdentifier(call) ? env.imports.get(call.text) : null;
205
- return binding?.source === '@modules/platform/database' && /^Inject.*EntityManager$/.test(binding.name);
206
- })).map(parameter => identifierText(env.ts, parameter.name)));
207
- for (const method of declaration.members) {
208
- if (!env.ts.isMethodDeclaration(method) || !method.body)
209
- continue;
210
- const statements = method.body.statements;
211
- const [first, second] = statements;
212
- if (statements.length === 1 && first && ownedRead(env, first)) {
213
- excluded.add(method);
214
- continue;
215
- }
216
- if (statements.length !== 2 || !first || !second || !env.ts.isVariableStatement(first) || !env.ts.isReturnStatement(second))
217
- continue;
218
- const variables = first.declarationList.declarations;
219
- const [variable] = variables;
220
- if (variables.length !== 1 || !variable || !env.ts.isIdentifier(variable.name) || !variable.initializer)
221
- continue;
222
- const call = unwrap(env.ts, variable.initializer);
223
- if (readCall(env, call, managers) && readResult(env, second.expression, variable.name.text))
224
- excluded.add(method);
225
- }
226
- if (managers.size && canonicalConstructor(env, constructor))
227
- excluded.add(constructor);
228
- }
229
- function queueDefinition(env, node) {
230
- const { ts } = env;
231
- if (!env.topLevelVariables.has(node) || !ts.isVariableDeclaration(node) || !node.initializer)
232
- return false;
233
- const value = unwrap(ts, node.initializer);
234
- if (ts.isCallExpression(value) && imported(env, value.expression, 'defineQueue', '@modules/platform/queue'))
235
- return value.arguments.length === 1 && value.arguments[0] !== undefined && ts.isObjectLiteralExpression(unwrap(ts, value.arguments[0])) && mapping(env, value.arguments[0]);
236
- if (!ts.isArrowFunction(value) || !portType(env, value.type, 'QueueSchedulerDefinition', '@modules/platform/queue'))
237
- return false;
238
- const returned = ts.isBlock(value.body) && value.body.statements.length === 1 ? value.body.statements[0] : undefined;
239
- const body = returned && ts.isReturnStatement(returned) ? returned.expression : value.body;
240
- const object = unwrap(ts, body);
241
- return Boolean(object && ts.isObjectLiteralExpression(object) && mapping(env, object));
242
- }
243
- function canonEnvironment(ts, sourceFile) {
244
- const env = { ts, imports: importsOf(ts, sourceFile, { moduleKey: 'source', stringLiteralsOnly: true }), mappers: new Set(), topLevelVariables: new Set(), topLevelClasses: new Set(sourceFile.statements.filter(ts.isClassDeclaration)) };
245
- for (const statement of sourceFile.statements) {
246
- if (!ts.isVariableStatement(statement))
247
- continue;
248
- for (const variable of statement.declarationList.declarations) {
249
- env.topLevelVariables.add(variable);
250
- const value = variable.initializer && unwrap(ts, variable.initializer);
251
- if (ts.isIdentifier(variable.name) && value && ts.isArrowFunction(value) && ts.isObjectLiteralExpression(unwrap(ts, value.body)) && mapping(env, value.body))
252
- env.mappers.add(variable.name.text);
253
- }
254
- }
255
- return env;
256
- }
257
- function classScaffolding(env, node, excluded) {
258
- const { ts } = env;
259
- const constructor = node.members.find(ts.isConstructorDeclaration);
260
- if (hasHeritage(env, node, 'ICQRSHandler', '@modules/platform/cqrs', ts.SyntaxKind.ExtendsKeyword)
261
- && ['CommandHandler', 'QueryHandler'].some(name => decoratorsHave(env, node, name, '@nestjs/cqrs'))) {
262
- if (canonicalConstructor(env, constructor, true))
263
- excluded.add(constructor);
264
- const members = propertyParameters(ts, constructor);
265
- for (const method of node.members)
266
- if (ts.isMethodDeclaration(method) && nameText(ts, method.name) === 'process'
267
- && method.body?.statements.length === 1 && delegation(env, method, method.body.statements[0], members))
268
- excluded.add(method);
269
- }
270
- if (hasHeritage(env, node, 'JobStep', '@modules/platform/jobs', ts.SyntaxKind.ImplementsKeyword))
271
- jobMethods(env, node, constructor, excluded);
272
- queueMethods(env, node, constructor, excluded);
273
- readMethods(env, node, constructor, excluded);
274
- }
275
- export function canonCloneNodes(ts, sourceFile, profile) {
276
- if (profile === 'fe')
277
- return canonJsxCloneNodes(ts, sourceFile);
278
- const excluded = new Set();
279
- const env = canonEnvironment(ts, sourceFile);
280
- const visit = (node) => {
281
- if (queueDefinition(env, node)) {
282
- excluded.add(node);
283
- return;
284
- }
285
- if (env.topLevelClasses.has(node) && ts.isClassDeclaration(node))
286
- classScaffolding(env, node, excluded);
287
- ts.forEachChild(node, visit);
288
- };
289
- visit(sourceFile);
290
- return excluded;
291
- }