@starci/eslint-canon-be 3.8.20 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/package.json +1 -1
  3. package/runtime/bundle.json +192 -0
  4. package/runtime/engine/by-code-unit.mjs +6 -6
  5. package/runtime/engine/model-config.mjs +98 -76
  6. package/runtime/engine/orca-config.mjs +22 -17
  7. package/runtime/engine/release-config.mjs +22 -18
  8. package/runtime/engine/removed-vocabulary.mjs +21 -20
  9. package/runtime/engine/resources-config.mjs +42 -35
  10. package/runtime/engine/runtime-root.mjs +11 -13
  11. package/runtime/engine/secrets.mjs +170 -102
  12. package/runtime/engine/sonar-config.mjs +16 -13
  13. package/runtime/engine/temp-root.mjs +3 -9
  14. package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
  15. package/runtime/knowledge/hfs/rules.yaml +6 -6
  16. package/runtime/knowledge/hfs/slots.yaml +2 -2
  17. package/runtime/knowledge/sonar-gate.yaml +1 -1
  18. package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
  19. package/runtime/scripts/api/git/cat-file.mjs +0 -1
  20. package/runtime/scripts/api/git/config-get.mjs +1 -2
  21. package/runtime/scripts/api/git/diff.mjs +0 -1
  22. package/runtime/scripts/api/git/lib.mjs +32 -32
  23. package/runtime/scripts/api/git/ls-files.mjs +0 -1
  24. package/runtime/scripts/api/git/ls-tree.mjs +0 -1
  25. package/runtime/scripts/api/git/merge-base.mjs +2 -3
  26. package/runtime/scripts/api/git/remote.mjs +0 -1
  27. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
  28. package/runtime/scripts/api/git/show.mjs +0 -1
  29. package/runtime/scripts/hfs/allows.mjs +37 -38
  30. package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
  31. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
  32. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
  33. package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
  34. package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
  35. package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
  36. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
  37. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
  38. package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
  39. package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
  40. package/runtime/scripts/hfs/architecture/config.mjs +302 -268
  41. package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
  42. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
  43. package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
  44. package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
  45. package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
  46. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
  47. package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
  48. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
  49. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
  50. package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
  51. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
  52. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
  53. package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
  54. package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
  55. package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
  56. package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
  57. package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
  58. package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
  59. package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
  60. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
  61. package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
  62. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
  63. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
  64. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
  65. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
  66. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
  67. package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
  68. package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
  69. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
  70. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
  71. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
  72. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
  73. package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
  74. package/runtime/scripts/hfs/architecture/index.mjs +272 -272
  75. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
  76. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
  77. package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
  78. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
  79. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
  80. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
  81. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
  82. package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
  83. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
  84. package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
  85. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
  86. package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
  87. package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
  88. package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
  89. package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
  90. package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
  91. package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
  92. package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
  93. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
  94. package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
  95. package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
  96. package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
  97. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
  98. package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
  99. package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
  100. package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
  101. package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
  102. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
  103. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
  104. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
  105. package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
  106. package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
  107. package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
  108. package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
  109. package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
  110. package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
  111. package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
  112. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
  113. package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
  114. package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
  115. package/runtime/scripts/hfs/edition-slots.mjs +43 -40
  116. package/runtime/scripts/hfs/linear-text.mjs +20 -20
  117. package/runtime/scripts/hfs/param-names.mjs +6 -11
  118. package/runtime/scripts/hfs/path-findings.mjs +120 -100
  119. package/runtime/scripts/hfs/repo-identity.mjs +55 -42
  120. package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
  121. package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
  122. package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
  123. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
  124. package/runtime/scripts/hfs/rules/read.mjs +19 -15
  125. package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
  126. package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
  127. package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
  128. package/runtime/scripts/hfs/slot-classify.mjs +42 -38
  129. package/runtime/scripts/hfs/slot-errors.mjs +8 -7
  130. package/runtime/scripts/hfs/slot-imports.mjs +70 -63
  131. package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
  132. package/runtime/scripts/hfs/slot-match.mjs +122 -111
  133. package/runtime/scripts/hfs/slot-path.mjs +0 -2
  134. package/runtime/scripts/hfs/slot-required.mjs +65 -59
  135. package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
  136. package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
  137. package/runtime/scripts/hfs/slots.mjs +175 -152
  138. package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
  139. package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
  140. package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
  141. package/runtime/scripts/hfs/view.mjs +38 -40
  142. package/runtime/scripts/lib/dockerfile.mjs +131 -114
  143. package/runtime/scripts/lib/env.mjs +3 -6
  144. package/runtime/scripts/lib/event-contract.mjs +73 -78
  145. package/runtime/scripts/lib/git.mjs +15 -16
  146. package/runtime/scripts/lib/glob.mjs +23 -14
  147. package/runtime/scripts/lib/grammar-lint.mjs +33 -37
  148. package/runtime/scripts/lib/i18n.mjs +32 -35
  149. package/runtime/scripts/lib/in-order.mjs +57 -38
  150. package/runtime/scripts/lib/json-clone.mjs +45 -36
  151. package/runtime/scripts/lib/json.mjs +26 -14
  152. package/runtime/scripts/lib/language.mjs +133 -129
  153. package/runtime/scripts/lib/list.mjs +9 -14
  154. package/runtime/scripts/lib/mutation-fence.mjs +10 -5
  155. package/runtime/scripts/lib/path-key.mjs +53 -38
  156. package/runtime/scripts/lib/read-text.mjs +30 -17
  157. package/runtime/scripts/lib/regex.mjs +1 -2
  158. package/runtime/scripts/lib/same-text.mjs +0 -1
  159. package/runtime/scripts/lib/sops-envelope.mjs +148 -120
  160. package/runtime/scripts/lib/stack-services.mjs +128 -129
  161. package/runtime/scripts/lib/ts-analysis.mjs +28 -26
  162. package/runtime/scripts/lib/ts-ast.mjs +36 -42
  163. package/runtime/scripts/lib/walk.mjs +33 -27
  164. package/runtime/scripts/lib/yaml-cached.mjs +4 -4
