@starci/eslint-canon-be 3.5.0 → 3.6.1

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 (198) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +13 -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-view.d.ts +127 -0
  78. package/dist/lib/hfs-view.js +1 -0
  79. package/dist/lib/hfs.d.ts +34 -0
  80. package/{lib/hfs.mjs → dist/lib/hfs.js} +31 -29
  81. package/dist/lib/import-source.d.ts +63 -0
  82. package/dist/lib/import-source.js +134 -0
  83. package/dist/lib/path.d.ts +8 -0
  84. package/{lib/path.mjs → dist/lib/path.js} +4 -7
  85. package/dist/lib/persistence.d.ts +115 -0
  86. package/{lib/persistence.mjs → dist/lib/persistence.js} +55 -66
  87. package/dist/lib/ports.d.ts +70 -0
  88. package/{lib/ports.mjs → dist/lib/ports.js} +62 -66
  89. package/dist/lib/rule.d.ts +31 -0
  90. package/dist/lib/rule.js +2 -0
  91. package/dist/lib/runtime-root.d.ts +4 -0
  92. package/dist/lib/runtime-root.js +10 -0
  93. package/dist/lib/schema-values.d.ts +13 -0
  94. package/{lib/schema-values.mjs → dist/lib/schema-values.js} +67 -59
  95. package/dist/lib/specifier.d.ts +12 -0
  96. package/dist/lib/specifier.js +29 -0
  97. package/dist/lib/subject-values.d.ts +4 -0
  98. package/dist/lib/subject-values.js +61 -0
  99. package/dist/lib/transactions.d.ts +18 -0
  100. package/{lib/transactions.mjs → dist/lib/transactions.js} +17 -15
  101. package/dist/lib/transport-slots.d.ts +17 -0
  102. package/{lib/transport-slots.mjs → dist/lib/transport-slots.js} +2 -4
  103. package/dist/lib/types.d.ts +96 -0
  104. package/dist/lib/types.js +150 -0
  105. package/dist/lib/unit-spec.d.ts +48 -0
  106. package/{lib/unit-spec.mjs → dist/lib/unit-spec.js} +35 -44
  107. package/dist/lib/webhook-ack.d.ts +4 -0
  108. package/{lib/webhook-ack.mjs → dist/lib/webhook-ack.js} +78 -62
  109. package/dist/lib/webhook-handoff-inbox.d.ts +3 -0
  110. package/{lib/webhook-handoff-inbox.mjs → dist/lib/webhook-handoff-inbox.js} +195 -167
  111. package/dist/lib/webhook-handoff-intake.d.ts +4 -0
  112. package/{lib/webhook-handoff-intake.mjs → dist/lib/webhook-handoff-intake.js} +125 -102
  113. package/dist/lib/webhook-handoff.d.ts +15 -0
  114. package/{lib/webhook-handoff.mjs → dist/lib/webhook-handoff.js} +107 -100
  115. package/dist/lib/webhook-verifiers.d.ts +20 -0
  116. package/{lib/webhook-verifiers.mjs → dist/lib/webhook-verifiers.js} +108 -104
  117. package/dist/lib/why.d.ts +9 -0
  118. package/{lib/why.mjs → dist/lib/why.js} +6 -6
  119. package/dist/log-safety.d.ts +20 -0
  120. package/dist/log-safety.js +308 -0
  121. package/dist/module-layering.d.ts +42 -0
  122. package/dist/module-layering.js +257 -0
  123. package/dist/module-shape.d.ts +47 -0
  124. package/{module-shape.mjs → dist/module-shape.js} +278 -236
  125. package/dist/naming.d.ts +49 -0
  126. package/dist/naming.js +246 -0
  127. package/dist/observability.d.ts +25 -0
  128. package/dist/observability.js +245 -0
  129. package/dist/operations.d.ts +23 -0
  130. package/{operations.mjs → dist/operations.js} +410 -332
  131. package/dist/project-graph.d.ts +9 -0
  132. package/{project-graph.mjs → dist/project-graph.js} +8 -13
  133. package/dist/projections.d.ts +11 -0
  134. package/{projections.mjs → dist/projections.js} +60 -57
  135. package/dist/query-safety.d.ts +21 -0
  136. package/{query-safety.mjs → dist/query-safety.js} +92 -108
  137. package/dist/realtime.d.ts +15 -0
  138. package/{realtime.mjs → dist/realtime.js} +108 -106
  139. package/dist/resilience.d.ts +15 -0
  140. package/dist/resilience.js +319 -0
  141. package/dist/schema-authority.d.ts +15 -0
  142. package/{schema-authority.mjs → dist/schema-authority.js} +256 -220
  143. package/dist/service-deps.d.ts +17 -0
  144. package/{service-deps.mjs → dist/service-deps.js} +133 -130
  145. package/dist/service-isolation.d.ts +8 -0
  146. package/dist/service-isolation.js +76 -0
  147. package/dist/size-budget.d.ts +9 -0
  148. package/{size-budget.mjs → dist/size-budget.js} +27 -27
  149. package/dist/sonar-parity.d.ts +5 -0
  150. package/{sonar-parity.mjs → dist/sonar-parity.js} +7 -7
  151. package/dist/spec-no-skip.d.ts +9 -0
  152. package/dist/spec-no-skip.js +81 -0
  153. package/dist/spec-quality.d.ts +10 -0
  154. package/{spec-quality.mjs → dist/spec-quality.js} +103 -87
  155. package/dist/suppression.d.ts +26 -0
  156. package/{suppression.mjs → dist/suppression.js} +30 -45
  157. package/dist/temporal.d.ts +9 -0
  158. package/{temporal.mjs → dist/temporal.js} +75 -65
  159. package/dist/test-world.d.ts +28 -0
  160. package/{test-world.mjs → dist/test-world.js} +102 -83
  161. package/dist/testing.d.ts +51 -0
  162. package/dist/testing.js +368 -0
  163. package/dist/third-party-owner.d.ts +8 -0
  164. package/{third-party-owner.mjs → dist/third-party-owner.js} +105 -91
  165. package/dist/transport.d.ts +24 -0
  166. package/{transport.mjs → dist/transport.js} +279 -240
  167. package/dist/type-safety.d.ts +25 -0
  168. package/dist/type-safety.js +297 -0
  169. package/dist/unit-spec.d.ts +30 -0
  170. package/{unit-spec.mjs → dist/unit-spec.js} +473 -399
  171. package/dist/uploads.d.ts +9 -0
  172. package/{uploads.mjs → dist/uploads.js} +62 -54
  173. package/dist/user-copy.d.ts +9 -0
  174. package/{user-copy.mjs → dist/user-copy.js} +76 -67
  175. package/dist/webhooks.d.ts +16 -0
  176. package/{webhooks.mjs → dist/webhooks.js} +243 -231
  177. package/package.json +23 -8
  178. package/runtime/knowledge/hfs/canon-pins.yaml +18 -11
  179. package/runtime/scripts/hfs/repo-identity.mjs +5 -4
  180. package/async-discipline.mjs +0 -83
  181. package/comments.mjs +0 -416
  182. package/edge-hardening.mjs +0 -139
  183. package/health-ready.mjs +0 -539
  184. package/jwt-alg.mjs +0 -45
  185. package/lib/ast.mjs +0 -88
  186. package/lib/import-source.mjs +0 -104
  187. package/lib/specifier.mjs +0 -27
  188. package/lib/subject-values.mjs +0 -55
  189. package/lib/types.mjs +0 -133
  190. package/log-safety.mjs +0 -303
  191. package/module-layering.mjs +0 -270
  192. package/naming.mjs +0 -251
  193. package/observability.mjs +0 -241
  194. package/resilience.mjs +0 -304
  195. package/service-isolation.mjs +0 -75
  196. package/spec-no-skip.mjs +0 -85
  197. package/testing.mjs +0 -384
  198. package/type-safety.mjs +0 -323
