@starci/eslint-canon-be 3.8.19 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +1 -1
  3. package/runtime/bundle.json +192 -0
  4. package/runtime/engine/by-code-unit.mjs +6 -6
  5. package/runtime/engine/model-config.mjs +98 -76
  6. package/runtime/engine/orca-config.mjs +22 -17
  7. package/runtime/engine/release-config.mjs +22 -18
  8. package/runtime/engine/removed-vocabulary.mjs +21 -20
  9. package/runtime/engine/resources-config.mjs +42 -35
  10. package/runtime/engine/runtime-root.mjs +11 -13
  11. package/runtime/engine/secrets.mjs +170 -102
  12. package/runtime/engine/sonar-config.mjs +16 -13
  13. package/runtime/engine/temp-root.mjs +3 -9
  14. package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
  15. package/runtime/knowledge/hfs/rules.yaml +6 -6
  16. package/runtime/knowledge/hfs/slots.yaml +2 -2
  17. package/runtime/knowledge/sonar-gate.yaml +1 -1
  18. package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
  19. package/runtime/scripts/api/git/cat-file.mjs +0 -1
  20. package/runtime/scripts/api/git/config-get.mjs +1 -2
  21. package/runtime/scripts/api/git/diff.mjs +0 -1
  22. package/runtime/scripts/api/git/lib.mjs +32 -32
  23. package/runtime/scripts/api/git/ls-files.mjs +0 -1
  24. package/runtime/scripts/api/git/ls-tree.mjs +0 -1
  25. package/runtime/scripts/api/git/merge-base.mjs +2 -3
  26. package/runtime/scripts/api/git/remote.mjs +0 -1
  27. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
  28. package/runtime/scripts/api/git/show.mjs +0 -1
  29. package/runtime/scripts/hfs/allows.mjs +37 -38
  30. package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
  31. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
  32. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
  33. package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
  34. package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
  35. package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
  36. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
  37. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
  38. package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
  39. package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
  40. package/runtime/scripts/hfs/architecture/config.mjs +302 -268
  41. package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
  42. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
  43. package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
  44. package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
  45. package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
  46. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
  47. package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
  48. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
  49. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
  50. package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
  51. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
  52. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
  53. package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
  54. package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
  55. package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
  56. package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
  57. package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
  58. package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
  59. package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
  60. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
  61. package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
  62. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
  63. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
  64. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
  65. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
  66. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
  67. package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
  68. package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
  69. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
  70. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
  71. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
  72. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
  73. package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
  74. package/runtime/scripts/hfs/architecture/index.mjs +272 -272
  75. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
  76. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
  77. package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
  78. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
  79. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
  80. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
  81. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
  82. package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
  83. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
  84. package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
  85. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
  86. package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
  87. package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
  88. package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
  89. package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
  90. package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
  91. package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
  92. package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
  93. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
  94. package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
  95. package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
  96. package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
  97. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
  98. package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
  99. package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
  100. package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
  101. package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
  102. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
  103. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
  104. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
  105. package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
  106. package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
  107. package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
  108. package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
  109. package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
  110. package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
  111. package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
  112. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
  113. package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
  114. package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
  115. package/runtime/scripts/hfs/edition-slots.mjs +43 -40
  116. package/runtime/scripts/hfs/linear-text.mjs +20 -20
  117. package/runtime/scripts/hfs/param-names.mjs +6 -11
  118. package/runtime/scripts/hfs/path-findings.mjs +120 -100
  119. package/runtime/scripts/hfs/repo-identity.mjs +55 -42
  120. package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
  121. package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
  122. package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
  123. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
  124. package/runtime/scripts/hfs/rules/read.mjs +19 -15
  125. package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
  126. package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
  127. package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
  128. package/runtime/scripts/hfs/slot-classify.mjs +42 -38
  129. package/runtime/scripts/hfs/slot-errors.mjs +8 -7
  130. package/runtime/scripts/hfs/slot-imports.mjs +70 -63
  131. package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
  132. package/runtime/scripts/hfs/slot-match.mjs +122 -111
  133. package/runtime/scripts/hfs/slot-path.mjs +0 -2
  134. package/runtime/scripts/hfs/slot-required.mjs +65 -59
  135. package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
  136. package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
  137. package/runtime/scripts/hfs/slots.mjs +175 -152
  138. package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
  139. package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
  140. package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
  141. package/runtime/scripts/hfs/view.mjs +38 -40
  142. package/runtime/scripts/lib/dockerfile.mjs +131 -114
  143. package/runtime/scripts/lib/env.mjs +3 -6
  144. package/runtime/scripts/lib/event-contract.mjs +73 -78
  145. package/runtime/scripts/lib/git.mjs +15 -16
  146. package/runtime/scripts/lib/glob.mjs +23 -14
  147. package/runtime/scripts/lib/grammar-lint.mjs +33 -37
  148. package/runtime/scripts/lib/i18n.mjs +32 -35
  149. package/runtime/scripts/lib/in-order.mjs +57 -38
  150. package/runtime/scripts/lib/json-clone.mjs +45 -36
  151. package/runtime/scripts/lib/json.mjs +26 -14
  152. package/runtime/scripts/lib/language.mjs +133 -129
  153. package/runtime/scripts/lib/list.mjs +9 -14
  154. package/runtime/scripts/lib/mutation-fence.mjs +10 -5
  155. package/runtime/scripts/lib/path-key.mjs +53 -38
  156. package/runtime/scripts/lib/read-text.mjs +30 -17
  157. package/runtime/scripts/lib/regex.mjs +1 -2
  158. package/runtime/scripts/lib/same-text.mjs +0 -1
  159. package/runtime/scripts/lib/sops-envelope.mjs +148 -120
  160. package/runtime/scripts/lib/stack-services.mjs +128 -129
  161. package/runtime/scripts/lib/ts-analysis.mjs +28 -26
  162. package/runtime/scripts/lib/ts-ast.mjs +36 -42
  163. package/runtime/scripts/lib/walk.mjs +33 -27
  164. package/runtime/scripts/lib/yaml-cached.mjs +4 -4