@@ -1,153 +1,171 @@
1
1
  import path from 'node:path';
2
-
3
2
  export const keyOf = (ts, property) => (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) && (ts.isIdentifier(property.name) || ts.isStringLiteralLike(property.name)) ? property.name.text : null;
4
3
  const exported = (ts, statement) => statement.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword) ?? false;
5
-
6
4
  /** The directly defined functions among the declarations of an exported variable statement; other values are not callable. */
7
5
  function callableVariables(ts, statement) {
8
- const names = [];
9
- for (const declaration of statement.declarationList.declarations) {
10
- const initializer = declaration.initializer;
11
- if (ts.isIdentifier(declaration.name) && initializer && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))) names.push(declaration.name.text);
12
- }
13
- return names;
6
+ const names = [];
7
+ for (const declaration of statement.declarationList.declarations) {
8
+ const initializer = declaration.initializer;
9
+ if (ts.isIdentifier(declaration.name) && initializer && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer)))
10
+ names.push(declaration.name.text);
11
+ }
12
+ return names;
14
13
  }
15
-
16
14
  /** The callable exports of one statement: an overload signature has no body and collapses into its implementation. */
17
15
  function callableExports(ts, statement) {
18
- if (!exported(ts, statement)) return [];
19
- if (ts.isFunctionDeclaration(statement)) return statement.name && statement.body ? [statement.name.text] : [];
20
- return ts.isVariableStatement(statement) ? callableVariables(ts, statement) : [];
16
+ if (!exported(ts, statement))
17
+ return [];
18
+ if (ts.isFunctionDeclaration(statement))
19
+ return statement.name && statement.body ? [statement.name.text] : [];
20
+ return ts.isVariableStatement(statement) ? callableVariables(ts, statement) : [];
21
21
  }
22
-
23
22
  /**
24
23
  * The callable subset of a function subject's exports. Constants, enums, objects, types, `export ... from` re-exports and overload
25
24
  * signatures do not change which functions the spec must provide; a class or a default-export assignment keeps the class-provider
26
25
  * check (null), as does a module with no callable export.
27
26
  */
28
27
  export function functionSubjectExports(ts, sourceFile) {
29
- const names = [];
30
- for (const statement of sourceFile.statements) {
31
- if (ts.isClassDeclaration(statement) || ts.isExportAssignment(statement)) return null;
32
- for (const name of callableExports(ts, statement)) if (!names.includes(name)) names.push(name);
33
- }
34
- return names.length ? names : null;
28
+ const names = [];
29
+ for (const statement of sourceFile.statements) {
30
+ if (ts.isClassDeclaration(statement) || ts.isExportAssignment(statement))
31
+ return null;
32
+ for (const name of callableExports(ts, statement))
33
+ if (!names.includes(name))
34
+ names.push(name);
35
+ }
36
+ return names.length ? names : null;
35
37
  }
36
-
37
38
  const decoratorsOf = (ts, node) => (ts.canHaveDecorators(node) ? ts.getDecorators(node) ?? [] : []);
38
-
39
39
  /** A value object: an undecorated class whose constructor parameters carry no injection token, so Nest never builds it. */
40
40
  function isValueObjectClass(ts, declaration) {
41
- if (decoratorsOf(ts, declaration).length > 0) return false;
42
- const constructors = declaration.members.filter(member => ts.isConstructorDeclaration(member));
43
- return constructors.every(constructor => constructor.parameters.every(parameter => decoratorsOf(ts, parameter).length === 0));
41
+ if (decoratorsOf(ts, declaration).length > 0)
42
+ return false;
43
+ const constructors = declaration.members.filter((member) => ts.isConstructorDeclaration(member));
44
+ return constructors.every(constructor => constructor.parameters.every(parameter => decoratorsOf(ts, parameter).length === 0));
44
45
  }
