@starci/eslint-canon-be 3.8.20 → 3.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/CHANGELOG.md +16 -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
@@ -1,5 +1,4 @@
1
1
  import path from 'node:path';
2
-
3
2
  /**
4
3
  * The constructor dependencies of a service, read the way the injection law (R85) writes them, shared by the unit-spec
5
4
  * provider check (R48 `unit-spec-providers`) and the token export check (R85 `injection-token-exported`).
@@ -13,94 +12,104 @@ import path from 'node:path';
13
12
  * a type that is not a class).
14
13
  */
15
14
  const INJECT_NAME = /^Inject(?:[A-Z][A-Za-z0-9]*)?$/u;
16
-
17
15
  /** The identifier a decorator names: `@InjectFoo()` -> InjectFoo, `@Inject(TOKEN)` -> Inject; null for a non-identifier decorator. */
18
16
  function decoratorCallee(ts, decorator) {
19
- const expression = decorator.expression;
20
- const callee = ts.isCallExpression(expression) ? expression.expression : expression;
21
- return ts.isIdentifier(callee) ? { callee, call: ts.isCallExpression(expression) ? expression : null } : null;
17
+ const expression = decorator.expression;
18
+ const callee = ts.isCallExpression(expression) ? expression.expression : expression;
19
+ return ts.isIdentifier(callee) ? { callee, call: ts.isCallExpression(expression) ? expression : null } : null;
22
20
  }
23
-
24
21
  /** The token a `injector(<arg>)` call names, judged where the argument is declared. */
25
22
  function tokenOfArgument(kit, checker, argument) {
26
- const { ts } = kit;
27
- if (!argument || !ts.isIdentifier(argument)) return { kind: 'unresolved', reason: 'its token is not a plain identifier of an exported const', node: argument ?? null };
28
- const declaration = kit.declarationsOf(checker, argument).find(item => ts.isVariableDeclaration(item) || ts.isBindingElement(item));
29
- if (declaration) {
30
- const name = ts.isIdentifier(declaration.name) ? declaration.name.text : argument.text;
31
- return { kind: 'token', name, declaration, argument, external: false };
32
- }
33
- const binding = kit.importBinding(checker, argument);
34
- if (binding && !binding.module.startsWith('.')) return { kind: 'token', name: binding.name, declaration: null, argument, external: true };
35
- return { kind: 'unresolved', reason: `the token ${argument.text} does not resolve to a declaration`, node: argument };
23
+ const { ts } = kit;
24
+ if (!argument || !ts.isIdentifier(argument))
25
+ return { kind: 'unresolved', reason: 'its token is not a plain identifier of an exported const', node: argument ?? null };
26
+ const declaration = kit.declarationsOf(checker, argument).find((item) => ts.isVariableDeclaration(item) || ts.isBindingElement(item));
27
+ if (declaration) {
28
+ const name = ts.isIdentifier(declaration.name) ? declaration.name.text : argument.text;
29
+ return { kind: 'token', name, declaration, argument, external: false };
30
+ }
31
+ const binding = kit.importBinding(checker, argument);
32
+ if (binding && !binding.module.startsWith('.'))
33
+ return { kind: 'token', name: binding.name, declaration: null, argument, external: true };
34
+ return { kind: 'unresolved', reason: `the token ${argument.text} does not resolve to a declaration`, node: argument };
36
35
  }
37
-
38
36
  /** The first `injector(...)` call under a decorator declaration, or null. */
39
37
  function injectorCallOf(kit, declaration) {
40
- const { ts } = kit;
41
- let injectorCall = null;
42
- kit.walk(declaration, node => {
43
- if (injectorCall) return false;
44
- if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
45
- const binding = kit.importBinding(kit.checkerOf(declaration.getSourceFile()), node.expression);
46
- if ((binding?.name ?? node.expression.text) === 'injector') injectorCall = node;
47
- }
48
- return true;
49
- });
50
- return injectorCall;
38
+ const { ts } = kit;
39
+ let injectorCall = null;
40
+ kit.walk(declaration, node => {
41
+ if (injectorCall)
42
+ return false;
43
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
44
+ const binding = kit.importBinding(kit.checkerOf(declaration.getSourceFile()), node.expression);
45
+ if ((binding?.name ?? node.expression.text) === 'injector')
46
+ injectorCall = node;
47
+ }
48
+ return true;
49
+ });
50
+ return injectorCall;
51
51
  }
