@starci/eslint-canon-be 3.5.0 → 3.6.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 (195) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +11 -2
  3. package/dist/ambient-id.d.ts +9 -0
  4. package/{ambient-id.mjs → dist/ambient-id.js} +43 -39
  5. package/dist/app-composition.d.ts +21 -0
  6. package/{app-composition.mjs → dist/app-composition.js} +396 -300
  7. package/dist/async-discipline.d.ts +9 -0
  8. package/dist/async-discipline.js +77 -0
  9. package/dist/authorization.d.ts +12 -0
  10. package/{authorization.mjs → dist/authorization.js} +53 -59
  11. package/dist/boundary-schema.d.ts +15 -0
  12. package/{boundary-schema.mjs → dist/boundary-schema.js} +120 -105
  13. package/dist/builders.d.ts +21 -0
  14. package/{builders.mjs → dist/builders.js} +162 -146
  15. package/dist/cli.d.ts +15 -0
  16. package/{cli.mjs → dist/cli.js} +97 -82
  17. package/dist/comments.d.ts +52 -0
  18. package/dist/comments.js +412 -0
  19. package/dist/config-owner.d.ts +24 -0
  20. package/{config-owner.mjs → dist/config-owner.js} +256 -219
  21. package/dist/connections.d.ts +12 -0
  22. package/{connections.mjs → dist/connections.js} +138 -140
  23. package/dist/cookies.d.ts +9 -0
  24. package/{cookies.mjs → dist/cookies.js} +58 -58
  25. package/dist/cqrs.d.ts +27 -0
  26. package/{cqrs.mjs → dist/cqrs.js} +341 -282
  27. package/dist/data-access.d.ts +50 -0
  28. package/{data-access.mjs → dist/data-access.js} +238 -243
  29. package/dist/default-deny.d.ts +15 -0
  30. package/{default-deny.mjs → dist/default-deny.js} +103 -88
  31. package/dist/e2e-flow.d.ts +25 -0
  32. package/{e2e-flow.mjs → dist/e2e-flow.js} +113 -112
  33. package/dist/edge-hardening.d.ts +10 -0
  34. package/dist/edge-hardening.js +173 -0
  35. package/dist/error-handling.d.ts +21 -0
  36. package/{error-handling.mjs → dist/error-handling.js} +289 -239
  37. package/dist/event-bus.d.ts +14 -0
  38. package/{event-bus.mjs → dist/event-bus.js} +146 -129
  39. package/dist/feature-thin.d.ts +12 -0
  40. package/{feature-thin.mjs → dist/feature-thin.js} +208 -191
  41. package/dist/function-budget.d.ts +7 -0
  42. package/{function-budget.mjs → dist/function-budget.js} +24 -27
  43. package/dist/health-ready.d.ts +10 -0
  44. package/dist/health-ready.js +703 -0
  45. package/dist/idempotency.d.ts +10 -0
  46. package/{idempotency.mjs → dist/idempotency.js} +296 -226
  47. package/dist/index.d.ts +54 -0
  48. package/{index.mjs → dist/index.js} +91 -109
  49. package/dist/infra-owner.d.ts +18 -0
  50. package/{infra-owner.mjs → dist/infra-owner.js} +58 -51
  51. package/dist/injection.d.ts +30 -0
  52. package/{injection.mjs → dist/injection.js} +397 -344
  53. package/dist/input-bounds.d.ts +15 -0
  54. package/{input-bounds.mjs → dist/input-bounds.js} +143 -132
  55. package/dist/jobs.d.ts +17 -0
  56. package/{jobs.mjs → dist/jobs.js} +116 -99
  57. package/dist/jwt-alg.d.ts +6 -0
  58. package/dist/jwt-alg.js +49 -0
  59. package/dist/lib/app-roles.d.ts +25 -0
  60. package/{lib/app-roles.mjs → dist/lib/app-roles.js} +15 -17
  61. package/dist/lib/ast.d.ts +40 -0
  62. package/dist/lib/ast.js +100 -0
  63. package/dist/lib/client-idempotency.d.ts +3 -0
  64. package/{lib/client-idempotency.mjs → dist/lib/client-idempotency.js} +316 -268
  65. package/dist/lib/config.d.ts +36 -0
  66. package/{lib/config.mjs → dist/lib/config.js} +24 -27
  67. package/dist/lib/db-writes.d.ts +29 -0
  68. package/{lib/db-writes.mjs → dist/lib/db-writes.js} +27 -27
  69. package/dist/lib/declared.d.ts +77 -0
  70. package/{lib/declared.mjs → dist/lib/declared.js} +83 -78
  71. package/dist/lib/doors.d.ts +50 -0
  72. package/{lib/doors.mjs → dist/lib/doors.js} +45 -50
  73. package/dist/lib/edition.d.ts +25 -0
  74. package/{lib/edition.mjs → dist/lib/edition.js} +15 -20
  75. package/dist/lib/entity-writes.d.ts +7 -0
  76. package/{lib/entity-writes.mjs → dist/lib/entity-writes.js} +31 -22
  77. package/dist/lib/hfs.d.ts +34 -0
  78. package/{lib/hfs.mjs → dist/lib/hfs.js} +31 -29
  79. package/dist/lib/import-source.d.ts +63 -0
  80. package/dist/lib/import-source.js +134 -0
  81. package/dist/lib/path.d.ts +8 -0
  82. package/{lib/path.mjs → dist/lib/path.js} +4 -7
  83. package/dist/lib/persistence.d.ts +115 -0
  84. package/{lib/persistence.mjs → dist/lib/persistence.js} +55 -66
  85. package/dist/lib/ports.d.ts +70 -0
  86. package/{lib/ports.mjs → dist/lib/ports.js} +62 -66
  87. package/dist/lib/rule.d.ts +31 -0
  88. package/dist/lib/rule.js +2 -0
  89. package/dist/lib/runtime-root.d.ts +4 -0
  90. package/dist/lib/runtime-root.js +10 -0
  91. package/dist/lib/schema-values.d.ts +13 -0
  92. package/{lib/schema-values.mjs → dist/lib/schema-values.js} +67 -59
  93. package/dist/lib/specifier.d.ts +12 -0
  94. package/dist/lib/specifier.js +29 -0
  95. package/dist/lib/subject-values.d.ts +4 -0
  96. package/dist/lib/subject-values.js +61 -0
  97. package/dist/lib/transactions.d.ts +18 -0
  98. package/{lib/transactions.mjs → dist/lib/transactions.js} +17 -15
  99. package/dist/lib/transport-slots.d.ts +17 -0
  100. package/{lib/transport-slots.mjs → dist/lib/transport-slots.js} +2 -4
  101. package/dist/lib/types.d.ts +96 -0
  102. package/dist/lib/types.js +150 -0
  103. package/dist/lib/unit-spec.d.ts +48 -0
  104. package/{lib/unit-spec.mjs → dist/lib/unit-spec.js} +35 -44
  105. package/dist/lib/webhook-ack.d.ts +4 -0
  106. package/{lib/webhook-ack.mjs → dist/lib/webhook-ack.js} +78 -62
  107. package/dist/lib/webhook-handoff-inbox.d.ts +3 -0
  108. package/{lib/webhook-handoff-inbox.mjs → dist/lib/webhook-handoff-inbox.js} +195 -167
  109. package/dist/lib/webhook-handoff-intake.d.ts +4 -0
  110. package/{lib/webhook-handoff-intake.mjs → dist/lib/webhook-handoff-intake.js} +125 -102
  111. package/dist/lib/webhook-handoff.d.ts +15 -0
  112. package/{lib/webhook-handoff.mjs → dist/lib/webhook-handoff.js} +107 -100
  113. package/dist/lib/webhook-verifiers.d.ts +20 -0
  114. package/{lib/webhook-verifiers.mjs → dist/lib/webhook-verifiers.js} +108 -104
  115. package/dist/lib/why.d.ts +9 -0
  116. package/{lib/why.mjs → dist/lib/why.js} +6 -6
  117. package/dist/log-safety.d.ts +20 -0
  118. package/dist/log-safety.js +308 -0
  119. package/dist/module-layering.d.ts +42 -0
  120. package/dist/module-layering.js +257 -0
  121. package/dist/module-shape.d.ts +47 -0
  122. package/{module-shape.mjs → dist/module-shape.js} +278 -236
  123. package/dist/naming.d.ts +49 -0
  124. package/dist/naming.js +246 -0
  125. package/dist/observability.d.ts +25 -0
  126. package/dist/observability.js +245 -0
  127. package/dist/operations.d.ts +23 -0
  128. package/{operations.mjs → dist/operations.js} +410 -332
  129. package/dist/project-graph.d.ts +9 -0
  130. package/{project-graph.mjs → dist/project-graph.js} +8 -13
  131. package/dist/projections.d.ts +11 -0
  132. package/{projections.mjs → dist/projections.js} +60 -57
  133. package/dist/query-safety.d.ts +21 -0
  134. package/{query-safety.mjs → dist/query-safety.js} +92 -108
  135. package/dist/realtime.d.ts +15 -0
  136. package/{realtime.mjs → dist/realtime.js} +108 -106
  137. package/dist/resilience.d.ts +15 -0
  138. package/dist/resilience.js +319 -0
  139. package/dist/schema-authority.d.ts +15 -0
  140. package/{schema-authority.mjs → dist/schema-authority.js} +256 -220
  141. package/dist/service-deps.d.ts +17 -0
  142. package/{service-deps.mjs → dist/service-deps.js} +133 -130
  143. package/dist/service-isolation.d.ts +8 -0
  144. package/dist/service-isolation.js +76 -0
  145. package/dist/size-budget.d.ts +9 -0
  146. package/{size-budget.mjs → dist/size-budget.js} +27 -27
  147. package/dist/sonar-parity.d.ts +5 -0
  148. package/{sonar-parity.mjs → dist/sonar-parity.js} +7 -7
  149. package/dist/spec-no-skip.d.ts +9 -0
  150. package/dist/spec-no-skip.js +81 -0
  151. package/dist/spec-quality.d.ts +10 -0
  152. package/{spec-quality.mjs → dist/spec-quality.js} +103 -87
  153. package/dist/suppression.d.ts +26 -0
  154. package/{suppression.mjs → dist/suppression.js} +30 -45
  155. package/dist/temporal.d.ts +9 -0
  156. package/{temporal.mjs → dist/temporal.js} +75 -65
  157. package/dist/test-world.d.ts +28 -0
  158. package/{test-world.mjs → dist/test-world.js} +102 -83
  159. package/dist/testing.d.ts +51 -0
  160. package/dist/testing.js +368 -0
  161. package/dist/third-party-owner.d.ts +8 -0
  162. package/{third-party-owner.mjs → dist/third-party-owner.js} +105 -91
  163. package/dist/transport.d.ts +24 -0
  164. package/{transport.mjs → dist/transport.js} +279 -240
  165. package/dist/type-safety.d.ts +25 -0
  166. package/dist/type-safety.js +297 -0
  167. package/dist/unit-spec.d.ts +30 -0
  168. package/{unit-spec.mjs → dist/unit-spec.js} +473 -399
  169. package/dist/uploads.d.ts +9 -0
  170. package/{uploads.mjs → dist/uploads.js} +62 -54
  171. package/dist/user-copy.d.ts +9 -0
  172. package/{user-copy.mjs → dist/user-copy.js} +76 -67
  173. package/dist/webhooks.d.ts +16 -0
  174. package/{webhooks.mjs → dist/webhooks.js} +243 -231
  175. package/package.json +17 -8
  176. package/runtime/knowledge/hfs/canon-pins.yaml +17 -10
  177. package/async-discipline.mjs +0 -83
  178. package/comments.mjs +0 -416
  179. package/edge-hardening.mjs +0 -139
  180. package/health-ready.mjs +0 -539
  181. package/jwt-alg.mjs +0 -45
  182. package/lib/ast.mjs +0 -88
  183. package/lib/import-source.mjs +0 -104
  184. package/lib/specifier.mjs +0 -27
  185. package/lib/subject-values.mjs +0 -55
  186. package/lib/types.mjs +0 -133
  187. package/log-safety.mjs +0 -303
  188. package/module-layering.mjs +0 -270
  189. package/naming.mjs +0 -251
  190. package/observability.mjs +0 -241
  191. package/resilience.mjs +0 -304
  192. package/service-isolation.mjs +0 -75
  193. package/spec-no-skip.mjs +0 -85
  194. package/testing.mjs +0 -384
  195. package/type-safety.mjs +0 -323
