@starci/eslint-canon-be 3.18.3 → 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 +8 -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 -716
  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,177 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { checkerScope } from './required-files.mjs';
4
- import { allowsFile } from '../allows.mjs';
5
- import { unwrapEach } from './ast-walks.mjs';
6
- import { byCodeUnit } from '../../lib/list.mjs';
7
- /**
8
- * R47 `contract-fixture-guard` (BE_CONTRACT_UNGUARDED). A fake at the network edge serves payload fixtures; a fixture drifts
9
- * from the real provider silently unless a sandbox contract spec compares them. So every `fakes/<provider>/` of the test
10
- * world that holds payload fixtures (`payloads/*.json`) needs at least one `src/tests/contract/<provider>/*.contract-spec.ts`
11
- * (slot be.tests.contract) that, in its AST,
12
- * 1. references that provider's fixtures: a call whose argument is the provider's name as a string literal (the world's
13
- * fixture accessor, `renderPayload("<provider>", ...)`), an import from `fakes/<provider>/`, or a string that names
14
- * `fakes/<provider>/`; and
15
- * 2. compares shapes: an `expect(<... shapeOf(real) ...>).toEqual(<... shapeOf(fixture) ...>)` (or `toStrictEqual`), where
16
- * `shapeOf` is the helper the world vocabulary names (knowledge/hfs/slots.yaml ruleParams.be.contractShape.helper) and
17
- * is imported, never declared in the spec.
18
- * A fake with fixtures and no such spec is a finding at its first fixture. The tree is the tracked one over the world's own
19
- * slot; nothing names a path. Fakes the test-world library provides (declared in `test-world.config.ts`, held in the library
20
- * package) have no tracked fixtures here and are guarded by the library's own contract specs, so only the repository's own
21
- * `fakes/<provider>/` folders are judged.
22
- */
23
- export const CONTRACT_FIXTURE_GUARD_RULE_IDS = ['BE_CONTRACT_UNGUARDED'];
24
- const RULE = 'BE_CONTRACT_UNGUARDED';
25
- const WORLD_SLOT = 'be.tests.world';
26
- const CONTRACT_SLOT = 'be.tests.contract';
27
- const FAKES_DIRECTORY = 'fakes/';
28
- const PAYLOADS = /^([^/]+)\/payloads\/[^/]+\.json$/u;
29
- const SHAPE_MATCHERS = new Set(['toEqual', 'toStrictEqual']);
30
- const unwrap = (ts, node) => unwrapEach(ts, node, [ts.isParenthesizedExpression, ts.isAsExpression, ts.isNonNullExpression, ts.isAwaitExpression]);
31
- /** Every node under `root`, itself included. */
32
- function walk(ts, root, visit) {
33
- const step = (node) => { visit(node); ts.forEachChild(node, step); };
34
- step(root);
35
- }
36
- const importsFakesOf = (from, provider) => from.includes(`${FAKES_DIRECTORY}${provider}/`) || from.endsWith(`${FAKES_DIRECTORY}${provider}`);
37
- const shapeNamesOf = (ts, statement, helper) => {
38
- const named = statement.importClause?.namedBindings;
39
- if (!named || !ts.isNamedImports(named))
40
- return [];
41
- return named.elements.filter(element => (element.propertyName ?? element.name).text === helper).map(element => element.name.text);
42
- };
43
- const importsOfSpec = (ts, sourceFile, helper, providers) => {
44
- const shapeNames = new Set();
45
- const referenced = new Set();
46
- for (const statement of sourceFile.statements) {
47
- if (!ts.isImportDeclaration(statement))
48
- continue;
49
- // The module specifier of an import declaration is a string literal by the grammar.
50
- const from = statement.moduleSpecifier.text;
51
- for (const provider of providers)
52
- if (importsFakesOf(from, provider))
53
- referenced.add(provider);
54
- for (const name of shapeNamesOf(ts, statement, helper))
55
- shapeNames.add(name);
56
- }
57
- return { shapeNames, referenced };
58
- };
59
- const callsShape = (ts, node, shapeNames) => {
60
- let found = false;
61
- walk(ts, node, inner => { if (ts.isCallExpression(inner) && ts.isIdentifier(inner.expression) && shapeNames.has(inner.expression.text))
62
- found = true; });
63
- return found;
64
- };
65
- const TEST_BLOCKS = new Set(['describe', 'it', 'test']);
66
- const referenceProviders = (ts, node, providers, referenced) => {
67
- if (ts.isStringLiteralLike(node))
68
- for (const provider of providers)
69
- if (node.text.includes(`${FAKES_DIRECTORY}${provider}/`))
70
- referenced.add(provider);
71
- if (!ts.isCallExpression(node))
72
- return;
73
- const named = ts.isIdentifier(node.expression) ? node.expression.text : null;
74
- for (const argument of node.arguments) {
75
- const value = unwrap(ts, argument);
76
- if (value && ts.isStringLiteralLike(value) && providers.has(value.text) && !TEST_BLOCKS.has(named))
77
- referenced.add(value.text);
78
- }
79
- };
80
- /** The `expect(...)` call a shape matcher call hangs off (through `.not` / `.resolves`), or null. */
81
- const expectReceiverOf = (ts, callee) => {
82
- let receiver = unwrap(ts, callee.expression);
83
- while (receiver && ts.isPropertyAccessExpression(receiver))
84
- receiver = unwrap(ts, receiver.expression);
85
- return receiver && ts.isCallExpression(receiver) && ts.isIdentifier(receiver.expression) && receiver.expression.text === 'expect' ? receiver : null;
86
- };
87
- const comparesShapes = (ts, node, shapeNames) => {
88
- if (!ts.isCallExpression(node))
89
- return false;
90
- const callee = node.expression;
91
- if (!ts.isPropertyAccessExpression(callee) || !SHAPE_MATCHERS.has(callee.name.text))
92
- return false;
93
- const receiver = expectReceiverOf(ts, callee);
94
- if (!receiver)
95
- return false;
96
- const [actual] = receiver.arguments;
97
- const [expected] = node.arguments;
98
- return Boolean(actual && expected && callsShape(ts, actual, shapeNames) && callsShape(ts, expected, shapeNames));
99
- };
100
- const referenceAndCompare = (ts, node, providers, referenced, shapeNames) => {
101
- referenceProviders(ts, node, providers, referenced);
102
- return comparesShapes(ts, node, shapeNames);
103
- };
104
- /** Facts one contract spec establishes: the providers whose fixtures it references and whether it compares shapes. */
105
- function readSpec(ts, sourceFile, helper, providers) {
106
- const { shapeNames, referenced } = importsOfSpec(ts, sourceFile, helper, providers);
107
- let compares = false;
108
- walk(ts, sourceFile, node => { if (referenceAndCompare(ts, node, providers, referenced, shapeNames))
109
- compares = true; });
110
- return { referenced, compares };
111
- }
112
- /** The payload fixture a file of the world slot is, as `[provider, file]`, or null. */
113
- const payloadFixtureOf = (resolver, file) => {
114
- const verdict = allowsFile(resolver, file);
115
- if (verdict?.slot !== WORLD_SLOT || !verdict.relative.startsWith(FAKES_DIRECTORY))
116
- return null;
117
- const match = PAYLOADS.exec(verdict.relative.slice(FAKES_DIRECTORY.length));
118
- const provider = match?.[1];
119
- return provider === undefined ? null : [provider, file];
120
- };
121
- const fixtureFilesOf = (tree, resolver) => {
122
- const fixtures = new Map(); // provider -> first payload fixture file
123
- const specs = new Map(); // provider -> [contract spec file]
124
- for (const file of [...tree.files].sort(byCodeUnit)) {
125
- const classified = resolver.classifyPath(file);
126
- if (classified.status !== 'owned')
127
- continue;
128
- if (classified.slot === CONTRACT_SLOT) {
129
- const provider = classified.bindings?.provider;
130
- if (provider && file.endsWith('.contract-spec.ts'))
131
- specs.set(provider, [...(specs.get(provider) ?? []), file]);
132
- continue;
133
- }
134
- const fixture = classified.slot === WORLD_SLOT ? payloadFixtureOf(resolver, file) : null;
135
- if (fixture && !fixtures.has(fixture[0]))
136
- fixtures.set(...fixture);
137
- }
138
- return { fixtures, specs };
139
- };
140
- const guardedProvidersOf = (config, ts, contractShape, providers, specs) => {
141
- const guarded = new Set();
142
- for (const [provider, files] of specs) {
143
- for (const file of files) {
144
- const absolute = path.join(config.root, ...file.split('/'));
145
- if (!fs.existsSync(absolute))
146
- continue;
147
- const sourceFile = ts.createSourceFile(absolute, fs.readFileSync(absolute, 'utf8'), ts.ScriptTarget.Latest, true); // the contract tree is outside the root program
148
- const facts = readSpec(ts, sourceFile, contractShape.helper, providers);
149
- if (facts.compares && facts.referenced.has(provider))
150
- guarded.add(provider);
151
- }
152
- }
153
- return guarded;
154
- };
155
- export function checkContractFixtureGuard(input) {
156
- const { config, ts, tree } = checkerScope(input);
157
- const { resolver } = input.graph;
158
- // reason: the machine runs this check on a back-end tree, whose scope resolver always carries rule parameters
159
- const { contractShape } = resolver.ruleParams();
160
- const violations = [];
161
- const { fixtures, specs } = fixtureFilesOf(tree, resolver);
162
- const providers = new Set(fixtures.keys());
163
- const guarded = guardedProvidersOf(config, ts, contractShape, providers, specs);
164
- for (const [provider, file] of [...fixtures].sort(([a], [b]) => a.localeCompare(b))) {
165
- if (guarded.has(provider))
166
- continue;
167
- const has = specs.get(provider)?.length ?? 0;
168
- const why = has === 0
169
- ? `there is no src/tests/contract/${provider}/*.contract-spec.ts`
170
- : `none of the ${has} contract spec(s) under src/tests/contract/${provider}/ both references the ${provider} fixtures and asserts expect(${contractShape.helper}(real)).toEqual(${contractShape.helper}(fixture))`;
171
- violations.push({
172
- ruleId: RULE, path: file, line: 1, column: 1, slot: WORLD_SLOT,
173
- message: `fakes/${provider}/ serves payload fixtures but no contract spec guards them: ${why}. A fixture that is not compared with the real provider drifts silently; add the sandbox contract spec (the contract helper of the world skips it when the sandbox config is absent).`,
174
- });
175
- }
176
- return { violations, coverage: { status: 'checked', fixtureFakes: fixtures.size, contractSpecs: [...specs.values()].flat().length } };
177
- }
@@ -1,360 +0,0 @@
1
- import path from 'node:path';
2
- import { relativePath, unwrapExpression } from './typescript.mjs';
3
- import { anyDescendant, nodeDecorators, normalizedSymbol, normalizedSymbolValue, valueSymbol, violation } from './ast-walks.mjs';
4
- import { hasAnyFlag, isConstVariable, sourceLocation } from '../../lib/ts-ast.mjs';
5
- export const READONLY_BOUNDARY_RULE_ID = 'BE_READONLY_BOUNDARY';
6
- export function canonical(file) {
7
- return path.resolve(file);
8
- }
9
- export function modifiers(ts, node) {
10
- return ts.canHaveModifiers(node) ? ts.getModifiers(node) ?? [] : node.modifiers ?? [];
11
- }
12
- export function hasModifier(ts, node, kind) {
13
- return modifiers(ts, node).some(modifier => modifier.kind === kind);
14
- }
15
- export function isPublicMember(ts, node) {
16
- return !hasModifier(ts, node, ts.SyntaxKind.PrivateKeyword) && !hasModifier(ts, node, ts.SyntaxKind.ProtectedKeyword);
17
- }
18
- function readonlyMember(ts, node) {
19
- return hasModifier(ts, node, ts.SyntaxKind.ReadonlyKeyword);
20
- }
21
- function propertyName(ts, node) {
22
- const name = node.name;
23
- if (ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isPrivateIdentifier(name))
24
- return name.text;
25
- return null;
26
- }
27
- function thisOriginStatus(ts, checker, input, seen = new Set(), depth = 0) {
28
- if (!input || depth > 8)
29
- return 'no';
30
- const expression = unwrapExpression(ts, input);
31
- if (expression.kind === ts.SyntaxKind.ThisKeyword)
32
- return 'yes';
33
- const symbol = normalizedSymbol(ts, checker, expression);
34
- if (!symbol || seen.has(symbol))
35
- return 'no';
36
- const declarations = symbol.getDeclarations?.() ?? [];
37
- const [only] = declarations;
38
- if (declarations.length !== 1 || !only || !ts.isVariableDeclaration(only) || !only.initializer)
39
- return 'no';
40
- const origin = thisOriginStatus(ts, checker, only.initializer, new Set(seen).add(symbol), depth + 1);
41
- if (origin === 'no')
42
- return 'no';
43
- return isConstVariable(ts, only) ? origin : 'unavailable';
44
- }
45
- function instanceAssignmentTarget(ts, checker, input) {
46
- const expression = unwrapExpression(ts, input);
47
- if (!ts.isPropertyAccessExpression(expression) && !ts.isElementAccessExpression(expression))
48
- return { instance: false };
49
- const owner = thisOriginStatus(ts, checker, expression.expression);
50
- if (owner === 'no')
51
- return { instance: false };
52
- if (owner === 'unavailable')
53
- return { instance: true, name: null, supported: false };
54
- if (ts.isPropertyAccessExpression(expression))
55
- return { instance: true, name: expression.name.text, supported: true };
56
- if (ts.isStringLiteralLike(expression.argumentExpression))
57
- return { instance: true, name: expression.argumentExpression.text, supported: true };
58
- return { instance: true, name: null, supported: false };
59
- }
60
- function expressionOrigin(ts, checker, input, seen = new Set(), depth = 0) {
61
- if (!input || depth > 8)
62
- return null;
63
- const expression = unwrapExpression(ts, input);
64
- const shorthand = ts.isShorthandPropertyAssignment(expression) ? checker.getShorthandAssignmentValueSymbol?.(expression) : null;
65
- const symbol = normalizedSymbolValue(ts, checker, shorthand) ?? normalizedSymbol(ts, checker, expression);
66
- if (!symbol || seen.has(symbol))
67
- return symbol;
68
- const declarations = symbol.getDeclarations?.() ?? [];
69
- const [only] = declarations;
70
- if (declarations.length === 1 && only && ts.isVariableDeclaration(only) && only.initializer
71
- && isConstVariable(ts, only)) {
72
- return expressionOrigin(ts, checker, only.initializer, new Set(seen).add(symbol), depth + 1);
73
- }
74
- return symbol;
75
- }
76
- const expressionReferencesOrigin = (ts, checker, expression, expected) => anyDescendant(ts, expression, node => expressionOrigin(ts, checker, node) === expected);
77
- function isSafePrimitiveProjection(ts, checker, input, expected) {
78
- const expression = unwrapExpression(ts, input);
79
- if (!ts.isPropertyAccessExpression(expression) && !ts.isElementAccessExpression(expression))
80
- return false;
81
- if (expressionOrigin(ts, checker, expression.expression) !== expected)
82
- return false;
83
- const type = checker.getTypeAtLocation(expression);
84
- const primitive = ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike | ts.TypeFlags.BooleanLike
85
- | ts.TypeFlags.BigIntLike | ts.TypeFlags.ESSymbolLike | ts.TypeFlags.Null | ts.TypeFlags.Undefined
86
- | ts.TypeFlags.Void | ts.TypeFlags.Never;
87
- return hasAnyFlag(type.flags, primitive) && !hasAnyFlag(type.flags, ts.TypeFlags.Any | ts.TypeFlags.Unknown);
88
- }
89
- function assignmentPatternHasInstanceTarget(ts, checker, expression) {
90
- let found = false;
91
- const visit = (node) => {
92
- if (found)
93
- return;
94
- const target = instanceAssignmentTarget(ts, checker, node);
95
- if (target.instance) {
96
- found = true;
97
- return;
98
- }
99
- ts.forEachChild(node, visit);
100
- };
101
- visit(expression);
102
- return found;
103
- }
104
- function globalLibraryIdentifier(ts, checker, node, expected) {
105
- if (!ts.isIdentifier(node) || node.text !== expected)
106
- return false;
107
- const symbol = normalizedSymbol(ts, checker, node);
108
- const declarations = symbol?.getDeclarations?.() ?? [];
109
- return declarations.length > 0 && declarations.every(declaration => declaration.getSourceFile().isDeclarationFile
110
- && /(?:^|[/\\])lib\.[^/\\]+\.d\.ts$/i.test(declaration.getSourceFile().fileName));
111
- }
112
- function instanceMutationCall(ts, checker, node) {
113
- if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression))
114
- return null;
115
- const owner = node.expression.expression;
116
- const method = node.expression.name.text;
117
- const known = (method === 'assign' || method === 'defineProperty') && globalLibraryIdentifier(ts, checker, owner, 'Object')
118
- || method === 'set' && globalLibraryIdentifier(ts, checker, owner, 'Reflect');
119
- if (!known || thisOriginStatus(ts, checker, node.arguments[0]) === 'no')
120
- return null;
121
- return { node, values: node.arguments.slice(method === 'assign' ? 1 : 2) };
122
- }
123
- const isFunctionBoundary = (ts, node) => ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)
124
- || ts.isArrowFunction(node) || ts.isMethodDeclaration(node);
125
- /** The instance assignment a `<left> = <origin-derived>` expression performs, or null when its target is no instance member. */
126
- function instanceAssignment(ts, checker, node) {
127
- const target = instanceAssignmentTarget(ts, checker, node.left);
128
- if (target.instance) {
129
- return { node, name: target.name, supported: target.supported && node.operatorToken.kind === ts.SyntaxKind.EqualsToken };
130
- }
131
- return assignmentPatternHasInstanceTarget(ts, checker, node.left) ? { node, name: null, supported: false } : null;
132
- }
133
- /** The assignment or mutation call at `node` that stores the constructor parameter on the instance, or null. */
134
- function constructorFinding(ts, checker, node, parameterSymbol) {
135
- if (ts.isBinaryExpression(node) && expressionReferencesOrigin(ts, checker, node.right, parameterSymbol)
136
- && !isSafePrimitiveProjection(ts, checker, node.right, parameterSymbol)) {
137
- return instanceAssignment(ts, checker, node);
138
- }
139
- const mutation = instanceMutationCall(ts, checker, node);
140
- return mutation?.values.some(argument => expressionReferencesOrigin(ts, checker, argument, parameterSymbol))
141
- ? { node, name: null, supported: false }
142
- : null;
143
- }
144
- function constructorAssignments(ts, checker, constructor, parameter) {
145
- if (!constructor.body)
146
- return [];
147
- const parameterSymbol = normalizedSymbol(ts, checker, parameter.name);
148
- const assignments = [];
149
- const visit = (node) => {
150
- if (node !== constructor.body && isFunctionBoundary(ts, node))
151
- return;
152
- const finding = constructorFinding(ts, checker, node, parameterSymbol);
153
- if (finding)
154
- assignments.push(finding);
155
- else
156
- ts.forEachChild(node, visit);
157
- };
158
- visit(constructor.body);
159
- return assignments;
160
- }
161
- function checkReadonlyProperty(config, context, sourceFile, node, label, violations) {
162
- if (!readonlyMember(context.ts, node))
163
- violations.push(violation(config, sourceFile, node.name ?? node, READONLY_BOUNDARY_RULE_ID, `${label} must be readonly; execution state must not mutate an injected dependency or received message.`));
164
- }
165
- function classValuesForSymbol(ts, symbol, localFiles) {
166
- return (symbol?.getDeclarations?.() ?? []).flatMap((declaration) => {
167
- if (ts.isClassDeclaration(declaration))
168
- return [declaration];
169
- if (ts.isVariableDeclaration(declaration) && declaration.initializer) {
170
- const initializer = unwrapExpression(ts, declaration.initializer);
171
- if (ts.isClassExpression(initializer))
172
- return [initializer];
173
- }
174
- return [];
175
- }).filter(declaration => localFiles.has(canonical(declaration.getSourceFile().fileName)));
176
- }
177
- export function messageClass(config, context, checker, decorator, localFiles, reasons) {
178
- const expression = decorator.expression;
179
- const messageArgument = context.ts.isCallExpression(expression) ? expression.arguments[0] : undefined;
180
- if (!messageArgument) {
181
- reasons.push(`${relativePath(config.root, decorator.getSourceFile().fileName)} has a handler decorator without a static message class`);
182
- return null;
183
- }
184
- const symbol = valueSymbol(context.ts, checker, messageArgument);
185
- const declarations = classValuesForSymbol(context.ts, symbol, localFiles);
186
- if (declarations.length !== 1) {
187
- reasons.push(`${relativePath(config.root, decorator.getSourceFile().fileName)} cannot resolve one local message class from its handler decorator`);
188
- return null;
189
- }
190
- return declarations[0] ?? null;
191
- }
192
- function inspectMessageConstructor(state, constructor) {
193
- const { config, context, checker, sourceFile, violations, reasons } = state;
194
- const ts = context.ts;
195
- for (const parameter of constructor.parameters) {
196
- if (modifiers(ts, parameter).some(modifier => [ts.SyntaxKind.PublicKeyword,
197
- ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.ReadonlyKeyword].includes(modifier.kind))) {
198
- checkReadonlyProperty(config, context, sourceFile, parameter, `Message field ${propertyName(ts, parameter) ?? '(computed)'}`, violations);
199
- }
200
- }
201
- if (!constructor.body)
202
- return;
203
- const inspectAssign = (node) => {
204
- if (node !== constructor.body && (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isArrowFunction(node)))
205
- return;
206
- if (instanceMutationCall(ts, checker, node)) {
207
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} constructs message fields through an unsupported instance mutation`);
208
- }
209
- else if (ts.isBinaryExpression(node) && assignmentPatternHasInstanceTarget(ts, checker, node.left)
210
- && !instanceAssignmentTarget(ts, checker, node.left).supported) {
211
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} constructs message fields through an unsupported instance assignment`);
212
- }
213
- else
214
- ts.forEachChild(node, inspectAssign);
215
- };
216
- inspectAssign(constructor.body);
217
- }
218
- function inspectMessageMembers(state) {
219
- const { config, context, declaration, violations, reasons } = state;
220
- const ts = context.ts;
221
- const sourceFile = declaration.getSourceFile();
222
- const classState = { ...state, sourceFile };
223
- for (const member of declaration.members) {
224
- if (ts.isPropertyDeclaration(member) && !hasModifier(ts, member, ts.SyntaxKind.StaticKeyword)) {
225
- checkReadonlyProperty(config, context, sourceFile, member, `Message field ${propertyName(ts, member) ?? '(computed)'}`, violations);
226
- }
227
- if (ts.isSetAccessorDeclaration(member))
228
- violations.push(violation(config, sourceFile, member.name, READONLY_BOUNDARY_RULE_ID, `Message field ${propertyName(ts, member) ?? '(computed)'} cannot expose a setter.`));
229
- if (ts.isIndexSignatureDeclaration(member))
230
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} has a message index signature whose mutation boundary cannot be proved`);
231
- if (ts.isConstructorDeclaration(member))
232
- inspectMessageConstructor(classState, member);
233
- }
234
- }
235
- function classCarriesState(ts, declaration) {
236
- return declaration.members.some(member => (ts.isPropertyDeclaration(member) && !hasModifier(ts, member, ts.SyntaxKind.StaticKeyword))
237
- || ts.isSetAccessorDeclaration(member) || ts.isIndexSignatureDeclaration(member)
238
- || (ts.isConstructorDeclaration(member) && member.parameters.length > 0));
239
- }
240
- function classInstanceType(ts, checker, declaration) {
241
- const symbol = declaration.name ? normalizedSymbol(ts, checker, declaration.name) : null;
242
- if (symbol && ts.isClassDeclaration(declaration))
243
- return checker.getDeclaredTypeOfSymbol(symbol);
244
- const value = checker.getTypeAtLocation(declaration);
245
- const constructor = checker.getSignaturesOfType(value, ts.SignatureKind.Construct)[0];
246
- return constructor ? checker.getReturnTypeOfSignature(constructor) : value;
247
- }
248
- function inspectMessageBases(state) {
249
- const { config, context, checker, declaration, localFiles, sourceFile, reasons } = state;
250
- const ts = context.ts;
251
- const type = classInstanceType(ts, checker, declaration);
252
- // reason: a class symbol's declared type is an interface type; the construct-signature fallback is read the same way.
253
- for (const base of type && checker.getBaseTypes ? checker.getBaseTypes(type) : []) {
254
- const declarations = (base.symbol?.getDeclarations?.() ?? []).filter((item) => ts.isClassDeclaration(item)) ?? [];
255
- const [base0] = declarations;
256
- if (declarations.length !== 1 || !base0) {
257
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} inherits message fields from a class outside the checked production program`);
258
- }
259
- else if (localFiles.has(canonical(base0.getSourceFile().fileName))) {
260
- visitMessageClass({ ...state, declaration: base0, sourceFile: base0.getSourceFile() });
261
- }
262
- else if (classCarriesState(ts, base0)) {
263
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} inherits message state from a class outside the checked production program`);
264
- }
265
- }
266
- }
267
- function visitMessageClass(state) {
268
- const { declaration, visited } = state;
269
- const key = `${canonical(declaration.getSourceFile().fileName)}:${declaration.pos}`;
270
- if (visited.has(key))
271
- return;
272
- visited.add(key);
273
- const classState = { ...state, sourceFile: declaration.getSourceFile() };
274
- inspectMessageMembers(classState);
275
- inspectMessageBases(classState);
276
- }
277
- export function checkMessageReadonly(config, context, checker, declaration, localFiles, violations, reasons) {
278
- visitMessageClass({ config, context, checker, declaration, localFiles, visited: new Set(), violations, reasons });
279
- }
280
- function inspectInjectedParameter(state, member, parameter) {
281
- const { config, context, checker, sourceFile, classKind, properties, framework, violations, reasons } = state;
282
- const ts = context.ts;
283
- const explicitInject = nodeDecorators(ts, parameter).some(decorator => framework.decoratorKind(ts, checker, decorator, framework.targets) === 'Inject');
284
- if (!classKind && !explicitInject)
285
- return;
286
- const parameterProperty = modifiers(ts, parameter).some(modifier => [ts.SyntaxKind.PublicKeyword, ts.SyntaxKind.PrivateKeyword,
287
- ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.ReadonlyKeyword].includes(modifier.kind));
288
- if (parameterProperty) {
289
- checkReadonlyProperty(config, context, sourceFile, parameter, `Injected dependency ${propertyName(ts, parameter) ?? '(computed)'}`, violations);
290
- return;
291
- }
292
- for (const assignment of constructorAssignments(ts, checker, member, parameter)) {
293
- if (!assignment.supported || !assignment.name) {
294
- reasons.push(`${relativePath(config.root, sourceFile.fileName)}:${sourceLocation(sourceFile, assignment.node).line} stores an injected dependency through an unsupported instance assignment`);
295
- continue;
296
- }
297
- const property = properties.get(assignment.name);
298
- if (!property)
299
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} assigns injected dependency ${assignment.name} without a declared field`);
300
- else
301
- checkReadonlyProperty(config, context, sourceFile, property, `Injected dependency ${assignment.name}`, violations);
302
- }
303
- }
304
- function inspectInjectedConstructor(state, member) {
305
- for (const parameter of member.parameters) {
306
- inspectInjectedParameter(state, member, parameter);
307
- }
308
- }
309
- function inspectInjectedMembers(state) {
310
- const { config, context, checker, declaration, framework, violations } = state;
311
- const ts = context.ts;
312
- const sourceFile = declaration.getSourceFile();
313
- const classState = { ...state, sourceFile };
314
- for (const member of declaration.members) {
315
- if (ts.isPropertyDeclaration(member)) {
316
- const injected = nodeDecorators(ts, member).some(decorator => framework.decoratorKind(ts, checker, decorator, framework.targets) === 'Inject');
317
- if (injected)
318
- checkReadonlyProperty(config, context, sourceFile, member, `Injected property ${propertyName(ts, member) ?? '(computed)'}`, violations);
319
- continue;
320
- }
321
- if (!ts.isConstructorDeclaration(member))
322
- continue;
323
- inspectInjectedConstructor(classState, member);
324
- }
325
- }
326
- function inspectInjectedBases(state) {
327
- const { config, context, checker, declaration, localFiles, reasons } = state;
328
- const ts = context.ts;
329
- const sourceFile = declaration.getSourceFile();
330
- const type = classInstanceType(ts, checker, declaration);
331
- // reason: a class symbol's declared type is an interface type; the construct-signature fallback is read the same way.
332
- for (const base of type && checker.getBaseTypes ? checker.getBaseTypes(type) : []) {
333
- const declarations = (base.symbol?.getDeclarations?.() ?? []).filter((item) => ts.isClassDeclaration(item)) ?? [];
334
- const [base0] = declarations;
335
- if (declarations.length !== 1 || !base0) {
336
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} inherits an injected constructor from a class outside the checked production program`);
337
- }
338
- else if (localFiles.has(canonical(base0.getSourceFile().fileName))) {
339
- checkInjectedClass({ ...state, declaration: base0 });
340
- }
341
- else if (classCarriesState(ts, base0)) {
342
- reasons.push(`${relativePath(config.root, sourceFile.fileName)} inherits an injected constructor from a class outside the checked production program`);
343
- }
344
- }
345
- }
346
- export function checkInjectedClass({ config, context, checker, declaration, classKind, targets, decoratorKind, localFiles, violations, reasons, visited = new Set() }) {
347
- const ts = context.ts;
348
- const classKey = `${canonical(declaration.getSourceFile().fileName)}:${declaration.pos}`;
349
- if (visited.has(classKey))
350
- return;
351
- visited.add(classKey);
352
- const properties = new Map(declaration.members.filter((member) => ts.isPropertyDeclaration(member))
353
- .map((member) => [propertyName(ts, member), member])
354
- .filter((entry) => Boolean(entry[0])));
355
- const state = { config, context, checker, declaration, classKind, properties, targets, decoratorKind,
356
- framework: { targets, decoratorKind }, localFiles, violations, reasons, visited };
357
- inspectInjectedMembers(state);
358
- if (classKind)
359
- inspectInjectedBases(state);
360
- }