45
-
46
46
  /** The names of the value-object classes a module exports, or null when it exports a class that is not one (or none). */
47
47
  export function valueObjectSubjects(ts, sourceFile) {
48
- const classes = sourceFile.statements.filter(statement => ts.isClassDeclaration(statement) && statement.name && exported(ts, statement));
49
- if (!classes.length || !classes.every(declaration => isValueObjectClass(ts, declaration))) return null;
50
- return classes.map(declaration => declaration.name.text);
48
+ const classes = sourceFile.statements.filter((statement) => ts.isClassDeclaration(statement) && Boolean(statement.name) && exported(ts, statement));
49
+ if (!classes.length || !classes.every(declaration => isValueObjectClass(ts, declaration)))
50
+ return null;
51
+ return classes.map(declaration => declaration.name.text);
51
52
  }
52
-
53
53
  /** The value of one literal object property. */
54
54
  export const propertyValue = (ts, object, key) => {
55
- const property = object.properties.find(item => keyOf(ts, item) === key);
56
- return property && ts.isPropertyAssignment(property) ? property.initializer : null;
55
+ const property = object.properties.find(item => keyOf(ts, item) === key);
56
+ return property && ts.isPropertyAssignment(property) ? property.initializer : null;
57
57
  };
58
-
59
58
  /** A literal value object maps every exported function to its real sibling import. */
60
59
  function realFunctions(ts, value, names, imports, rel, serviceRel) {
61
- if (!value || !ts.isObjectLiteralExpression(value) || value.properties.length !== names.length) return false;
62
- const remaining = new Set(names);
63
- for (const property of value.properties) {
64
- const key = keyOf(ts, property);
65
- const identifier = ts.isShorthandPropertyAssignment(property) ? property.name : property.initializer;
66
- if (!remaining.delete(key) || !identifier || !ts.isIdentifier(identifier)) return false;
67
- const binding = imports.get(identifier.text);
68
- if (!binding || binding.name !== key || !binding.module.startsWith('.')) return false;
69
- const target = path.posix.normalize(path.posix.join(path.posix.dirname(rel), binding.module)).replace(/\.[cm]?[jt]s$/u, '');
70
- if (target !== serviceRel.replace(/\.ts$/u, '')) return false;
71
- }
72
- return remaining.size === 0;
60
+ if (!value || !ts.isObjectLiteralExpression(value) || value.properties.length !== names.length)
61
+ return false;
62
+ const remaining = new Set(names);
63
+ for (const property of value.properties) {
64
+ const key = keyOf(ts, property);
65
+ const identifier = ts.isShorthandPropertyAssignment(property) ? property.name : ts.isPropertyAssignment(property) ? property.initializer : undefined;
66
+ if (key === null || !remaining.delete(key) || !identifier || !ts.isIdentifier(identifier))
67
+ return false;
68
+ const binding = imports.get(identifier.text);
69
+ if (!binding || binding.name !== key || !binding.module.startsWith('.'))
70
+ return false;
71
+ const target = path.posix.normalize(path.posix.join(path.posix.dirname(rel), binding.module)).replace(/\.[cm]?[jt]s$/u, '');
72
+ if (target !== serviceRel.replace(/\.ts$/u, ''))
73
+ return false;
74
+ }
75
+ return remaining.size === 0;
73
76
  }
74
-
75
77
  /** Every node satisfying a predicate, within one spec. */
76
78
  function matchingNodes(ts, root, predicate) {
77
- const found = [];
78
- const visit = node => {
79
- if (predicate(node)) found.push(node);
80
- ts.forEachChild(node, visit);
81
- };
82
- visit(root);
83
- return found;
79
+ const found = [];
80
+ const visit = (node) => {
81
+ if (predicate(node))
82
+ found.push(node);
83
+ ts.forEachChild(node, visit);
84
+ };
85
+ visit(root);
86
+ return found;
84
87
  }
85
-
86
88
  /** The declared variable initialized by an expression, including its await and call chain. */
