@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
@@ -1,149 +1,163 @@
1
1
  import path from 'node:path';
2
2
  import { pascal } from './machine-ast.mjs';
3
3
  import { DATABASE_DIR } from './connection-map.mjs';
4
-
5
- const camel = name => { const text = pascal(name); return text[0].toLowerCase() + text.slice(1); };
6
-
7
- const unparenWhile = (node, isWrapper) => {
8
- let current = node;
9
- while (current && isWrapper(current)) current = current.expression;
10
- return current;
11
- };
12
-
4
+ const camel = (name) => { const text = pascal(name); return text.charAt(0).toLowerCase() + text.slice(1); };
5
+ function unparenWhile(node, isWrapper) {
6
+ let current = node;
7
+ while (current && isWrapper(current))
8
+ current = current.expression;
9
+ return current;
10
+ }
13
11
  const isExpressionWrapper = (ts, node) => ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isNonNullExpression(node);
14
-
15
- const unparenRegistration = (ts, node) => unparenWhile(node, current => isExpressionWrapper(ts, current) || ts.isSatisfiesExpression?.(current));
16
-
12
+ const isRegistrationWrapper = (ts, node) => isExpressionWrapper(ts, node) || Boolean(ts.isSatisfiesExpression?.(node));
13
+ const unparenRegistration = (ts, node) => unparenWhile(node, (current) => isRegistrationWrapper(ts, current));
17
14
  const returnedExpression = (ts, declaration) => {
18
- const fn = ts.isVariableDeclaration(declaration) && declaration.initializer ? unparenRegistration(ts, declaration.initializer) : declaration;
19
- if (!(ts.isArrowFunction(fn) || ts.isFunctionDeclaration(fn) || ts.isFunctionExpression(fn) || ts.isMethodDeclaration(fn)) || !fn.body) return null;
20
- if (!ts.isBlock(fn.body)) return fn.body;
21
- const statements = fn.body.statements.filter(ts.isReturnStatement);
22
- return statements.length === 1 ? statements[0].expression ?? null : null;
15
+ const fn = ts.isVariableDeclaration(declaration) && declaration.initializer ? unparenRegistration(ts, declaration.initializer) : declaration;
16
+ if (!(ts.isArrowFunction(fn) || ts.isFunctionDeclaration(fn) || ts.isFunctionExpression(fn) || ts.isMethodDeclaration(fn)) || !fn.body)
17
+ return null;
18
+ if (!ts.isBlock(fn.body))
19
+ return fn.body;
20
+ const statements = fn.body.statements.filter(ts.isReturnStatement);
21
+ return statements.length === 1 ? statements[0]?.expression ?? null : null;
23
22
  };
24
-
25
23
  /** Every `name:` property assignment of an object literal over the program: [{declaration, initializer, checker}], memoised per name. */
26
24
  const assignmentsOf = (ctx, name) => {
27
- if (ctx.propertySources.has(name)) return ctx.propertySources.get(name);
28
- const { kit } = ctx;
29
- const { ts } = kit;
30
- const list = [];
31
- for (const file of ctx.graph.files.values()) {
32
- if (!file.sourceFile.text.includes(name)) continue;
33
- const checker = kit.checkerOf(file.sourceFile);
34
- kit.walk(file.sourceFile, node => {
35
- if (ts.isPropertyAssignment(node) && kit.propertyNameText(node.name) === name && ts.isObjectLiteralExpression(node.parent)) {
36
- // the property of the type the literal is checked against, so a typed const and a typed return meet the same declaration
37
- const contextual = checker.getContextualType(node.parent)?.getProperty(name);
38
- const symbol = contextual ?? checker.getSymbolAtLocation(node.name);
39
- list.push({ declaration: symbol?.declarations?.[0] ?? null, initializer: node.initializer, checker });
40
- }
41
- return true;
42
- });
43
- }
44
- ctx.propertySources.set(name, list);
45
- return list;
25
+ const cached = ctx.propertySources.get(name);
26
+ if (cached)
27
+ return cached;
28
+ const { kit } = ctx;
29
+ const { ts } = kit;
30
+ const list = [];
31
+ for (const file of ctx.graph.files.values()) {
32
+ if (!file.sourceFile.text.includes(name))
33
+ continue;
34
+ const checker = kit.checkerOf(file.sourceFile);
35
+ kit.walk(file.sourceFile, (node) => {
36
+ if (ts.isPropertyAssignment(node) && kit.propertyNameText(node.name) === name && ts.isObjectLiteralExpression(node.parent)) {
37
+ // the property of the type the literal is checked against, so a typed const and a typed return meet the same declaration
38
+ const contextual = checker.getContextualType(node.parent)?.getProperty(name);
39
+ const symbol = contextual ?? checker.getSymbolAtLocation(node.name);
40
+ list.push({ declaration: symbol?.declarations?.[0] ?? null, initializer: node.initializer, checker });
41
+ }
42
+ return true;
43
+ });
44
+ }
45
+ ctx.propertySources.set(name, list);
46
+ return list;
46
47
  };