@@ -23,148 +23,160 @@
23
23
  * repository) is a custom injector and is judged by its shape, not by its name. Nothing here reads a variable name:
24
24
  * `Inject*` names are the convention's fixed vocabulary, not a receiver guess.
25
25
  */
26
- import { posix } from "node:path"
27
- import ts from "typescript"
28
- import { paramParts, staticText } from "./lib/ast.mjs"
29
- import { hfsOf } from "./lib/hfs.mjs"
30
- import { isImportedFrom } from "./lib/import-source.mjs"
31
- import { normalizePath } from "./lib/path.mjs"
32
- import { isPackageExport, packageOfExport, typed, typeOrigins } from "./lib/types.mjs"
33
-
26
+ import { posix } from "node:path";
27
+ import ts from "typescript";
28
+ import { nameOrValue, paramParts, staticText } from "./lib/ast.js";
29
+ import { hfsOf } from "./lib/hfs.js";
30
+ import { normalizePath } from "./lib/path.js";
31
+ import { isPackageExport, isTsNode, packageOfExport, typed, typeOrigins } from "./lib/types.js";
32
+ import { isNode } from "./lib/rule.js";
34
33
  /** The raw decorators of Nest and TypeORM whose call is the injector's job. */
35
- const RAW_NAMES = new Set(["Inject", "InjectEntityManager", "InjectDataSource", "InjectQueue", "InjectRepository"])
36
-
34
+ const RAW_NAMES = new Set(["Inject", "InjectEntityManager", "InjectDataSource", "InjectQueue", "InjectRepository"]);
37
35
  /** The package a declaration file belongs to (the innermost `node_modules/<pkg>`), else null; the compiler's own lib files are not packages. */
38
36
  const packageOfFile = (file) => {
39
- if (/\/node_modules\/typescript\/lib\//.test(file)) return null
40
- const match = /^.*\/node_modules\/((?:@[^/]+\/)?[^/]+)\//.exec(file)
41
- return match ? match[1] : null
42
- }
43
-
37
+ if (/\/node_modules\/typescript\/lib\//.test(file))
38
+ return null;
39
+ const match = /^.*\/node_modules\/((?:@[^/]+\/)?[^/]+)\//.exec(file);
40
+ return match ? (match[1] ?? null) : null;
41
+ };
44
42
  /** The name a declaration is known by: its own name, or the variable an arrow/function expression is assigned to. */
45
43
  const declarationName = (declaration) => {
46
- if (declaration.name && ts.isIdentifier(declaration.name)) return declaration.name.text
47
- const parent = declaration.parent
48
- if (parent && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name)) return parent.name.text
49
- return null
50
- }
51
-
44
+ const name = "name" in declaration ? declaration.name : undefined;
45
+ if (name && isTsNode(name) && ts.isIdentifier(name))
46
+ return name.text;
47
+ const parent = declaration.parent;
48
+ if (parent && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name))
49
+ return parent.name.text;
50
+ return null;
51
+ };
52
52
  /** The variable a name resolves to from a node's scope. */
53
53
  const variableOf = (context, node, name) => {
54
54
  for (let scope = context.sourceCode.getScope(node); scope; scope = scope.upper) {
55
- const variable = scope.set.get(name)
56
- if (variable) return variable
55
+ const variable = scope.set.get(name);
56
+ if (variable)
57
+ return variable;
57
58
  }
58
- return null
59
- }
60
-
59
+ return null;
60
+ };
61
+ /** The first import-binding definition of a variable. */
62
+ const importDefinitionOf = (variable) => {
63
+ for (const entry of variable?.defs ?? [])
64
+ if (entry.type === "ImportBinding")
65
+ return entry;
66
+ return undefined;
67
+ };
61
68
  /** The import a callee identifier or namespace member comes from, read from syntax when the checker cannot resolve it. */