87
89
  function initializedVariable(ts, expression) {
88
- let cursor = expression;
89
- let compiled = false;
90
- let awaited = false;
91
- while (cursor.parent && (ts.isAwaitExpression(cursor.parent) || ts.isPropertyAccessExpression(cursor.parent) || ts.isCallExpression(cursor.parent))) {
92
- cursor = cursor.parent;
93
- if (ts.isAwaitExpression(cursor)) awaited = true;
94
- if (ts.isCallExpression(cursor) && ts.isPropertyAccessExpression(cursor.expression) && cursor.expression.name.text === 'compile') compiled = true;
95
- }
96
- if (!compiled || !awaited) return null;
97
- const declaration = cursor.parent;
98
- return declaration && ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name) ? declaration.name.text : null;
90
+ let cursor = expression;
91
+ let compiled = false;
92
+ let awaited = false;
93
+ while (cursor.parent && (ts.isAwaitExpression(cursor.parent) || ts.isPropertyAccessExpression(cursor.parent) || ts.isCallExpression(cursor.parent))) {
94
+ cursor = cursor.parent;
95
+ if (ts.isAwaitExpression(cursor))
96
+ awaited = true;
97
+ if (ts.isCallExpression(cursor) && ts.isPropertyAccessExpression(cursor.expression) && cursor.expression.name.text === 'compile')
98
+ compiled = true;
99
+ }
100
+ if (!compiled || !awaited)
101
+ return null;
102
+ const declaration = cursor.parent;
103
+ return declaration && ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name) ? declaration.name.text : null;
99
104
  }
100
-
101
105
  /** The subject variable receiving module.get, whether declared there or assigned in beforeEach. */
102
106
  function retrievedVariable(ts, call) {
103
- let cursor = call;
104
- while (cursor.parent && (ts.isAwaitExpression(cursor.parent) || ts.isParenthesizedExpression(cursor.parent))) cursor = cursor.parent;
105
- const parent = cursor.parent;
106
- if (parent && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name)) return parent.name.text;
107
- if (parent && ts.isBinaryExpression(parent) && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken && ts.isIdentifier(parent.left)) return parent.left.text;
108
- return null;
107
+ let cursor = call;
108
+ while (cursor.parent && (ts.isAwaitExpression(cursor.parent) || ts.isParenthesizedExpression(cursor.parent)))
109
+ cursor = cursor.parent;
110
+ const parent = cursor.parent;
111
+ if (parent && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name))
112
+ return parent.name.text;
113
+ if (parent && ts.isBinaryExpression(parent) && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken && ts.isIdentifier(parent.left))
114
+ return parent.left.text;
115
+ return null;
116
+ }
117
+ /** The `.text` of a node's `name`, when the node has an identifier-like name. */
118
+ function nameTextOf(node) {
119
+ // reason: a loose compiler-API node; only declarations carry a `name`, and its `.text` is read when it is a string
120
+ const name = node?.name;
121
+ return typeof name?.text === 'string' ? name.text : undefined;
109
122
  }
110
-
111
123
  /** A helper returning the retrieved subject is followed to the variable receiving its result. */
112
124
  function retrievedSubjects(ts, read, sourceFile) {
113
- const direct = retrievedVariable(ts, read);
114
- if (direct) return [direct];
115
- if (!read.parent || !ts.isReturnStatement(read.parent)) return [];
116
- let helper = read.parent;
117
- while (helper.parent && !ts.isFunctionLike(helper)) helper = helper.parent;
118
- const name = helper.name?.text ?? helper.parent?.name?.text;
119
- if (!name) return [];
120
- return matchingNodes(ts, sourceFile, node => ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === name)
121
- .map(node => retrievedVariable(ts, node)).filter(Boolean);
125
+ const direct = retrievedVariable(ts, read);
126
+ if (direct)
127
+ return [direct];
128
+ if (!read.parent || !ts.isReturnStatement(read.parent))
129
+ return [];
130
+ let helper = read.parent;
131
+ while (helper.parent && !ts.isFunctionLike(helper))
132
+ helper = helper.parent;
133
+ const name = nameTextOf(helper) ?? nameTextOf(helper.parent);
134
+ if (!name)
135
+ return [];
136
+ return matchingNodes(ts, sourceFile, node => ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === name)
137
+ .map(node => retrievedVariable(ts, node)).filter((variable) => Boolean(variable));
122
138
  }
123
-
124
139
  /** The same compiled module retrieves the matching token and its result actually calls a subject function. */