47
-
48
48
  const nameOfLiteral = (ctx, checker, literal, depth) => {
49
- const { kit } = ctx;
50
- const own = kit.propertyOf(literal, 'name');
51
- if (own) return kit.stringValue(checker, kit.valueOfProperty(own));
52
- for (const property of literal.properties) {
53
- if (!kit.ts.isSpreadAssignment(property)) continue;
54
- const value = nameOfExpression(ctx, checker, property.expression, depth + 1);
55
- if (value) return value;
56
- }
57
- return null;
49
+ const { kit } = ctx;
50
+ const own = kit.propertyOf(literal, 'name');
51
+ if (own)
52
+ return kit.stringValue(checker, kit.valueOfProperty(own));
53
+ for (const property of literal.properties) {
54
+ if (!kit.ts.isSpreadAssignment(property))
55
+ continue;
56
+ const value = nameOfExpression(ctx, checker, property.expression, depth + 1);
57
+ if (value)
58
+ return value;
59
+ }
60
+ return null;
58
61
  };
59
-
60
62
  const nameFromCall = (ctx, checker, node, depth) => {
61
- const { kit } = ctx;
62
- for (const declaration of kit.declarationsOf(checker, node.expression)) {
63
- const body = returnedExpression(kit.ts, declaration);
64
- const value = body ? nameOfExpression(ctx, kit.checkerOf(declaration.getSourceFile()), body, depth + 1) : null;
65
- if (value) return value;
66
- }
67
- return null;
63
+ const { kit } = ctx;
64
+ const declarations = kit.declarationsOf(checker, node.expression);
65
+ for (const declaration of declarations) {
66
+ const body = returnedExpression(kit.ts, declaration);
67
+ const value = body ? nameOfExpression(ctx, kit.checkerOf(declaration.getSourceFile()), body, depth + 1) : null;
68
+ if (value)
69
+ return value;
70
+ }
71
+ return null;
68
72
  };
69
-
70
73
  const nameFromIdentifier = (ctx, checker, node, depth) => {
71
- const { kit } = ctx;
72
- const declaration = kit.declarationsOf(checker, node)[0];
73
- return declaration && kit.ts.isVariableDeclaration(declaration) && declaration.initializer
74
- ? nameOfExpression(ctx, kit.checkerOf(declaration.getSourceFile()), declaration.initializer, depth + 1) : null;
74
+ const { kit } = ctx;
75
+ const declaration = kit.declarationsOf(checker, node)[0];
76
+ return declaration && kit.ts.isVariableDeclaration(declaration) && declaration.initializer
77
+ ? nameOfExpression(ctx, kit.checkerOf(declaration.getSourceFile()), declaration.initializer, depth + 1) : null;
75
78
  };
76
-
77
79
  /** True when `declaration` is a property of the `<app>.env.ts` reader's own interface that sits beside the options file declaring `target`: the reader feeds the options value, so its property is the same slot. */
