@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,237 +0,0 @@
1
- import path from 'node:path';
2
- import { isComposeGroup, machineKit } from './machine-ast.mjs';
3
- import { treeOf } from './required-files.mjs';
4
- import { byCodeUnit } from '../../lib/list.mjs';
5
- import { trimTrailingSlashes } from '../trailing-slashes.mjs';
6
- /**
7
- * R45 `module-per-transport` (BE_MODULE_SHAPE, BE-CONVENTION 1.2 and 1.6). A feature has one Nest module for its
8
- * application (`<f>.module.ts` at the feature root) and exactly one module per transport folder
9
- * (`transport/<protocol>/<f>-<protocol>.module.ts`). Never one module per operation. Every one of these modules has its own
10
- * module-definition beside it (owner ruling module-definition-everywhere, R295):
11
- *
12
- * - a `*.module.ts` file below a transport folder other than `<f>-<protocol>.module.ts`, a `*.module-definition.ts` that is
13
- * not the sibling of the application module or of the transport module of its own folder, and an `@Module` class (or a
14
- * `ConfigurableModuleBuilder`/`ConfigurableModuleClass` use or a `defineConfigurableModule`/`defineCapabilityModule` call of `@starci/nest-kit`) in a feature file that is neither the application module, the
15
- * transport module of its own folder nor that module's definition are refused;
16
- * - apps import only transport modules, of the kinds their app kind allows: the `composedBy` list of each transport slot
17
- * in knowledge/hfs/slots.yaml (api: graphql, http, websocket; worker: schedule, message). An app root (app.module.ts
18
- * or a composition group of its compose/ folder) that lists a feature's application module, or the transport module
19
- * of another kind, is refused;
20
- * - a composed root (an owner slot with `composedBy`, be.cli) holds one static module per folder, named after the folder
21
- * (cli.module.ts, <group>/<group>.module.ts); an app of a composing kind lists the root module only, and an app of any
22
- * other kind lists none of its modules.
23
- */
24
- export const MODULE_PER_TRANSPORT_RULE_IDS = ['BE_MODULE_SHAPE'];
25
- const RULE = 'BE_MODULE_SHAPE';
26
- function featureOwnerOf(resolver, rel) {
27
- const owner = resolver.ownerOf(rel);
28
- return owner && resolver.slot(owner.slot)?.tier === 'feature' ? owner : null;
29
- }
30
- function modulePlaceOf(resolver, rel) {
31
- const owner = featureOwnerOf(resolver, rel);
32
- if (!owner)
33
- return null;
34
- const composedBy = resolver.slot(owner.slot)?.composedBy;
35
- const below = rel.slice(owner.root.length + 1).split('/');
36
- const feature = path.posix.basename(owner.root);
37
- if (composedBy)
38
- return { root: owner.root, feature, protocol: null, below, composed: { composedBy } };
39
- return { root: owner.root, feature, protocol: below[0] === 'transport' && below.length > 2 ? below[1] ?? null : null, below, composed: null };
40
- }
41
- function composedModulePath(rel) {
42
- const dir = path.posix.dirname(rel);
43
- return `${dir}/${path.posix.basename(dir)}.module.ts`;
44
- }
45
- function composedByProtocol(resolver) {
46
- const composedBy = new Map();
47
- for (const slot of resolver.slots()) {
48
- if (!slot.composedBy || slot.owner)
49
- continue;
50
- composedBy.set(path.posix.basename(trimTrailingSlashes(slot.path)), slot.composedBy);
51
- }
52
- return composedBy;
53
- }
54
- /** The module a feature file may declare: the application module, the transport module of its folder, or a cli folder's module. */
55
- function ownModuleOf(place, rel, composedModuleOf) {
56
- if (place.composed)
57
- return composedModuleOf(rel);
58
- return place.protocol === null
59
- ? `${place.root}/${place.feature}.module.ts`
60
- : `${place.root}/transport/${place.protocol}/${place.feature}-${place.protocol}.module.ts`;
61
- }
62
- const DEFINITION_FILE = /\.module-definition\.[cm]?tsx?$/u;
63
- const definitionOf = (modulePath) => modulePath.replace(/\.module\.([cm]?tsx?)$/u, '.module-definition.$1');
64
- function reportTransportFileShapes(tree, placeOf, plain, composedModuleOf) {
65
- let transportModules = 0;
66
- for (const rel of [...tree.files].sort(byCodeUnit)) {
67
- const place = placeOf(rel);
68
- if (!place)
69
- continue;
70
- const base = path.posix.basename(rel);
71
- if (DEFINITION_FILE.test(base) && rel !== definitionOf(ownModuleOf(place, rel, composedModuleOf)))
72
- plain(rel, `${rel} is a module-definition that is not beside a module of feature ${place.feature}; the definition of a module is its sibling ${path.posix.basename(definitionOf(ownModuleOf(place, rel, composedModuleOf)))}, one per application module, transport module or cli folder.`, { feature: place.feature });
73
- if (place.protocol && /\.module\.[cm]?tsx?$/u.test(base)) {
74
- const expected = `${place.feature}-${place.protocol}.module.ts`;
75
- if (place.below.length !== 3 || base !== expected)
76
- plain(rel, `${rel} is a second module of the ${place.protocol} transport of ${place.feature}; a transport folder has exactly one module, transport/${place.protocol}/${expected}, never one module per operation. List the operation's providers in that module and delete this file.`, { feature: place.feature, protocol: place.protocol });
77
- else
78
- transportModules += 1;
79
- }
80
- }
81
- return transportModules;
82
- }
83
- function isNestModuleClass(ts, kit, checker, statement) {
84
- return kit.decorators(statement).some(decorator => {
85
- const call = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
86
- return kit.isImportOf(checker, call, 'Module', '@nestjs/common');
87
- });
88
- }
89
- function reportModuleClassLocation(file, name, place, composedModuleOf, report) {
90
- if (place.composed) {
91
- const own = composedModuleOf(file.rel);
92
- if (file.rel !== own)
93
- report(file, name, `${name.text} is a Nest module declared in ${file.rel}; every folder of ${place.root} holds exactly one static module named after it (${own}), never a module per command.`, { feature: place.feature });
94
- return;
95
- }
96
- const own = place.protocol === null
97
- ? `${place.root}/${place.feature}.module.ts`
98
- : `${place.root}/transport/${place.protocol}/${place.feature}-${place.protocol}.module.ts`;
99
- if (file.rel !== own)
100
- report(file, name, `${name.text} is a Nest module declared in ${file.rel}; a feature declares exactly its application module (${place.feature}.module.ts) and one module per transport folder (${place.feature}-<protocol>.module.ts), never a module per operation.`, { feature: place.feature });
101
- }
102
- /** The module-definition helpers of @starci/nest-kit: each builds the definition of one module, like `ConfigurableModuleBuilder`. */
103
- const DEFINITION_HELPERS = ['defineConfigurableModule', 'defineCapabilityModule'];
104
- function reportConfigurableModuleUses(file, checker, kit, ts, place, env) {
105
- const own = ownModuleOf(place, file.rel, env.composedModuleOf);
106
- const isDefinition = file.rel === definitionOf(own);
107
- kit.walk(file.sourceFile, node => {
108
- if (ts.isNewExpression(node) && kit.isImportOf(checker, node.expression, 'ConfigurableModuleBuilder', '@nestjs/common') && !isDefinition) {
109
- env.report(file, node, `ConfigurableModuleBuilder builds the module definition of one module; in feature ${place.feature} it is written only in ${path.posix.basename(definitionOf(own))}, beside ${path.posix.basename(own)}.`, { feature: place.feature });
110
- }
111
- else if (ts.isCallExpression(node) && !isDefinition && DEFINITION_HELPERS.some(name => kit.isImportOf(checker, node.expression, name, '@starci/nest-kit'))) {
112
- env.report(file, node, `${node.expression.getText(file.sourceFile)} builds the module definition of one module; in feature ${place.feature} it is written only in ${path.posix.basename(definitionOf(own))}, beside ${path.posix.basename(own)}.`, { feature: place.feature });
113
- }
114
- else if (ts.isHeritageClause(node) && file.rel !== own) {
115
- for (const type of node.types)
116
- if (kit.isImportOf(checker, type.expression, 'ConfigurableModuleClass', '@nestjs/common')) {
117
- env.report(file, type, `ConfigurableModuleClass is extended by the module of its own definition only (${path.posix.basename(own)}); feature ${place.feature} declares no other configurable module.`, { feature: place.feature });
118
- }
119
- }
120
- return true;
121
- });
122
- }
123
- function moduleClassesInFile(file, checker, place, modules, env) {
124
- const { kit, ts, composedModuleOf, report } = env;
125
- for (const statement of file.sourceFile.statements) {
126
- if (!ts.isClassDeclaration(statement) || !statement.name)
127
- continue;
128
- if (!isNestModuleClass(ts, kit, checker, statement))
129
- continue;
130
- modules.set(statement, { name: statement.name.text, file });
131
- if (place)
132
- reportModuleClassLocation(file, statement.name, place, composedModuleOf, report);
133
- }
134
- }
135
- function collectFeatureModules(graph, kit, ts, placeOf, composedModuleOf, report) {
136
- const modules = new Map(); // class declaration -> {name, file}
137
- const env = { kit, ts, composedModuleOf, report };
138
- for (const file of graph.files.values()) {
139
- const checker = kit.checkerOf(file.sourceFile);
140
- const place = placeOf(file.rel);
141
- moduleClassesInFile(file, checker, place, modules, env);
142
- if (place)
143
- reportConfigurableModuleUses(file, checker, kit, ts, place, env);
144
- }
145
- return modules;
146
- }
147
- function reportModuleReference(root, node, app, module, place, composedBy, report) {
148
- if (place.composed) {
149
- const rootModule = `${place.root}/${place.feature}.module.ts`;
150
- if (!place.composed.composedBy.includes(app.kind))
151
- report(root, node, `App ${app.name} is of kind ${app.kind} and lists ${module.name} from ${place.root}; only an app of kind ${place.composed.composedBy.join(', ')} composes that root.`, { app: app.name, module: module.name });
152
- else if (module.file.rel !== rootModule)
153
- report(root, node, `App ${app.name} lists ${module.name}, a group module inside ${place.root}; the app lists the root module (${rootModule}), which imports every group module.`, { app: app.name, module: module.name });
154
- }
155
- else if (place.protocol === null) {
156
- report(root, node, `App ${app.name} lists ${module.name}, the application module of feature ${place.feature}; apps import only transport modules (${place.feature}-<protocol>.module.ts), which import the application module themselves.`, { app: app.name, module: module.name });
157
- }
158
- else if (!(composedBy.get(place.protocol) ?? []).includes(app.kind)) {
159
- const allowed = [...composedBy].filter(([, kinds]) => kinds.includes(app.kind)).map(([protocol]) => protocol).sort(byCodeUnit);
160
- const transports = allowed.length ? `${allowed.join(', ')} transports only` : 'no transport module';
161
- report(root, node, `App ${app.name} is of kind ${app.kind} and lists ${module.name}, a ${place.protocol} transport module; an app of kind ${app.kind} composes ${transports}.`, { app: app.name, module: module.name, protocol: place.protocol });
162
- }
163
- }
164
- function inspectModuleDeclarations(checker, target, node, root, app, scan, seen) {
165
- const { moduleMap, kit, placeOf, composedBy, report } = scan;
166
- let references = 0;
167
- for (const declaration of kit.declarationsOf(checker, target)) {
168
- const module = moduleMap.get(declaration);
169
- if (!module)
170
- continue;
171
- const place = placeOf(module.file.rel);
172
- if (!place || seen.has(node))
173
- continue;
174
- seen.add(node);
175
- references += 1;
176
- reportModuleReference(root, node, app, module, place, composedBy, report);
177
- }
178
- return references;
179
- }
180
- /** The module references of one composition file: the root's `imports` lists, or every array of a composition group (its module list). */
181
- function inspectCompositionFile(file, app, scan, seen) {
182
- const { kit, ts } = scan;
183
- const checker = kit.checkerOf(file.sourceFile);
184
- const group = isComposeGroup(file.rel);
185
- let references = 0;
186
- const judge = (node) => {
187
- const call = ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register';
188
- if (ts.isCallExpression(node) && !call)
189
- return;
190
- const target = call ? node.expression.expression : node;
191
- references += inspectModuleDeclarations(checker, target, node, file, app, scan, seen);
192
- };
193
- kit.walk(file.sourceFile, node => {
194
- if (ts.isPropertyAssignment(node) && kit.propertyNameText(node.name) === 'imports' && ts.isArrayLiteralExpression(node.initializer)) {
195
- for (const element of node.initializer.elements)
196
- judge(element);
197
- }
198
- else if (group && ts.isArrayLiteralExpression(node)) {
199
- for (const element of node.elements)
200
- judge(element);
201
- }
202
- else if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register')
203
- judge(node);
204
- return true;
205
- });
206
- return references;
207
- }
208
- /** The module references of an app: its root `apps/<app>/src/app.module.ts` and the composition groups of `compose/` it spreads. */
209
- function inspectAppRoot(app, scan) {
210
- const seen = new Set();
211
- let references = 0;
212
- for (const file of scan.kit.appCompositionFiles(app.name))
213
- references += inspectCompositionFile(file, app, scan, seen);
214
- return references;
215
- }
216
- function appModuleReferences(apps, scan) {
217
- let references = 0;
218
- for (const app of apps)
219
- references += inspectAppRoot(app, scan);
220
- return references;
221
- }
222
- export function checkModulePerTransport(input) {
223
- const { config, graph } = input;
224
- const kit = machineKit(input);
225
- const { ts, resolver } = kit;
226
- const violations = [];
227
- const report = (file, node, message, extra = {}) => violations.push({ ruleId: RULE, path: file.rel, ...kit.at(file.rel, file.sourceFile, node), message, ...extra });
228
- const plain = (rel, message, extra = {}) => violations.push({ ruleId: RULE, path: rel, line: 1, column: 1, message, ...extra });
229
- const placeOf = rel => modulePlaceOf(resolver, rel);
230
- const composedModuleOf = composedModulePath;
231
- const composedBy = composedByProtocol(resolver);
232
- const tree = treeOf(config.root);
233
- const transportModules = reportTransportFileShapes(tree, placeOf, plain, composedModuleOf);
234
- const modules = collectFeatureModules(graph, kit, ts, placeOf, composedModuleOf, report);
235
- const references = appModuleReferences(resolver.repo.apps, { graph, kit, ts, moduleMap: modules, placeOf, composedBy, report });
236
- return { violations, coverage: { status: 'checked', modules: modules.size, transportModules, appReferences: references } };
237
- }
@@ -1,126 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { createRequire } from 'node:module';
4
- import { canonical, exactKeys, isInside, slash } from './config.mjs';
5
- const CONTRACT_SCHEMA = 'starci/next-data-lifecycle@1';
6
- const SOURCE = /\.[cm]?[jt]sx?$/i;
7
- const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
8
- const BINDING = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/;
9
- const TOP_KEYS = new Set(['schema', 'swr', 'hooks']);
10
- const SWR_KEYS = new Set(['package', 'major']);
11
- const HOOK_KEYS = new Set(['id', 'path', 'export', 'kind', 'resultBinding', 'identities']);
12
- const IDENTITY_KEYS = new Set(['id', 'binding', 'gatesRequest', 'resource']);
13
- function relativeSource(repository, value, label) {
14
- repository = path.resolve(repository);
15
- if (typeof value !== 'string' || !value.trim() || path.isAbsolute(value))
16
- throw new Error(`${label} must be a non-empty repository-relative source path.`);
17
- const relative = slash(value.trim()).replace(/^\.\//, '');
18
- if (relative.split('/').includes('..') || !SOURCE.test(relative) || /\.d\.[cm]?[jt]s$/i.test(relative)) {
19
- throw new Error(`${label} must be a production TypeScript or JavaScript source path.`);
20
- }
21
- const absolute = path.resolve(repository, ...relative.split('/'));
22
- if (!isInside(repository, absolute))
23
- throw new Error(`${label} must stay inside the repository.`);
24
- for (let current = absolute; current !== repository; current = path.dirname(current)) {
25
- const stat = fs.lstatSync(current);
26
- if (stat.isSymbolicLink())
27
- throw new Error(`${label} must not cross a symbolic-link or junction ancestor.`);
28
- }
29
- const stat = fs.lstatSync(absolute);
30
- if (!stat.isFile() || !isInside(canonical(repository), canonical(absolute)))
31
- throw new Error(`${label} must be a regular source file inside the repository.`);
32
- return relative;
33
- }
34
- const CONTRACT_LABEL = 'package.json#starci.codePatterns.next.dataLifecycle';
35
- function parseSwr(swr) {
36
- exactKeys(swr, SWR_KEYS, 'dataLifecycle.swr');
37
- if (swr.package !== 'swr' || !Number.isInteger(swr.major) || swr.major < 1) {
38
- throw new Error('dataLifecycle.swr must bind package swr and one positive integer major version.');
39
- }
40
- }
41
- function parseIdentity(identity, identityLabel, seen) {
42
- exactKeys(identity, IDENTITY_KEYS, identityLabel);
43
- if (typeof identity.id !== 'string' || !identity.id.trim() || seen.ids.has(identity.id.trim()))
44
- throw new Error(`${identityLabel}.id must be unique and non-empty.`);
45
- if (typeof identity.binding !== 'string' || !BINDING.test(identity.binding) || seen.bindings.has(identity.binding))
46
- throw new Error(`${identityLabel}.binding must be a unique identifier or property path.`);
47
- if (typeof identity.gatesRequest !== 'boolean' || typeof identity.resource !== 'boolean')
48
- throw new Error(`${identityLabel} must explicitly declare gatesRequest and resource booleans.`);
49
- seen.ids.add(identity.id.trim());
50
- seen.bindings.add(identity.binding);
51
- return { id: identity.id.trim(), binding: identity.binding, gatesRequest: identity.gatesRequest, resource: identity.resource };
52
- }
53
- function parseHook(config, hook, label, ids) {
54
- exactKeys(hook, HOOK_KEYS, label);
55
- if (typeof hook.id !== 'string' || !hook.id.trim() || ids.has(hook.id.trim()))
56
- throw new Error(`${label}.id must be unique and non-empty.`);
57
- ids.add(hook.id.trim());
58
- const source = relativeSource(config.root, hook.path, `${label}.path`);
59
- if (typeof hook.export !== 'string' || !/^use[A-Z0-9_$][\w$]*$/.test(hook.export))
60
- throw new Error(`${label}.export must name one exported use* hook.`);
61
- if (!['query', 'mutation'].includes(hook.kind))
62
- throw new Error(`${label}.kind must be query or mutation.`);
63
- if (hook.resultBinding !== undefined && (typeof hook.resultBinding !== 'string' || !IDENTIFIER.test(hook.resultBinding))) {
64
- throw new Error(`${label}.resultBinding must be one local identifier.`);
65
- }
66
- if (!Array.isArray(hook.identities))
67
- throw new Error(`${label}.identities must be an array.`);
68
- const seen = { ids: new Set(), bindings: new Set() };
69
- const identities = hook.identities.map((identity, identityIndex) => parseIdentity(identity, `${label}.identities[${identityIndex}]`, seen));
70
- if (hook.kind === 'mutation' && !identities.some((identity) => identity.resource))
71
- throw new Error(`${label} mutation must declare at least one resource identity.`);
72
- return { id: hook.id.trim(), path: source, export: hook.export, kind: hook.kind,
73
- ...(hook.resultBinding === undefined ? {} : { resultBinding: hook.resultBinding }), identities };
74
- }
75
- export function parseContract(config) {
76
- // The contract is declared in the app's one package.json (the app root; config.root is the fe side folder).
77
- const manifest = JSON.parse(fs.readFileSync(path.join(config.packageRoot ?? config.root, 'package.json'), 'utf8'));
78
- const next = manifest?.starci?.codePatterns?.next;
79
- const value = next?.dataLifecycle;
80
- if (value === undefined)
81
- return null;
82
- if (next.schema !== 'starci/next-code-pattern-contract@1')
83
- throw new Error('package.json#starci.codePatterns.next must use starci/next-code-pattern-contract@1.');
84
- exactKeys(value, TOP_KEYS, CONTRACT_LABEL);
85
- if (value.schema !== CONTRACT_SCHEMA)
86
- throw new Error(`dataLifecycle.schema must be ${CONTRACT_SCHEMA}.`);
87
- parseSwr(value.swr);
88
- if (!Array.isArray(value.hooks) || value.hooks.length === 0)
89
- throw new Error('dataLifecycle.hooks must contain at least one declared lifecycle call.');
90
- const ids = new Set();
91
- const hooks = value.hooks.map((hook, index) => parseHook(config, hook, `dataLifecycle.hooks[${index}]`, ids));
92
- return { schema: value.schema, swr: { package: 'swr', major: value.swr.major }, hooks };
93
- }
94
- export function installedSWR(repository) {
95
- const require = createRequire(path.join(repository, 'package.json'));
96
- let manifest;
97
- try {
98
- manifest = require.resolve('swr/package.json');
99
- }
100
- catch {
101
- let entry;
102
- try {
103
- entry = require.resolve('swr');
104
- }
105
- catch {
106
- throw new Error('Installed swr package is unavailable.');
107
- }
108
- let current = path.dirname(entry);
109
- while (isInside(repository, current) && current !== repository) {
110
- const candidate = path.join(current, 'package.json');
111
- if (fs.existsSync(candidate)) {
112
- manifest = candidate;
113
- break;
114
- }
115
- current = path.dirname(current);
116
- }
117
- }
118
- if (!manifest)
119
- throw new Error('Installed swr package.json is unavailable.');
120
- const actualManifest = canonical(manifest);
121
- const parsed = JSON.parse(fs.readFileSync(actualManifest, 'utf8'));
122
- const major = Number.parseInt(String(parsed.version ?? '').split('.')[0] ?? '', 10);
123
- if (parsed.name !== 'swr' || !Number.isInteger(major))
124
- throw new Error('Installed swr package identity or version is invalid.');
125
- return { name: parsed.name, version: parsed.version, major, root: canonical(path.dirname(actualManifest)) };
126
- }
@@ -1,192 +0,0 @@
1
- import { unwrapExpression } from './typescript.mjs';
2
- import { anyDescendant, normalizedSymbol } from './ast-walks.mjs';
3
- import { sourceLocation } from '../../lib/ts-ast.mjs';
4
- import { functionNode, constInitializer, inferredSymbol } from './next-data-symbols.mjs';
5
- export function inferredFetcher(ts, checker, input, seen = new Set()) {
6
- if (!input)
7
- return null;
8
- const expression = unwrapExpression(ts, input);
9
- if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression))
10
- return expression;
11
- const symbol = normalizedSymbol(ts, checker, expression);
12
- if (!symbol || seen.has(symbol))
13
- return null;
14
- for (const declaration of symbol.getDeclarations?.() ?? []) {
15
- const fn = functionNode(ts, declaration);
16
- if (fn)
17
- return fn;
18
- }
19
- const initializer = constInitializer(ts, symbol);
20
- return initializer ? inferredFetcher(ts, checker, initializer, new Set(seen).add(symbol)) : null;
21
- }
22
- function importedIdentityReference(ts, checker, expression) {
23
- const reference = ts.isShorthandPropertyAssignment(expression.parent)
24
- ? checker.getShorthandAssignmentValueSymbol(expression.parent) : checker.getSymbolAtLocation(expression);
25
- return (reference?.getDeclarations?.() ?? []).some(declaration => ts.isImportSpecifier(declaration)
26
- || ts.isImportClause(declaration) || ts.isNamespaceImport(declaration) || ts.isImportEqualsDeclaration(declaration));
27
- }
28
- function identifierIdentityAccess(ts, checker, expression, seen) {
29
- const imported = importedIdentityReference(ts, checker, expression);
30
- const symbol = inferredSymbol(ts, checker, expression);
31
- const initializer = symbol && !seen.has(symbol) ? constInitializer(ts, symbol) : null;
32
- if (initializer && symbol) {
33
- const alias = identityAccess(ts, checker, initializer, new Set(seen).add(symbol));
34
- if (alias)
35
- return { ...alias, imported: imported || alias.imported };
36
- }
37
- return { symbol, parts: [expression.text], imported };
38
- }
39
- function isSelectedMember(ts, expression) {
40
- return ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression)
41
- && ts.isStringLiteralLike(expression.argumentExpression);
42
- }
43
- function selectedMemberKey(ts, expression) {
44
- return ts.isPropertyAccessExpression(expression) ? expression.name.text : expression.argumentExpression.text;
45
- }
46
- function identityAccess(ts, checker, input, seen = new Set()) {
47
- const expression = unwrapExpression(ts, input);
48
- if (ts.isIdentifier(expression))
49
- return identifierIdentityAccess(ts, checker, expression, seen);
50
- if (isSelectedMember(ts, expression)) {
51
- const base = identityAccess(ts, checker, expression.expression, seen);
52
- return base ? { ...base, parts: [...base.parts, selectedMemberKey(ts, expression)] } : null;
53
- }
54
- return null;
55
- }
56
- function containerWritten(ts, checker, symbol, source) {
57
- const incrementOperators = [ts.SyntaxKind.PlusPlusToken, ts.SyntaxKind.MinusMinusToken];
58
- const rootSymbol = (input) => {
59
- let expression = unwrapExpression(ts, input);
60
- while (ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression))
61
- expression = unwrapExpression(ts, expression.expression);
62
- return ts.isIdentifier(expression) ? inferredSymbol(ts, checker, expression) : null;
63
- };
64
- return anyDescendant(ts, source, (node) => {
65
- if (ts.isBinaryExpression(node) && node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment
66
- && node.operatorToken.kind <= ts.SyntaxKind.LastAssignment)
67
- return rootSymbol(node.left) === symbol;
68
- if (ts.isDeleteExpression(node))
69
- return rootSymbol(node.expression) === symbol;
70
- if (ts.isPostfixUnaryExpression(node) || ts.isPrefixUnaryExpression(node)
71
- && incrementOperators.includes(node.operator))
72
- return rootSymbol(node.operand) === symbol;
73
- return false;
74
- });
75
- }
76
- function selectedRequestAlias(ts, checker, expression, seen) {
77
- const symbol = inferredSymbol(ts, checker, expression);
78
- const initializer = symbol && !seen.has(symbol) ? constInitializer(ts, symbol) : null;
79
- if (!initializer || !symbol)
80
- return null;
81
- const value = unwrapExpression(ts, initializer);
82
- if (ts.isObjectLiteralExpression(value) && (initializer.getSourceFile() !== expression.getSourceFile()
83
- || containerWritten(ts, checker, symbol, expression.getSourceFile())))
84
- return null;
85
- if (ts.isObjectLiteralExpression(value) || ts.isIdentifier(value) || ts.isPropertyAccessExpression(value)
86
- || ts.isElementAccessExpression(value))
87
- return { symbol, value };
88
- return null;
89
- }
90
- function selectedRequestProperty(ts, object, key) {
91
- if (!ts.isObjectLiteralExpression(object) || !object.properties.every((property) => ts.isPropertyAssignment(property)
92
- || ts.isShorthandPropertyAssignment(property)))
93
- return null;
94
- return [...object.properties].reverse().find(candidate => (ts.isIdentifier(candidate.name)
95
- || ts.isStringLiteralLike(candidate.name)) && candidate.name.text === key) ?? null;
96
- }
97
- function selectedRequestValue(ts, checker, input, seen = new Set()) {
98
- const expression = unwrapExpression(ts, input);
99
- if (ts.isIdentifier(expression)) {
100
- const alias = selectedRequestAlias(ts, checker, expression, seen);
101
- return alias ? selectedRequestValue(ts, checker, alias.value, new Set(seen).add(alias.symbol)) : expression;
102
- }
103
- if (!isSelectedMember(ts, expression))
104
- return expression;
105
- const object = selectedRequestValue(ts, checker, expression.expression, seen);
106
- const property = selectedRequestProperty(ts, object, selectedMemberKey(ts, expression));
107
- if (!property)
108
- return expression;
109
- const value = ts.isShorthandPropertyAssignment(property) ? property.name : property.initializer;
110
- return selectedRequestValue(ts, checker, value, seen);
111
- }
112
- function capturedInput(ts, access, region) {
113
- const declarations = access.symbol?.getDeclarations?.() ?? [];
114
- return declarations.some(declaration => {
115
- if (declaration.getSourceFile() === region.getSourceFile() && declaration.pos >= region.pos && declaration.end <= region.end)
116
- return false;
117
- if (declaration.getSourceFile().isDeclarationFile && !access.imported)
118
- return false;
119
- return access.imported || ts.isVariableDeclaration(declaration) || ts.isParameter(declaration) || ts.isBindingElement(declaration);
120
- });
121
- }
122
- function capturedIdentityAlias(ts, access, seen) {
123
- // Follow transparent aliases and request objects, so {id} and an alias of id carry the same identity.
124
- if (access.parts.length !== 1 || (access.symbol ? seen.has(access.symbol) : false))
125
- return null;
126
- const initializer = constInitializer(ts, access.symbol);
127
- if (!initializer)
128
- return null;
129
- const value = unwrapExpression(ts, initializer);
130
- return ts.isCallExpression(value) || ts.isAwaitExpression(value) || ts.isNewExpression(value) ? null : value;
131
- }
132
- function recordInferredIdentity(scope, access, seen, visit) {
133
- const { ts, call, region, reasons, identities } = scope;
134
- if (access?.imported && !(access.symbol?.getDeclarations?.() ?? []).length) {
135
- reasons.push(`${call.source.fileName}:${sourceLocation(call.source, call.node).line} imported SWR request input ${access.parts.join('.')} cannot be resolved; dataLifecycle is required to declare identities`);
136
- return true;
137
- }
138
- if (!access || !capturedInput(ts, access, region))
139
- return false;
140
- const alias = capturedIdentityAlias(ts, access, seen);
141
- if (alias && access.symbol)
142
- visit(alias, new Set(seen).add(access.symbol));
143
- else {
144
- const binding = access.parts.join('.');
145
- identities.set(binding, { id: binding, binding, ...access, gatesRequest: false, resource: call.kind === 'mutation' });
146
- }
147
- return true;
148
- }
149
- function visitRequestChildren(ts, node, seen, visit) {
150
- if (ts.isPropertyAssignment(node)) {
151
- visit(node.initializer, seen);
152
- return;
153
- }
154
- // A method name is not a request input; its receiver and arguments can be.
155
- if (ts.isCallExpression(node)) {
156
- const callee = unwrapExpression(ts, node.expression);
157
- if (ts.isPropertyAccessExpression(callee) || ts.isElementAccessExpression(callee))
158
- visit(callee.expression, seen);
159
- for (const argument of node.arguments)
160
- visit(argument, seen);
161
- return;
162
- }
163
- if (ts.isVariableDeclaration(node)) {
164
- if (node.initializer)
165
- visit(node.initializer, seen);
166
- return;
167
- }
168
- ts.forEachChild(node, (child) => { visit(child, seen); });
169
- }
170
- /** Infer lexical request inputs; parameters declared inside the fetcher are supplied by SWR. */
171
- export function inferredIdentities(context, call, region, reasons) {
172
- const { ts } = context;
173
- const identities = new Map();
174
- const scope = { ts, call, region, reasons, identities };
175
- const visit = (node, seen = new Set()) => {
176
- const selectedValue = selectedRequestValue(ts, call.checker, node);
177
- if (selectedValue !== node && selectedValue !== unwrapExpression(ts, node)) {
178
- visit(selectedValue, seen);
179
- return;
180
- }
181
- if (recordInferredIdentity(scope, identityAccess(ts, call.checker, node), seen, visit))
182
- return;
183
- visitRequestChildren(ts, node, seen, visit);
184
- };
185
- visit(region.body ?? region);
186
- return [...identities.values()];
187
- }
188
- export function inferredKeyBindings(context, call, reasons) {
189
- const key = call.node.arguments[0];
190
- // A key or its null guard can contain bindings that are not captured request inputs.
191
- return key ? inferredIdentities(context, call, key, reasons).map(identity => ({ ...identity, id: `key:${identity.id}`, resource: false })) : [];
192
- }