52
-
53
52
  /** The injector token of a custom `Inject<Thing>()` decorator, or an unresolved reason. */
54
53
  function tokenOfInjectDecorator(kit, checker, parameter, decorator) {
55
- const { ts } = kit;
56
- const head = decoratorCallee(ts, decorator);
57
- if (!head || !INJECT_NAME.test(head.callee.text)) return null;
58
- if (head.callee.text === 'Inject' && head.call?.arguments.length) return { ...tokenOfArgument(kit, checker, head.call.arguments[0]), decorator: head.callee.text, decoratorDeclaration: null, decoratorFile: null };
59
- const declaration = kit.declarationsOf(checker, head.callee).find(item => ts.isVariableDeclaration(item) || ts.isFunctionDeclaration(item));
60
- if (!declaration) return { kind: 'unresolved', reason: `${head.callee.text}() does not resolve to a declaration`, node: parameter, decorator: head.callee.text };
61
- const injectorCall = injectorCallOf(kit, declaration);
62
- if (!injectorCall) return { kind: 'unresolved', reason: `${head.callee.text}() is not built with injector<T>(TOKEN)`, node: parameter, decorator: head.callee.text };
63
- const declarationChecker = kit.checkerOf(declaration.getSourceFile());
64
- return { ...tokenOfArgument(kit, declarationChecker, injectorCall.arguments[0]), decorator: head.callee.text, decoratorDeclaration: declaration, injectorCall };
54
+ const { ts } = kit;
55
+ const head = decoratorCallee(ts, decorator);
56
+ if (!head || !INJECT_NAME.test(head.callee.text))
57
+ return null;
58
+ if (head.callee.text === 'Inject' && head.call?.arguments.length)
59
+ return { ...tokenOfArgument(kit, checker, head.call.arguments[0]), decorator: head.callee.text, decoratorDeclaration: null, decoratorFile: null };
60
+ const declaration = kit.declarationsOf(checker, head.callee).find((item) => ts.isVariableDeclaration(item) || ts.isFunctionDeclaration(item));
61
+ if (!declaration)
62
+ return { kind: 'unresolved', reason: `${head.callee.text}() does not resolve to a declaration`, node: parameter, decorator: head.callee.text };
63
+ const injectorCall = injectorCallOf(kit, declaration);
64
+ if (!injectorCall)
65
+ return { kind: 'unresolved', reason: `${head.callee.text}() is not built with injector<T>(TOKEN)`, node: parameter, decorator: head.callee.text };
66
+ const declarationChecker = kit.checkerOf(declaration.getSourceFile());
67
+ return { ...tokenOfArgument(kit, declarationChecker, injectorCall.arguments[0]), decorator: head.callee.text, decoratorDeclaration: declaration, injectorCall };
65
68
  }
66
-
67
69
  /** The class a parameter type names, judged where the type is declared; null when it is not a class type. */
68
70
  function classOfType(kit, checker, type) {
69
- const { ts } = kit;
70
- if (!type || !ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName)) return null;
71
- const declarations = kit.declarationsOf(checker, type.typeName);
72
- const declared = declarations.find(item => ts.isClassDeclaration(item) && item.name);
73
- if (declared) return declared.name.text;
74
- if (declarations.length) return null;
75
- const binding = kit.importBinding(checker, type.typeName);
76
- return binding && binding.name !== 'default' && binding.name !== '*' ? binding.name : null;
71
+ const { ts } = kit;
72
+ if (!type || !ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName))
73
+ return null;
74
+ const declarations = kit.declarationsOf(checker, type.typeName);
75
+ const declared = declarations.find((item) => ts.isClassDeclaration(item) && Boolean(item.name));
76
+ if (declared)
77
+ return declared.name.text;
78
+ if (declarations.length)
79
+ return null;
80
+ const binding = kit.importBinding(checker, type.typeName);
81
+ return binding && binding.name !== 'default' && binding.name !== '*' ? binding.name : null;
77
82
  }