78
80
  const feedsOptions = (declaration, target) => {
79
- const home = declaration?.getSourceFile().fileName ?? '';
80
- return home.endsWith('.env.ts') && path.posix.dirname(home) === path.posix.dirname(target.getSourceFile().fileName);
81
+ const home = declaration?.getSourceFile().fileName ?? '';
82
+ return home.endsWith('.env.ts') && path.posix.dirname(home) === path.posix.dirname(target.getSourceFile().fileName);
81
83
  };
82
-
83
84
  const nameFromProperty = (ctx, checker, node, depth) => {
84
- const target = ctx.kit.declarationsOf(checker, node.name)[0];
85
- if (!target) return null;
86
- const names = new Set(assignmentsOf(ctx, node.name.text).filter(item => item.declaration === target || feedsOptions(item.declaration, target))
87
- .map(item => nameOfExpression(ctx, item.checker, item.initializer, depth + 1)).filter(Boolean));
88
- return names.size === 1 ? [...names][0] : null;
85
+ const target = ctx.kit.declarationsOf(checker, node.name)[0];
86
+ if (!target)
87
+ return null;
88
+ const names = new Set(assignmentsOf(ctx, node.name.text).filter(item => item.declaration === target || feedsOptions(item.declaration, target))
89
+ .map(item => nameOfExpression(ctx, item.checker, item.initializer, depth + 1)).filter(Boolean));
90
+ return names.size === 1 ? [...names][0] ?? null : null;
89
91
  };
90
-
91
92
  function nameOfExpression(ctx, checker, expression, depth) {
92
- const { ts } = ctx.kit;
93
- const node = unparenRegistration(ts, expression);
94
- if (!node || depth > 5) return null;
95
- if (ts.isObjectLiteralExpression(node)) return nameOfLiteral(ctx, checker, node, depth);
96
- if (ts.isCallExpression(node)) return nameFromCall(ctx, checker, node, depth);
97
- if (ts.isIdentifier(node)) return nameFromIdentifier(ctx, checker, node, depth);
98
- if (ts.isPropertyAccessExpression(node)) return nameFromProperty(ctx, checker, node, depth);
99
- return null;
93
+ const { ts } = ctx.kit;
94
+ const node = unparenRegistration(ts, expression);
95
+ if (!node || depth > 5)
96
+ return null;
97
+ if (ts.isObjectLiteralExpression(node))
98
+ return nameOfLiteral(ctx, checker, node, depth);
99
+ if (ts.isCallExpression(node))
100
+ return nameFromCall(ctx, checker, node, depth);
101
+ if (ts.isIdentifier(node))
102
+ return nameFromIdentifier(ctx, checker, node, depth);
103
+ if (ts.isPropertyAccessExpression(node))
104
+ return nameFromProperty(ctx, checker, node, depth);
105
+ return null;
100
106
  }
101
-
102
107
  const isCapabilityArray = (owner, name) => owner?.below.length === 1 && owner.below[0] === 'connection.ts' && [`${camel(owner.capability)}Entities`, `${camel(owner.capability)}Migrations`].includes(name);
103
-
104
108
  const arrayDeclarationOf = (kit, checker, node) => {
105
- const { ts } = kit;
106
- const declaration = kit.declarationsOf(checker, ts.isPropertyAccessExpression(node) ? node.name : node)[0];
107
- return declaration && ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name) ? declaration : null;
109
+ const { ts } = kit;
110
+ const declaration = kit.declarationsOf(checker, ts.isPropertyAccessExpression(node) ? node.name : node)[0];
111
+ // the identifier name was just checked, so the declaration is a named variable
112
+ return declaration && ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name) ? declaration : null;
108
113
  };
109
-
110
114
  /** The capability arrays (`<c>Entities` / `<c>Migrations` of persistence/connection.ts) an expression lists. */