62
69
  const importedCallee = (context, callee) => {
63
- const identifier = callee.type === "MemberExpression" && callee.object.type === "Identifier" ? callee.object : callee
64
- if (identifier.type !== "Identifier") return { name: null, module: null, file: null, ambient: false }
65
- const variable = variableOf(context, identifier, identifier.name)
66
- const def = variable?.defs.find((entry) => entry.type === "ImportBinding")
70
+ const identifier = callee.type === "MemberExpression" && callee.object.type === "Identifier" ? callee.object : callee;
71
+ if (identifier.type !== "Identifier")
72
+ return { name: null, module: null, file: null, ambient: false };
73
+ const variable = variableOf(context, identifier, identifier.name);
74
+ const def = importDefinitionOf(variable);
67
75
  if (!def) {
68
- const local = (variable?.defs.length ?? 0) > 0
69
- const name = callee.type === "MemberExpression" ? staticPropertyName(callee) : identifier.name
70
- return { name, module: null, file: null, ambient: !local }
76
+ const local = (variable?.defs.length ?? 0) > 0;
77
+ const name = callee.type === "MemberExpression" ? staticPropertyName(callee) : identifier.name;
78
+ return { name, module: null, file: null, ambient: !local };
71
79
  }
72
- const source = String(def.parent.source.value)
73
- const specifier = def.node
80
+ // an `import x = require()` binding has no `source`: the read fails as it always has
81
+ if (def.parent?.type !== "ImportDeclaration")
82
+ throw new TypeError("Cannot read properties of undefined (reading 'value')");
83
+ const source = String(def.parent.source.value);
84
+ const specifier = def.node;
74
85
  const name = specifier.type === "ImportSpecifier"
75
- ? (specifier.imported.name ?? specifier.imported.value)
76
- : callee.type === "MemberExpression" ? staticPropertyName(callee) : "default"
77
- return { name, module: source.startsWith(".") ? null : source, file: null, ambient: false }
78
- }
79
-
86
+ ? (specifier.imported.type === "Identifier" ? specifier.imported.name : specifier.imported.value)
87
+ : callee.type === "MemberExpression" ? staticPropertyName(callee) : "default";
88
+ return { name, module: source.startsWith(".") ? null : source, file: null, ambient: false };
89
+ };
80
90
  /** The property a member expression names (`common.Inject` -> `Inject`), else null. */
81
91
  const staticPropertyName = (member) => {
82
- if (member.computed) return staticText(member.property)
83
- return member.property.type === "Identifier" ? member.property.name : null
84
- }
85
-
92
+ if (member.computed)
93
+ return staticText(member.property);
94
+ return member.property.type === "Identifier" ? member.property.name : null;
95
+ };
86
96
  /**
87
97
  * Where the function a call invokes (or a decorator names) is declared.
88
98
  *
89
- * @param {object} context - The ESLint rule context.
90
- * @param {object} node - A CallExpression, or an Identifier used as a bare decorator.
91
- * @returns {{ name: string | null, file: string | null, module: string | null, ambient: boolean }} The declaration's name, file and package; `ambient` is true for a name nothing declares.
99
+ * @param context - The ESLint rule context.
100
+ * @param node - A CallExpression, or an Identifier used as a bare decorator.
101
+ * @returns The declaration's name, file and package; `ambient` is true for a name nothing declares.
92
102
  */
93
103
  const calleeOf = (context, node) => {
94
- const { checker, toTs } = typed(context)
95
- const tsNode = toTs(node)
96
- let declaration
97
- if (tsNode && ts.isCallExpression(tsNode)) declaration = checker.getResolvedSignature(tsNode)?.declaration
104
+ const { checker, toTs } = typed(context);
105
+ const tsNode = toTs(node);
106
+ let declaration;
107
+ if (tsNode && ts.isCallExpression(tsNode))
108
+ declaration = checker.getResolvedSignature(tsNode)?.declaration;
98
109
  else if (tsNode) {
99
- let symbol = checker.getSymbolAtLocation(tsNode)
100
- if (symbol && symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol)
101
- declaration = symbol?.getDeclarations?.()?.[0]
110
+ let symbol = checker.getSymbolAtLocation(tsNode);
111
+ if (symbol && symbol.flags & ts.SymbolFlags.Alias)
112
+ symbol = checker.getAliasedSymbol(symbol);
113
+ declaration = symbol?.getDeclarations?.()?.[0];
102
114
  }
103
115
  if (declaration?.getSourceFile()) {
104
- const file = normalizePath(declaration.getSourceFile().fileName)
105
- return { name: declarationName(declaration), file, module: packageOfFile(file), ambient: false }
116
+ const file = normalizePath(declaration.getSourceFile().fileName);
117
+ return { name: declarationName(declaration), file, module: packageOfFile(file), ambient: false };
106
118
  }
107
- return importedCallee(context, node.type === "CallExpression" ? node.callee : node)
108
- }
109
-
119
+ return importedCallee(context, node.type === "CallExpression" ? node.callee : node);
120
+ };
110
121
  /** The convention's injector vocabulary: `Inject` or `Inject<Thing>`; `Injectable` and other lower-case continuations are not injectors. */
111
- const INJECT_NAME = /^Inject(?:[A-Z][A-Za-z0-9]*)?$/
112
-
122
+ const INJECT_NAME = /^Inject(?:[A-Z][A-Za-z0-9]*)?$/;
113
123
  /** True for a raw injection call: one of the five reserved names, or any `Inject*` function a package declares. */
114
124
  const isRawInject = (callee) => {
115
- if (callee.name === null) return false
116
- if (callee.module !== null) return INJECT_NAME.test(callee.name)
117
- return callee.ambient && RAW_NAMES.has(callee.name)
118
- }
119
-
125
+ if (callee.name === null)
126
+ return false;
127
+ if (callee.module !== null)
128
+ return INJECT_NAME.test(callee.name);
129
+ return callee.ambient && RAW_NAMES.has(callee.name);
130
+ };
120
131
  /** True for a file whose NAME role is an injector home. */
121
- const isDecoratorsFile = (filename) => posix.basename(normalizePath(filename)).endsWith(".decorators.ts")
122
-
132
+ const isDecoratorsFile = (filename) => posix.basename(normalizePath(filename)).endsWith(".decorators.ts");
123
133
  /** True when the file sits directly in an owner root (`src/modules/platform/clock/`), which is where its owner's injectors live. */
124
134
  const atOwnerRoot = (hfs, filename) => {
125
- const owner = hfs.ownerOf(filename)
126
- return owner !== null && posix.dirname(hfs.relative(filename)) === owner
127
- }
128
-
135
+ const owner = hfs.ownerOf(filename);
136
+ return owner !== null && posix.dirname(hfs.relative(filename)) === owner;
137
+ };
129
138
  /** True for a file of the `platform/composition` owner, the home of `injector` and `TypedParameterDecorator`. */
130
139
  const inComposition = (hfs, file) => {
131
- if (!file || file.includes("/node_modules/") || hfs.tierOf(file) !== "platform") return false
132
- const owner = hfs.ownerOf(file)
133
- return owner !== null && posix.basename(owner) === "composition"
134
- }
135
-
140
+ if (!file || file.includes("/node_modules/") || hfs.tierOf(file) !== "platform")
141
+ return false;
142
+ const owner = hfs.ownerOf(file);
143
+ return owner !== null && posix.basename(owner) === "composition";
144
+ };
136
145
  /** The injected type `T` of a call typed `TypedParameterDecorator<T>`, else null. */
137
146
  const injectedTypeOf = (context, call) => {
138
- const { checker, toTs } = typed(context)
139
- const tsCall = toTs(call)
140
- if (!tsCall) return null
141
- const type = checker.getTypeAtLocation(tsCall)
142
- if (type.aliasSymbol?.name === "TypedParameterDecorator" && type.aliasTypeArguments?.length === 1) return type.aliasTypeArguments[0]
143
- return null
144
- }
145
-
147
+ const { checker, toTs } = typed(context);
148
+ const tsCall = toTs(call);
149
+ if (!tsCall)
150
+ return null;
151
+ const type = checker.getTypeAtLocation(tsCall);
152
+ const [injected] = type.aliasTypeArguments ?? [];
153
+ if (type.aliasSymbol?.name === "TypedParameterDecorator" && type.aliasTypeArguments?.length === 1 && injected)
154
+ return injected;
155
+ return null;
156
+ };
146
157
  /** The parameters of every constructor a class declares. */