@@ -3,7 +3,6 @@ import { isComposeGroup, machineKit } from './machine-ast.mjs';
3
3
  import { treeOf } from './required-files.mjs';
4
4
  import { byCodeUnit } from '../../lib/list.mjs';
5
5
  import { trimTrailingSlashes } from '../trailing-slashes.mjs';
6
-
7
6
  /**
8
7
  * R45 `module-per-transport` (BE_MODULE_SHAPE, BE-CONVENTION 1.2 and 1.6). A feature has one Nest module for its
9
8
  * application (`<f>.module.ts` at the feature root) and exactly one module per transport folder
@@ -23,205 +22,211 @@ import { trimTrailingSlashes } from '../trailing-slashes.mjs';
23
22
  * other kind lists none of its modules.
24
23
  */
25
24
  export const MODULE_PER_TRANSPORT_RULE_IDS = ['BE_MODULE_SHAPE'];
26
-
27
25
  const RULE = 'BE_MODULE_SHAPE';
28
-
29
26
  function featureOwnerOf(resolver, rel) {
30
- const owner = resolver.ownerOf(rel);
31
- return owner && resolver.slot(owner.slot)?.tier === 'feature' ? owner : null;
27
+ const owner = resolver.ownerOf(rel);
28
+ return owner && resolver.slot(owner.slot)?.tier === 'feature' ? owner : null;
32
29
  }
33
-
34
30
  function modulePlaceOf(resolver, rel) {
35
- const owner = featureOwnerOf(resolver, rel);
36
- if (!owner) return null;
37
- const slot = resolver.slot(owner.slot);
38
- const below = rel.slice(owner.root.length + 1).split('/');
39
- const feature = path.posix.basename(owner.root);
40
- if (slot.composedBy) return { root: owner.root, feature, protocol: null, below, composed: slot };
41
- return { root: owner.root, feature, protocol: below[0] === 'transport' && below.length > 2 ? below[1] : null, below, composed: null };
42
- }
43
-
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
+ }
44
41
  function composedModulePath(rel) {
45
- const dir = path.posix.dirname(rel);
46
- return `${dir}/${path.posix.basename(dir)}.module.ts`;
42
+ const dir = path.posix.dirname(rel);
43
+ return `${dir}/${path.posix.basename(dir)}.module.ts`;
47
44
  }