111
115
  function arraysIn(ctx, checker, expression, out, depth) {
112
- const { kit } = ctx;
113
- const { ts } = kit;
114
- const node = unparenRegistration(ts, expression);
115
- if (!node || depth > 6) return;
116
- if (ts.isArrayLiteralExpression(node)) {
117
- for (const element of node.elements) { arraysIn(ctx, checker, ts.isSpreadElement(element) ? element.expression : element, out, depth + 1); }
118
- return;
119
- }
120
- if (!ts.isIdentifier(node) && !ts.isPropertyAccessExpression(node)) return;
121
- const declaration = arrayDeclarationOf(kit, checker, node);
122
- if (!declaration) return;
123
- const rel = kit.graphPath(declaration);
124
- const owner = rel ? ctx.persistenceOf(rel) : null;
125
- if (isCapabilityArray(owner, declaration.name.text)) {
126
- out.push({ key: path.posix.dirname(owner.root), name: owner.capability });
127
- } else if (declaration.initializer) arraysIn(ctx, kit.checkerOf(declaration.getSourceFile()), declaration.initializer, out, depth + 1);
116
+ const { kit } = ctx;
117
+ const { ts } = kit;
118
+ const node = unparenRegistration(ts, expression);
119
+ if (!node || depth > 6)
120
+ return;
121
+ if (ts.isArrayLiteralExpression(node)) {
122
+ for (const element of node.elements) {
123
+ arraysIn(ctx, checker, ts.isSpreadElement(element) ? element.expression : element, out, depth + 1);
124
+ }
125
+ return;
126
+ }
127
+ if (!ts.isIdentifier(node) && !ts.isPropertyAccessExpression(node))
128
+ return;
129
+ const declaration = arrayDeclarationOf(kit, checker, node);
130
+ if (!declaration)
131
+ return;
132
+ const rel = kit.graphPath(declaration);
133
+ const owner = rel ? ctx.persistenceOf(rel) : null;
134
+ if (isCapabilityArray(owner, declaration.name.text)) {
135
+ out.push({ key: path.posix.dirname(owner.root), name: owner.capability });
136
+ }
137
+ else if (declaration.initializer)
138
+ arraysIn(ctx, kit.checkerOf(declaration.getSourceFile()), declaration.initializer, out, depth + 1);
128
139
  }
129
-
130
140
  const registrationArrays = (ctx, checker, literal) => {
131
- const arrays = [];
132
- for (const key of ['entities', 'migrations']) {
133
- const property = ctx.kit.propertyOf(literal, key);
134
- if (property) arraysIn(ctx, checker, ctx.kit.valueOfProperty(property), arrays, 0);
135
- }
136
- return arrays;
141
+ const arrays = [];
142
+ for (const key of ['entities', 'migrations']) {
143
+ const property = ctx.kit.propertyOf(literal, key);
144
+ if (property)
145
+ arraysIn(ctx, checker, ctx.kit.valueOfProperty(property), arrays, 0);
146
+ }
147
+ return arrays;
137
148
  };
138
-
139
149
  const recordRegistration = (registered, arrays, connection, site) => {
140
- for (const { key, name } of new Map(arrays.map(item => [item.key, item])).values()) {
141
- if (!registered.has(key)) registered.set(key, { name, connections: new Map() });
142
- const sites = registered.get(key).connections;
143
- if (!sites.has(connection)) sites.set(connection, site);
144
- }
150
+ for (const { key, name } of new Map(arrays.map((item) => [item.key, item])).values()) {
151
+ let entry = registered.get(key);
152
+ if (!entry) {
153
+ entry = { name, connections: new Map() };
154
+ registered.set(key, entry);
155
+ }
156
+ const sites = entry.connections;
157
+ if (!sites.has(connection))
158
+ sites.set(connection, site);
159
+ }
145
160
  };
146
-
147
161
  /**
148
162
  * Where the persistence arrays of every capability are registered: the object literals (`DatabaseModule.register` connections and the
149
163
  * migrate/cli app's) that list `<c>Entities` / `<c>Migrations` of a capability's `persistence/connection.ts` under a connection `name`.
@@ -154,92 +168,98 @@ const recordRegistration = (registered, arrays, connection, site) => {
154
168
  * `registered`: capability root -> its name and the connections it is registered on (first site each).
155
169
  */