125
140
  function retrievedSubjectUsed(ts, call, token, names) {
126
- const moduleName = initializedVariable(ts, call);
127
- if (!moduleName) return false;
128
- const sourceFile = call.getSourceFile();
129
- const reads = matchingNodes(ts, sourceFile, node => {
130
- if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression)) return false;
131
- const member = node.expression;
132
- return ts.isIdentifier(member.expression) && member.expression.text === moduleName && member.name.text === 'get'
133
- && node.arguments.length === 1 && ts.isIdentifier(node.arguments[0]) && node.arguments[0].text === token.text;
134
- });
135
- return reads.some(read => {
136
- const subjects = retrievedSubjects(ts, read, sourceFile);
137
- return matchingNodes(ts, sourceFile, node => ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)
138
- && ts.isIdentifier(node.expression.expression) && subjects.includes(node.expression.expression.text) && names.includes(node.expression.name.text)).length > 0;
139
- });
141
+ const moduleName = initializedVariable(ts, call);
142
+ if (!moduleName)
143
+ return false;
144
+ const sourceFile = call.getSourceFile();
145
+ const reads = matchingNodes(ts, sourceFile, node => {
146
+ if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression))
147
+ return false;
148
+ const member = node.expression;
149
+ const first = node.arguments[0];
150
+ return ts.isIdentifier(member.expression) && member.expression.text === moduleName && member.name.text === 'get'
151
+ && node.arguments.length === 1 && first !== undefined && ts.isIdentifier(first) && first.text === token.text;
152
+ });
153
+ return reads.some(read => {
154
+ const subjects = retrievedSubjects(ts, read, sourceFile);
155
+ return matchingNodes(ts, sourceFile, node => ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)
156
+ && ts.isIdentifier(node.expression.expression) && subjects.includes(node.expression.expression.text) && names.includes(node.expression.name.text)).length > 0;
157
+ });
140
158
  }
141
-
142
159
  /** A pure function service has exactly one real subject value provider, retrieved and exercised through Nest. */
143
160
  export function checkFunctionSubject({ ts, rel, serviceRel, imports, call, list, names, report }) {
144
- const provider = list.elements[0];
145
- const shape = provider && ts.isObjectLiteralExpression(provider) && provider.properties.length === 2;
146
- const token = shape ? propertyValue(ts, provider, 'provide') : null;
147
- const value = shape ? propertyValue(ts, provider, 'useValue') : null;
148
- if (list.elements.length !== 1 || !token || !ts.isIdentifier(token) || !realFunctions(ts, value, names, imports, rel, serviceRel)) {
149
- report(provider ?? list, `${rel}: a function-only subject needs exactly one { provide: TOKEN, useValue: { ...realFunctions } } provider containing every callable export imported from ./${path.posix.basename(serviceRel, '.ts')}.`);
150
- return;
151
- }
152
- if (!retrievedSubjectUsed(ts, call, token, names)) report(list, `${rel}: retrieve the function subject with moduleRef.get(TOKEN) from this compiled testing module and exercise a function on the returned subject.`);
161
+ const provider = list.elements[0];
162
+ const shape = provider && ts.isObjectLiteralExpression(provider) && provider.properties.length === 2;
163
+ const token = shape ? propertyValue(ts, provider, 'provide') : null;
164
+ const value = shape ? propertyValue(ts, provider, 'useValue') : null;
165
+ if (list.elements.length !== 1 || !token || !ts.isIdentifier(token) || !realFunctions(ts, value, names, imports, rel, serviceRel)) {
166
+ report(provider ?? list, `${rel}: a function-only subject needs exactly one { provide: TOKEN, useValue: { ...realFunctions } } provider containing every callable export imported from ./${path.posix.basename(serviceRel, '.ts')}.`);
167
+ return;
168
+ }
169
+ if (!retrievedSubjectUsed(ts, call, token, names))
170
+ report(list, `${rel}: retrieve the function subject with moduleRef.get(TOKEN) from this compiled testing module and exercise a function on the returned subject.`);
153
171
  }
@@ -1,6 +1,5 @@
1
1
  import { canonical } from './config.mjs';
2
2
  import { relativePath } from './typescript.mjs';
3
-
4
3
  /**
5
4
  * The file and owner graph the HFS architecture checks share. Every production source file the TypeScript context
6
5
  * loaded becomes a node classified by the slot manifest (config.hfs is the resolver of scripts/hfs/slots.mjs);
@@ -17,57 +16,62 @@ import { relativePath } from './typescript.mjs';
17
16
  */
18
17
  // Every production source file the TypeScript context loaded, as a graph.files node keyed by its repository-relative path.
19
18
  const collectFiles = (config, context, resolver) => {
20
- const files = new Map();
21
- const absolute = new Map();
22
- for (const sourceFile of context.files) {
23
- const file = canonical(sourceFile.fileName);
24
- const rel = relativePath(config.root, file);
25
- if (rel.startsWith('..')) continue;
26
- const classified = resolver.classifyPath(rel);
27
- files.set(rel, {
28
- abs: file,
29
- rel,
30
- slot: classified.slot ?? null,
31
- status: classified.status,
32
- tier: classified.slot ? resolver.tierOf(rel) : null,
33
- owner: classified.slot ? resolver.ownerOf(rel) : null,
34
- sourceFile,
35
- });
36
- absolute.set(file, rel);
37
- }
38
- return { files, absolute };
19
+ const files = new Map();
20
+ const absolute = new Map();
21
+ for (const sourceFile of context.files) {
22
+ const file = canonical(sourceFile.fileName);
23
+ const rel = relativePath(config.root, file);
24
+ if (rel.startsWith('..'))
25
+ continue;
26
+ const classified = resolver.classifyPath(rel);
27
+ files.set(rel, {
28
+ abs: file,
29
+ rel,
30
+ slot: classified.slot ?? null,
31
+ status: classified.status,
32
+ tier: classified.slot ? resolver.tierOf(rel) : null,
33
+ owner: classified.slot ? resolver.ownerOf(rel) : null,
34
+ sourceFile,
35
+ });
36
+ absolute.set(file, rel);
37
+ }
38
+ return { files, absolute };
39
39
  };