78
-
79
83
  /** The dependencies of one class, in constructor order: [{kind, name?, node, parameter, ...}]. */
80
84
  export function constructorDependencies(kit, file, classDeclaration) {
81
- const { ts } = kit;
82
- const checker = kit.checkerOf(file.sourceFile);
83
- const constructor = classDeclaration.members.find(member => ts.isConstructorDeclaration(member) && member.body !== undefined)
84
- ?? classDeclaration.members.find(member => ts.isConstructorDeclaration(member));
85
- if (!constructor) return [];
86
- return constructor.parameters.map(parameter => {
87
- for (const decorator of kit.decorators(parameter)) {
88
- const token = tokenOfInjectDecorator(kit, checker, parameter, decorator);
89
- if (token) return { ...token, parameter, node: token.node ?? parameter };
90
- }
91
- const name = classOfType(kit, checker, parameter.type);
92
- if (name) return { kind: 'class', name, parameter, node: parameter };
93
- return { kind: 'unresolved', reason: 'it has no Inject<Thing>() decorator and its type is not a class', parameter, node: parameter };
94
- });
85
+ const { ts } = kit;
86
+ const checker = kit.checkerOf(file.sourceFile);
87
+ const constructor = classDeclaration.members.find((member) => ts.isConstructorDeclaration(member) && member.body !== undefined)
88
+ ?? classDeclaration.members.find((member) => ts.isConstructorDeclaration(member));
89
+ if (!constructor)
90
+ return [];
91
+ return constructor.parameters.map((parameter) => {
92
+ for (const decorator of kit.decorators(parameter)) {
93
+ const token = tokenOfInjectDecorator(kit, checker, parameter, decorator);
94
+ if (token)
95
+ return { ...token, parameter, node: token.node ?? parameter };
96
+ }
97
+ const name = classOfType(kit, checker, parameter.type);
98
+ if (name)
99
+ return { kind: 'class', name, parameter, node: parameter };
100
+ return { kind: 'unresolved', reason: 'it has no Inject<Thing>() decorator and its type is not a class', parameter, node: parameter };
101
+ });
95
102
  }
96
-
97
103
  /** Every service class of the program: [{file, declaration}] for each class of a `*.service.ts` file. */
98
104
  export function serviceClasses(kit, graph) {
99
- const { ts } = kit;
100
- const found = [];
101
- for (const file of graph.files.values()) {
102
- if (!path.posix.basename(file.rel).endsWith('.service.ts')) continue;
103
- for (const statement of file.sourceFile.statements) if (ts.isClassDeclaration(statement) && statement.name) found.push({ file, declaration: statement });
104
- }
105
- return found;
105
+ const { ts } = kit;
106
+ const found = [];
107
+ for (const file of graph.files.values()) {
108
+ if (!path.posix.basename(file.rel).endsWith('.service.ts'))
109
+ continue;
110
+ for (const statement of file.sourceFile.statements)
111
+ if (ts.isClassDeclaration(statement) && statement.name)
112
+ found.push({ file, declaration: statement });
113
+ }
114
+ return found;
106
115
  }
@@ -2,7 +2,6 @@ import path from 'node:path';
2
2
  import { contextModelOf } from './context-map.mjs';
3
3
  import { machineKit } from './machine-ast.mjs';
4
4
  import { entitiesOf } from './sql-owner.mjs';
5
-
6
5
  /**
7
6
  * R175 `context-coupling` (BE_CONTEXT_COUPLING). Bounded contexts are coupled by events only, never by their schema or their code:
8
7
  * - an entity relation (`OneToOne`, `OneToMany`, `ManyToOne`, `ManyToMany` imported from typeorm) whose target class is an entity of a capability
@@ -14,99 +13,105 @@ import { entitiesOf } from './sql-owner.mjs';
14
13
  * program declares, never guessed from a name.
15
14
  */
16
15
  export const CONTEXT_COUPLING_RULE_IDS = ['BE_CONTEXT_COUPLING'];
17
-
18
16
  const RULE = 'BE_CONTEXT_COUPLING';