156
170
  export function collectRegistrations({ kit, graph, persistenceOf }) {
157
- const { ts } = kit;
158
- const ctx = { kit, graph, persistenceOf, propertySources: new Map() }; // property name -> [{declaration, initializer, checker}] over the program
159
- const all = []; // every registration literal: {file, node, connection | null when the name cannot be resolved}
160
- const registered = new Map(); // capability root -> {name, connections: Map(connection -> first site)}
161
- let registrations = 0;
162
- for (const file of graph.files.values()) {
163
- const checker = kit.checkerOf(file.sourceFile);
164
- kit.walk(file.sourceFile, node => {
165
- if (!ts.isObjectLiteralExpression(node)) return;
166
- const arrays = registrationArrays(ctx, checker, node);
167
- if (!arrays.length) return;
168
- registrations += 1;
169
- const connection = nameOfLiteral(ctx, checker, node, 0);
170
- all.push({ file, node, connection });
171
- if (connection !== null) recordRegistration(registered, arrays, connection, { file, node });
172
- });
173
- }
174
- return { registered, registrations, all };
171
+ const { ts } = kit;
172
+ const ctx = { kit, graph, persistenceOf, propertySources: new Map() }; // property name -> [{declaration, initializer, checker}] over the program
173
+ const all = []; // every registration literal: {file, node, connection | null when the name cannot be resolved}
174
+ const registered = new Map(); // capability root -> {name, connections: Map(connection -> first site)}
175
+ let registrations = 0;
176
+ for (const file of graph.files.values()) {
177
+ const checker = kit.checkerOf(file.sourceFile);
178
+ kit.walk(file.sourceFile, (node) => {
179
+ if (!ts.isObjectLiteralExpression(node))
180
+ return;
181
+ const arrays = registrationArrays(ctx, checker, node);
182
+ if (!arrays.length)
183
+ return;
184
+ registrations += 1;
185
+ const connection = nameOfLiteral(ctx, checker, node, 0);
186
+ all.push({ file, node, connection });
187
+ if (connection !== null)
188
+ recordRegistration(registered, arrays, connection, { file, node });
189
+ });
190
+ }
191
+ return { registered, registrations, all };
175
192
  }
176
-
177
193
  const PERSISTENCE_SLOT = 'be.persistence';
178
194
  /** The slot tiers whose capabilities belong to a bounded context: the domain and the projections (a projection is owned by the context whose connection holds its tables). */
179
195
  const CONTEXT_TIERS = new Set(['domain', 'projections']);
180
-
181
196
  /** True for a platform capability the manifest lists as `perConnection` (its tables exist on every connection that uses it); `root` is the capability root. */
182
197
  export const isPerConnection = (resolver, root, name) => root.split('/').includes('platform') && (resolver.slot(PERSISTENCE_SLOT)?.perConnection ?? []).includes(name);
183
-
184
198
  /**
185
199
  * `persistenceOf(rel)`: {root, capability, folder, below} of a be.persistence file (`folder` the directory below persistence/, '' at its root),
186
200
  * null for any other file. `root` is the persistence root, its dirname the capability root.
187
201
  */