147
- const constructorParams = (node) => (node.kind === "constructor" && node.value?.params) || []
148
-
158
+ const constructorParams = (node) => (node.kind === "constructor" && node.value?.params) || [];
149
159
  /** True when the function around a decorated parameter is a class constructor. */
150
160
  const isConstructorParam = (decorator) => {
151
- let node = decorator.parent
152
- while (node && !/^(?:FunctionExpression|ArrowFunctionExpression|FunctionDeclaration|ClassDeclaration|ClassExpression|Program)$/.test(node.type)) node = node.parent
153
- return node?.type === "FunctionExpression" && node.parent?.type === "MethodDefinition" && node.parent.kind === "constructor"
154
- }
155
-
161
+ let node = decorator.parent;
162
+ while (node && !/^(?:FunctionExpression|ArrowFunctionExpression|FunctionDeclaration|ClassDeclaration|ClassExpression|Program)$/.test(node.type))
163
+ node = node.parent;
164
+ return node?.type === "FunctionExpression" && node.parent?.type === "MethodDefinition" && node.parent.kind === "constructor";
165
+ };
156
166
  /** True when a decorator sits on a class member or a parameter (not on a class). */
157
167
  const memberKind = (decorator) => {
158
- const owner = decorator.parent
159
- if (!owner) return "class"
160
- if (owner.type === "PropertyDefinition" || owner.type === "AccessorProperty") return "property"
161
- if (owner.type === "MethodDefinition") return "method"
162
- if (owner.type === "ClassDeclaration" || owner.type === "ClassExpression") return "class"
163
- return "parameter"
164
- }
165
-
168
+ const owner = decorator.parent;
169
+ if (!owner)
170
+ return "class";
171
+ if (owner.type === "PropertyDefinition" || owner.type === "AccessorProperty")
172
+ return "property";
173
+ if (owner.type === "MethodDefinition")
174
+ return "method";
175
+ if (owner.type === "ClassDeclaration" || owner.type === "ClassExpression")
176
+ return "class";
177
+ return "parameter";
178
+ };
166
179
  // -- injector-only ---------------------------------------------------------------------------------
167
-
168
180
  /** Raw injection lives only in `<owner>.decorators.ts`; injectors decorate constructor parameters. */
169
181
  export const injectorOnly = {
170
182
  meta: {
@@ -179,92 +191,101 @@ export const injectorOnly = {
179
191
  },
180
192
  },
181
193
  create(context) {
182
- const hfs = hfsOf(context)
183
- const filename = context.filename || context.getFilename()
184
- const home = isDecoratorsFile(filename) && atOwnerRoot(hfs, filename)
194
+ const hfs = hfsOf(context);
195
+ const filename = context.filename || context.getFilename();
196
+ const home = isDecoratorsFile(filename) && atOwnerRoot(hfs, filename);
185
197
  return {
186
198
  Decorator(node) {
187
- const expression = node.expression
188
- if (expression.type !== "CallExpression" && expression.type !== "Identifier") return
189
- const callee = calleeOf(context, expression)
190
- const written = callee.name ?? ""
199
+ const expression = node.expression;
200
+ if (expression.type !== "CallExpression" && expression.type !== "Identifier")
201
+ return;
202
+ const callee = calleeOf(context, expression);
203
+ const written = callee.name ?? "";
191
204
  if (isRawInject(callee)) {
192
- context.report({ node, messageId: "rawDecorator", data: { name: written } })
193
- return
205
+ context.report({ node, messageId: "rawDecorator", data: { name: written } });
206
+ return;
194
207
  }
195
- if (!INJECT_NAME.test(written)) return
196
- const kind = memberKind(node)
197
- if (kind === "property" || kind === "method") context.report({ node, messageId: "property", data: { name: written } })
198
- else if (kind === "parameter" && !isConstructorParam(node)) context.report({ node, messageId: "notConstructor", data: { name: written } })
208
+ if (!INJECT_NAME.test(written))
209
+ return;
210
+ const kind = memberKind(node);
211
+ if (kind === "property" || kind === "method")
212
+ context.report({ node, messageId: "property", data: { name: written } });
213
+ else if (kind === "parameter" && !isConstructorParam(node))
214
+ context.report({ node, messageId: "notConstructor", data: { name: written } });
199
215
  },
200
216
  CallExpression(node) {
201
- if (home) return
202
- if (node.parent?.type === "Decorator" && node.parent.expression === node) return
203
- const callee = calleeOf(context, node)
204
- if (isRawInject(callee)) context.report({ node, messageId: "rawCall", data: { name: callee.name } })
217
+ if (home)
218
+ return;
219
+ if (node.parent?.type === "Decorator" && node.parent.expression === node)
220
+ return;
221
+ const callee = calleeOf(context, node);
222
+ if (isRawInject(callee))
223
+ context.report({ node, messageId: "rawCall", data: { name: callee.name } });
205
224
  },
206
- }
225
+ };
207
226
  },
208
- }
209
-
227
+ };
210
228
  // -- injector-shape --------------------------------------------------------------------------------
211
-
212
229
  /** The nearest JSDoc block before a statement, else null. */
213
230
  const jsdocBefore = (context, statement) => {
214
- const comments = context.sourceCode.getCommentsBefore(statement)
215
- const last = comments[comments.length - 1]
216
- return last && last.type === "Block" && last.value.startsWith("*") && last.value.replace(/[*\s]/g, "") !== "" ? last.value : null
217
- }
218
-
231
+ const comments = context.sourceCode.getCommentsBefore(statement);
232
+ const last = comments[comments.length - 1];
233
+ return last && last.type === "Block" && last.value.startsWith("*") && last.value.replace(/[*\s]/g, "") !== "" ? last.value : null;
234
+ };
219
235
  /** The single expression a function returns (arrow expression body, or a block that is one `return`), else null. */
220
236
  const returnedExpression = (fn) => {
221
- if (fn.body.type !== "BlockStatement") return fn.body
222
- const [only] = fn.body.body
223
- return fn.body.body.length === 1 && only.type === "ReturnStatement" ? only.argument : null
224
- }
225
-
237
+ if (fn.body.type !== "BlockStatement")
238
+ return fn.body;
239
+ const [only] = fn.body.body;
240
+ return fn.body.body.length === 1 && only?.type === "ReturnStatement" ? only.argument : null;
241
+ };
226
242
  /** The type arguments of a node, in either parser spelling. */
227
- const typeArgsOf = (node) => (node.typeArguments ?? node.typeParameters)?.params ?? []
228
-
243
+ const typeArgsOf = (node) => {
244
+ const legacy = Reflect.get(node, "typeParameters");
245
+ return (node.typeArguments ?? (isNode(legacy) && legacy.type === "TSTypeParameterInstantiation" ? legacy : undefined))?.params ?? [];
246
+ };
229
247
  /** The exported function-like bindings of a statement: `[{ name, fn, id }]`. */
230
248
  const exportedFunctions = (statement) => {
231
- const declaration = statement.declaration
232
- if (!declaration) return []
233
- if (declaration.type === "FunctionDeclaration" && declaration.id) return [{ name: declaration.id.name, fn: declaration, id: declaration.id }]
234
- if (declaration.type !== "VariableDeclaration") return []
235
- return declaration.declarations
236
- .filter((entry) => entry.id.type === "Identifier" && entry.init && /^(?:ArrowFunctionExpression|FunctionExpression)$/.test(entry.init.type))
237
- .map((entry) => ({ name: entry.id.name, fn: entry.init, id: entry.id }))
238
- }
239
-
249
+ const declaration = statement.declaration;
250
+ if (!declaration)
251
+ return [];
252
+ if (declaration.type === "FunctionDeclaration" && declaration.id)
253
+ return [{ name: declaration.id.name, fn: declaration, id: declaration.id }];
254
+ if (declaration.type !== "VariableDeclaration")
255
+ return [];
256
+ return declaration.declarations.flatMap((entry) => entry.id.type === "Identifier" && (entry.init?.type === "ArrowFunctionExpression" || entry.init?.type === "FunctionExpression")
257
+ ? [{ name: entry.id.name, fn: entry.init, id: entry.id }]
258
+ : []);
259
+ };
240
260
  /** True when an initializer is a call whose callee is named `injector` (whatever it resolves to is checked later). */