19
17
  const TYPEORM = 'typeorm';
20
18
  const RELATIONS = new Set(['OneToOne', 'OneToMany', 'ManyToOne', 'ManyToMany']);
21
19
  // The table identifier after the SQL keyword REFERENCES, optionally schema-qualified and quoted.
22
20
  const REFERENCES = /\bREFERENCES\s+(?:"?\w+"?\s*\.\s*)?"?(\w+)"?/giu;
23
-
24
21
  const checkCrossContextImports = (graph, model, covered, counts, once) => {
25
- for (const edge of graph.edges) {
26
- const from = graph.files.get(edge.from);
27
- const to = graph.files.get(edge.to);
28
- if (!from?.owner || !to?.owner || from.owner.root === to.owner.root) continue;
29
- const fromContext = model.contextOfFile(edge.from);
30
- const toContext = model.contextOfFile(edge.to);
31
- if (!fromContext || !toContext || fromContext === toContext) continue;
32
- counts.imports += 1;
33
- if (covered.has(`${edge.from}|${to.owner.root}`)) continue;
34
- once(`${edge.from}|${edge.to}|${edge.line}`, { path: edge.from, line: edge.line, column: edge.column, message: `${from.owner.root} (context ${fromContext}) imports ${to.owner.root} (context ${toContext}); a context never imports another context's entities, services or SQL. Keep a local copy fed by its events, or read a projection.`, context: fromContext, targetContext: toContext });
35
- }
22
+ for (const edge of graph.edges) {
23
+ const from = graph.files.get(edge.from);
24
+ const to = graph.files.get(edge.to);
25
+ if (!from?.owner || !to?.owner || from.owner.root === to.owner.root)
26
+ continue;
27
+ const fromContext = model.contextOfFile(edge.from);
28
+ const toContext = model.contextOfFile(edge.to);
29
+ if (!fromContext || !toContext || fromContext === toContext)
30
+ continue;
31
+ counts.imports += 1;
32
+ if (covered.has(`${edge.from}|${to.owner.root}`))
33
+ continue;
34
+ once(`${edge.from}|${edge.to}|${edge.line}`, { path: edge.from, line: edge.line, column: edge.column, message: `${from.owner.root} (context ${fromContext}) imports ${to.owner.root} (context ${toContext}); a context never imports another context's entities, services or SQL. Keep a local copy fed by its events, or read a projection.`, context: fromContext, targetContext: toContext });
35
+ }
36
36
  };
37
-
38
37
  const relationTarget = (kit, model, argument) => {
39
- const { ts } = kit;
40
- const isFunction = argument && (ts.isArrowFunction(argument) || ts.isFunctionExpression(argument));
41
- const target = isFunction && !ts.isBlock(argument.body) ? model.unparen(argument.body) : null;
42
- return target && (ts.isIdentifier(target) || ts.isPropertyAccessExpression(target)) ? target : null;
38
+ const { ts } = kit;
39
+ const isFunction = argument && (ts.isArrowFunction(argument) || ts.isFunctionExpression(argument));
40
+ const target = isFunction && !ts.isBlock(argument.body) ? model.unparen(argument.body) : null;
41
+ return target && (ts.isIdentifier(target) || ts.isPropertyAccessExpression(target)) ? target : null;
43
42
  };
44
-
45
43
  // The relation decorator (`@ManyToOne(() => Target)` imported from typeorm) a node is, with the checker that resolves it, else null.
46
44
  const relationBinding = (kit, checker, node) => {
47
- const { ts } = kit;
48
- if (!ts.isDecorator(node) || !ts.isCallExpression(node.expression)) return null;
49
- const binding = kit.importBinding(checker, node.expression.expression);
50
- return binding?.module === TYPEORM && RELATIONS.has(binding.name) ? binding : null;
45
+ const { ts } = kit;
46
+ if (!ts.isDecorator(node) || !ts.isCallExpression(node.expression))
47
+ return null;
48
+ const binding = kit.importBinding(checker, node.expression.expression);
49
+ return binding?.module === TYPEORM && RELATIONS.has(binding.name) ? binding : null;
51
50
  };