188
- export const persistenceOfFactory = resolver => rel => {
189
- const classified = resolver.classifyPath(rel);
190
- if (classified.slot !== PERSISTENCE_SLOT) return null;
191
- const below = rel.slice(classified.root.length + 1).split('/');
192
- return { root: classified.root, capability: path.posix.basename(path.posix.dirname(classified.root)), folder: below.length > 1 ? below[0] : '', below };
202
+ export const persistenceOfFactory = (resolver) => (rel) => {
203
+ const classified = resolver.classifyPath(rel);
204
+ if (classified.slot !== PERSISTENCE_SLOT)
205
+ return null;
206
+ const below = rel.slice(classified.root.length + 1).split('/');
207
+ return { root: classified.root, capability: path.posix.basename(path.posix.dirname(classified.root)), folder: below.length > 1 ? below[0] ?? '' : '', below };
193
208
  };
194
-
195
209
  /** The declared connection named by a decorator whose callee is declared in src/modules/platform/database/<conn>.decorators.ts, else null. */
196
210
  const connectionOfDecorators = (kit, declared, checker, declaration) => {
197
- const { ts } = kit;
198
- for (const decorator of kit.decorators(declaration)) {
199
- const callee = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
200
- for (const target of kit.declarationsOf(checker, callee)) {
201
- const rel = kit.graphPath(target);
202
- const name = rel ? [...declared.keys()].find(connection => rel === `${DATABASE_DIR}/${connection}.decorators.ts`) : null;
203
- if (name) return name;
211
+ const { ts } = kit;
212
+ const decorators = kit.decorators(declaration);
213
+ for (const decorator of decorators) {
214
+ const callee = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
215
+ const targets = kit.declarationsOf(checker, callee);
216
+ for (const target of targets) {
217
+ const rel = kit.graphPath(target);
218
+ const name = rel ? [...declared.keys()].find(connection => rel === `${DATABASE_DIR}/${connection}.decorators.ts`) : null;
219
+ if (name)
220
+ return name;
221
+ }
204
222
  }
205
- }
206
- return null;
223
+ return null;
207
224
  };
208
-
209
225
  const managerTarget = (ts, node) => {
210
- if (ts.isPropertyAccessExpression(node)) return node.name;
211
- return ts.isIdentifier(node) ? node : null;
226
+ if (ts.isPropertyAccessExpression(node))
227
+ return node.name;
228
+ return ts.isIdentifier(node) ? node : null;
212
229
  };
213
-
214
230
  /** The receiver expression of the `.transaction(callback)` call whose first callback parameter is `declaration`, else null. */
215
231
  const transactionReceiver = (ts, declaration) => {
216
- const callback = declaration.parent;
217
- if (!ts.isParameter(declaration) || !(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)) || callback.parameters[0] !== declaration) return null;
218
- const call = callback.parent;
219
- if (!ts.isCallExpression(call) || !call.arguments.includes(callback) || !ts.isPropertyAccessExpression(call.expression) || call.expression.name.text !== 'transaction') return null;
220
- return call.expression.expression;
232
+ const callback = declaration.parent;
233
+ if (!ts.isParameter(declaration) || !(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)) || callback.parameters[0] !== declaration)
234
+ return null;
235
+ const call = callback.parent;
236
+ if (!ts.isCallExpression(call) || !call.arguments.includes(callback) || !ts.isPropertyAccessExpression(call.expression) || call.expression.name.text !== 'transaction')
237
+ return null;
238
+ return call.expression.expression;
221
239
  };
222
-
223
240
  /**
224
241
  * The declared connection whose entity manager `expression` is: a property or parameter injected with `Inject<Conn>EntityManager`, or the
225
242
  * manager parameter of the callback of a `.transaction(...)` on such a manager. Null when the origin cannot be proven (never guessed).
226
243
  */
227
244
  function connectionOfManagerIn(kit, declared, checker, expression, depth = 0) {
228
- const { ts } = kit;
229
- const node = unparenWhile(expression, current => isExpressionWrapper(ts, current));
230
- if (!node || depth > 4) return null;
231
- const target = managerTarget(ts, node);
232
- if (!target) return null;
233
- const declaration = kit.declarationsOf(checker, target)[0];
234
- if (!declaration) return null;
235
- if (ts.isPropertyDeclaration(declaration) || ts.isParameter(declaration)) {
236
- const direct = connectionOfDecorators(kit, declared, checker, declaration);
237
- if (direct) return direct;
238
- }
239
- const receiver = transactionReceiver(ts, declaration);
240
- return receiver ? connectionOfManagerIn(kit, declared, checker, receiver, depth + 1) : null;
245
+ const { ts } = kit;
246
+ const node = unparenWhile(expression, (current) => isExpressionWrapper(ts, current));
247
+ if (!node || depth > 4)
248
+ return null;
249
+ const target = managerTarget(ts, node);
250
+ if (!target)
251
+ return null;
252
+ const declaration = kit.declarationsOf(checker, target)[0];
253
+ if (!declaration)
254
+ return null;
255
+ if (ts.isPropertyDeclaration(declaration) || ts.isParameter(declaration)) {
256
+ const direct = connectionOfDecorators(kit, declared, checker, declaration);
257
+ if (direct)
258
+ return direct;
259
+ }
260
+ const receiver = transactionReceiver(ts, declaration);
261
+ return receiver ? connectionOfManagerIn(kit, declared, checker, receiver, depth + 1) : null;
241
262
  }