241
- const callsInjector = (context, fn) => {
242
- const expression = returnedExpression(fn)
243
- return expression?.type === "CallExpression" && expression.callee.type === "Identifier" && expression.callee.name === "injector"
244
- }
245
-
261
+ const callsInjector = (fn) => {
262
+ const expression = returnedExpression(fn);
263
+ return expression?.type === "CallExpression" && expression.callee.type === "Identifier" && expression.callee.name === "injector";
264
+ };
246
265
  /** True for an `Inject<Thing>`-looking export (the convention's fixed vocabulary), excluding the builder itself. */
247
- const looksLikeInjector = (context, entry) =>
248
- entry.name !== "injector" && (/^[Ii]nject/.test(entry.name) || entry.fn.returnType?.typeAnnotation?.typeName?.name === "TypedParameterDecorator" || callsInjector(context, entry.fn))
249
-
266
+ const looksLikeInjector = (entry) => {
267
+ const annotation = entry.fn.returnType?.typeAnnotation;
268
+ return entry.name !== "injector" && (/^[Ii]nject/.test(entry.name) || (annotation?.type === "TSTypeReference" && annotation.typeName.type === "Identifier" && annotation.typeName.name === "TypedParameterDecorator") || callsInjector(entry.fn));
269
+ };
250
270
  /** True for a value that is a class or a `unique symbol`. */
251
271
  const isTokenType = (type) => {
252
- if (type.isUnion()) return type.types.every(isTokenType)
253
- if (type.flags & ts.TypeFlags.UniqueESSymbol) return true
254
- if (type.getConstructSignatures().length > 0) return true
255
- return Boolean(type.symbol && type.symbol.flags & ts.SymbolFlags.Class)
256
- }
257
-
272
+ if (type.isUnion())
273
+ return type.types.every(isTokenType);
274
+ if (type.flags & ts.TypeFlags.UniqueESSymbol)
275
+ return true;
276
+ if (type.getConstructSignatures().length > 0)
277
+ return true;
278
+ return Boolean(type.symbol && type.symbol.flags & ts.SymbolFlags.Class);
279
+ };
258
280
  /** True for a string type (a string literal, `string`, or a union of them). */
259
- const isStringType = (type) => (type.isUnion() ? type.types.every(isStringType) : (type.flags & ts.TypeFlags.StringLike) !== 0)
260
-
281
+ const isStringType = (type) => (type.isUnion() ? type.types.every(isStringType) : (type.flags & ts.TypeFlags.StringLike) !== 0);
261
282
  /** The value of a `provide:` property. */
262
283
  const provideValue = (node) => {
263
- if (node.type !== "Property" || node.parent?.type !== "ObjectExpression" || (node.computed && node.key.type !== "Literal")) return null
264
- const name = node.key.type === "Identifier" ? node.key.name : node.key.value
265
- return name === "provide" ? node.value : null
266
- }
267
-
284
+ if (node.type !== "Property" || node.parent?.type !== "ObjectExpression" || (node.computed && node.key.type !== "Literal"))
285
+ return null;
286
+ const name = nameOrValue(node.key);
287
+ return name === "provide" ? node.value : null;
288
+ };
268
289
  /** The shape of an injector, its token and the `provide:` tokens of a module. */
269
290
  export const injectorShape = {
270
291
  meta: {
@@ -283,73 +304,83 @@ export const injectorShape = {
283
304
  },
284
305
  },
285
306
  create(context) {
286
- const hfs = hfsOf(context)
287
- const filename = context.filename || context.getFilename()
307
+ const hfs = hfsOf(context);
308
+ const filename = context.filename || context.getFilename();
288
309
  const listeners = {
289
310
  Property(node) {
290
- const value = provideValue(node)
291
- if (!value) return
292
- const { checker, toTs } = typed(context)
293
- const tsValue = toTs(value)
294
- if (!tsValue) return
295
- const type = checker.getTypeAtLocation(tsValue)
296
- if (isStringType(type) || isTokenType(type)) return
297
- context.report({ node: value, messageId: "provide", data: { type: checker.typeToString(type) } })
311
+ const value = provideValue(node);
312
+ if (!value)
313
+ return;
314
+ const { checker, toTs } = typed(context);
315
+ const tsValue = toTs(value);
316
+ if (!tsValue)
317
+ return;
318
+ const type = checker.getTypeAtLocation(tsValue);
319
+ if (isStringType(type) || isTokenType(type))
320
+ return;
321
+ context.report({ node: value, messageId: "provide", data: { type: checker.typeToString(type) } });
298
322
  },
299
- }
300
- if (!isDecoratorsFile(filename)) return listeners
301
- const ownerName = posix.basename(hfs.ownerOf(filename) ?? "")
323
+ };
324
+ if (!isDecoratorsFile(filename))
325
+ return listeners;
326
+ const ownerName = posix.basename(hfs.ownerOf(filename) ?? "");
302
327
  return {
303
328
  ...listeners,
304
329
  ExportNamedDeclaration(statement) {
305
330
  for (const entry of exportedFunctions(statement)) {
306
- if (!looksLikeInjector(context, entry)) continue
307
- checkInjector(context, hfs, statement, entry)
331
+ if (!looksLikeInjector(entry))
332
+ continue;
333
+ checkInjector(context, hfs, statement, entry);
308
334
  }
309
- if (statement.declaration?.type !== "VariableDeclaration") return
335
+ if (statement.declaration?.type !== "VariableDeclaration")
336
+ return;
310
337
  for (const entry of statement.declaration.declarations) {
311
- const init = entry.init
312
- const symbolCall = init?.type === "CallExpression" && (init.callee.type === "Identifier" ? init.callee.name === "Symbol" : init.callee.type === "MemberExpression" && init.callee.object.type === "Identifier" && init.callee.object.name === "Symbol")
313
- if (!symbolCall || entry.id.type !== "Identifier") continue
314
- const annotation = entry.id.typeAnnotation?.typeAnnotation
315
- const unique = annotation?.type === "TSTypeOperator" && annotation.operator === "unique" && annotation.typeAnnotation?.type === "TSSymbolKeyword"
316
- const description = init.callee.type === "Identifier" && init.arguments.length === 1 ? staticText(init.arguments[0]) : null
317
- if (!unique) context.report({ node: entry.id, messageId: "tokenType", data: { name: entry.id.name } })
338
+ const init = entry.init;
339
+ const symbolCall = init?.type === "CallExpression" && (init.callee.type === "Identifier" ? init.callee.name === "Symbol" : init.callee.type === "MemberExpression" && init.callee.object.type === "Identifier" && init.callee.object.name === "Symbol");
340
+ if (!symbolCall || entry.id.type !== "Identifier")
341
+ continue;
342
+ const annotation = entry.id.typeAnnotation?.typeAnnotation;
343
+ const unique = annotation?.type === "TSTypeOperator" && annotation.operator === "unique" && annotation.typeAnnotation?.type === "TSSymbolKeyword";
344
+ const description = init.callee.type === "Identifier" && init.arguments.length === 1 ? staticText(init.arguments[0]) : null;
345
+ if (!unique)
346
+ context.report({ node: entry.id, messageId: "tokenType", data: { name: entry.id.name } });
318
347
  else if (description === null || !/^[a-z][a-z0-9-]*(?:\.[a-z][a-z0-9-]*)+$/.test(description) || !description.split(".").includes(ownerName)) {
319
- context.report({ node: entry.id, messageId: "tokenName", data: { name: entry.id.name, owner: ownerName } })
348
+ context.report({ node: entry.id, messageId: "tokenName", data: { name: entry.id.name, owner: ownerName } });
320
349
  }
321
350
  }
322
351
  },
323
- }
352
+ };
324
353
  },
325
- }
326
-
354
+ };
327
355
  /** Reports every way one exported injector departs from the canonical shape. */