40
-
41
40
  // Every import, re-export and type-only import between two graphed files, as rel-path edges.
42
41
  const collectEdges = (context, absolute) => {
43
- const edges = [];
44
- for (const [file, list] of context.edges) {
45
- const from = absolute.get(file);
46
- if (!from) continue;
47
- for (const edge of list) {
48
- const to = absolute.get(edge.to);
49
- if (!to || to === from) continue;
50
- edges.push({ from, to, runtime: edge.runtime, line: edge.line, column: edge.column, specifier: edge.specifier, reexport: Boolean(edge.reexport), edge });
42
+ const edges = [];
43
+ for (const [file, list] of context.edges) {
44
+ const from = absolute.get(file);
45
+ if (!from)
46
+ continue;
47
+ for (const edge of list) {
48
+ const to = absolute.get(edge.to);
49
+ if (!to || to === from)
50
+ continue;
51
+ edges.push({ from, to, runtime: edge.runtime, line: edge.line, column: edge.column, specifier: edge.specifier, reexport: Boolean(edge.reexport), edge });
52
+ }
51
53
  }
52
- }
53
- return edges;
54
+ return edges;
54
55
  };
55
-
56
56
  export function buildHfsGraph(config, context) {
57
- const resolver = config.hfs;
58
- const { files, absolute } = collectFiles(config, context, resolver);
59
- const edges = collectEdges(context, absolute);
60
- const unit = rel => {
61
- const node = files.get(rel);
62
- if (!node) return null;
63
- if (node.owner) return `${node.owner.slot}:${node.owner.root}`;
64
- const classified = resolver.classifyPath(rel);
65
- return classified.slot ? `${classified.slot}:${classified.root}` : null;
66
- };
67
- const ownerRoots = new Map();
68
- for (const node of files.values()) if (node.owner) {
69
- const key = `${node.owner.slot}:${node.owner.root}`;
70
- if (!ownerRoots.has(key)) ownerRoots.set(key, { slot: node.owner.slot, root: node.owner.root, tier: resolver.slot(node.owner.slot).tier });
71
- }
72
- return { profile: resolver.repo.profile, resolver, files, edges, unit, ownerRoots, abs: file => absolute.get(file) ?? null };
57
+ const resolver = config.hfs;
58
+ const { files, absolute } = collectFiles(config, context, resolver);
59
+ const edges = collectEdges(context, absolute);
60
+ const unit = (rel) => {
61
+ const node = files.get(rel);
62
+ if (!node)
63
+ return null;
64
+ if (node.owner)
65
+ return `${node.owner.slot}:${node.owner.root}`;
66
+ const classified = resolver.classifyPath(rel);
67
+ return classified.slot ? `${classified.slot}:${classified.root}` : null;
68
+ };
69
+ const ownerRoots = new Map();
70
+ for (const node of files.values())
71
+ if (node.owner) {
72
+ const key = `${node.owner.slot}:${node.owner.root}`;
73
+ if (!ownerRoots.has(key))
74
+ ownerRoots.set(key, { slot: node.owner.slot, root: node.owner.root, tier: resolver.slot(node.owner.slot)?.tier ?? '' }); // the owner slot came from resolver.ownerOf, so the manifest holds it
75
+ }
76
+ return { profile: resolver.repo.profile, resolver, files, edges, unit, ownerRoots, abs: (file) => absolute.get(file) ?? null };
73
77
  }
@@ -2,7 +2,6 @@ import path from 'node:path';
2
2
  import { machineKit } from './machine-ast.mjs';
3
3
  import { treeOf } from './required-files.mjs';
4
4
  import { byCodeUnit } from '../../lib/list.mjs';
5
-
6
5
  /**
7
6
  * R56 `hooks-are-hooks` (FE_HOOKS_ARE_HOOKS), the repository half of the eslint rule `hooks-folder-holds-hooks-only`, which
8
7
  * judges one file at a time and leaves to the machine what needs the whole tree. Per domain folder of hooks
@@ -14,95 +13,106 @@ import { byCodeUnit } from '../../lib/list.mjs';
14
13
  * in two files of the domain is a helper copied instead of shared; it lives once in `<domain>.shared.ts`.
15
14
  */
16
15
  export const HOOKS_ARE_HOOKS_RULE_IDS = ['FE_HOOKS_ARE_HOOKS'];
17
-
18
16
  const RULE = 'FE_HOOKS_ARE_HOOKS';
19
17
  const HOOKS_SLOT = 'fe.hooks';
20
18
  const HOOK_NAME = /^use[A-Z0-9]/u;
21
19
  const SHARED = /\.shared\.[cm]?tsx?$/u;
22
-
23
20
  const sharedFilesOf = (tree, resolver, domains) => {
24
- const shared = new Map(); // domain root -> [file]
25
- for (const rel of [...tree.files].sort(byCodeUnit)) {
26
- const classified = resolver.classifyPath(rel);
27
- if (classified.slot !== HOOKS_SLOT || !classified.root) continue;
28
- domains.add(classified.root);
29
- const base = path.posix.basename(rel);
30
- if (/\.spec\.[cm]?tsx?$/u.test(base) || !SHARED.test(base)) continue;
31
- if (!shared.has(classified.root)) shared.set(classified.root, []);
32
- shared.get(classified.root).push({ rel, base });
33
- }
34
- return shared;
21
+ const shared = new Map(); // domain root -> [file]
22
+ for (const rel of [...tree.files].sort(byCodeUnit)) {
23
+ const classified = resolver.classifyPath(rel);
24
+ if (classified.slot !== HOOKS_SLOT || !classified.root)
25
+ continue;
26
+ domains.add(classified.root);
27
+ const base = path.posix.basename(rel);
28
+ if (/\.spec\.[cm]?tsx?$/u.test(base) || !SHARED.test(base))
29
+ continue;
30
+ let list = shared.get(classified.root);
31
+ if (!list) {
32
+ list = [];
33
+ shared.set(classified.root, list);
34
+ }
35
+ list.push({ rel, base });
36
+ }
37
+ return shared;
35
38
  };
36
-
37
39
  const reportSharedFileViolations = (shared, violations) => {
38
- for (const [root, list] of shared) {
39
- const domain = path.posix.basename(root);
40
- const at = (item, message) => violations.push({ ruleId: RULE, path: item.rel, line: 1, column: 1, domain, message });
41
- for (const item of list) {
42
- if (item.base.replace(/\.[cm]?tsx?$/u, '') !== `${domain}.shared` || path.posix.dirname(item.rel) !== root) {
43
- at(item, `${item.rel} is a shared file that is not ${root}/${domain}.shared.ts; a hooks domain has one shared file for its non-hook helpers, named after the domain. Move the helpers into it.`);
44
- }
45
- }
46
- for (const item of list.filter(entry => entry.base.replace(/\.[cm]?tsx?$/u, '') === `${domain}.shared` && path.posix.dirname(entry.rel) === root).slice(1)) {
47
- at(item, `${item.rel} is a second shared file of hooks domain ${domain}; a domain has exactly one, ${domain}.shared.ts. Merge the helpers into it.`);
40
+ for (const [root, list] of shared) {
41
+ const domain = path.posix.basename(root);
42
+ const at = (item, message) => violations.push({ ruleId: RULE, path: item.rel, line: 1, column: 1, domain, message });
43
+ for (const item of list) {
44
+ if (item.base.replace(/\.[cm]?tsx?$/u, '') !== `${domain}.shared` || path.posix.dirname(item.rel) !== root) {
45
+ at(item, `${item.rel} is a shared file that is not ${root}/${domain}.shared.ts; a hooks domain has one shared file for its non-hook helpers, named after the domain. Move the helpers into it.`);
46
+ }
47
+ }
48
+ for (const item of list.filter(entry => entry.base.replace(/\.[cm]?tsx?$/u, '') === `${domain}.shared` && path.posix.dirname(entry.rel) === root).slice(1)) {
49
+ at(item, `${item.rel} is a second shared file of hooks domain ${domain}; a domain has exactly one, ${domain}.shared.ts. Merge the helpers into it.`);
50
+ }
48
51
  }
49
- }
50
52
  };
51
-
52
53
  const helperDeclarationsOf = (statement, ts) => {
53
- const found = [];
54
- if (ts.isFunctionDeclaration(statement) && statement.name) found.push([statement.name.text, statement.name]);
55
- if (ts.isVariableStatement(statement)) {
56
- for (const declaration of statement.declarationList.declarations) {
57
- const init = declaration.initializer;
58
- if (ts.isIdentifier(declaration.name) && init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init))) found.push([declaration.name.text, declaration.name]);
54
+ const found = [];
55
+ if (ts.isFunctionDeclaration(statement) && statement.name)
56
+ found.push([statement.name.text, statement.name]);
57
+ if (ts.isVariableStatement(statement)) {
58
+ for (const declaration of statement.declarationList.declarations) {
59
+ const init = declaration.initializer;
60
+ if (ts.isIdentifier(declaration.name) && init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init)))
61
+ found.push([declaration.name.text, declaration.name]);
62
+ }
59
63
  }
60
- }
61
- return found;
64
+ return found;
62
65
  };
