@starci/eslint-canon-be 3.8.19 → 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 +12 -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
@@ -3,7 +3,6 @@ import path from 'node:path';
3
3
  import { machineKit, pascal, upperSnake } from './machine-ast.mjs';
4
4
  import { locateDeclaration } from '../slots.mjs';
5
5
  import { byCodeUnit } from '../../lib/list.mjs';
6
-
7
6
  /**
8
7
  * R84 `connection-map` (BE_CONNECTION_DUPLICATE), folding RED01 and RED02. One physical database is one connection, one
9
8
  * `Inject<Conn>EntityManager()` injector, one config; the `hfs.json` connections, the connection files, the injectors, the
@@ -25,7 +24,6 @@ import { byCodeUnit } from '../../lib/list.mjs';
25
24
  * `isolation: schema` with different `<PREFIX>_SCHEMA` values (two contexts as schemas of one database).
26
25
  */
27
26
  export const CONNECTION_RULE_IDS = ['BE_CONNECTION_DUPLICATE'];
28
-
29
27
  const RULE = 'BE_CONNECTION_DUPLICATE';
30
28
  export const DATABASE_DIR = 'src/modules/platform/database';
31
29
  const TYPEORM = '@nestjs/typeorm';
@@ -33,285 +31,320 @@ const MANAGER_CALLS = new Set(['getEntityManagerToken', 'InjectEntityManager']);
33
31
  const SOURCE_CALLS = new Set(['getDataSourceToken', 'InjectDataSource']);
34
32
  const ENV_KEY = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$/u;
35
33
  const INJECTOR_NAME = /^Inject[A-Za-z0-9]*EntityManager$/u;
36
-
37
34
  const exportedInjectorName = (ts, kit, node) => {
38
- if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && ts.isVariableStatement(node.parent?.parent) && kit.isExported(node.parent.parent)) return node.name.text;
39
- if (ts.isFunctionDeclaration(node) && kit.isExported(node) && node.name) return node.name.text;
40
- return null;
35
+ const statement = node.parent?.parent;
36
+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && statement && ts.isVariableStatement(statement) && kit.isExported(statement))
37
+ return node.name.text;
38
+ if (ts.isFunctionDeclaration(node) && kit.isExported(node) && node.name)
39
+ return node.name.text;
40
+ return null;
41
41
  };
42
-
43
42
  const ENV_ASSIGNMENT = /^(?:export\s+)?([A-Za-z_]\w*)\s*=/u;
44
43
  const LINE_BREAK = /[\n\r\u2028\u2029]/u;
45
-
46
44
  // One `KEY=value` line: a value holding a line break after its first character is not an assignment.
