@starci/eslint-canon-be 3.18.4 → 3.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/comments.js +1 -1
  3. package/dist/explicit-surface.js +1 -1
  4. package/dist/index.js +1 -1
  5. package/dist/jwt-alg.js +1 -1
  6. package/dist/lib/edition.d.ts +1 -1
  7. package/dist/lib/edition.js +1 -1
  8. package/dist/lib/hfs.d.ts +0 -6
  9. package/dist/lib/hfs.js +7 -5
  10. package/dist/lib/runtime-root.d.ts +6 -1
  11. package/dist/lib/runtime-root.js +8 -2
  12. package/dist/lib/subject-values.js +1 -1
  13. package/dist/module-layering.js +2 -2
  14. package/dist/project-graph.js +1 -1
  15. package/dist/third-party-owner.js +1 -1
  16. package/package.json +3 -1
  17. package/runtime/bundle.json +1 -190
  18. package/runtime/scripts/api/git/recorded-lines.mjs +5 -5
  19. package/runtime/scripts/hfs/project-graph.mjs +6 -6
  20. package/runtime/scripts/hfs/project-rule.mjs +3 -3
  21. package/runtime/config.example.yaml +0 -194
  22. package/runtime/engine/by-code-unit.mjs +0 -12
  23. package/runtime/engine/config.mjs +0 -461
  24. package/runtime/engine/invalid-config.mjs +0 -15
  25. package/runtime/engine/model-config.mjs +0 -112
  26. package/runtime/engine/orca-config.mjs +0 -34
  27. package/runtime/engine/plain-object.mjs +0 -5
  28. package/runtime/engine/release-config.mjs +0 -32
  29. package/runtime/engine/removed-vocabulary.mjs +0 -27
  30. package/runtime/engine/resources-config.mjs +0 -59
  31. package/runtime/engine/runtime-root.mjs +0 -138
  32. package/runtime/engine/secrets.mjs +0 -202
  33. package/runtime/engine/sonar-config.mjs +0 -24
  34. package/runtime/engine/temp-root.mjs +0 -26
  35. package/runtime/engine/yaml.mjs +0 -161
  36. package/runtime/knowledge/grammars/index.yaml +0 -114
  37. package/runtime/knowledge/hfs/canon-pins.yaml +0 -720
  38. package/runtime/knowledge/hfs/fe-stack.yaml +0 -272
  39. package/runtime/knowledge/hfs/rules.yaml +0 -6760
  40. package/runtime/knowledge/hfs/slots.yaml +0 -2783
  41. package/runtime/knowledge/patterns/fe/folder.yaml +0 -425
  42. package/runtime/knowledge/sonar-gate.yaml +0 -151
  43. package/runtime/modules/kernel/removed-vocabulary.yaml +0 -458
  44. package/runtime/modules/models/registry.yaml +0 -221
  45. package/runtime/modules/models/runtimes.yaml +0 -556
  46. package/runtime/modules/models/tiers.yaml +0 -66
  47. package/runtime/scripts/api/git/cat-file.mjs +0 -4
  48. package/runtime/scripts/api/git/config-get.mjs +0 -9
  49. package/runtime/scripts/api/git/diff.mjs +0 -4
  50. package/runtime/scripts/api/git/lib.mjs +0 -80
  51. package/runtime/scripts/api/git/ls-files.mjs +0 -5
  52. package/runtime/scripts/api/git/ls-tree.mjs +0 -4
  53. package/runtime/scripts/api/git/merge-base.mjs +0 -8
  54. package/runtime/scripts/api/git/remote.mjs +0 -4
  55. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -4
  56. package/runtime/scripts/api/git/show.mjs +0 -5
  57. package/runtime/scripts/api/git/tag.mjs +0 -4
  58. package/runtime/scripts/hfs/allows.mjs +0 -77
  59. package/runtime/scripts/hfs/architecture/ast-walks.mjs +0 -250
  60. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +0 -103
  61. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +0 -114
  62. package/runtime/scripts/hfs/architecture/backend.mjs +0 -462
  63. package/runtime/scripts/hfs/architecture/background-unowned.mjs +0 -254
  64. package/runtime/scripts/hfs/architecture/canon-clones.mjs +0 -291
  65. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +0 -224
  66. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +0 -102
  67. package/runtime/scripts/hfs/architecture/clones.mjs +0 -287
  68. package/runtime/scripts/hfs/architecture/component-tiers.mjs +0 -161
  69. package/runtime/scripts/hfs/architecture/config-unread.mjs +0 -16
  70. package/runtime/scripts/hfs/architecture/config.mjs +0 -407
  71. package/runtime/scripts/hfs/architecture/connection-map.mjs +0 -350
  72. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +0 -115
  73. package/runtime/scripts/hfs/architecture/context-coupling.mjs +0 -117
  74. package/runtime/scripts/hfs/architecture/context-map.mjs +0 -355
  75. package/runtime/scripts/hfs/architecture/context-owner.mjs +0 -102
  76. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +0 -65
  77. package/runtime/scripts/hfs/architecture/context-transaction.mjs +0 -75
  78. package/runtime/scripts/hfs/architecture/contract-bounds.mjs +0 -72
  79. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +0 -177
  80. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +0 -360
  81. package/runtime/scripts/hfs/architecture/contracts.mjs +0 -489
  82. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +0 -81
  83. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +0 -173
  84. package/runtime/scripts/hfs/architecture/dead-exports.mjs +0 -420
  85. package/runtime/scripts/hfs/architecture/default-deny.mjs +0 -175
  86. package/runtime/scripts/hfs/architecture/doc-language.mjs +0 -45
  87. package/runtime/scripts/hfs/architecture/entity-registry.mjs +0 -51
  88. package/runtime/scripts/hfs/architecture/entrypoint.mjs +0 -75
  89. package/runtime/scripts/hfs/architecture/error-codes.mjs +0 -54
  90. package/runtime/scripts/hfs/architecture/error-masked.mjs +0 -78
  91. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +0 -44
  92. package/runtime/scripts/hfs/architecture/feature-shape.mjs +0 -60
  93. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +0 -41
  94. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +0 -112
  95. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +0 -193
  96. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +0 -198
  97. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +0 -368
  98. package/runtime/scripts/hfs/architecture/frontend-world.mjs +0 -374
  99. package/runtime/scripts/hfs/architecture/frontend.mjs +0 -248
  100. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +0 -185
  101. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +0 -77
  102. package/runtime/scripts/hfs/architecture/hfs.mjs +0 -394
  103. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +0 -118
  104. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +0 -213
  105. package/runtime/scripts/hfs/architecture/identity.mjs +0 -121
  106. package/runtime/scripts/hfs/architecture/index.mjs +0 -402
  107. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +0 -69
  108. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +0 -231
  109. package/runtime/scripts/hfs/architecture/machine-ast.mjs +0 -232
  110. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +0 -32
  111. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +0 -237
  112. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +0 -126
  113. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +0 -192
  114. package/runtime/scripts/hfs/architecture/next-data-key.mjs +0 -374
  115. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +0 -26
  116. package/runtime/scripts/hfs/architecture/next-data.mjs +0 -425
  117. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +0 -55
  118. package/runtime/scripts/hfs/architecture/owners.mjs +0 -142
  119. package/runtime/scripts/hfs/architecture/package-shape.mjs +0 -92
  120. package/runtime/scripts/hfs/architecture/presentation.mjs +0 -173
  121. package/runtime/scripts/hfs/architecture/reachability.mjs +0 -330
  122. package/runtime/scripts/hfs/architecture/register-once.mjs +0 -212
  123. package/runtime/scripts/hfs/architecture/registration.mjs +0 -372
  124. package/runtime/scripts/hfs/architecture/render-graph.mjs +0 -449
  125. package/runtime/scripts/hfs/architecture/render-prop-across-server.mjs +0 -58
  126. package/runtime/scripts/hfs/architecture/required-files.mjs +0 -237
  127. package/runtime/scripts/hfs/architecture/root-packages.mjs +0 -78
  128. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +0 -133
  129. package/runtime/scripts/hfs/architecture/schema-owner.mjs +0 -288
  130. package/runtime/scripts/hfs/architecture/server-action.mjs +0 -78
  131. package/runtime/scripts/hfs/architecture/side-boundary.mjs +0 -13
  132. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +0 -320
  133. package/runtime/scripts/hfs/architecture/source-names.mjs +0 -393
  134. package/runtime/scripts/hfs/architecture/sql-keys.mjs +0 -344
  135. package/runtime/scripts/hfs/architecture/sql-owner.mjs +0 -291
  136. package/runtime/scripts/hfs/architecture/sql-returning.mjs +0 -76
  137. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +0 -117
  138. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +0 -587
  139. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +0 -42
  140. package/runtime/scripts/hfs/architecture/surface-nesting.mjs +0 -127
  141. package/runtime/scripts/hfs/architecture/surface.mjs +0 -88
  142. package/runtime/scripts/hfs/architecture/symbols.mjs +0 -273
  143. package/runtime/scripts/hfs/architecture/test-world-files.mjs +0 -263
  144. package/runtime/scripts/hfs/architecture/tiers.mjs +0 -256
  145. package/runtime/scripts/hfs/architecture/transport-owner.mjs +0 -248
  146. package/runtime/scripts/hfs/architecture/type-context.mjs +0 -335
  147. package/runtime/scripts/hfs/architecture/typescript.mjs +0 -477
  148. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +0 -165
  149. package/runtime/scripts/hfs/declaration-shape.mjs +0 -196
  150. package/runtime/scripts/hfs/declaration-slots.mjs +0 -89
  151. package/runtime/scripts/hfs/edition-slots.mjs +0 -80
  152. package/runtime/scripts/hfs/explicit-surface-rule.mjs +0 -59
  153. package/runtime/scripts/hfs/linear-text.mjs +0 -30
  154. package/runtime/scripts/hfs/manifest-shape.mjs +0 -474
  155. package/runtime/scripts/hfs/param-names.mjs +0 -13
  156. package/runtime/scripts/hfs/path-findings.mjs +0 -167
  157. package/runtime/scripts/hfs/repo-identity.mjs +0 -89
  158. package/runtime/scripts/hfs/rule-catalog.mjs +0 -458
  159. package/runtime/scripts/hfs/rule-params-shape.mjs +0 -103
  160. package/runtime/scripts/hfs/rules/database-constants.mjs +0 -25
  161. package/runtime/scripts/hfs/rules/database-migrations.mjs +0 -684
  162. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +0 -68
  163. package/runtime/scripts/hfs/rules/read.mjs +0 -45
  164. package/runtime/scripts/hfs/rules/secrets.mjs +0 -79
  165. package/runtime/scripts/hfs/rules/spec-placement.mjs +0 -28
  166. package/runtime/scripts/hfs/slot-app-view.mjs +0 -97
  167. package/runtime/scripts/hfs/slot-classify.mjs +0 -69
  168. package/runtime/scripts/hfs/slot-errors.mjs +0 -12
  169. package/runtime/scripts/hfs/slot-imports.mjs +0 -98
  170. package/runtime/scripts/hfs/slot-manifest-shape.mjs +0 -111
  171. package/runtime/scripts/hfs/slot-match.mjs +0 -164
  172. package/runtime/scripts/hfs/slot-path.mjs +0 -6
  173. package/runtime/scripts/hfs/slot-required.mjs +0 -95
  174. package/runtime/scripts/hfs/slot-semantic-problems.mjs +0 -175
  175. package/runtime/scripts/hfs/slot-side-problems.mjs +0 -32
  176. package/runtime/scripts/hfs/slots.mjs +0 -306
  177. package/runtime/scripts/hfs/test-world-slot.mjs +0 -5
  178. package/runtime/scripts/hfs/trailing-slashes.mjs +0 -8
  179. package/runtime/scripts/hfs/typescript-programs.mjs +0 -95
  180. package/runtime/scripts/hfs/view.mjs +0 -180
  181. package/runtime/scripts/lib/declaration-surface.mjs +0 -166
  182. package/runtime/scripts/lib/dockerfile.mjs +0 -164
  183. package/runtime/scripts/lib/env.mjs +0 -18
  184. package/runtime/scripts/lib/event-contract.mjs +0 -102
  185. package/runtime/scripts/lib/git-hooks-dir.mjs +0 -30
  186. package/runtime/scripts/lib/git.mjs +0 -35
  187. package/runtime/scripts/lib/glob.mjs +0 -32
  188. package/runtime/scripts/lib/grammar-lint.mjs +0 -57
  189. package/runtime/scripts/lib/i18n.mjs +0 -82
  190. package/runtime/scripts/lib/in-order.mjs +0 -90
  191. package/runtime/scripts/lib/json-clone.mjs +0 -70
  192. package/runtime/scripts/lib/json.mjs +0 -44
  193. package/runtime/scripts/lib/language.mjs +0 -233
  194. package/runtime/scripts/lib/list.mjs +0 -26
  195. package/runtime/scripts/lib/mutation-fence.mjs +0 -20
  196. package/runtime/scripts/lib/package-at.mjs +0 -62
  197. package/runtime/scripts/lib/path-key.mjs +0 -124
  198. package/runtime/scripts/lib/read-text.mjs +0 -47
  199. package/runtime/scripts/lib/regex.mjs +0 -3
  200. package/runtime/scripts/lib/rule-packs.mjs +0 -243
  201. package/runtime/scripts/lib/same-text.mjs +0 -4
  202. package/runtime/scripts/lib/secret-patterns.mjs +0 -230
  203. package/runtime/scripts/lib/sops-envelope.mjs +0 -212
  204. package/runtime/scripts/lib/stack-services.mjs +0 -191
  205. package/runtime/scripts/lib/ts-analysis.mjs +0 -39
  206. package/runtime/scripts/lib/ts-ast.mjs +0 -63
  207. package/runtime/scripts/lib/walk.mjs +0 -45
  208. package/runtime/scripts/lib/yaml-cached.mjs +0 -14