242
-
243
263
  /**
244
264
  * The bounded-context model of a program: which context (declared connection) each capability belongs to, and which connection an expression's
245
265
  * entity manager is. A context is a connection; a capability belongs to the one declared connection its persistence arrays are registered on
@@ -248,75 +268,88 @@ function connectionOfManagerIn(kit, declared, checker, expression, depth = 0) {
248
268
  * transaction callback parameter's receiver. Nothing is matched by a variable name.
249
269
  */
250
270
  const MODELS = new WeakMap();
251
-
252
271
  export function contextModelOf(kit, graph) {
253
- if (MODELS.has(graph)) return MODELS.get(graph);
254
- const { ts, resolver } = kit;
255
- const persistenceOf = persistenceOfFactory(resolver);
256
- const declared = new Map(resolver.repo.connections.map(connection => [connection.name, connection]));
257
- const { registered, all } = collectRegistrations({ kit, graph, persistenceOf });
258
- const perConnection = new Set();
259
- const contextOfRoot = new Map(); // capability root -> declared connection name (single registration only)
260
- for (const [root, { name, connections }] of registered) {
261
- if (isPerConnection(resolver, root, name)) { perConnection.add(root); continue; }
262
- const names = [...connections.keys()].filter(connection => declared.has(connection));
263
- if (names.length === 1) contextOfRoot.set(root, names[0]);
264
- }
265
- /** The declared connection a capability root belongs to, or null (no registration, several, or a per-connection platform capability). */
266
- const contextOfCapability = root => contextOfRoot.get(root) ?? null;
267
- /** The context of the capability owning `rel` when that capability is of a context tier (domain, projections), else null: only these are bounded contexts. */
268
- const contextOfFile = rel => {
269
- const file = graph.files.get(rel);
270
- if (!file?.owner || !CONTEXT_TIERS.has(file.tier)) return null;
271
- return contextOfCapability(file.owner.root);
272
- };
273
- const model = {
274
- declared, registered, registrations: all, perConnection, contextOfCapability, contextOfFile,
275
- connectionOfManager: (checker, expression) => connectionOfManagerIn(kit, declared, checker, expression),
276
- unparen: node => unparenWhile(node, current => isExpressionWrapper(ts, current)),
277
- };
278
- MODELS.set(graph, model);
279
- return model;
272
+ const cached = MODELS.get(graph);
273
+ if (cached)
274
+ return cached;
275
+ const { ts, resolver } = kit;
276
+ const persistenceOf = persistenceOfFactory(resolver);
277
+ const connections = resolver.repo.connections;
278
+ const declared = new Map(connections.map((connection) => [connection.name, connection]));
279
+ const { registered, all } = collectRegistrations({ kit, graph, persistenceOf });
280
+ const perConnection = new Set();
281
+ const contextOfRoot = new Map(); // capability root -> declared connection name (single registration only)
282
+ for (const [root, { name, connections: sites }] of registered) {
283
+ if (isPerConnection(resolver, root, name)) {
284
+ perConnection.add(root);
285
+ continue;
286
+ }
287
+ const names = [...sites.keys()].filter(connection => declared.has(connection));
288
+ const [only] = names;
289
+ if (names.length === 1 && only !== undefined)
290
+ contextOfRoot.set(root, only);
291
+ }
292
+ /** The declared connection a capability root belongs to, or null (no registration, several, or a per-connection platform capability). */
293
+ const contextOfCapability = (root) => contextOfRoot.get(root) ?? null;
294
+ /** The context of the capability owning `rel` when that capability is of a context tier (domain, projections), else null: only these are bounded contexts. */
295
+ const contextOfFile = (rel) => {
296
+ const file = graph.files.get(rel);
297
+ if (!file?.owner || !CONTEXT_TIERS.has(file.tier ?? ''))
298
+ return null;
299
+ return contextOfCapability(file.owner.root);
300
+ };
301
+ const model = {
302
+ declared, registered, registrations: all, perConnection, contextOfCapability, contextOfFile,
303
+ connectionOfManager: (checker, expression) => connectionOfManagerIn(kit, declared, checker, expression),
304
+ unparen: node => unparenWhile(node, (current) => isExpressionWrapper(ts, current)),
305
+ };
306
+ MODELS.set(graph, model);
307
+ return model;
280
308
  }