63
-
64
66
  const recordDeclaredHelper = (declared, root, name, file, node) => {
65
- if (HOOK_NAME.test(name)) return;
66
- if (!declared.has(root)) declared.set(root, new Map());
67
- const byName = declared.get(root);
68
- if (!byName.has(name)) byName.set(name, []);
69
- byName.get(name).push({ file, node });
67
+ if (HOOK_NAME.test(name))
68
+ return;
69
+ let byName = declared.get(root);
70
+ if (!byName) {
71
+ byName = new Map();
72
+ declared.set(root, byName);
73
+ }
74
+ let list = byName.get(name);
75
+ if (!list) {
76
+ list = [];
77
+ byName.set(name, list);
78
+ }
79
+ list.push({ file, node });
70
80
  };
71
-
72
81
  const declaredHelpersOf = (graph, resolver, ts) => {
73
- const declared = new Map(); // domain root -> name -> [{file, node}]
74
- for (const file of graph.files.values()) {
75
- if (file.slot !== HOOKS_SLOT) continue;
76
- const root = resolver.classifyPath(file.rel).root;
77
- for (const statement of file.sourceFile.statements) {
78
- for (const [name, node] of helperDeclarationsOf(statement, ts)) recordDeclaredHelper(declared, root, name, file, node);
82
+ const declared = new Map(); // domain root -> name -> [{file, node}]
83
+ for (const file of graph.files.values()) {
84
+ if (file.slot !== HOOKS_SLOT)
85
+ continue;
86
+ const root = resolver.classifyPath(file.rel).root ?? ''; // a file in the hooks slot was placed by that slot, so its root exists
87
+ for (const statement of file.sourceFile.statements) {
88
+ for (const [name, node] of helperDeclarationsOf(statement, ts))
89
+ recordDeclaredHelper(declared, root, name, file, node);
90
+ }
79
91
  }
80
- }
81
- return declared;
92
+ return declared;
82
93
  };
83
-
84
94
  const reportDuplicateHelpers = (declared, violations, kit) => {
85
- for (const [root, byName] of declared) {
86
- const domain = path.posix.basename(root);
87
- for (const [name, list] of byName) {
88
- if (list.length < 2) continue;
89
- for (const item of list.slice(1)) {
90
- const others = list.map(entry => entry.file.rel).filter(rel => rel !== item.file.rel).sort(byCodeUnit);
91
- violations.push({ ruleId: RULE, path: item.file.rel, ...kit.at(item.file.rel, item.file.sourceFile, item.node), domain, helper: name,
92
- message: `${name} is declared again in hooks domain ${domain} (also in ${others.join(', ')}); a helper the hooks share is written once, in ${domain}.shared.ts.` });
93
- }
95
+ for (const [root, byName] of declared) {
96
+ const domain = path.posix.basename(root);
97
+ for (const [name, list] of byName) {
98
+ if (list.length < 2)
99
+ continue;
100
+ for (const item of list.slice(1)) {
101
+ const others = list.map(entry => entry.file.rel).filter(rel => rel !== item.file.rel).sort(byCodeUnit);
102
+ violations.push({ ruleId: RULE, path: item.file.rel, ...kit.at(item.file.rel, item.file.sourceFile, item.node), domain, helper: name,
103
+ message: `${name} is declared again in hooks domain ${domain} (also in ${others.join(', ')}); a helper the hooks share is written once, in ${domain}.shared.ts.` });
104
+ }
105
+ }
94
106
  }
95
- }
96
107
  };
97
-
98
108
  export function checkHooksAreHooks(input) {
99
- const { config, graph } = input;
100
- const kit = machineKit(input);
101
- const { ts, resolver } = kit;
102
- const violations = [];
103
- const domains = new Set();
104
- const shared = sharedFilesOf(treeOf(config.root), resolver, domains);
105
- reportSharedFileViolations(shared, violations);
106
- reportDuplicateHelpers(declaredHelpersOf(graph, resolver, ts), violations, kit);
107
- return { violations, coverage: { status: 'checked', domains: domains.size } };
109
+ const { config, graph } = input;
110
+ const kit = machineKit(input);
111
+ const { ts, resolver } = kit;
112
+ const violations = [];
113
+ const domains = new Set();
114
+ const shared = sharedFilesOf(treeOf(config.root), resolver, domains);
115
+ reportSharedFileViolations(shared, violations);
116
+ reportDuplicateHelpers(declaredHelpersOf(graph, resolver, ts), violations, kit);
117
+ return { violations, coverage: { status: 'checked', domains: domains.size } };
108
118
  }