328
356
  const checkInjector = (context, hfs, statement, { name, fn, id }) => {
329
- const data = { name }
330
- if (!/^Inject[A-Z][A-Za-z0-9]*$/.test(name)) context.report({ node: id, messageId: "name", data })
331
- if (fn.params.length > 0) context.report({ node: fn.params[0], messageId: "params", data })
332
- const annotation = fn.returnType?.typeAnnotation
333
- const wellTyped = annotation?.type === "TSTypeReference" && annotation.typeName.type === "Identifier" && annotation.typeName.name === "TypedParameterDecorator" && typeArgsOf(annotation).length === 1
334
- const fromComposition = wellTyped && typeOrigins(context, annotation).some((origin) => origin.name === "TypedParameterDecorator" && inComposition(hfs, origin.file))
357
+ const data = { name };
358
+ if (!/^Inject[A-Z][A-Za-z0-9]*$/.test(name))
359
+ context.report({ node: id, messageId: "name", data });
360
+ const [firstParam] = fn.params;
361
+ if (fn.params.length > 0 && firstParam)
362
+ context.report({ node: firstParam, messageId: "params", data });
363
+ const annotation = fn.returnType?.typeAnnotation;
364
+ const wellTyped = annotation?.type === "TSTypeReference" && annotation.typeName.type === "Identifier" && annotation.typeName.name === "TypedParameterDecorator" && typeArgsOf(annotation).length === 1;
365
+ const fromComposition = wellTyped && typeOrigins(context, annotation).some((origin) => origin.name === "TypedParameterDecorator" && inComposition(hfs, origin.file));
335
366
  if (!fromComposition) {
336
- context.report({ node: fn.returnType ?? id, messageId: "returnType", data })
337
- return
367
+ context.report({ node: fn.returnType ?? id, messageId: "returnType", data });
368
+ return;
338
369
  }
339
- const injected = typeArgsOf(annotation)[0]
340
- const injectedText = context.sourceCode.getText(injected)
341
- const call = returnedExpression(fn)
370
+ const injected = typeArgsOf(annotation)[0];
371
+ const injectedText = context.sourceCode.getText(injected);
372
+ const call = returnedExpression(fn);
342
373
  const built = call?.type === "CallExpression" && call.callee.type === "Identifier" && call.callee.name === "injector"
343
374
  && call.arguments.length === 1 && typeArgsOf(call).length === 1 && context.sourceCode.getText(typeArgsOf(call)[0]) === injectedText
344
- && inComposition(hfs, calleeOf(context, call).file)
345
- if (!built) context.report({ node: call ?? fn, messageId: "body", data })
346
- const doc = jsdocBefore(context, statement)
347
- const mention = injectedText.match(/[A-Za-z_$][\w$]*/)?.[0] ?? injectedText
348
- if (doc === null || !new RegExp(`\\b${mention.replace(/\$/g, "\\$")}\\b`).test(doc)) context.report({ node: id, messageId: "jsdoc", data })
349
- }
350
-
375
+ && inComposition(hfs, calleeOf(context, call).file);
376
+ if (!built)
377
+ context.report({ node: call ?? fn, messageId: "body", data });
378
+ const doc = jsdocBefore(context, statement);
379
+ const mention = injectedText.match(/[A-Za-z_$][\w$]*/)?.[0] ?? injectedText;
380
+ if (doc === null || !new RegExp(`\\b${mention.replace(/\$/g, "\\$")}\\b`).test(doc))
381
+ context.report({ node: id, messageId: "jsdoc", data });
382
+ };
351
383
  // -- injector-type-match ---------------------------------------------------------------------------
352
-
353
384
  /** `T` of the injector equals the parameter annotation. */
354
385
  export const injectorTypeMatch = {
355
386
  meta: {
@@ -366,71 +397,77 @@ export const injectorTypeMatch = {
366
397
  return {
367
398
  MethodDefinition(node) {
368
399
  for (const param of constructorParams(node)) {
369
- const { target, decorators } = paramParts(param)
400
+ const { target, decorators } = paramParts(param);
370
401
  for (const decorator of decorators) {
371
- if (decorator.expression.type !== "CallExpression") continue
372
- const injected = injectedTypeOf(context, decorator.expression)
373
- if (!injected) continue
374
- check(context, decorator, target, injected)
402
+ if (decorator.expression.type !== "CallExpression")
403
+ continue;
404
+ const injected = injectedTypeOf(context, decorator.expression);
405
+ if (!injected)
406
+ continue;
407
+ check(context, decorator, decorator.expression, target, injected);
375
408
  }
376
409
  }
377
410
  },
378
- }
411
+ };
379
412
  },
380
- }
381
-
413
+ };
382
414
  /** Compares one injector's `T` with the annotation of the parameter it decorates. */
383
- const check = (context, decorator, target, injected) => {
384
- const { checker, toTs } = typed(context)
385
- const callee = decorator.expression.callee
386
- const injector = callee.type === "Identifier" ? callee.name : (staticPropertyName(callee) ?? "Inject")
387
- const annotation = target.typeAnnotation?.typeAnnotation
388
- const injectedName = checker.typeToString(injected)
415
+ const check = (context, decorator, call, target, injected) => {
416
+ const { checker, toTs } = typed(context);
417
+ const callee = call.callee;
418
+ const injector = callee.type === "Identifier" ? callee.name : ((callee.type === "MemberExpression" ? staticPropertyName(callee) : null) ?? "Inject");
419
+ const annotation = target.typeAnnotation?.typeAnnotation;
420
+ const injectedName = checker.typeToString(injected);
389
421
  if (!annotation) {
390
- context.report({ node: target, messageId: "unannotated", data: { injector, injected: injectedName } })
391
- return
422
+ context.report({ node: target, messageId: "unannotated", data: { injector, injected: injectedName } });
423
+ return;
392
424
  }
393
- const tsAnnotation = toTs(annotation)
394
- if (!tsAnnotation) return
395
- const declared = checker.getNonNullableType(checker.getTypeFromTypeNode(tsAnnotation))
396
- const declaredName = checker.typeToString(declared)
425
+ const tsAnnotation = toTs(annotation);
426
+ // every type annotation maps to a TypeScript type node; the guard only tells the compiler so
427
+ if (!tsAnnotation || !ts.isTypeNode(tsAnnotation))
428
+ return;
429
+ const declared = checker.getNonNullableType(checker.getTypeFromTypeNode(tsAnnotation));
430
+ const declaredName = checker.typeToString(declared);
397
431
  if (injected.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
398
- context.report({ node: decorator, messageId: "unresolved", data: { injector, declared: declaredName } })
399
- return
432
+ context.report({ node: decorator, messageId: "unresolved", data: { injector, declared: declaredName } });
433
+ return;
400
434
  }
401
- if (injected === declared) return
402
- if (checker.isTypeAssignableTo(injected, declared) && checker.isTypeAssignableTo(declared, injected)) return
403
- context.report({ node: annotation, messageId: "mismatch", data: { injector, injected: injectedName, declared: declaredName } })
404
- }
405
-
435
+ if (injected === declared)
436
+ return;
437
+ if (checker.isTypeAssignableTo(injected, declared) && checker.isTypeAssignableTo(declared, injected))
438
+ return;
439
+ context.report({ node: annotation, messageId: "mismatch", data: { injector, injected: injectedName, declared: declaredName } });
440
+ };
406
441
  // -- infra-needs-injector --------------------------------------------------------------------------
407
-
408
442
  /** True when the class of a constructor can be built by the Nest container: a Nest class decorator, or any decorated constructor parameter. */
409
443
  const isContainerBuilt = (context, constructor) => {
410
- const declaration = constructor.parent?.parent
411
- if (declaration?.type !== "ClassDeclaration" && declaration?.type !== "ClassExpression") return false
412
- if ((declaration.decorators ?? []).some((decorator) => packageOfExport(context, decorator.expression)?.startsWith("@nestjs/"))) return true
413
- return constructorParams(constructor).some((param) => paramParts(param).decorators.length > 0)
414
- }
415
-
444
+ const declaration = constructor.parent?.parent;
445
+ if (declaration?.type !== "ClassDeclaration" && declaration?.type !== "ClassExpression")
446
+ return false;
447
+ if ((declaration.decorators ?? []).some((decorator) => packageOfExport(context, decorator.expression)?.startsWith("@nestjs/")))
448
+ return true;
449
+ return constructorParams(constructor).some((param) => paramParts(param).decorators.length > 0);
450
+ };
416
451
  /**
417
452
  * The origin that makes an annotated type infrastructure: declared by a package, or by a `platform/*` or `integrations/*` owner other
418
453
  * than `own`; null for a domain type, a type of the same owner or a language global.
419
454
  */