52
-
53
51
  const checkRelations = (scan, file, ownContext) => {
54
- const { kit, model, counts, covered, once } = scan;
55
- const { ts } = kit;
56
- const checker = kit.checkerOf(file.sourceFile);
57
- kit.walk(file.sourceFile, node => {
58
- const binding = relationBinding(kit, checker, node);
59
- if (!binding) return;
60
- counts.relations += 1;
61
- const target = relationTarget(kit, model, node.expression.arguments[0]);
62
- if (!target) return;
63
- const home = kit.declarationsOf(checker, ts.isPropertyAccessExpression(target) ? target.name : target).map(kit.ownerOfDeclaration).find(Boolean);
64
- const targetContext = home ? model.contextOfCapability(home.root) : null;
65
- if (!home || !targetContext || targetContext === ownContext) return;
66
- covered.add(`${file.rel}|${home.root}`);
67
- once(`${file.rel}|${node.getStart()}`, { ...kit.at(file.rel, file.sourceFile, node), message: `A ${binding.name} relation in ${file.owner.root} (context ${ownContext}) targets an entity of ${home.root} (context ${targetContext}); a relation never crosses a context. Keep the other context's id as a plain column and fill a local copy or a projection from its events.`, context: ownContext, targetContext });
68
- });
52
+ const { kit, model, counts, covered, once } = scan;
53
+ const { ts } = kit;
54
+ const checker = kit.checkerOf(file.sourceFile);
55
+ kit.walk(file.sourceFile, (node) => {
56
+ const binding = relationBinding(kit, checker, node);
57
+ if (!binding)
58
+ return;
59
+ counts.relations += 1;
60
+ const target = relationTarget(kit, model, node.expression.arguments[0]);
61
+ if (!target)
62
+ return;
63
+ const home = kit.declarationsOf(checker, ts.isPropertyAccessExpression(target) ? target.name : target).map(kit.ownerOfDeclaration).find(Boolean);
64
+ const targetContext = home ? model.contextOfCapability(home.root) : null;
65
+ if (!home || !targetContext || targetContext === ownContext)
66
+ return;
67
+ covered.add(`${file.rel}|${home.root}`);
68
+ once(`${file.rel}|${node.getStart()}`, { ...kit.at(file.rel, file.sourceFile, node), message: `A ${binding.name} relation in ${file.owner?.root} (context ${ownContext}) targets an entity of ${home.root} (context ${targetContext}); a relation never crosses a context. Keep the other context's id as a plain column and fill a local copy or a projection from its events.`, context: ownContext, targetContext });
69
+ });
69
70
  };
70
-
71
- const stringText = (ts, node) => (ts.isTemplateExpression(node) ? node.head.text + node.templateSpans.map(span => ` ${span.literal.text}`).join('') : node.text);
72
-
71
+ const stringText = (ts, node) => (ts.isTemplateExpression(node) ? node.head.text + node.templateSpans.map((span) => ` ${span.literal.text}`).join('') : node.text);
73
72
  const checkForeignKeys = (scan, file, ownContext) => {
74
- const { kit, model, tables, counts, once } = scan;
75
- const { ts } = kit;
76
- kit.walk(file.sourceFile, node => {
77
- if (!(ts.isStringLiteralLike(node) || ts.isTemplateExpression(node))) return;
78
- for (const match of stringText(ts, node).matchAll(REFERENCES)) {
79
- counts.foreignKeys += 1;
80
- const entity = tables.get(match[1].toLowerCase());
81
- const targetContext = entity ? model.contextOfCapability(entity.owner) : null;
82
- if (!entity || !targetContext || targetContext === ownContext) continue;
83
- once(`${file.rel}|${node.getStart()}|${match[1]}`, { ...kit.at(file.rel, file.sourceFile, node), message: `A foreign key in ${file.rel} references table ${entity.table} of ${entity.owner} (context ${targetContext}), but this migration belongs to context ${ownContext}; a foreign key never crosses a context. Keep the id as a plain column and validate it through the other context's API or a local copy fed by its events.`, context: ownContext, targetContext, table: entity.table });
84
- }
85
- });
73
+ const { kit, model, tables, counts, once } = scan;
74
+ const { ts } = kit;
75
+ kit.walk(file.sourceFile, (node) => {
76
+ if (!(ts.isStringLiteralLike(node) || ts.isTemplateExpression(node)))
77
+ return;
78
+ for (const match of stringText(ts, node).matchAll(REFERENCES)) {
79
+ counts.foreignKeys += 1;
80
+ const entity = tables.get(match[1].toLowerCase());
81
+ const targetContext = entity?.owner ? model.contextOfCapability(entity.owner) : null;
82
+ if (!entity || !targetContext || targetContext === ownContext)
83
+ continue;
84
+ once(`${file.rel}|${node.getStart()}|${match[1]}`, { ...kit.at(file.rel, file.sourceFile, node), message: `A foreign key in ${file.rel} references table ${entity.table} of ${entity.owner} (context ${targetContext}), but this migration belongs to context ${ownContext}; a foreign key never crosses a context. Keep the id as a plain column and validate it through the other context's API or a local copy fed by its events.`, context: ownContext, targetContext, table: entity.table });
85
+ }
86
+ });
86
87
  };