281
-
282
309
  const composedConnection = (kit, declared, checker, inner) => {
283
- const { ts } = kit;
284
- const parent = inner.parent;
285
- if (parent?.name === inner && (ts.isPropertyAssignment(parent) || ts.isPropertyAccessExpression(parent))) return null;
286
- if (!(ts.isIdentifier(inner) || ts.isStringLiteralLike(inner) || ts.isPropertyAccessExpression(inner))) return null;
287
- const value = kit.stringValue(checker, inner);
288
- return value !== null && declared.has(value) ? value : null;
310
+ const { ts } = kit;
311
+ const parent = inner.parent;
312
+ if ((ts.isPropertyAssignment(parent) || ts.isPropertyAccessExpression(parent)) && parent.name === inner)
313
+ return null;
314
+ if (!(ts.isIdentifier(inner) || ts.isStringLiteralLike(inner) || ts.isPropertyAccessExpression(inner)))
315
+ return null;
316
+ const value = kit.stringValue(checker, inner);
317
+ return value !== null && declared.has(value) ? value : null;
289
318
  };
290
-
291
319
  const collectComposedConnections = (kit, declared, checker, call, file, items) => {
292
- for (const argument of call.arguments) {
293
- kit.walk(argument, inner => {
294
- const value = composedConnection(kit, declared, checker, inner);
295
- if (value === null) return true;
296
- items.push({ connection: value, node: inner, file });
297
- return false;
298
- });
299
- }
320
+ for (const argument of call.arguments) {
321
+ kit.walk(argument, (inner) => {
322
+ const value = composedConnection(kit, declared, checker, inner);
323
+ if (value === null)
324
+ return true;
325
+ items.push({ connection: value, node: inner, file });
326
+ return false;
327
+ });
328
+ }
300
329
  };
301
-
302
330
  /**
303
331
  * The declared connection names the composition of `app` (its root app.module.ts and the composition groups of compose/) passes to
304
332
  * the database module registration, as [{connection, node, file}] in path then source order.
305
333
  */
306
334
  export function connectionsComposedBy(kit, app) {
307
- const { ts } = kit;
308
- const files = kit.appCompositionFiles(app);
309
- if (!files.length) return { root: null, items: [] };
310
- const declared = new Set(kit.resolver.repo.connections.map(connection => connection.name));
311
- const items = [];
312
- for (const file of files) {
313
- const checker = kit.checkerOf(file.sourceFile);
314
- kit.walk(file.sourceFile, node => {
315
- if (!(ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register')) return;
316
- const owner = kit.declarationsOf(checker, node.expression.expression).map(kit.ownerOfDeclaration).find(Boolean);
317
- if (owner?.tier !== 'platform' || owner.name !== 'database') return;
318
- collectComposedConnections(kit, declared, checker, node, file, items);
319
- });
320
- }
321
- return { root: files[0], items };
335
+ const { ts } = kit;
336
+ const files = kit.appCompositionFiles(app);
337
+ if (!files.length)
338
+ return { root: null, items: [] };
339
+ const connections = kit.resolver.repo.connections;
340
+ const declared = new Set(connections.map(connection => connection.name));
341
+ const items = [];
342
+ for (const file of files) {
343
+ const checker = kit.checkerOf(file.sourceFile);
344
+ kit.walk(file.sourceFile, (node) => {
345
+ if (!(ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register'))
346
+ return;
347
+ const declarations = kit.declarationsOf(checker, node.expression.expression);
348
+ const owner = declarations.map(kit.ownerOfDeclaration).find(Boolean);
349
+ if (owner?.tier !== 'platform' || owner.name !== 'database')
350
+ return;
351
+ collectComposedConnections(kit, declared, checker, node, file, items);
352
+ });
353
+ }
354
+ return { root: files[0] ?? null, items };
322
355
  }