48
-
49
45
  function composedByProtocol(resolver) {
50
- const composedBy = new Map();
51
- for (const slot of resolver.slots()) {
52
- if (!slot.composedBy || slot.owner) continue;
53
- composedBy.set(path.posix.basename(trimTrailingSlashes(slot.path)), slot.composedBy);
54
- }
55
- return composedBy;
56
- }
57
-
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
+ }
58
54
  /** The module a feature file may declare: the application module, the transport module of its folder, or a cli folder's module. */
59
55
  function ownModuleOf(place, rel, composedModuleOf) {
60
- if (place.composed) return composedModuleOf(rel);
61
- return place.protocol === null
62
- ? `${place.root}/${place.feature}.module.ts`
63
- : `${place.root}/transport/${place.protocol}/${place.feature}-${place.protocol}.module.ts`;
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`;
64
61
  }
65
-
66
62
  const DEFINITION_FILE = /\.module-definition\.[cm]?tsx?$/u;
67
- const definitionOf = modulePath => modulePath.replace(/\.module\.([cm]?tsx?)$/u, '.module-definition.$1');
68
-
63
+ const definitionOf = (modulePath) => modulePath.replace(/\.module\.([cm]?tsx?)$/u, '.module-definition.$1');
69
64
  function reportTransportFileShapes(tree, placeOf, plain, composedModuleOf) {
70
- let transportModules = 0;
71
- for (const rel of [...tree.files].sort(byCodeUnit)) {
72
- const place = placeOf(rel);
73
- if (!place) continue;
74
- const base = path.posix.basename(rel);
75
- if (DEFINITION_FILE.test(base) && rel !== definitionOf(ownModuleOf(place, rel, composedModuleOf))) 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 });
76
- if (place.protocol && /\.module\.[cm]?tsx?$/u.test(base)) {
77
- const expected = `${place.feature}-${place.protocol}.module.ts`;
78
- if (place.below.length !== 3 || base !== expected) 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 });
79
- else transportModules += 1;
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
80
  }
81
- }
82
- return transportModules;
81
+ return transportModules;
83
82
  }
84
-
85
83
  function isNestModuleClass(ts, kit, checker, statement) {
86
- return kit.decorators(statement).some(decorator => {
87
- const call = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
88
- return kit.isImportOf(checker, call, 'Module', '@nestjs/common');
89
- });
90
- }
91
-
92
- function reportModuleClassLocation(file, statement, place, composedModuleOf, report) {
93
- if (place.composed) {
94
- const own = composedModuleOf(file.rel);
95
- if (file.rel !== own) report(file, statement.name, `${statement.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 });
96
- return;
97
- }
98
- const own = place.protocol === null
99
- ? `${place.root}/${place.feature}.module.ts`
100
- : `${place.root}/transport/${place.protocol}/${place.feature}-${place.protocol}.module.ts`;
101
- if (file.rel !== own) report(file, statement.name, `${statement.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 });
102
- }
103
-
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
- } else if (ts.isHeritageClause(node) && file.rel !== own) {
111
- for (const type of node.types) if (kit.isImportOf(checker, type.expression, 'ConfigurableModuleClass', '@nestjs/common')) {
112
- 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 });
113
- }
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;
114
95
  }
115
- return true;
116
- });
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
+ function reportConfigurableModuleUses(file, checker, kit, ts, place, env) {
103
+ const own = ownModuleOf(place, file.rel, env.composedModuleOf);
104
+ const isDefinition = file.rel === definitionOf(own);
105
+ kit.walk(file.sourceFile, node => {
106
+ if (ts.isNewExpression(node) && kit.isImportOf(checker, node.expression, 'ConfigurableModuleBuilder', '@nestjs/common') && !isDefinition) {
107
+ 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 });
108
+ }
109
+ else if (ts.isHeritageClause(node) && file.rel !== own) {
110
+ for (const type of node.types)
111
+ if (kit.isImportOf(checker, type.expression, 'ConfigurableModuleClass', '@nestjs/common')) {
112
+ 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 });
113
+ }
114
+ }
115
+ return true;
116
+ });
117
117
  }
118
-
119
118
  function moduleClassesInFile(file, checker, place, modules, env) {
120
- const { kit, ts, composedModuleOf, report } = env;
121
- for (const statement of file.sourceFile.statements) {
122
- if (!ts.isClassDeclaration(statement) || !statement.name) continue;
123
- if (!isNestModuleClass(ts, kit, checker, statement)) continue;
124
- modules.set(statement, { name: statement.name.text, file });
125
- if (place) reportModuleClassLocation(file, statement, place, composedModuleOf, report);
126
- }
127
- }
128
-
119
+ const { kit, ts, composedModuleOf, report } = env;
120
+ for (const statement of file.sourceFile.statements) {
121
+ if (!ts.isClassDeclaration(statement) || !statement.name)
122
+ continue;
123
+ if (!isNestModuleClass(ts, kit, checker, statement))
124
+ continue;
125
+ modules.set(statement, { name: statement.name.text, file });
126
+ if (place)
127
+ reportModuleClassLocation(file, statement.name, place, composedModuleOf, report);
128
+ }
129
+ }
129
130
  function collectFeatureModules(graph, kit, ts, placeOf, composedModuleOf, report) {
130
- const modules = new Map(); // class declaration -> {name, file}
131
- const env = { kit, ts, composedModuleOf, report };
132
- for (const file of graph.files.values()) {
133
- const checker = kit.checkerOf(file.sourceFile);
134
- const place = placeOf(file.rel);
135
- moduleClassesInFile(file, checker, place, modules, env);
136
- if (place) reportConfigurableModuleUses(file, checker, kit, ts, place, env);
137
- }
138
- return modules;
131
+ const modules = new Map(); // class declaration -> {name, file}
132
+ const env = { kit, ts, composedModuleOf, report };
133
+ for (const file of graph.files.values()) {
134
+ const checker = kit.checkerOf(file.sourceFile);
135
+ const place = placeOf(file.rel);
136
+ moduleClassesInFile(file, checker, place, modules, env);
137
+ if (place)
138
+ reportConfigurableModuleUses(file, checker, kit, ts, place, env);
139
+ }
140
+ return modules;
139
141
  }
140
-
141
142
  function reportModuleReference(root, node, app, module, place, composedBy, report) {
142
- if (place.composed) {
143
- const rootModule = `${place.root}/${place.feature}.module.ts`;
144
- if (!place.composed.composedBy.includes(app.kind)) 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 });
145
- else if (module.file.rel !== rootModule) 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 });
146
- } else if (place.protocol === null) {
147
- 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 });
148
- } else if (!(composedBy.get(place.protocol) ?? []).includes(app.kind)) {
149
- const allowed = [...composedBy].filter(([, kinds]) => kinds.includes(app.kind)).map(([protocol]) => protocol).sort(byCodeUnit);
150
- const transports = allowed.length ? `${allowed.join(', ')} transports only` : 'no transport module';
151
- 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 });
152
- }
153
- }
154
-
143
+ if (place.composed) {
144
+ const rootModule = `${place.root}/${place.feature}.module.ts`;
145
+ if (!place.composed.composedBy.includes(app.kind))
146
+ 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 });
147
+ else if (module.file.rel !== rootModule)
148
+ 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 });
149
+ }
150
+ else if (place.protocol === null) {
151
+ 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 });
152
+ }
153
+ else if (!(composedBy.get(place.protocol) ?? []).includes(app.kind)) {
154
+ const allowed = [...composedBy].filter(([, kinds]) => kinds.includes(app.kind)).map(([protocol]) => protocol).sort(byCodeUnit);
155
+ const transports = allowed.length ? `${allowed.join(', ')} transports only` : 'no transport module';
156
+ 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 });
157
+ }
158
+ }
155
159
  function inspectModuleDeclarations(checker, target, node, root, app, scan, seen) {
156
- const { moduleMap, kit, placeOf, composedBy, report } = scan;
157
- let references = 0;
158
- for (const declaration of kit.declarationsOf(checker, target)) {
159
- const module = moduleMap.get(declaration);
160
- if (!module) continue;
161
- const place = placeOf(module.file.rel);
162
- if (!place || seen.has(node)) continue;
163
- seen.add(node);
164
- references += 1;
165
- reportModuleReference(root, node, app, module, place, composedBy, report);
166
- }
167
- return references;
168
- }
169
-
160
+ const { moduleMap, kit, placeOf, composedBy, report } = scan;
161
+ let references = 0;
162
+ for (const declaration of kit.declarationsOf(checker, target)) {
163
+ const module = moduleMap.get(declaration);
164
+ if (!module)
165
+ continue;
166
+ const place = placeOf(module.file.rel);
167
+ if (!place || seen.has(node))
168
+ continue;
169
+ seen.add(node);
170
+ references += 1;
171
+ reportModuleReference(root, node, app, module, place, composedBy, report);
172
+ }
173
+ return references;
174
+ }
170
175
  /** The module references of one composition file: the root's `imports` lists, or every array of a composition group (its module list). */
171
176
  function inspectCompositionFile(file, app, scan, seen) {
172
- const { kit, ts } = scan;
173
- const checker = kit.checkerOf(file.sourceFile);
174
- const group = isComposeGroup(file.rel);
175
- let references = 0;
176
- const judge = node => {
177
- const call = ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register';
178
- if (ts.isCallExpression(node) && !call) return;
179
- const target = call ? node.expression.expression : node;
180
- references += inspectModuleDeclarations(checker, target, node, file, app, scan, seen);
181
- };
182
- kit.walk(file.sourceFile, node => {
183
- if (ts.isPropertyAssignment(node) && kit.propertyNameText(node.name) === 'imports' && ts.isArrayLiteralExpression(node.initializer)) {
184
- for (const element of node.initializer.elements) judge(element);
185
- } else if (group && ts.isArrayLiteralExpression(node)) {
186
- for (const element of node.elements) judge(element);
187
- } else if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register') judge(node);
188
- return true;
189
- });
190
- return references;
191
- }
192
-
177
+ const { kit, ts } = scan;
178
+ const checker = kit.checkerOf(file.sourceFile);
179
+ const group = isComposeGroup(file.rel);
180
+ let references = 0;
181
+ const judge = (node) => {
182
+ const call = ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register';
183
+ if (ts.isCallExpression(node) && !call)
184
+ return;
185
+ const target = call ? node.expression.expression : node;
186
+ references += inspectModuleDeclarations(checker, target, node, file, app, scan, seen);
187
+ };
188
+ kit.walk(file.sourceFile, node => {
189
+ if (ts.isPropertyAssignment(node) && kit.propertyNameText(node.name) === 'imports' && ts.isArrayLiteralExpression(node.initializer)) {
190
+ for (const element of node.initializer.elements)
191
+ judge(element);
192
+ }
193
+ else if (group && ts.isArrayLiteralExpression(node)) {
194
+ for (const element of node.elements)
195
+ judge(element);
196
+ }
197
+ else if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'register')
198
+ judge(node);
199
+ return true;
200
+ });
201
+ return references;
202
+ }
193
203
  /** The module references of an app: its root `apps/<app>/src/app.module.ts` and the composition groups of `compose/` it spreads. */
194
204
  function inspectAppRoot(app, scan) {
195
- const seen = new Set();
196
- let references = 0;
197
- for (const file of scan.kit.appCompositionFiles(app.name)) references += inspectCompositionFile(file, app, scan, seen);
198
- return references;
205
+ const seen = new Set();
206
+ let references = 0;
207
+ for (const file of scan.kit.appCompositionFiles(app.name))
208
+ references += inspectCompositionFile(file, app, scan, seen);
209
+ return references;
199
210
  }
200
-
201
211
  function appModuleReferences(apps, scan) {
202
- let references = 0;
203
- for (const app of apps) references += inspectAppRoot(app, scan);
204
- return references;
212
+ let references = 0;
213
+ for (const app of apps)
214
+ references += inspectAppRoot(app, scan);
215
+ return references;
205
216
  }
206
-
207
217
  export function checkModulePerTransport(input) {
208
- const { config, graph } = input;
209
- const kit = machineKit(input);
210
- const { ts, resolver } = kit;
211
- const violations = [];
212
- const report = (file, node, message, extra = {}) => violations.push({ ruleId: RULE, path: file.rel, ...kit.at(file.rel, file.sourceFile, node), message, ...extra });
213
- const plain = (rel, message, extra = {}) => violations.push({ ruleId: RULE, path: rel, line: 1, column: 1, message, ...extra });
214
-
215
- const placeOf = rel => modulePlaceOf(resolver, rel);
216
- const composedModuleOf = composedModulePath;
217
-
218
- const composedBy = composedByProtocol(resolver);
219
-
220
- const tree = treeOf(config.root);
221
- const transportModules = reportTransportFileShapes(tree, placeOf, plain, composedModuleOf);
222
-
223
- const modules = collectFeatureModules(graph, kit, ts, placeOf, composedModuleOf, report);
224
-
225
- const references = appModuleReferences(resolver.repo.apps, { graph, kit, ts, moduleMap: modules, placeOf, composedBy, report });
226
- return { violations, coverage: { status: 'checked', modules: modules.size, transportModules, appReferences: references } };
218
+ const { config, graph } = input;
219
+ const kit = machineKit(input);
220
+ const { ts, resolver } = kit;
221
+ const violations = [];
222
+ const report = (file, node, message, extra = {}) => violations.push({ ruleId: RULE, path: file.rel, ...kit.at(file.rel, file.sourceFile, node), message, ...extra });
223
+ const plain = (rel, message, extra = {}) => violations.push({ ruleId: RULE, path: rel, line: 1, column: 1, message, ...extra });
224
+ const placeOf = rel => modulePlaceOf(resolver, rel);
225
+ const composedModuleOf = composedModulePath;
226
+ const composedBy = composedByProtocol(resolver);
227
+ const tree = treeOf(config.root);
228
+ const transportModules = reportTransportFileShapes(tree, placeOf, plain, composedModuleOf);
229
+ const modules = collectFeatureModules(graph, kit, ts, placeOf, composedModuleOf, report);
230
+ const references = appModuleReferences(resolver.repo.apps, { graph, kit, ts, moduleMap: modules, placeOf, composedBy, report });
231
+ return { violations, coverage: { status: 'checked', modules: modules.size, transportModules, appReferences: references } };
227
232
  }
@@ -2,7 +2,6 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { createRequire } from 'node:module';
4
4
  import { canonical, exactKeys, isInside, slash } from './config.mjs';
5
-
6
5
  const CONTRACT_SCHEMA = 'starci/next-data-lifecycle@1';
7
6
  const SOURCE = /\.[cm]?[jt]sx?$/i;
8
7
  const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
@@ -12,93 +11,116 @@ const SWR_KEYS = new Set(['package', 'major']);
12
11
  const HOOK_KEYS = new Set(['id', 'path', 'export', 'kind', 'resultBinding', 'identities']);
13
12
  const IDENTITY_KEYS = new Set(['id', 'binding', 'gatesRequest', 'resource']);
14
13
  function relativeSource(repository, value, label) {
15
- repository = path.resolve(repository);
16
- if (typeof value !== 'string' || !value.trim() || path.isAbsolute(value)) 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)) throw new Error(`${label} must stay inside the repository.`);
23
- for (let current = absolute; current !== repository; current = path.dirname(current)) {
24
- const stat = fs.lstatSync(current);
25
- if (stat.isSymbolicLink()) throw new Error(`${label} must not cross a symbolic-link or junction ancestor.`);
26
- }
27
- const stat = fs.lstatSync(absolute);
28
- if (!stat.isFile() || !isInside(canonical(repository), canonical(absolute))) throw new Error(`${label} must be a regular source file inside the repository.`);
29
- return relative;
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;
30
33
  }
31
-
32
34
  const CONTRACT_LABEL = 'package.json#starci.codePatterns.next.dataLifecycle';
33
-
34
35
  function parseSwr(swr) {
35
- exactKeys(swr, SWR_KEYS, 'dataLifecycle.swr');
36
- if (swr.package !== 'swr' || !Number.isInteger(swr.major) || swr.major < 1) {
37
- throw new Error('dataLifecycle.swr must bind package swr and one positive integer major version.');
38
- }
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
+ }
39
40
  }
40
-
41
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())) throw new Error(`${identityLabel}.id must be unique and non-empty.`);
44
- if (typeof identity.binding !== 'string' || !BINDING.test(identity.binding) || seen.bindings.has(identity.binding)) throw new Error(`${identityLabel}.binding must be a unique identifier or property path.`);
45
- if (typeof identity.gatesRequest !== 'boolean' || typeof identity.resource !== 'boolean') throw new Error(`${identityLabel} must explicitly declare gatesRequest and resource booleans.`);
46
- seen.ids.add(identity.id.trim());
47
- seen.bindings.add(identity.binding);
48
- return { id: identity.id.trim(), binding: identity.binding, gatesRequest: identity.gatesRequest, resource: identity.resource };
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 };
49
52
  }
50
-
51
53
  function parseHook(config, hook, label, ids) {
52
- exactKeys(hook, HOOK_KEYS, label);
53
- if (typeof hook.id !== 'string' || !hook.id.trim() || ids.has(hook.id.trim())) throw new Error(`${label}.id must be unique and non-empty.`);
54
- ids.add(hook.id.trim());
55
- const source = relativeSource(config.root, hook.path, `${label}.path`);
56
- if (typeof hook.export !== 'string' || !/^use[A-Z0-9_$][\w$]*$/.test(hook.export)) throw new Error(`${label}.export must name one exported use* hook.`);
57
- if (!['query', 'mutation'].includes(hook.kind)) throw new Error(`${label}.kind must be query or mutation.`);
58
- if (hook.resultBinding !== undefined && (typeof hook.resultBinding !== 'string' || !IDENTIFIER.test(hook.resultBinding))) {
59
- throw new Error(`${label}.resultBinding must be one local identifier.`);
60
- }
61
- if (!Array.isArray(hook.identities)) throw new Error(`${label}.identities must be an array.`);
62
- const seen = { ids: new Set(), bindings: new Set() };
63
- const identities = hook.identities.map((identity, identityIndex) => parseIdentity(identity, `${label}.identities[${identityIndex}]`, seen));
64
- if (hook.kind === 'mutation' && !identities.some(identity => identity.resource)) throw new Error(`${label} mutation must declare at least one resource identity.`);
65
- return { id: hook.id.trim(), path: source, export: hook.export, kind: hook.kind,
66
- ...(hook.resultBinding === undefined ? {} : { resultBinding: hook.resultBinding }), identities };
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 };
67
74
  }
68
-
69
75
  export function parseContract(config) {
70
- // The contract is declared in the app's one package.json (the app root; config.root is the fe side folder).
71
- const manifest = JSON.parse(fs.readFileSync(path.join(config.packageRoot ?? config.root, 'package.json'), 'utf8'));
72
- const next = manifest?.starci?.codePatterns?.next;
73
- const value = next?.dataLifecycle;
74
- if (value === undefined) return null;
75
- if (next.schema !== 'starci/next-code-pattern-contract@1') throw new Error('package.json#starci.codePatterns.next must use starci/next-code-pattern-contract@1.');
76
- exactKeys(value, TOP_KEYS, CONTRACT_LABEL);
77
- if (value.schema !== CONTRACT_SCHEMA) throw new Error(`dataLifecycle.schema must be ${CONTRACT_SCHEMA}.`);
78
- parseSwr(value.swr);
79
- if (!Array.isArray(value.hooks) || value.hooks.length === 0) throw new Error('dataLifecycle.hooks must contain at least one declared lifecycle call.');
80
- const ids = new Set();
81
- const hooks = value.hooks.map((hook, index) => parseHook(config, hook, `dataLifecycle.hooks[${index}]`, ids));
82
- return { schema: value.schema, swr: { package: 'swr', major: value.swr.major }, hooks };
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 };
83
93
  }
84
-
85
94
  export function installedSWR(repository) {
86
- const require = createRequire(path.join(repository, 'package.json'));
87
- let manifest;
88
- try { manifest = require.resolve('swr/package.json'); } catch {
89
- let entry;
90
- try { entry = require.resolve('swr'); } catch { throw new Error('Installed swr package is unavailable.'); }
91
- let current = path.dirname(entry);
92
- while (isInside(repository, current) && current !== repository) {
93
- const candidate = path.join(current, 'package.json');
94
- if (fs.existsSync(candidate)) { manifest = candidate; break; }
95
- current = path.dirname(current);
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
+ }
96
117
  }
97
- }
98
- if (!manifest) throw new Error('Installed swr package.json is unavailable.');
99
- const actualManifest = canonical(manifest);
100
- const parsed = JSON.parse(fs.readFileSync(actualManifest, 'utf8'));
101
- const major = Number.parseInt(String(parsed.version ?? '').split('.')[0], 10);
102
- if (parsed.name !== 'swr' || !Number.isInteger(major)) throw new Error('Installed swr package identity or version is invalid.');
103
- return { name: parsed.name, version: parsed.version, major, root: canonical(path.dirname(actualManifest)) };
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)) };
104
126
  }