420
455
  const infraOriginOf = (context, hfs, own, annotation) => {
421
- const origins = typeOrigins(context, annotation)
456
+ const origins = typeOrigins(context, annotation);
422
457
  // A language global (`ReadonlyArray`, `Array`) is declared in the TypeScript lib and may be augmented by a package
423
458
  // (`@types/node`): the augmentation does not make it infrastructure.
424
- const isLanguageGlobal = (origin) => origins.some((other) => other.name === origin.name && /\/node_modules\/typescript\/lib\//.test(other.file))
459
+ const isLanguageGlobal = (origin) => origins.some((other) => other.name === origin.name && /\/node_modules\/typescript\/lib\//.test(other.file));
425
460
  return origins.find((origin) => {
426
- if (isLanguageGlobal(origin)) return false
427
- if (origin.module !== null) return true
428
- if (own !== null && hfs.ownerOf(origin.file) === own) return false
429
- const tier = hfs.tierOf(origin.file)
430
- return tier === "platform" || tier === "integrations"
431
- }) ?? null
432
- }
433
-
461
+ if (isLanguageGlobal(origin))
462
+ return false;
463
+ if (origin.module !== null)
464
+ return true;
465
+ if (own !== null && hfs.ownerOf(origin.file) === own)
466
+ return false;
467
+ const tier = hfs.tierOf(origin.file);
468
+ return tier === "platform" || tier === "integrations";
469
+ }) ?? null;
470
+ };
434
471
  /** A constructor parameter of an infrastructure type carries an injector. */
435
472
  export const infraNeedsInjector = {
436
473
  meta: {
@@ -442,40 +479,42 @@ export const infraNeedsInjector = {
442
479
  },
443
480
  },
444
481
  create(context) {
445
- const hfs = hfsOf(context)
446
- const filename = context.filename || context.getFilename()
447
- const own = hfs.ownerOf(filename)
482
+ const hfs = hfsOf(context);
483
+ const filename = context.filename || context.getFilename();
484
+ const own = hfs.ownerOf(filename);
448
485
  return {
449
486
  MethodDefinition(node) {
450
487
  // Only a class the container can construct is judged: it carries a Nest class decorator (`@Injectable()`, `@Controller()`,
451
488
  // `@Resolver()`, `@Catch()`, `@CommandHandler(...)`), or a parameter decorator that makes TypeScript emit its parameter
452
489
  // metadata. A plain class (a CQRS message, a factory-built adapter, a test fake) is never DI-constructed.
453
- if (!isContainerBuilt(context, node)) return
490
+ if (!isContainerBuilt(context, node))
491
+ return;
454
492
  for (const param of constructorParams(node)) {
455
- const { target, decorators } = paramParts(param)
456
- const annotation = target.typeAnnotation?.typeAnnotation
457
- if (!annotation) continue
458
- const infra = infraOriginOf(context, hfs, own, annotation)
459
- if (!infra) continue
493
+ const { target, decorators } = paramParts(param);
494
+ const annotation = target.typeAnnotation?.typeAnnotation;
495
+ if (!annotation)
496
+ continue;
497
+ const infra = infraOriginOf(context, hfs, own, annotation);
498
+ if (!infra)
499
+ continue;
460
500
  const carries = decorators.some((decorator) => {
461
- const expression = decorator.expression
462
- if (expression.type !== "CallExpression") return false
463
- return INJECT_NAME.test(calleeOf(context, expression).name ?? "") || injectedTypeOf(context, expression) !== null
464
- })
465
- if (carries) continue
466
- const where = infra.module !== null ? `package ${infra.module}` : `${hfs.tierOf(infra.file)} ${posix.basename(hfs.ownerOf(infra.file) ?? "")}`
467
- context.report({ node: target, messageId: "missing", data: { type: infra.name || context.sourceCode.getText(annotation), where } })
501
+ const expression = decorator.expression;
502
+ if (expression.type !== "CallExpression")
503
+ return false;
504
+ return INJECT_NAME.test(calleeOf(context, expression).name ?? "") || injectedTypeOf(context, expression) !== null;
505
+ });
506
+ if (carries)
507
+ continue;
508
+ const where = infra.module !== null ? `package ${infra.module}` : `${hfs.tierOf(infra.file)} ${posix.basename(hfs.ownerOf(infra.file) ?? "")}`;
509
+ context.report({ node: target, messageId: "missing", data: { type: infra.name || context.sourceCode.getText(annotation), where } });
468
510
  }
469
511
  },
470
- }
512
+ };
471
513
  },
472
- }
473
-
514
+ };
474
515
  // -- injected-param-name --------------------------------------------------------------------------
475
-
476
516
  /** The `ruleParams.be.paramNames` entry for an origin's declared name, or null. */
477
- const paramEntryOf = (entries, name) => entries.find((entry) => (entry.type !== undefined ? entry.type === name : name.length > entry.typeSuffix.length && name.endsWith(entry.typeSuffix))) ?? null
478
-
517
+ const paramEntryOf = (entries, name) => entries.find((entry) => (entry.type !== undefined ? entry.type === name : name.length > entry.typeSuffix.length && name.endsWith(entry.typeSuffix))) ?? null;
479
518
  /** An injected port of the table of `paramNames` is named the way every class names it. */
480
519
  export const injectedParamName = {
481
520
  meta: {
@@ -487,36 +526,40 @@ export const injectedParamName = {
487
526
  },
488
527
  },
489
528
  create(context) {
490
- const hfs = hfsOf(context)
491
- const entries = hfs.ruleParams.paramNames
492
- const own = hfs.ownerOf(context.filename || context.getFilename())
529
+ const hfs = hfsOf(context);
530
+ const entries = hfs.ruleParams.paramNames;
531
+ const own = hfs.ownerOf(context.filename || context.getFilename());
493
532
  return {
494
533
  MethodDefinition(node) {
495
- if (!isContainerBuilt(context, node)) return
534
+ if (!isContainerBuilt(context, node))
535
+ return;
496
536
  for (const param of constructorParams(node)) {
497
- const { target, decorators } = paramParts(param)
498
- if (target.type !== "Identifier") continue
499
- const annotation = target.typeAnnotation?.typeAnnotation
500
- if (!annotation) continue
501
- const injected = decorators.some((decorator) => decorator.expression.type === "CallExpression" && (INJECT_NAME.test(calleeOf(context, decorator.expression).name ?? "") || injectedTypeOf(context, decorator.expression) !== null))
502
- if (!injected) continue
503
- const infra = infraOriginOf(context, hfs, own, annotation)
504
- if (!infra) continue
505
- const entry = paramEntryOf(entries, infra.name)
506
- if (!entry || entry.names.includes(target.name)) continue
507
- if (entry.nameSuffix !== undefined && target.name.length > entry.nameSuffix.length && target.name.endsWith(entry.nameSuffix)) continue
508
- const expected = [...entry.names.map((name) => `\`${name}\``), ...(entry.nameSuffix !== undefined ? [`\`<name>${entry.nameSuffix}\``] : [])].join(" or ")
509
- context.report({ node: target, messageId: "name", data: { param: target.name, type: infra.name, expected } })
537
+ const { target, decorators } = paramParts(param);
538
+ if (target.type !== "Identifier")
539
+ continue;
540
+ const annotation = target.typeAnnotation?.typeAnnotation;
541
+ if (!annotation)
542
+ continue;
543
+ const injected = decorators.some((decorator) => decorator.expression.type === "CallExpression" && (INJECT_NAME.test(calleeOf(context, decorator.expression).name ?? "") || injectedTypeOf(context, decorator.expression) !== null));
544
+ if (!injected)
545
+ continue;
546
+ const infra = infraOriginOf(context, hfs, own, annotation);
547
+ if (!infra)
548
+ continue;
549
+ const entry = paramEntryOf(entries, infra.name);
550
+ if (!entry || entry.names.includes(target.name))
551
+ continue;
552
+ if (entry.nameSuffix !== undefined && target.name.length > entry.nameSuffix.length && target.name.endsWith(entry.nameSuffix))
553
+ continue;
554
+ const expected = [...entry.names.map((name) => `\`${name}\``), ...(entry.nameSuffix !== undefined ? [`\`<name>${entry.nameSuffix}\``] : [])].join(" or ");
555
+ context.report({ node: target, messageId: "name", data: { param: target.name, type: infra.name, expected } });
510
556
  }
511
557
  },
512
- }
558
+ };
513
559
  },