47
45
  function parseEnvLine(raw) {
48
- const line = raw.trim();
49
- if (!line || line.startsWith('#')) return null;
50
- const match = ENV_ASSIGNMENT.exec(line);
51
- if (!match) return null;
52
- let value = line.slice(match[0].length).trim();
53
- if (LINE_BREAK.test(value)) return null;
54
- if (/^(['"]).*\1$/u.test(value)) value = value.slice(1, -1);
55
- return [match[1], value.trim()];
46
+ const line = raw.trim();
47
+ if (!line || line.startsWith('#'))
48
+ return null;
49
+ const match = ENV_ASSIGNMENT.exec(line);
50
+ if (!match)
51
+ return null;
52
+ const key = match[1];
53
+ if (key === undefined)
54
+ return null;
55
+ let value = line.slice(match[0].length).trim();
56
+ if (LINE_BREAK.test(value))
57
+ return null;
58
+ if (/^(['"]).*\1$/u.test(value))
59
+ value = value.slice(1, -1);
60
+ return [key, value.trim()];
56
61
  }
57
-
58
62
  function readEnvFile(file, merged) {
59
- let text;
60
- try { text = fs.readFileSync(file, 'utf8'); } catch { return; }
61
- for (const raw of text.split(/\r?\n/u)) {
62
- const parsed = parseEnvLine(raw);
63
- if (parsed) merged.set(parsed[0], parsed[1]);
64
- }
63
+ let text;
64
+ try {
65
+ text = fs.readFileSync(file, 'utf8');
66
+ }
67
+ catch {
68
+ return;
69
+ }
70
+ for (const raw of text.split(/\r?\n/u)) {
71
+ const parsed = parseEnvLine(raw);
72
+ if (parsed)
73
+ merged.set(parsed[0], parsed[1]);
74
+ }
65
75
  }
66
-
67
76
  function readEnvFiles(target) {
68
- const merged = new Map();
69
- let stat;
70
- try { stat = fs.statSync(target); } catch { return null; }
71
- if (stat.isFile()) readEnvFile(target, merged);
72
- else for (const entry of fs.readdirSync(target, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
73
- if (entry.isFile() && !entry.name.toLowerCase().endsWith('.md')) readEnvFile(path.join(target, entry.name), merged);
74
- }
75
- return merged;
77
+ const merged = new Map();
78
+ let stat;
79
+ try {
80
+ stat = fs.statSync(target);
81
+ }
82
+ catch {
83
+ return null;
84
+ }
85
+ if (stat.isFile())
86
+ readEnvFile(target, merged);
87
+ else
88
+ for (const entry of fs.readdirSync(target, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
89
+ if (entry.isFile() && !entry.name.toLowerCase().endsWith('.md'))
90
+ readEnvFile(path.join(target, entry.name), merged);
91
+ }
92
+ return merged;
76
93
  }
77
-
78
94
  const exportsVariable = (ts, kit, item, name) => ts.isVariableStatement(item) && kit.isExported(item)
79
- && item.declarationList.declarations.some(declaration => ts.isIdentifier(declaration.name) && declaration.name.text === name);
80
-
95
+ && item.declarationList.declarations.some(declaration => ts.isIdentifier(declaration.name) && declaration.name.text === name);
81
96
  const checkConnectionFile = (connection, kit, ts, report) => {
82
- const connectionRel = `${DATABASE_DIR}/${connection.name}.connection.ts`;
83
- const constant = `${upperSnake(connection.name)}_CONNECTION`;
84
- const connectionFile = kit.graphFile(connectionRel);
85
- if (!connectionFile) {
86
- report(connectionRel, `hfs.json declares connection ${connection.name} but ${connectionRel} does not exist; declare \`export const ${constant} = "${connection.name}"\` there.`, { connection: connection.name });
87
- return;
88
- }
89
- const checker = kit.checkerOf(connectionFile.sourceFile);
90
- const statement = connectionFile.sourceFile.statements.find(item => exportsVariable(ts, kit, item, constant));
91
- const initializer = statement?.declarationList.declarations.find(declaration => declaration.name.text === constant)?.initializer;
92
- if (!statement || kit.stringValue(checker, initializer) !== connection.name) {
93
- report(connectionRel, `${connectionRel} must export \`${constant} = "${connection.name}"\` (the connection name declared in hfs.json).`, { connection: connection.name });
94
- }
97
+ const connectionRel = `${DATABASE_DIR}/${connection.name}.connection.ts`;
98
+ const constant = `${upperSnake(connection.name)}_CONNECTION`;
99
+ const connectionFile = kit.graphFile(connectionRel);
100
+ if (!connectionFile) {
101
+ report(connectionRel, `hfs.json declares connection ${connection.name} but ${connectionRel} does not exist; declare \`export const ${constant} = "${connection.name}"\` there.`, { connection: connection.name });
102
+ return;
103
+ }
104
+ const checker = kit.checkerOf(connectionFile.sourceFile);
105
+ const statement = connectionFile.sourceFile.statements.find(item => exportsVariable(ts, kit, item, constant));
106
+ const initializer = statement?.declarationList.declarations.find(declaration => ts.isIdentifier(declaration.name) && declaration.name.text === constant)?.initializer;
107
+ if (!statement || kit.stringValue(checker, initializer) !== connection.name) {
108
+ report(connectionRel, `${connectionRel} must export \`${constant} = "${connection.name}"\` (the connection name declared in hfs.json).`, { connection: connection.name });
109
+ }
95
110
  };
96
-
97
111
  const checkInjectorFile = (connection, kit, ts, report, decoratorsFile) => {
98
- const decorators = kit.graphFile(decoratorsFile(connection.name));
99
- const injector = `Inject${pascal(connection.name)}EntityManager`;
100
- const exported = decorators?.sourceFile.statements.some(item => exportsVariable(ts, kit, item, injector)
101
- || (ts.isFunctionDeclaration(item) && kit.isExported(item) && item.name?.text === injector));
102
- if (!exported) report(decoratorsFile(connection.name), `Connection ${connection.name} needs ${decoratorsFile(connection.name)} exporting ${injector}, the one injector of its shared EntityManager.`, { connection: connection.name });
112
+ const decorators = kit.graphFile(decoratorsFile(connection.name));
113
+ const injector = `Inject${pascal(connection.name)}EntityManager`;
114
+ const exported = decorators?.sourceFile.statements.some(item => exportsVariable(ts, kit, item, injector)
115
+ || (ts.isFunctionDeclaration(item) && kit.isExported(item) && item.name?.text === injector));
116
+ if (!exported)
117
+ report(decoratorsFile(connection.name), `Connection ${connection.name} needs ${decoratorsFile(connection.name)} exporting ${injector}, the one injector of its shared EntityManager.`, { connection: connection.name });
103
118
  };
104
-
105
119
  const isProcessEnvAccess = (ts, node) => ts.isPropertyAccessExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'env'
106
- && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === 'process';
107
-
120
+ && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === 'process';
108
121
  const configEnvKey = (ts, node) => {
109
- if (ts.isStringLiteralLike(node) && ENV_KEY.test(node.text)) return node.text;
110
- if (isProcessEnvAccess(ts, node) && ENV_KEY.test(node.name.text)) return node.name.text;
111
- return null;
122
+ if (ts.isStringLiteralLike(node) && ENV_KEY.test(node.text))
123
+ return node.text;
124
+ if (isProcessEnvAccess(ts, node) && ENV_KEY.test(node.name.text))
125
+ return node.name.text;
126
+ return null;
112
127
  };
113
-
114
128
  const checkConfigFile = (connection, declared, kit, ts, report, configRel, configFile) => {
115
- let readsSchema = false;
116
- kit.walk(configFile.sourceFile, node => {
117
- const key = configEnvKey(ts, node);
118
- if (key === null) return true;
119
- if (key === `${connection.envPrefix}_SCHEMA`) readsSchema = true;
120
- const own = key.startsWith(`${connection.envPrefix}_`);
121
- const foreign = declared.some(other => other !== connection && key.startsWith(`${other.envPrefix}_`));
122
- if (!own || foreign) {
123
- report(configRel, `${configRel} reads ${key}; the config of connection ${connection.name} reads only ${connection.envPrefix}_* keys.`, { ...kit.at(configRel, configFile.sourceFile, node), connection: connection.name });
124
- }
125
- return false;
126
- });
127
- if (connection.isolation === 'schema' && !readsSchema) report(configRel, `Connection ${connection.name} is isolated as a schema of a shared database (hfs.json isolation), so ${configRel} must read ${connection.envPrefix}_SCHEMA and hand it to the data source; splitting the context out into its own database is then an env change only.`, { connection: connection.name });
129
+ let readsSchema = false;
130
+ kit.walk(configFile.sourceFile, node => {
131
+ const key = configEnvKey(ts, node);
132
+ if (key === null)
133
+ return true;
134
+ if (key === `${connection.envPrefix}_SCHEMA`)
135
+ readsSchema = true;
136
+ const own = key.startsWith(`${connection.envPrefix}_`);
137
+ const foreign = declared.some(other => other !== connection && key.startsWith(`${other.envPrefix}_`));
138
+ if (!own || foreign) {
139
+ report(configRel, `${configRel} reads ${key}; the config of connection ${connection.name} reads only ${connection.envPrefix}_* keys.`, { ...kit.at(configRel, configFile.sourceFile, node), connection: connection.name });
140
+ }
141
+ return false;
142
+ });
143
+ if (connection.isolation === 'schema' && !readsSchema)
144
+ report(configRel, `Connection ${connection.name} is isolated as a schema of a shared database (hfs.json isolation), so ${configRel} must read ${connection.envPrefix}_SCHEMA and hand it to the data source; splitting the context out into its own database is then an env change only.`, { connection: connection.name });
128
145
  };
129
-
130
146
  const checkDeclaredConnections = (declared, kit, ts, report, decoratorsFile) => {
131
- for (const connection of declared) {
132
- checkConnectionFile(connection, kit, ts, report);
133
- checkInjectorFile(connection, kit, ts, report, decoratorsFile);
134
- const configRel = `${DATABASE_DIR}/${connection.name}.config.ts`;
135
- const configFile = kit.graphFile(configRel);
136
- if (!configFile) {
137
- report(configRel, `Connection ${connection.name} needs ${configRel}, the one place its ${connection.envPrefix}_* keys are read.`, { connection: connection.name });
138
- continue;
147
+ for (const connection of declared) {
148
+ checkConnectionFile(connection, kit, ts, report);
149
+ checkInjectorFile(connection, kit, ts, report, decoratorsFile);
150
+ const configRel = `${DATABASE_DIR}/${connection.name}.config.ts`;
151
+ const configFile = kit.graphFile(configRel);
152
+ if (!configFile) {
153
+ report(configRel, `Connection ${connection.name} needs ${configRel}, the one place its ${connection.envPrefix}_* keys are read.`, { connection: connection.name });
154
+ continue;
155
+ }
156
+ checkConfigFile(connection, declared, kit, ts, report, configRel, configFile);
139
157
  }
140
- checkConfigFile(connection, declared, kit, ts, report, configRel, configFile);
141
- }
142
158
  };
143
-
144
159
  const reportUndeclaredDatabaseFiles = (databaseFiles, byName, report) => {
145
- for (const file of databaseFiles) {
146
- const match = /^(.+)\.(connection|decorators|config)\.ts$/u.exec(path.posix.basename(file.rel));
147
- if (match && path.posix.dirname(file.rel) === DATABASE_DIR && !byName.has(match[1])) {
148
- report(file.rel, `${file.rel} belongs to connection ${match[1]}, which hfs.json does not declare; declare the database in hfs.json connections or delete the file.`, { connection: match[1] });
160
+ for (const file of databaseFiles) {
161
+ const match = /^(.+)\.(connection|decorators|config)\.ts$/u.exec(path.posix.basename(file.rel));
162
+ const name = match?.[1];
163
+ if (match && name !== undefined && path.posix.dirname(file.rel) === DATABASE_DIR && !byName.has(name)) {
164
+ report(file.rel, `${file.rel} belongs to connection ${name}, which hfs.json does not declare; declare the database in hfs.json connections or delete the file.`, { connection: name });
165
+ }
149
166
  }
150
- }
151
167
  };
152
-
153
168
  const inspectManagerToken = (file, checker, node, binding, state) => {
154
- const { kit, byName, decoratorsFile, report, perDecorators } = state;
155
- const value = kit.stringValue(checker, node.arguments[0]);
156
- const home = value === null ? null : decoratorsFile(value);
157
- if (value === null || !byName.has(value)) {
158
- const target = value === null ? 'a connection that cannot be resolved to a constant' : `connection ${value}, which hfs.json does not declare`;
159
- report(file.rel, `${binding.name}() names ${target}; only the declared connections have an entity manager token.`, kit.at(file.rel, file.sourceFile, node));
160
- } else if (file.rel !== home) {
161
- report(file.rel, `${binding.name}(${value}) belongs in ${home} only; inject the shared manager with Inject${pascal(value)}EntityManager() instead of a second path to connection ${value}.`, { ...kit.at(file.rel, file.sourceFile, node), connection: value });
162
- } else {
163
- perDecorators.set(value, (perDecorators.get(value) ?? 0) + 1);
164
- if (perDecorators.get(value) > 1) report(file.rel, `${home} resolves connection ${value} more than once; one connection has one injector.`, { ...kit.at(file.rel, file.sourceFile, node), connection: value });
165
- }
169
+ const { kit, byName, decoratorsFile, report, perDecorators } = state;
170
+ const value = kit.stringValue(checker, node.arguments[0]);
171
+ const home = value === null ? null : decoratorsFile(value);
172
+ if (value === null || !byName.has(value)) {
173
+ const target = value === null ? 'a connection that cannot be resolved to a constant' : `connection ${value}, which hfs.json does not declare`;
174
+ report(file.rel, `${binding.name}() names ${target}; only the declared connections have an entity manager token.`, kit.at(file.rel, file.sourceFile, node));
175
+ }
176
+ else if (file.rel !== home) {
177
+ report(file.rel, `${binding.name}(${value}) belongs in ${home} only; inject the shared manager with Inject${pascal(value)}EntityManager() instead of a second path to connection ${value}.`, { ...kit.at(file.rel, file.sourceFile, node), connection: value });
178
+ }
179
+ else {
180
+ perDecorators.set(value, (perDecorators.get(value) ?? 0) + 1);
181
+ if ((perDecorators.get(value) ?? 0) > 1)
182
+ report(file.rel, `${home} resolves connection ${value} more than once; one connection has one injector.`, { ...kit.at(file.rel, file.sourceFile, node), connection: value });
183
+ }
166
184
  };
167
-
168
185
  const inspectDataSourceCall = (file, node, binding, state) => {
169
- const allowed = file.rel.startsWith(`${DATABASE_DIR}/`) || state.slotIs(file, 'be.app.cli') || state.slotIs(file, 'be.cli') || state.slotIs(file, 'be.tests.fixtures');
170
- if (!allowed) state.report(file.rel, `${binding.name}() reaches the DataSource outside platform/database and the cli; inject the shared EntityManager of the connection instead.`, state.kit.at(file.rel, file.sourceFile, node));
186
+ const allowed = file.rel.startsWith(`${DATABASE_DIR}/`) || state.slotIs(file, 'be.app.cli') || state.slotIs(file, 'be.cli') || state.slotIs(file, 'be.tests.fixtures');
187
+ if (!allowed)
188
+ state.report(file.rel, `${binding.name}() reaches the DataSource outside platform/database and the cli; inject the shared EntityManager of the connection instead.`, state.kit.at(file.rel, file.sourceFile, node));
171
189
  };
172
-
173
190
  const inspectTypeormCall = (file, checker, node, state) => {
174
- const binding = state.kit.importBinding(checker, node.expression);
175
- if (binding?.module === TYPEORM && MANAGER_CALLS.has(binding.name)) inspectManagerToken(file, checker, node, binding, state);
176
- else if (binding?.module === TYPEORM && SOURCE_CALLS.has(binding.name)) inspectDataSourceCall(file, node, binding, state);
191
+ const binding = state.kit.importBinding(checker, node.expression);
192
+ if (binding?.module === TYPEORM && MANAGER_CALLS.has(binding.name))
193
+ inspectManagerToken(file, checker, node, binding, state);
194
+ else if (binding?.module === TYPEORM && SOURCE_CALLS.has(binding.name))
195
+ inspectDataSourceCall(file, node, binding, state);
177
196
  };
178
-
179
197
  const inspectInjectorExport = (file, node, state) => {
180
- const { ts, kit, byName, decoratorsFile, report } = state;
181
- const exportedName = exportedInjectorName(ts, kit, node);
182
- if (!exportedName || !INJECTOR_NAME.test(exportedName)) return;
183
- const home = [...byName.keys()].find(name => `Inject${pascal(name)}EntityManager` === exportedName);
184
- if (!home || file.rel !== decoratorsFile(home)) {
185
- report(file.rel, `${exportedName} is exported here, but the only injector of an entity manager is Inject<Conn>EntityManager in src/modules/platform/database/<conn>.decorators.ts, one per declared connection.`, kit.at(file.rel, file.sourceFile, node));
186
- }
198
+ const { ts, kit, byName, decoratorsFile, report } = state;
199
+ const exportedName = exportedInjectorName(ts, kit, node);
200
+ if (!exportedName || !INJECTOR_NAME.test(exportedName))
201
+ return;
202
+ const home = [...byName.keys()].find(name => `Inject${pascal(name)}EntityManager` === exportedName);
203
+ if (!home || file.rel !== decoratorsFile(home)) {
204
+ report(file.rel, `${exportedName} is exported here, but the only injector of an entity manager is Inject<Conn>EntityManager in src/modules/platform/database/<conn>.decorators.ts, one per declared connection.`, kit.at(file.rel, file.sourceFile, node));
205
+ }
187
206
  };
188
-
189
207
  const inspectManagerNode = (file, checker, node, state) => {
190
- if (state.ts.isCallExpression(node)) inspectTypeormCall(file, checker, node, state);
191
- inspectInjectorExport(file, node, state);
192
- return true;
208
+ if (state.ts.isCallExpression(node))
209
+ inspectTypeormCall(file, checker, node, state);
210
+ inspectInjectorExport(file, node, state);
211
+ return true;
193
212
  };
194
-
195
213
  const checkManagerCalls = (graph, state) => {
196
- const perDecorators = new Map();
197
- const walkState = { ...state, perDecorators, slotIs: (file, id) => file.slot === id };
198
- for (const file of graph.files.values()) {
199
- const checker = state.kit.checkerOf(file.sourceFile);
200
- state.kit.walk(file.sourceFile, node => inspectManagerNode(file, checker, node, walkState));
201
- }
214
+ const perDecorators = new Map();
215
+ const walkState = { ...state, perDecorators, slotIs: (file, id) => file.slot === id };
216
+ for (const file of graph.files.values()) {
217
+ const checker = state.kit.checkerOf(file.sourceFile);
218
+ state.kit.walk(file.sourceFile, node => inspectManagerNode(file, checker, node, walkState));
219
+ }
202
220
  };
203
-
204
221
  /** The receiver of a `<receiver>.register(...)` call that `node` is, or null. */
205
222
  const registerReceiver = (ts, node) => {
206
- if (!ts.isCallExpression(node)) return null;
207
- const callee = node.expression;
208
- return ts.isPropertyAccessExpression(callee) && callee.name.text === 'register' ? callee.expression : null;
223
+ if (!ts.isCallExpression(node))
224
+ return null;
225
+ const callee = node.expression;
226
+ return ts.isPropertyAccessExpression(callee) && callee.name.text === 'register' ? callee.expression : null;
227
+ };
228
+ const isPropertyName = (ts, node) => {
229
+ const parent = node.parent;
230
+ if (!parent)
231
+ return false;
232
+ return (ts.isPropertyAssignment(parent) || ts.isPropertyAccessExpression(parent)) && parent.name === node;
209
233
  };
210
-
211
- const isPropertyName = (ts, node) => (ts.isPropertyAssignment(node.parent ?? {}) || ts.isPropertyAccessExpression(node.parent ?? {})) && node.parent.name === node;
212
-
213
234
  const passConnections = (argument, scope) => {
214
- const { ts, kit, checker, byName, passed, root, app, report } = scope;
215
- kit.walk(argument, inner => {
216
- if (isPropertyName(ts, inner)) return true;
217
- if (!(ts.isIdentifier(inner) || ts.isStringLiteralLike(inner) || ts.isPropertyAccessExpression(inner))) return true;
218
- const value = kit.stringValue(checker, inner);
219
- if (value === null || !byName.has(value)) return true;
220
- passed.set(value, (passed.get(value) ?? 0) + 1);
221
- if (passed.get(value) > 1) report(root.rel, `App ${app.name} passes connection ${value} to the database module registration more than once; one connection is registered once per app.`, { ...kit.at(root.rel, root.sourceFile, inner), connection: value, app: app.name });
222
- return false;
223
- });
235
+ const { ts, kit, checker, byName, passed, root, app, report } = scope;
236
+ kit.walk(argument, inner => {
237
+ if (isPropertyName(ts, inner))
238
+ return true;
239
+ if (!(ts.isIdentifier(inner) || ts.isStringLiteralLike(inner) || ts.isPropertyAccessExpression(inner)))
240
+ return true;
241
+ const value = kit.stringValue(checker, inner);
242
+ if (value === null || !byName.has(value))
243
+ return true;
244
+ passed.set(value, (passed.get(value) ?? 0) + 1);
245
+ if ((passed.get(value) ?? 0) > 1)
246
+ report(root.rel, `App ${app.name} passes connection ${value} to the database module registration more than once; one connection is registered once per app.`, { ...kit.at(root.rel, root.sourceFile, inner), connection: value, app: app.name });
247
+ return false;
248
+ });
224
249
  };
225
-
226
250
  const checkAppRegistrations = (config, kit, ts, byName, report) => {
227
- let registrations = 0;
228
- for (const app of config.apps) {
229
- // The root and its composition groups (compose/<area>.ts) are one composition: a connection is passed once across them.
230
- const passed = new Map();
231
- for (const root of kit.appCompositionFiles(app.name)) {
232
- const checker = kit.checkerOf(root.sourceFile);
233
- const scope = { ts, kit, checker, byName, passed, root, app, report };
234
- kit.walk(root.sourceFile, node => {
235
- const receiver = registerReceiver(ts, node);
236
- if (!receiver) return;
237
- const owner = kit.declarationsOf(checker, receiver).map(kit.ownerOfDeclaration).find(Boolean);
238
- if (owner?.tier !== 'platform' || owner.name !== 'database') return;
239
- registrations += 1;
240
- for (const argument of node.arguments) passConnections(argument, scope);
241
- });
251
+ let registrations = 0;
252
+ for (const app of config.apps) {
253
+ // The root and its composition groups (compose/<area>.ts) are one composition: a connection is passed once across them.
254
+ const passed = new Map();
255
+ for (const root of kit.appCompositionFiles(app.name)) {
256
+ const checker = kit.checkerOf(root.sourceFile);
257
+ const scope = { ts, kit, checker, byName, passed, root, app, report };
258
+ kit.walk(root.sourceFile, node => {
259
+ const receiver = registerReceiver(ts, node);
260
+ if (!receiver || !ts.isCallExpression(node))
261
+ return;
262
+ const owner = kit.declarationsOf(checker, receiver).map(kit.ownerOfDeclaration).find(Boolean);
263
+ if (owner?.tier !== 'platform' || owner.name !== 'database')
264
+ return;
265
+ registrations += 1;
266
+ for (const argument of node.arguments)
267
+ passConnections(argument, scope);
268
+ });
269
+ }
242
270
  }
243
- }
244
- return registrations;
271
+ return registrations;
245
272
  };
246
-
247
- const listStackEnvs = stacksRoot => {
248
- try { return fs.readdirSync(stacksRoot, { withFileTypes: true }).filter(entry => entry.isDirectory()).map(entry => entry.name).sort(byCodeUnit); } catch { return []; }
273
+ const listStackEnvs = (stacksRoot) => {
274
+ try {
275
+ return fs.readdirSync(stacksRoot, { withFileTypes: true }).filter(entry => entry.isDirectory()).map(entry => entry.name).sort(byCodeUnit);
276
+ }
277
+ catch {
278
+ return [];
279
+ }
249
280
  };
250
-
251
281
  // The resolved database of one connection in one stack env, or null while its host, port or name is unset.
252
282
  const resolveStackConnection = (kit, connection, values) => {
253
- const configFile = kit.graphFile(`${DATABASE_DIR}/${connection.name}.config.ts`);
254
- const nameKey = configFile?.sourceFile.text.includes(`${connection.envPrefix}_DATABASE`) ? `${connection.envPrefix}_DATABASE` : `${connection.envPrefix}_NAME`;
255
- const host = values.get(`${connection.envPrefix}_HOST`);
256
- const port = values.get(`${connection.envPrefix}_PORT`);
257
- const database = values.get(nameKey);
258
- if (!host || !port || !database) return null;
259
- const schema = connection.isolation === 'schema' ? (values.get(`${connection.envPrefix}_SCHEMA`) ?? '') : null;
260
- return { host, port, database, triple: `${host.toLowerCase()}|${port}|${database}`, schema };
283
+ const configFile = kit.graphFile(`${DATABASE_DIR}/${connection.name}.config.ts`);
284
+ const nameKey = configFile?.sourceFile.text.includes(`${connection.envPrefix}_DATABASE`) ? `${connection.envPrefix}_DATABASE` : `${connection.envPrefix}_NAME`;
285
+ const host = values.get(`${connection.envPrefix}_HOST`);
286
+ const port = values.get(`${connection.envPrefix}_PORT`);
287
+ const database = values.get(nameKey);
288
+ if (!host || !port || !database)
289
+ return null;
290
+ const schema = connection.isolation === 'schema' ? (values.get(`${connection.envPrefix}_SCHEMA`) ?? '') : null;
291
+ return { host, port, database, triple: `${host.toLowerCase()}|${port}|${database}`, schema };
261
292
  };
262
-
263
293
  // True when the env resolves at least one connection; false when it is skipped.
264
294
  const checkStackEnv = (config, kit, declared, report, stacksRoot, env) => {
265
- // Relative to the side folder the machine judges (`../.starcistacks/...` for a side): the app-relative path once the side is prefixed.
266
- const rel = path.relative(config.root, path.join(stacksRoot, env, 'runtime', 'env')).split(path.sep).join('/');
267
- const values = readEnvFiles(path.join(stacksRoot, env, 'runtime', 'env'));
268
- if (!values || values.size === 0) return false;
269
- const seen = new Map(); // host|port|database -> [{name, isolation, schema}]
270
- let resolved = 0;
271
- for (const connection of declared) {
272
- const found = resolveStackConnection(kit, connection, values);
273
- if (!found) continue;
274
- resolved += 1;
275
- const { host, port, database, triple, schema } = found;
276
- const sharing = seen.get(triple) ?? [];
277
- // Two contexts may share one database only as distinct, named schemas; a context that is its own database shares it with nobody.
278
- const clash = sharing.find(other => schema === null || other.schema === null || other.schema === schema);
279
- if (clash) {
280
- const schemaDetail = schema !== null && clash.schema === schema ? `, schema ${schema || '(none)'}` : '';
281
- report(rel, `Connections ${clash.name} and ${connection.name} resolve to the same database (${host}:${port}/${database}${schemaDetail}) in stack ${env}; contexts share a database only as distinct schemas (isolation schema, a different ${connection.envPrefix}_SCHEMA), otherwise one physical database is one connection.`, { connection: connection.name, env });
295
+ // Relative to the side folder the machine judges (`../.starcistacks/...` for a side): the app-relative path once the side is prefixed.
296
+ const rel = path.relative(config.root, path.join(stacksRoot, env, 'runtime', 'env')).split(path.sep).join('/');
297
+ const values = readEnvFiles(path.join(stacksRoot, env, 'runtime', 'env'));
298
+ if (!values || values.size === 0)
299
+ return false;
300
+ const seen = new Map(); // host|port|database -> [{name, isolation, schema}]
301
+ let resolved = 0;
302
+ for (const connection of declared) {
303
+ const found = resolveStackConnection(kit, connection, values);
304
+ if (!found)
305
+ continue;
306
+ resolved += 1;
307
+ const { host, port, database, triple, schema } = found;
308
+ const sharing = seen.get(triple) ?? [];
309
+ // Two contexts may share one database only as distinct, named schemas; a context that is its own database shares it with nobody.
310
+ const clash = sharing.find(other => schema === null || other.schema === null || other.schema === schema);
311
+ if (clash) {
312
+ const schemaDetail = schema !== null && clash.schema === schema ? `, schema ${schema || '(none)'}` : '';
313
+ report(rel, `Connections ${clash.name} and ${connection.name} resolve to the same database (${host}:${port}/${database}${schemaDetail}) in stack ${env}; contexts share a database only as distinct schemas (isolation schema, a different ${connection.envPrefix}_SCHEMA), otherwise one physical database is one connection.`, { connection: connection.name, env });
314
+ }
315
+ sharing.push({ name: connection.name, schema });
316
+ seen.set(triple, sharing);
282
317
  }
283
- sharing.push({ name: connection.name, schema });
284
- seen.set(triple, sharing);
285
- }
286
- return resolved > 0;
318
+ return resolved > 0;
287
319
  };
288
-
289
320
  const checkStackConnections = (config, kit, declared, report) => {
290
- let stacksChecked = 0;
291
- let stacksSkipped = 0;
292
- // .starcistacks sits at the app root: the side folder the machine judges reads its app's tree (locateDeclaration).
293
- const stacksRoot = path.join(locateDeclaration(config.root).appRoot, '.starcistacks');
294
- for (const env of listStackEnvs(stacksRoot)) {
295
- if (checkStackEnv(config, kit, declared, report, stacksRoot, env)) stacksChecked += 1; else stacksSkipped += 1;
296
- }
297
- return { stacksChecked, stacksSkipped };
321
+ let stacksChecked = 0;
322
+ let stacksSkipped = 0;
323
+ // .starcistacks sits at the app root: the side folder the machine judges reads its app's tree (locateDeclaration).
324
+ const stacksRoot = path.join(locateDeclaration(config.root).appRoot, '.starcistacks');
325
+ for (const env of listStackEnvs(stacksRoot)) {
326
+ if (checkStackEnv(config, kit, declared, report, stacksRoot, env))
327
+ stacksChecked += 1;
328
+ else
329
+ stacksSkipped += 1;
330
+ }
331
+ return { stacksChecked, stacksSkipped };
298
332
  };
299
-
300
333
  export function checkConnectionMap(input) {
301
- const { config, graph } = input;
302
- const kit = machineKit(input);
303
- const { ts } = kit;
304
- const declared = config.hfs.repo.connections ?? [];
305
- const byName = new Map(declared.map(connection => [connection.name, connection]));
306
- const violations = [];
307
- const report = (rel, message, extra = {}) => violations.push({ ruleId: RULE, path: rel, message, ...extra });
308
- const decoratorsFile = name => `${DATABASE_DIR}/${name}.decorators.ts`;
309
- const databaseFiles = [...graph.files.values()].filter(file => file.rel.startsWith(`${DATABASE_DIR}/`));
310
- const state = { config, graph, kit, ts, declared, byName, violations, report, decoratorsFile };
311
- checkDeclaredConnections(declared, kit, ts, report, decoratorsFile);
312
- reportUndeclaredDatabaseFiles(databaseFiles, byName, report);
313
- checkManagerCalls(graph, state);
314
- const registrations = checkAppRegistrations(config, kit, ts, byName, report);
315
- const { stacksChecked, stacksSkipped } = checkStackConnections(config, kit, declared, report);
316
- return { violations, coverage: { status: 'checked', connections: declared.length, registrations, stacksChecked, stacksSkipped } };
334
+ const { config, graph } = input;
335
+ const kit = machineKit(input);
336
+ const { ts } = kit;
337
+ const declared = config.hfs.repo.connections ?? [];
338
+ const byName = new Map(declared.map((connection) => [connection.name, connection]));
339
+ const violations = [];
340
+ const report = (rel, message, extra = {}) => { violations.push({ ruleId: RULE, path: rel, message, ...extra }); };
341
+ const decoratorsFile = name => `${DATABASE_DIR}/${name}.decorators.ts`;
342
+ const databaseFiles = [...graph.files.values()].filter(file => file.rel.startsWith(`${DATABASE_DIR}/`));
343
+ const state = { config, graph, kit, ts, declared, byName, violations, report, decoratorsFile };
344
+ checkDeclaredConnections(declared, kit, ts, report, decoratorsFile);
345
+ reportUndeclaredDatabaseFiles(databaseFiles, byName, report);
346
+ checkManagerCalls(graph, state);
347
+ const registrations = checkAppRegistrations(config, kit, ts, byName, report);
348
+ const { stacksChecked, stacksSkipped } = checkStackConnections(config, kit, declared, report);
349
+ return { violations, coverage: { status: 'checked', connections: declared.length, registrations, stacksChecked, stacksSkipped } };
317
350
  }