87
-
88
88
  const checkPersistenceFile = (scan, file) => {
89
- if (file.slot !== 'be.persistence' || !file.owner) return;
90
- const ownContext = scan.model.contextOfCapability(file.owner.root);
91
- if (!ownContext) return;
92
- if (path.posix.basename(file.rel).endsWith('.entity.ts')) checkRelations(scan, file, ownContext);
93
- if (file.rel.slice(file.owner.root.length + 1).startsWith('persistence/migrations/')) checkForeignKeys(scan, file, ownContext);
89
+ if (file.slot !== 'be.persistence' || !file.owner)
90
+ return;
91
+ const ownContext = scan.model.contextOfCapability(file.owner.root);
92
+ if (!ownContext)
93
+ return;
94
+ if (path.posix.basename(file.rel).endsWith('.entity.ts'))
95
+ checkRelations(scan, file, ownContext);
96
+ if (file.rel.slice(file.owner.root.length + 1).startsWith('persistence/migrations/'))
97
+ checkForeignKeys(scan, file, ownContext);
94
98
  };
95
-
96
99
  export function checkContextCoupling(input) {
97
- const { graph } = input;
98
- const kit = machineKit(input);
99
- const model = contextModelOf(kit, graph);
100
- const { tables } = entitiesOf(kit, graph);
101
- const violations = [];
102
- const seen = new Set();
103
- const once = (key, violation) => { if (!seen.has(key)) { seen.add(key); violations.push({ ruleId: RULE, ...violation }); } };
104
- const counts = { relations: 0, foreignKeys: 0, imports: 0 };
105
- const covered = new Set(); // `${fromRel}|${targetRoot}` pairs a relation already reported, so the import edge is not a second finding
106
- const scan = { kit, model, tables, counts, covered, once };
107
-
108
- for (const file of graph.files.values()) checkPersistenceFile(scan, file);
109
-
110
- checkCrossContextImports(graph, model, covered, counts, once);
111
- return { violations, coverage: { status: 'checked', ...counts } };
100
+ const { graph } = input;
101
+ const kit = machineKit(input);
102
+ const model = contextModelOf(kit, graph);
103
+ const { tables } = entitiesOf(kit, graph);
104
+ const violations = [];
105
+ const seen = new Set();
106
+ const once = (key, violation) => { if (!seen.has(key)) {
107
+ seen.add(key);
108
+ violations.push({ ruleId: RULE, ...violation });
109
+ } };
110
+ const counts = { relations: 0, foreignKeys: 0, imports: 0 };
111
+ const covered = new Set(); // `${fromRel}|${targetRoot}` pairs a relation already reported, so the import edge is not a second finding
112
+ const scan = { kit, model, tables, counts, covered, once };
113
+ for (const file of graph.files.values())
114
+ checkPersistenceFile(scan, file);
115
+ checkCrossContextImports(graph, model, covered, counts, once);
116
+ return { violations, coverage: { status: 'checked', ...counts } };
112
117
  }