514
- }
515
-
560
+ };
516
561
  // -- no-module-ref ---------------------------------------------------------------------------------
517
-
518
- const MODULE_REF_CALLS = new Set(["get", "resolve", "create"])
519
-
562
+ const MODULE_REF_CALLS = new Set(["get", "resolve", "create"]);
520
563
  /** No service locator: `ModuleRef` is not imported, typed or called. */
521
564
  export const noModuleRef = {
522
565
  meta: {
@@ -528,35 +571,40 @@ export const noModuleRef = {
528
571
  },
529
572
  },
530
573
  create(context) {
531
- const isModuleRef = (node) => typeOrigins(context, node).some((origin) => origin.name === "ModuleRef" && origin.module === "@nestjs/core")
574
+ const isModuleRef = (node) => typeOrigins(context, node).some((origin) => origin.name === "ModuleRef" && origin.module === "@nestjs/core");
532
575
  return {
533
576
  ImportDeclaration(node) {
534
- if (node.source.value !== "@nestjs/core") return
577
+ if (node.source.value !== "@nestjs/core")
578
+ return;
535
579
  for (const specifier of node.specifiers) {
536
- if (specifier.type === "ImportSpecifier" && (specifier.imported.name ?? specifier.imported.value) === "ModuleRef") context.report({ node: specifier, messageId: "use" })
580
+ if (specifier.type === "ImportSpecifier" && nameOrValue(specifier.imported) === "ModuleRef")
581
+ context.report({ node: specifier, messageId: "use" });
537
582
  }
538
583
  },
539
584
  ExportNamedDeclaration(node) {
540
- if (node.source?.value !== "@nestjs/core") return
585
+ if (node.source?.value !== "@nestjs/core")
586
+ return;
541
587
  for (const specifier of node.specifiers) {
542
- if ((specifier.local.name ?? specifier.local.value) === "ModuleRef") context.report({ node: specifier, messageId: "use" })
588
+ if (nameOrValue(specifier.local) === "ModuleRef")
589
+ context.report({ node: specifier, messageId: "use" });
543
590
  }
544
591
  },
545
592
  TSTypeReference(node) {
546
- const name = node.typeName.type === "Identifier" ? node.typeName.name : node.typeName.right?.name
547
- if (name === "ModuleRef" && isModuleRef(node)) context.report({ node, messageId: "use" })
593
+ const name = node.typeName.type === "Identifier" ? node.typeName.name : node.typeName.type === "TSQualifiedName" ? node.typeName.right?.name : undefined;
594
+ if (name === "ModuleRef" && isModuleRef(node))
595
+ context.report({ node, messageId: "use" });
548
596
  },
549
597
  CallExpression(node) {
550
- const callee = node.callee
551
- if (callee.type !== "MemberExpression" || !MODULE_REF_CALLS.has(staticPropertyName(callee) ?? "")) return
552
- if (isModuleRef(callee.object)) context.report({ node: callee, messageId: "use" })
598
+ const callee = node.callee;
599
+ if (callee.type !== "MemberExpression" || !MODULE_REF_CALLS.has(staticPropertyName(callee) ?? ""))
600
+ return;
601
+ if (isModuleRef(callee.object))
602
+ context.report({ node: callee, messageId: "use" });
553
603
  },
554
- }
604
+ };
555
605
  },
556
- }
557
-
606
+ };
558
607
  // -- no-forward-ref --------------------------------------------------------------------------------
559
-
560
608
  /** No `forwardRef(`: a cycle is a layering finding, not a DI trick. */
561
609
  export const noForwardRef = {
562
610
  meta: {
@@ -570,17 +618,15 @@ export const noForwardRef = {
570
618
  create(context) {
571
619
  return {
572
620
  CallExpression(node) {
573
- const callee = calleeOf(context, node)
574
- if (callee.name === "forwardRef" && (callee.module !== null || callee.ambient)) context.report({ node, messageId: "cycle" })
621
+ const callee = calleeOf(context, node);
622
+ if (callee.name === "forwardRef" && (callee.module !== null || callee.ambient))
623
+ context.report({ node, messageId: "cycle" });
575
624
  },
576
- }
625
+ };
577
626
  },
578
- }
579
-
627
+ };
580
628
  // -- no-string-token -------------------------------------------------------------------------------
581
-
582
- const TOKEN_GETTERS = new Set(["getEntityManagerToken", "getDataSourceToken"])
583
-
629
+ const TOKEN_GETTERS = new Set(["getEntityManagerToken", "getDataSourceToken"]);
584
630
  /** A token is a class or a `unique symbol`, never a string. */
585
631
  export const noStringToken = {
586
632
  meta: {
@@ -593,35 +639,43 @@ export const noStringToken = {
593
639
  },
594
640
  },
595
641
  create(context) {
596
- const filename = context.filename || context.getFilename()
642
+ const filename = context.filename || context.getFilename();
597
643
  const isString = (node) => {
598
- if (isPackageExport(context, node, "@nestjs/core")) return false
599
- if (node.type === "CallExpression" && TOKEN_GETTERS.has(calleeOf(context, node).name ?? "")) return false
600
- if (staticText(node) !== null) return true
601
- if (node.type === "TemplateLiteral") return true
602
- const { checker, toTs } = typed(context)
603
- const tsNode = toTs(node)
604
- return tsNode ? isStringType(checker.getTypeAtLocation(tsNode)) : false
605
- }
644
+ if (isPackageExport(context, node, "@nestjs/core"))
645
+ return false;
646
+ if (node.type === "CallExpression" && TOKEN_GETTERS.has(calleeOf(context, node).name ?? ""))
647
+ return false;
648
+ if (staticText(node) !== null)
649
+ return true;
650
+ if (node.type === "TemplateLiteral")
651
+ return true;
652
+ const { checker, toTs } = typed(context);
653
+ const tsNode = toTs(node);
654
+ return tsNode ? isStringType(checker.getTypeAtLocation(tsNode)) : false;
655
+ };
606
656
  return {
607
657
  Property(node) {
608
- const value = provideValue(node)
609
- if (value && isString(value)) context.report({ node: value, messageId: "string" })
658
+ const value = provideValue(node);
659
+ if (value && isString(value))
660
+ context.report({ node: value, messageId: "string" });
610
661
  },
611
662
  CallExpression(node) {
612
- const [first] = node.arguments
613
- if (!first) return
614
- const callee = calleeOf(context, node)
663
+ const [first] = node.arguments;
664
+ if (!first)
665
+ return;
666
+ const callee = calleeOf(context, node);
615
667
  if (isRawInject(callee) || callee.name === "injector") {
616
- if (isString(first)) context.report({ node: first, messageId: "string" })
617
- } else if (TOKEN_GETTERS.has(callee.name ?? "") && callee.module !== null && !posix.basename(normalizePath(filename)).endsWith(".connection.ts")) {
618
- if (staticText(first) !== null || first.type === "TemplateLiteral") context.report({ node: first, messageId: "connection", data: { name: callee.name } })
668
+ if (isString(first))
669
+ context.report({ node: first, messageId: "string" });
670
+ }
671
+ else if (TOKEN_GETTERS.has(callee.name ?? "") && callee.module !== null && !posix.basename(normalizePath(filename)).endsWith(".connection.ts")) {
672
+ if (staticText(first) !== null || first.type === "TemplateLiteral")
673
+ context.report({ node: first, messageId: "connection", data: { name: callee.name } });
619
674
  }
620
675
  },
621
- }
676
+ };
622
677
  },
623
- }
624
-
678
+ };
625
679
  /** The rules this law contributes to the plugin. */
626
680
  export const rules = {
627
681
  "injector-only": injectorOnly,
@@ -632,8 +686,7 @@ export const rules = {
632
686
  "no-module-ref": noModuleRef,
633
687
  "no-forward-ref": noForwardRef,
634
688
  "no-string-token": noStringToken,
635
- }
636
-
689
+ };
637
690
  /** All start at error: there is one way to inject and no baseline. */
638
691
  export const recommended = {
639
692
  "starci-be/injector-only": "error",
@@ -644,4 +697,4 @@ export const recommended = {
644
697
  "starci-be/no-module-ref": "error",
645
698
  "starci-be/no-forward-ref": "error",
646
699
  "starci-be/no-string-token": "error",
647
- }
700
+ };