package/lib/ast.mjs DELETED
@@ -1,88 +0,0 @@
1
- /**
2
- * Small AST helpers the HFS laws share, so each rule states its question instead of its traversal.
3
- *
4
- * Nothing here touches disk or the type checker: a rule reads one file's syntax and its path.
5
- */
6
-
7
- const SKIP_KEYS = new Set(["parent", "loc", "range", "tokens", "comments"])
8
-
9
- const isNode = (value) => value !== null && typeof value === "object" && typeof value.type === "string"
10
-
11
- const FUNCTION_TYPES = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"])
12
-
13
- /**
14
- * Visits `node` and every descendant, depth first.
15
- *
16
- * @param {object} node - The subtree root.
17
- * @param {(child: object) => void} visit - Called with each node.
18
- * @param {{ intoFunctions?: boolean }} [options] - `intoFunctions: false` stops at nested function boundaries.
19
- * @returns {void}
20
- */
21
- export const walk = (node, visit, { intoFunctions = true } = {}) => {
22
- if (!isNode(node)) return
23
- visit(node)
24
- for (const [key, value] of Object.entries(node)) {
25
- if (SKIP_KEYS.has(key)) continue
26
- if (Array.isArray(value)) {
27
- for (const child of value) {
28
- if (!isNode(child)) continue
29
- if (!intoFunctions && FUNCTION_TYPES.has(child.type)) continue
30
- walk(child, visit, { intoFunctions })
31
- }
32
- } else if (isNode(value)) {
33
- if (!intoFunctions && FUNCTION_TYPES.has(value.type)) continue
34
- walk(value, visit, { intoFunctions })
35
- }
36
- }
37
- }
38
-
39
- /** True when any node of the subtree satisfies `predicate`. */
40
- export const some = (node, predicate, options) => {
41
- let found = false
42
- walk(node, (child) => {
43
- if (!found && predicate(child)) found = true
44
- }, options)
45
- return found
46
- }
47
-
48
- /** The name a property key, identifier or string literal spells, else null. */
49
- export const keyName = (node) => {
50
- if (!node) return null
51
- if (node.type === "Identifier") return node.name
52
- if (node.type === "Literal" && typeof node.value === "string") return node.value
53
- if (node.type === "PrivateIdentifier") return node.name
54
- return null
55
- }
56
-
57
- /** The decorators of a constructor parameter: on the parameter, its wrapper (`private readonly x`) or its inner identifier. */
58
- export const paramParts = (param) => {
59
- const inner = param.type === "TSParameterProperty" ? param.parameter : param
60
- const target = inner.type === "AssignmentPattern" ? inner.left : inner
61
- return { target, decorators: [...new Set([...(param.decorators ?? []), ...(inner.decorators ?? []), ...(target.decorators ?? [])])] }
62
- }
63
-
64
- /** The name of a decorator (`@Foo` or `@Foo(...)`), else null. */
65
- export const decoratorName = (decorator) => {
66
- const expression = decorator?.expression
67
- if (!expression) return null
68
- if (expression.type === "Identifier") return expression.name
69
- if (expression.type === "CallExpression" && expression.callee.type === "Identifier") return expression.callee.name
70
- return null
71
- }
72
-
73
- /** True for a string literal or a template without substitutions, giving its text via `staticText`. */
74
- export const staticText = (node) => {
75
- if (!node) return null
76
- if (node.type === "Literal" && typeof node.value === "string") return node.value
77
- if (node.type === "TemplateLiteral" && node.expressions.length === 0) return node.quasis.map((q) => q.value.cooked ?? "").join("")
78
- return null
79
- }
80
-
81
- /** Splits `webhookSecret`, `WEBHOOK_SECRET` and `webhook-secret` into lower-case words. */
82
- export const wordsOf = (name) =>
83
- String(name)
84
- .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
85
- .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
86
- .split(/[^A-Za-z0-9]+/)
87
- .filter(Boolean)
88
- .map((word) => word.toLowerCase())
@@ -1,104 +0,0 @@
1
- /**
2
- * Import-owner helpers: what a name written in a file IS, by the import that binds it, never by how it is spelled.
3
- *
4
- * `import { CommandHandler as Handler } from "@nestjs/cqrs"` and `import { CommandHandler } from "@nestjs/cqrs"` are the
5
- * same decorator; a local class that happens to be called `CommandHandler` is not. A rule that asks
6
- * `importOf(context, identifier)` sees that difference and a rule that compares identifier text does not.
7
- */
8
-
9
- const textOf = (node) => (node?.type === "Identifier" ? node.name : node?.type === "Literal" ? String(node.value) : null)
10
-
11
- /**
12
- * The import an identifier resolves to.
13
- *
14
- * @param {object} context - The ESLint rule context.
15
- * @param {object} identifier - An `Identifier` node in a value or decorator position.
16
- * @returns {{ source: string, imported: string } | null} The module specifier and the exported name (`*` for a namespace import, `default` for a default import), or null when the name is not an import binding.
17
- */
18
- export const importOf = (context, identifier) => {
19
- if (identifier?.type !== "Identifier") return null
20
- for (let scope = context.sourceCode.getScope(identifier); scope; scope = scope.upper) {
21
- const variable = scope.set.get(identifier.name)
22
- if (!variable) continue
23
- const definition = variable.defs[0]
24
- if (definition?.type !== "ImportBinding") return null
25
- const specifier = definition.node
26
- const source = definition.parent.source.value
27
- if (specifier.type === "ImportSpecifier") return { source, imported: textOf(specifier.imported) }
28
- if (specifier.type === "ImportNamespaceSpecifier") return { source, imported: "*" }
29
- return { source, imported: "default" }
30
- }
31
- return null
32
- }
33
-
34
- /**
35
- * True when an expression is `name` exported by `source`, whether written as the imported (possibly renamed) identifier
36
- * or as a member of a namespace import of that module.
37
- *
38
- * @param {object} context - The ESLint rule context.
39
- * @param {object} expression - An `Identifier` or `MemberExpression` node.
40
- * @param {string | ReadonlyArray<string>} sources - The module specifier or specifiers.
41
- * @param {string | ReadonlyArray<string>} names - The exported name or names.
42
- * @returns {boolean} Whether the expression is one of those exports.
43
- */
44
- export const isImportedFrom = (context, expression, sources, names) => {
45
- const wantedSources = [].concat(sources)
46
- const wantedNames = [].concat(names)
47
- if (expression?.type === "Identifier") {
48
- const found = importOf(context, expression)
49
- return found !== null && wantedSources.includes(found.source) && wantedNames.includes(found.imported)
50
- }
51
- if (expression?.type === "MemberExpression" && !expression.computed && expression.object.type === "Identifier") {
52
- const found = importOf(context, expression.object)
53
- return found !== null && found.imported === "*" && wantedSources.includes(found.source) && wantedNames.includes(textOf(expression.property))
54
- }
55
- return false
56
- }
57
-
58
- /** The called expression of a decorator: `@Foo` and `@Foo(...)` both give `Foo`. */
59
- export const decoratorCallee = (decorator) => {
60
- const expression = decorator?.expression
61
- return expression?.type === "CallExpression" ? expression.callee : expression ?? null
62
- }
63
-
64
- /** The arguments of a decorator (`[]` for a bare `@Foo`). */
65
- export const decoratorArguments = (decorator) => (decorator?.expression?.type === "CallExpression" ? decorator.expression.arguments : [])
66
-
67
- /**
68
- * Visitors that report every place a file names another module: a static import, an `export ... from`, a dynamic
69
- * `import()`, `import x = require()` and a `require()` call with a literal argument.
70
- *
71
- * @param {(reference: { node: object, source: object, value: string, names: ReadonlyArray<string> }) => void} onReference - Called once per reference; `names` are the exported names taken (`*` for a whole module).
72
- * @returns {object} An ESLint visitor object.
73
- */
74
- export const moduleReferences = (onReference) => {
75
- const literal = (node) => (node?.type === "Literal" && typeof node.value === "string" ? node.value : node?.type === "TemplateLiteral" && node.expressions.length === 0 ? node.quasis[0].value.cooked : null)
76
- return {
77
- ImportDeclaration(node) {
78
- const names = node.specifiers.map((specifier) => specifier.type === "ImportSpecifier" ? textOf(specifier.imported) : specifier.type === "ImportNamespaceSpecifier" ? "*" : "default")
79
- onReference({ node, source: node.source, value: node.source.value, names: names.length > 0 ? names : ["*"] })
80
- },
81
- ExportNamedDeclaration(node) {
82
- if (!node.source) return
83
- onReference({ node, source: node.source, value: node.source.value, names: node.specifiers.map((specifier) => textOf(specifier.local)) })
84
- },
85
- ExportAllDeclaration(node) {
86
- onReference({ node, source: node.source, value: node.source.value, names: ["*"] })
87
- },
88
- ImportExpression(node) {
89
- const value = literal(node.source)
90
- if (value !== null) onReference({ node, source: node.source, value, names: ["*"] })
91
- },
92
- CallExpression(node) {
93
- if (node.callee.type !== "Identifier" || node.callee.name !== "require") return
94
- const value = literal(node.arguments[0])
95
- if (value !== null) onReference({ node, source: node.arguments[0], value, names: ["*"] })
96
- },
97
- TSImportEqualsDeclaration(node) {
98
- const reference = node.moduleReference
99
- if (reference.type !== "TSExternalModuleReference") return
100
- const value = literal(reference.expression)
101
- if (value !== null) onReference({ node, source: reference.expression, value, names: ["*"] })
102
- },
103
- }
104
- }
package/lib/specifier.mjs DELETED
@@ -1,27 +0,0 @@
1
- /**
2
- * Where an import specifier points. A `compilerOptions.paths` alias resolves through the program's own options, so a
3
- * rule asks the same question TypeScript asks instead of maintaining a second alias table.
4
- */
5
- import { posix } from "node:path"
6
- import { typed } from "./types.mjs"
7
-
8
- /**
9
- * The absolute path an alias specifier maps to under the program's `compilerOptions.paths`, or null when no pattern matches.
10
- *
11
- * @param {object} context - The ESLint rule context.
12
- * @param {string} specifier - An import specifier.
13
- * @returns {{ target: string, rest: string } | null} The absolute target without extension and the part the wildcard matched.
14
- */
15
- export const resolveAlias = (context, specifier) => {
16
- const options = typed(context).program.getCompilerOptions()
17
- const base = options.baseUrl ?? options.pathsBasePath
18
- if (!options.paths || typeof base !== "string") return null
19
- for (const [pattern, targets] of Object.entries(options.paths)) {
20
- if (!pattern.endsWith("/*") || !targets[0]) continue
21
- const prefix = pattern.slice(0, -1)
22
- if (!specifier.startsWith(prefix)) continue
23
- const rest = specifier.slice(prefix.length)
24
- return { target: posix.normalize(`${base.replace(/\\/g, "/")}/${targets[0].replace("*", rest)}`), rest }
25
- }
26
- return null
27
- }
@@ -1,55 +0,0 @@
1
- import fs from "node:fs"
2
- import path from "node:path"
3
- import ts from "typescript"
4
- import { functionSubjectExports, valueObjectSubjects } from "../runtime/scripts/hfs/architecture/function-spec-providers.mjs"
5
- import { importOf } from "./import-source.mjs"
6
- import { typed } from "./types.mjs"
7
-
8
- /** The exported real function of a function-only sibling subject, with aliases resolved by TypeScript. */
9
- const realSubjectFunction = (context, value) => {
10
- if (value.type !== "Identifier") return null
11
- const imported = importOf(context, value)
12
- if (!imported || !imported.source.startsWith(".")) return null
13
- const { checker, toTs } = typed(context)
14
- const tsNode = toTs(value)
15
- const symbol = ts.isShorthandPropertyAssignment(tsNode.parent)
16
- ? checker.getShorthandAssignmentValueSymbol(tsNode.parent) : checker.getSymbolAtLocation(tsNode)
17
- const alias = symbol?.declarations?.some(declaration => ts.isImportSpecifier(declaration))
18
- const target = alias ? checker.getAliasedSymbol(symbol) : symbol
19
- const declaration = target?.valueDeclaration
20
- if (!declaration) return null
21
- const sourceFile = declaration.getSourceFile()
22
- const sibling = (context.filename || context.getFilename()).replace(/\.spec\.ts$/u, ".ts")
23
- if (path.resolve(sourceFile.fileName) !== path.resolve(sibling)) return null
24
- const names = functionSubjectExports(ts, sourceFile)
25
- return names?.includes(imported.imported) ? imported.imported : null
26
- }
27
-
28
- const VALUE_OBJECTS = new Map()
29
-
30
- /** The sibling module's value-object classes (undecorated, no constructor injection token), read from disk once per file; `[]` when none. */
31
- export const valueObjectsOfSibling = (context) => {
32
- const sibling = path.resolve((context.filename || context.getFilename()).replace(/\.spec\.ts$/u, ".ts"))
33
- const stamp = fs.existsSync(sibling) ? fs.statSync(sibling).mtimeMs : -1
34
- const cached = VALUE_OBJECTS.get(sibling)
35
- if (cached?.stamp === stamp) return cached.names
36
- const names = stamp < 0 ? [] : valueObjectSubjects(ts, ts.createSourceFile(sibling, fs.readFileSync(sibling, "utf8"), ts.ScriptTarget.Latest, true)) ?? []
37
- VALUE_OBJECTS.set(sibling, { stamp, names })
38
- return names
39
- }
40
-
41
- /** `new Subject(...)` where Subject is a value-object class imported from the sibling module. */
42
- const isValueObjectSubject = (context, value) => {
43
- if (value.type !== "NewExpression" || value.callee.type !== "Identifier") return false
44
- const imported = importOf(context, value.callee)
45
- return Boolean(imported && imported.source.startsWith(".") && valueObjectsOfSibling(context).includes(imported.imported))
46
- }
47
-
48
- /** Real functions under test are subject values; an inline fake or a collaborator remains a kit double. */
49
- export const isRealSubjectValue = (context, value) => {
50
- if (isValueObjectSubject(context, value)) return true
51
- if (value.type === "Identifier") return realSubjectFunction(context, value) !== null
52
- if (value.type !== "ObjectExpression" || value.properties.length === 0) return false
53
- return value.properties.every((property) => property.type === "Property" && !property.computed
54
- && (property.key.name ?? property.key.value) === realSubjectFunction(context, property.value))
55
- }
package/lib/types.mjs DELETED
@@ -1,133 +0,0 @@
1
- /**
2
- * Type-aware helpers: a rule asks what a node IS (its TypeScript type and where that type is declared), never what it
3
- * is called. `starciBeConfig` turns typed linting on for every file (`parserOptions.projectService`), so these
4
- * helpers refuse to run without the program instead of falling back to a name match: a rule that silently degrades to
5
- * a heuristic is the loophole the owner closed.
6
- */
7
- import ts from "typescript"
8
-
9
- /**
10
- * The TypeScript services of the file being linted.
11
- *
12
- * @param {object} context - The ESLint rule context.
13
- * @returns {{ program: object, checker: object, toTs: (node: object) => object }} The program, its checker and the ESTree to TS node map.
14
- */
15
- export const typed = (context) => {
16
- const services = context.sourceCode?.parserServices ?? context.parserServices
17
- if (!services?.program || !services.esTreeNodeToTSNodeMap) {
18
- throw new Error(`${context.id ?? "a starci-be rule"} needs typed linting (parserOptions.projectService); lint through starciBeConfig, which turns it on`)
19
- }
20
- const program = services.program
21
- return { program, checker: program.getTypeChecker(), toTs: (node) => services.esTreeNodeToTSNodeMap.get(node) }
22
- }
23
-
24
- /**
25
- * The declarations of a type: those of its alias (a written `type Repo = Repository<T>`) and those of its own symbol, each
26
- * after alias resolution (an imported type alias resolves to its target). Both are origins, so a rename by alias hides nothing.
27
- */
28
- const declarationsOf = (checker, type) => {
29
- if (!type) return []
30
- return [type.aliasSymbol, type.getSymbol?.()].flatMap((symbol) => {
31
- if (!symbol) return []
32
- const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol
33
- return target.getDeclarations?.() ?? []
34
- })
35
- }
36
-
37
- /**
38
- * Where the type of an ESTree node (or of a type annotation node) is declared.
39
- *
40
- * @param {object} context - The ESLint rule context.
41
- * @param {object} node - An expression, identifier, parameter or type node.
42
- * @returns {Array<{ name: string, file: string, module: string | null }>} Each declaration's symbol name, forward-slash file path and package; `[]` for a primitive or an anonymous type.
43
- */
44
- export const typeOrigins = (context, node) => {
45
- const { checker, toTs } = typed(context)
46
- const tsNode = toTs(node)
47
- if (!tsNode) return []
48
- return originsOfType(checker, ts.isTypeNode(tsNode) ? checker.getTypeFromTypeNode(tsNode) : checker.getTypeAtLocation(tsNode))
49
- }
50
-
51
- /**
52
- * Where a TypeScript type is declared, for a rule that already holds a type from the checker (an alias argument, a call signature return).
53
- *
54
- * @param {object} checker - The program's type checker.
55
- * @param {object} type - A TypeScript type.
56
- * @returns {Array<{ name: string, file: string, module: string | null }>} The declaration origins, as `typeOrigins` returns them.
57
- */
58
- export const originsOfType = (checker, type) => {
59
- const parts = type?.isUnion?.() ? type.types : [type]
60
- return parts.flatMap((part) => declarationsOf(checker, part).map((declaration) => {
61
- const file = String(declaration.getSourceFile().fileName).replace(/\\/g, "/")
62
- return { name: declaration.name && ts.isIdentifier(declaration.name) ? declaration.name.text : "", file, module: moduleOf(declaration, file) }
63
- }))
64
- }
65
-
66
- /**
67
- * The package a declaration belongs to: the name of an enclosing `declare module "<pkg>"`, else the package directory
68
- * of a `node_modules/<pkg>/` (or `node_modules/@scope/<pkg>/`) file, else null for the repository's own source.
69
- */
70
- export const moduleOf = (declaration, file) => {
71
- for (let node = declaration.parent; node; node = node.parent) {
72
- if (ts.isModuleDeclaration(node) && ts.isStringLiteral(node.name)) return node.name.text
73
- }
74
- const match = /\/node_modules\/((?:@[^/]+\/)?[^/]+)\//.exec(file)
75
- return match ? match[1] : null
76
- }
77
-
78
- /**
79
- * True when the node's type is the named type declared in a file whose path satisfies `fromFile`.
80
- *
81
- * @param {object} context - The ESLint rule context.
82
- * @param {object} node - The node to type.
83
- * @param {string} name - The declared symbol name (`EntityManager`).
84
- * @param {(file: string) => boolean} [fromFile] - Filter on the declaring file (a package path, a slot).
85
- * @returns {boolean} Whether one of the node's type origins matches.
86
- */
87
- export const isTypeNamed = (context, node, name, fromFile = () => true) =>
88
- typeOrigins(context, node).some((origin) => origin.name === name && fromFile(origin.file))
89
-
90
- /**
91
- * True when the node's type is `name` declared by the package `pkg` (`typeorm`, `@nestjs/cqrs`).
92
- *
93
- * @param {object} context - The ESLint rule context.
94
- * @param {object} node - The node to type.
95
- * @param {string} name - The declared symbol name.
96
- * @param {string} pkg - The package that declares it.
97
- * @returns {boolean} Whether the node's type is that package's `name`.
98
- */
99
- export const isPackageType = (context, node, name, pkg) =>
100
- typeOrigins(context, node).some((origin) => origin.name === name && origin.module === pkg)
101
-
102
- /**
103
- * The package that declares the value an expression names (`APP_GUARD` and `Injectable` of `@nestjs/*`), after alias resolution, or
104
- * null for the repository's own declaration, an unresolved name or an expression that names no symbol.
105
- *
106
- * @param {object} context - The ESLint rule context.
107
- * @param {object} node - An identifier, member expression or call (its callee is read).
108
- * @returns {string | null} The package name.
109
- */
110
- export const packageOfExport = (context, node) => {
111
- const { checker, toTs } = typed(context)
112
- let tsNode = toTs(node)
113
- if (!tsNode) return null
114
- if (ts.isCallExpression(tsNode)) tsNode = tsNode.expression
115
- const symbol = checker.getSymbolAtLocation(ts.isPropertyAccessExpression(tsNode) ? tsNode.name : tsNode)
116
- if (!symbol) return null
117
- const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol
118
- for (const declaration of target.getDeclarations?.() ?? []) {
119
- const module = moduleOf(declaration, String(declaration.getSourceFile().fileName).replace(/\\/g, "/"))
120
- if (module !== null) return module
121
- }
122
- return null
123
- }
124
-
125
- /**
126
- * True when the node is a reference to a value the package `pkg` exports (`APP_GUARD` of `@nestjs/core`). A local constant of the same name is not.
127
- *
128
- * @param {object} context - The ESLint rule context.
129
- * @param {object} node - An identifier or member expression.
130
- * @param {string} pkg - The package that must declare the referenced value.
131
- * @returns {boolean} Whether the node references an export of that package.
132
- */
133
- export const isPackageExport = (context, node, pkg) => packageOfExport(context, node) === pkg