@@ -1,355 +0,0 @@
1
- import path from 'node:path';
2
- import { pascal } from './machine-ast.mjs';
3
- import { DATABASE_DIR } from './connection-map.mjs';
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
- }
11
- const isExpressionWrapper = (ts, node) => ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isNonNullExpression(node);
12
- const isRegistrationWrapper = (ts, node) => isExpressionWrapper(ts, node) || Boolean(ts.isSatisfiesExpression?.(node));
13
- const unparenRegistration = (ts, node) => unparenWhile(node, (current) => isRegistrationWrapper(ts, current));
14
- const returnedExpression = (ts, declaration) => {
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;
22
- };
23
- /** Every `name:` property assignment of an object literal over the program: [{declaration, initializer, checker}], memoised per name. */
24
- const assignmentsOf = (ctx, name) => {
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;
47
- };
48
- const nameOfLiteral = (ctx, checker, literal, depth) => {
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;
61
- };
62
- const nameFromCall = (ctx, checker, node, depth) => {
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;
72
- };
73
- const nameFromIdentifier = (ctx, checker, node, depth) => {
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;
78
- };
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. */
80
- const feedsOptions = (declaration, target) => {
81
- const home = declaration?.getSourceFile().fileName ?? '';
82
- return home.endsWith('.env.ts') && path.posix.dirname(home) === path.posix.dirname(target.getSourceFile().fileName);
83
- };
84
- const nameFromProperty = (ctx, checker, node, depth) => {
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;
91
- };
92
- function nameOfExpression(ctx, checker, expression, depth) {
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;
106
- }
107
- const isCapabilityArray = (owner, name) => owner?.below.length === 1 && owner.below[0] === 'connection.ts' && [`${camel(owner.capability)}Entities`, `${camel(owner.capability)}Migrations`].includes(name);
108
- const arrayDeclarationOf = (kit, checker, node) => {
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;
113
- };
114
- /** The capability arrays (`<c>Entities` / `<c>Migrations` of persistence/connection.ts) an expression lists. */
115
- function arraysIn(ctx, checker, expression, out, depth) {
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);
139
- }
140
- const registrationArrays = (ctx, checker, literal) => {
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;
148
- };
149
- const recordRegistration = (registered, arrays, connection, site) => {
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
- }
160
- };
161
- /**
162
- * Where the persistence arrays of every capability are registered: the object literals (`DatabaseModule.register` connections and the
163
- * migrate/cli app's) that list `<c>Entities` / `<c>Migrations` of a capability's `persistence/connection.ts` under a connection `name`.
164
- * The one walk BE_SCHEMA_OWNER and the bounded-context rules (R174 to R178) share, so a capability's context is decided once, by origin.
165
- *
166
- * @param {{kit: object, graph: object, persistenceOf: (rel: string) => ({root: string, capability: string, folder: string, below: string[]} | null)}} input
167
- * @returns {{registered: Map<string, {name: string, connections: Map<string, {file: object, node: object}>}>, registrations: number, all: Array<{file: object, node: object, connection: string | null}>}}
168
- * `registered`: capability root -> its name and the connections it is registered on (first site each).
169
- */
170
- export function collectRegistrations({ kit, graph, persistenceOf }) {
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 };
192
- }
193
- const PERSISTENCE_SLOT = 'be.persistence';
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). */
195
- const CONTEXT_TIERS = new Set(['domain', 'projections']);
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. */
197
- export const isPerConnection = (resolver, root, name) => root.split('/').includes('platform') && (resolver.slot(PERSISTENCE_SLOT)?.perConnection ?? []).includes(name);
198
- /**
199
- * `persistenceOf(rel)`: {root, capability, folder, below} of a be.persistence file (`folder` the directory below persistence/, '' at its root),
200
- * null for any other file. `root` is the persistence root, its dirname the capability root.
201
- */
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 };
208
- };
209
- /** The declared connection named by a decorator whose callee is declared in src/modules/platform/database/<conn>.decorators.ts, else null. */
210
- const connectionOfDecorators = (kit, declared, checker, declaration) => {
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
- }
222
- }
223
- return null;
224
- };
225
- const managerTarget = (ts, node) => {
226
- if (ts.isPropertyAccessExpression(node))
227
- return node.name;
228
- return ts.isIdentifier(node) ? node : null;
229
- };
230
- /** The receiver expression of the `.transaction(callback)` call whose first callback parameter is `declaration`, else null. */
231
- const transactionReceiver = (ts, declaration) => {
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;
239
- };
240
- /**
241
- * The declared connection whose entity manager `expression` is: a property or parameter injected with `Inject<Conn>EntityManager`, or the
242
- * manager parameter of the callback of a `.transaction(...)` on such a manager. Null when the origin cannot be proven (never guessed).
243
- */
244
- function connectionOfManagerIn(kit, declared, checker, expression, depth = 0) {
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;
262
- }
263
- /**
264
- * The bounded-context model of a program: which context (declared connection) each capability belongs to, and which connection an expression's
265
- * entity manager is. A context is a connection; a capability belongs to the one declared connection its persistence arrays are registered on
266
- * (collectRegistrations), a platform capability named by the manifest's `perConnection` list to none (it is infrastructure on every connection).
267
- * Everything is decided by declarations and origins: the registration literal, the `Inject<Conn>EntityManager` decorator's home file, the
268
- * transaction callback parameter's receiver. Nothing is matched by a variable name.
269
- */
270
- const MODELS = new WeakMap();
271
- export function contextModelOf(kit, graph) {
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;
308
- }
309
- const composedConnection = (kit, declared, checker, inner) => {
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;
318
- };
319
- const collectComposedConnections = (kit, declared, checker, call, file, items) => {
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
- }
329
- };
330
- /**
331
- * The declared connection names the composition of `app` (its root app.module.ts and the composition groups of compose/) passes to
332
- * the database module registration, as [{connection, node, file}] in path then source order.
333
- */
334
- export function connectionsComposedBy(kit, app) {
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 };
355
- }
@@ -1,102 +0,0 @@
1
- import { contextModelOf, connectionsComposedBy } from './context-map.mjs';
2
- import { machineKit } from './machine-ast.mjs';
3
- /**
4
- * R174 `context-owner` (BE_CONTEXT_OWNER). A connection of hfs.json IS a bounded context, and its `owner` is the one service app that composes it:
5
- * - an api or worker app passes to the database module registration (or lists in its options) only the connections it owns (a monolith's one
6
- * app owns them all);
7
- * - an api or worker app imports no domain or projection capability of a context it does not own: it composes only its own contexts;
8
- * - the cli app is the migration runner and composes every declared connection
9
- * that holds registered tables (migrations run only through it, once per connection).
10
- * Which connection an app composes is read from two typed origins in its own files: the connection passed to the database module registration
11
- * and the connection literal that lists a capability's `<c>Entities` / `<c>Migrations`. Which context a capability belongs to is the connection
12
- * its arrays are registered on (context-map.mjs); an import is judged by the owner graph edge and the target's slot tier, never by a path or
13
- * name pattern. A runner app with a registration whose connection name cannot be resolved is not judged for missing connections.
14
- */
15
- export const CONTEXT_OWNER_RULE_IDS = ['BE_CONTEXT_OWNER'];
16
- const RULE = 'BE_CONTEXT_OWNER';
17
- const SERVICE_KINDS = new Set(['api', 'worker']);
18
- const RUNNER_KINDS = new Set(['migrate', 'cli']);
19
- // What each app composes: [{connection, file, node}] from the database module registration and from the registration literals under its files.
20
- const compositionsOf = (kit, resolver, model, appOf) => {
21
- const apps = resolver.repo.apps;
22
- const composed = new Map(apps.map((app) => [app.name, { items: [], unresolved: false }]));
23
- for (const app of apps) {
24
- const entry = composed.get(app.name);
25
- if (!entry)
26
- continue;
27
- for (const item of connectionsComposedBy(kit, app.name).items)
28
- entry.items.push(item);
29
- }
30
- for (const site of model.registrations) {
31
- const app = appOf(site.file.rel);
32
- const entry = app ? composed.get(app) : undefined;
33
- if (!app || !entry)
34
- continue;
35
- if (site.connection === null)
36
- entry.unresolved = true;
37
- else
38
- entry.items.push({ connection: site.connection, file: site.file, node: site.node });
39
- }
40
- return composed;
41
- };
42
- // A service app composes only the connections it owns; a runner app composes every declared connection with tables.
43
- const judgeComposition = (app, { items, unresolved }, { owners, model, kit, once }) => {
44
- if (SERVICE_KINDS.has(app.kind)) {
45
- for (const { connection, file, node } of items) {
46
- const owner = owners.get(connection);
47
- if (owner && owner !== app.name) {
48
- once(`${app.name}|${connection}`, { ...kit.at(file.rel, file.sourceFile, node), message: `App ${app.name} composes connection ${connection}, the context owned by app ${owner}; an app composes only the contexts it owns. Reach that context through the API or the events of ${owner}.`, connection, app: app.name, owner });
49
- }
50
- }
51
- }
52
- else if (RUNNER_KINDS.has(app.kind) && !unresolved) {
53
- const root = kit.appRoot(app.name);
54
- const present = new Set(items.map(item => item.connection));
55
- const held = new Set([...model.registered.values()].flatMap(entry => [...entry.connections.keys()]));
56
- for (const connection of owners.keys()) {
57
- if (!root || present.has(connection) || !held.has(connection) || !items.length)
58
- continue;
59
- once(`${app.name}|missing|${connection}`, { ...kit.at(root.rel, root.sourceFile, root.sourceFile), message: `The ${app.kind} app ${app.name} does not compose connection ${connection}; migrations run only through it, once per connection, so it registers every declared connection that holds tables.`, connection, app: app.name });
60
- }
61
- }
62
- return items.length;
63
- };
64
- export function checkContextOwner(input) {
65
- const { graph } = input;
66
- const kit = machineKit(input);
67
- const { resolver } = kit;
68
- const model = contextModelOf(kit, graph);
69
- const violations = [];
70
- const connections = resolver.repo.connections;
71
- const apps = resolver.repo.apps;
72
- const owners = new Map(connections.map((connection) => [connection.name, connection.owner]));
73
- let compositions = 0;
74
- let imports = 0;
75
- const seen = new Set();
76
- const once = (key, violation) => { if (!seen.has(key)) {
77
- seen.add(key);
78
- violations.push({ ruleId: RULE, ...violation });
79
- } };
80
- const appOf = (rel) => { const classified = resolver.classifyPath(rel); return classified.slot?.startsWith('be.app.') ? classified.bindings?.app ?? null : null; };
81
- const composed = compositionsOf(kit, resolver, model, appOf);
82
- for (const app of apps) {
83
- const composition = composed.get(app.name);
84
- if (composition)
85
- compositions += judgeComposition(app, composition, { owners, model, kit, once });
86
- }
87
- const kinds = new Map(apps.map((app) => [app.name, app.kind]));
88
- for (const edge of graph.edges) {
89
- const app = appOf(edge.from);
90
- if (!app || !SERVICE_KINDS.has(kinds.get(app) ?? ''))
91
- continue;
92
- const context = model.contextOfFile(edge.to);
93
- if (!context)
94
- continue;
95
- imports += 1;
96
- const owner = owners.get(context);
97
- if (owner && owner !== app) {
98
- once(`${edge.from}|${edge.to}`, { path: edge.from, line: edge.line, column: edge.column, message: `App ${app} imports ${graph.files.get(edge.to)?.owner?.root}, a capability of context ${context} owned by app ${owner}; an app composes only the contexts it owns, so use the API or the events of ${owner} instead.`, connection: context, app, owner });
99
- }
100
- }
101
- return { violations, coverage: { status: 'checked', apps: apps.length, compositions, contextImports: imports } };
102
- }
@@ -1,65 +0,0 @@
1
- import { contextModelOf } from './context-map.mjs';
2
- import { machineKit } from './machine-ast.mjs';
3
- import { byCodeUnit } from '../../lib/list.mjs';
4
- /**
5
- * R178 `context-platform-tables` (BE_CONTEXT_PLATFORM_TABLES). A platform capability the manifest lists as `perConnection` (the event-bus outbox,
6
- * the job table) keeps its tables on EVERY connection that uses it: when a call into the capability passes the entity manager of connection C
7
- * (an `Inject<C>EntityManager` property, or the manager of a transaction on one), the capability's `<c>Entities` and `<c>Migrations` must be
8
- * registered on C, or the row the call writes has no table in C's database. The caller's connection is proven by the manager's origin; a call
9
- * whose manager origin cannot be proven is not judged.
10
- */
11
- export const CONTEXT_PLATFORM_TABLES_RULE_IDS = ['BE_CONTEXT_PLATFORM_TABLES'];
12
- const RULE = 'BE_CONTEXT_PLATFORM_TABLES';
13
- function perConnectionHome(kit, model, checker, file, node) {
14
- const { ts } = kit;
15
- const callee = ts.isPropertyAccessExpression(node.expression) ? node.expression.name : node.expression;
16
- const home = kit.declarationsOf(checker, callee).map(kit.ownerOfDeclaration).find(Boolean);
17
- if (!home || !model.perConnection.has(home.root) || file.owner?.root === home.root)
18
- return undefined;
19
- return home;
20
- }
21
- function unregisteredViolations(kit, model, scope, home, connections, seen) {
22
- const { file, node } = scope;
23
- const registeredOn = model.registered.get(home.root)?.connections ?? new Map();
24
- const violations = [];
25
- for (const connection of connections) {
26
- if (registeredOn.has(connection))
27
- continue;
28
- const key = `${file.rel}|${node.getStart()}|${connection}`;
29
- if (seen.has(key))
30
- continue;
31
- seen.add(key);
32
- violations.push({ ruleId: RULE, ...kit.at(file.rel, file.sourceFile, node), message: `${file.rel} passes the entity manager of connection ${connection} to ${home.name}, but the tables of ${home.name} are not registered on ${connection} (registered: ${[...registeredOn.keys()].sort(byCodeUnit).join(', ') || 'none'}); register its entities and migrations on ${connection} too, so the table exists in the database of that context.`, connection, capability: home.name });
33
- }
34
- return violations;
35
- }
36
- export function checkContextPlatformTables(input) {
37
- const { graph } = input;
38
- const kit = machineKit(input);
39
- const { ts } = kit;
40
- const model = contextModelOf(kit, graph);
41
- const violations = [];
42
- const counts = { calls: 0, unproven: 0 };
43
- if (!model.perConnection.size)
44
- return { violations, coverage: { status: 'checked', perConnectionCapabilities: 0, ...counts } };
45
- const seen = new Set();
46
- for (const file of graph.files.values()) {
47
- const checker = kit.checkerOf(file.sourceFile);
48
- kit.walk(file.sourceFile, (node) => {
49
- if (!ts.isCallExpression(node))
50
- return true;
51
- const home = perConnectionHome(kit, model, checker, file, node);
52
- if (!home)
53
- return true;
54
- counts.calls += 1;
55
- const connections = new Set(node.arguments.map(argument => model.connectionOfManager(checker, argument)).filter((connection) => Boolean(connection)));
56
- if (!connections.size) {
57
- counts.unproven += 1;
58
- return true;
59
- }
60
- violations.push(...unregisteredViolations(kit, model, { file, node }, home, connections, seen));
61
- return true;
62
- });
63
- }
64
- return { violations, coverage: { status: 'checked', perConnectionCapabilities: model.perConnection.size, ...counts } };
65
- }
@@ -1,75 +0,0 @@
1
- import { contextModelOf } from './context-map.mjs';
2
- import { machineKit } from './machine-ast.mjs';
3
- /**
4
- * R176 `context-transaction` (BE_CONTEXT_TRANSACTION). One transaction touches one context's connection. In the callback of
5
- * `<manager>.transaction(async (manager) => ...)`, where `<manager>` is proven to be the entity manager of connection C (an
6
- * `Inject<C>EntityManager` property or parameter), the code never:
7
- * - uses the entity manager of another declared connection (proven by its injector's home file); or
8
- * - calls a method of a domain or projection capability of another context (that capability writes through its own connection).
9
- * Work that spans contexts is a saga of one transaction per context, linked by events. A receiver whose connection cannot be proven is not judged.
10
- */
11
- export const CONTEXT_TRANSACTION_RULE_IDS = ['BE_CONTEXT_TRANSACTION'];
12
- const RULE = 'BE_CONTEXT_TRANSACTION';
13
- const SPAN_ADVICE = "one transaction touches one context's connection. Span contexts with a saga: one transaction per context, linked by events.";
14
- /** Reports an entity manager of another connection, or a call into another context's capability, found inside a transaction callback. */
15
- function inspectInner(scope, file, checker, connection, inner) {
16
- const { ts, kit, model, graph, report } = scope;
17
- if (ts.isPropertyAccessExpression(inner)) {
18
- const other = model.connectionOfManager(checker, inner);
19
- if (other && other !== connection)
20
- report(file, inner, `A transaction on connection ${connection} uses the entity manager of connection ${other}; ${SPAN_ADVICE}`, { connection, other });
21
- }
22
- if (!ts.isCallExpression(inner))
23
- return;
24
- const callee = ts.isPropertyAccessExpression(inner.expression) ? inner.expression.name : inner.expression;
25
- const declaration = kit.declarationsOf(checker, callee)[0];
26
- const rel = declaration ? kit.graphPath(declaration) : null;
27
- const context = rel ? model.contextOfFile(rel) : null;
28
- if (rel && context && context !== connection) {
29
- report(file, inner, `A transaction on connection ${connection} calls ${graph.files.get(rel)?.owner?.root} of context ${context}; ${SPAN_ADVICE}`, { connection, other: context });
30
- }
31
- }
32
- /** Judges a `.transaction(...)` call: counts it as proven or unproven and inspects its callback body; any other node is skipped. */
33
- function inspectTransaction(scope, file, checker, node) {
34
- const { ts, kit, model, counts } = scope;
35
- if (!(ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'transaction'))
36
- return;
37
- const callback = node.arguments.find(argument => ts.isArrowFunction(argument) || ts.isFunctionExpression(argument));
38
- if (!callback)
39
- return;
40
- const connection = model.connectionOfManager(checker, node.expression.expression);
41
- if (!connection) {
42
- counts.unproven += 1;
43
- return;
44
- }
45
- counts.transactions += 1;
46
- kit.walk(callback.body, inner => {
47
- inspectInner(scope, file, checker, connection, inner);
48
- return true;
49
- });
50
- }
51
- export function checkContextTransaction(input) {
52
- const { graph } = input;
53
- const kit = machineKit(input);
54
- const { ts } = kit;
55
- const model = contextModelOf(kit, graph);
56
- const violations = [];
57
- const counts = { transactions: 0, unproven: 0 };
58
- const seen = new Set();
59
- const report = (file, node, message, extra) => {
60
- const key = `${file.rel}|${node.getStart()}|${message}`;
61
- if (seen.has(key))
62
- return;
63
- seen.add(key);
64
- violations.push({ ruleId: RULE, ...kit.at(file.rel, file.sourceFile, node), message, ...extra });
65
- };
66
- const scope = { ts, kit, model, graph, report, counts };
67
- for (const file of graph.files.values()) {
68
- const checker = kit.checkerOf(file.sourceFile);
69
- kit.walk(file.sourceFile, node => {
70
- inspectTransaction(scope, file, checker, node);
71
- return true;
72
- });
73
- }
74
- return { violations, coverage: { status: 'checked', transactions: counts.transactions, unproven: counts.unproven } };
75
- }
@@ -1,72 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { dirname, resolve } from 'node:path';
3
- /** The generated input-bounds owner is contract data, not an operation. */
4
- const BOUNDS_MODULE = /(?:^|\/)__generated__\/input-bounds\.ts$/;
5
- /** Directly exported declarations with the requested name; multiple declarations are ambiguous. */
6
- function declaredExports(ts, source, name) {
7
- let count = 0;
8
- for (const statement of source.statements) {
9
- if (!ts.canHaveModifiers(statement) || !ts.getModifiers(statement)?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword))
10
- continue;
11
- if (ts.isVariableStatement(statement)) {
12
- count += statement.declarationList.declarations.filter(declaration => ts.isIdentifier(declaration.name) && declaration.name.text === name).length;
13
- }
14
- else if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isInterfaceDeclaration(statement) ||
15
- ts.isTypeAliasDeclaration(statement) || ts.isEnumDeclaration(statement)) && statement.name?.text === name)
16
- count += 1;
17
- }
18
- return count;
19
- }
20
- /** The concrete file a relative module specifier resolves to, in the same order as the rule's owner resolver. */
21
- function readSource(ts, target) {
22
- for (const file of [`${target}.ts`, `${target}/index.ts`, target]) {
23
- let text;
24
- try {
25
- text = readFileSync(file, 'utf8');
26
- }
27
- catch {
28
- continue;
29
- }
30
- const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true);
31
- if (source.parseDiagnostics.length)
32
- return null;
33
- return { file, source };
34
- }
35
- return null;
36
- }
37
- /** True only for an unambiguous named export proven through actual declarations to a generated contract declaration. */
38
- export function isBoundsExport(ts, target, name, ownsGenerated, seen = new Set()) {
39
- const found = readSource(ts, target);
40
- if (!found)
41
- return false;
42
- const { file, source } = found;
43
- const key = `${file}#${name}`;
44
- if (seen.has(key) || seen.size >= 32)
45
- return false;
46
- const direct = declaredExports(ts, source, name);
47
- const matches = [];
48
- let wildcard = false;
49
- for (const statement of source.statements) {
50
- if (!ts.isExportDeclaration(statement))
51
- continue;
52
- const module = statement.moduleSpecifier && ts.isStringLiteralLike(statement.moduleSpecifier) ? statement.moduleSpecifier.text : null;
53
- if (!statement.exportClause) {
54
- wildcard = true;
55
- continue;
56
- }
57
- if (!ts.isNamedExports(statement.exportClause))
58
- continue;
59
- for (const item of statement.exportClause.elements)
60
- if (item.name.text === name) {
61
- matches.push({ origin: (item.propertyName ?? item.name).text, module });
62
- }
63
- }
64
- if (wildcard || direct + matches.length !== 1)
65
- return false;
66
- if (BOUNDS_MODULE.test(file.replaceAll('\\', '/')))
67
- return ownsGenerated(file) && direct === 1 && matches.length === 0;
68
- const match = matches[0];
69
- if (direct || !match || !match.module?.startsWith('.'))
70
- return false;
71
- return isBoundsExport(ts, resolve(dirname(file), match.module), match.origin, ownsGenerated, new Set([...seen, key]));
72
- }