@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.
- package/CHANGELOG.md +15 -0
- package/README.md +11 -2
- package/dist/ambient-id.d.ts +9 -0
- package/{ambient-id.mjs → dist/ambient-id.js} +43 -39
- package/dist/app-composition.d.ts +21 -0
- package/{app-composition.mjs → dist/app-composition.js} +396 -300
- package/dist/async-discipline.d.ts +9 -0
- package/dist/async-discipline.js +77 -0
- package/dist/authorization.d.ts +12 -0
- package/{authorization.mjs → dist/authorization.js} +53 -59
- package/dist/boundary-schema.d.ts +15 -0
- package/{boundary-schema.mjs → dist/boundary-schema.js} +120 -105
- package/dist/builders.d.ts +21 -0
- package/{builders.mjs → dist/builders.js} +162 -146
- package/dist/cli.d.ts +15 -0
- package/{cli.mjs → dist/cli.js} +97 -82
- package/dist/comments.d.ts +52 -0
- package/dist/comments.js +412 -0
- package/dist/config-owner.d.ts +24 -0
- package/{config-owner.mjs → dist/config-owner.js} +256 -219
- package/dist/connections.d.ts +12 -0
- package/{connections.mjs → dist/connections.js} +138 -140
- package/dist/cookies.d.ts +9 -0
- package/{cookies.mjs → dist/cookies.js} +58 -58
- package/dist/cqrs.d.ts +27 -0
- package/{cqrs.mjs → dist/cqrs.js} +341 -282
- package/dist/data-access.d.ts +50 -0
- package/{data-access.mjs → dist/data-access.js} +238 -243
- package/dist/default-deny.d.ts +15 -0
- package/{default-deny.mjs → dist/default-deny.js} +103 -88
- package/dist/e2e-flow.d.ts +25 -0
- package/{e2e-flow.mjs → dist/e2e-flow.js} +113 -112
- package/dist/edge-hardening.d.ts +10 -0
- package/dist/edge-hardening.js +173 -0
- package/dist/error-handling.d.ts +21 -0
- package/{error-handling.mjs → dist/error-handling.js} +289 -239
- package/dist/event-bus.d.ts +14 -0
- package/{event-bus.mjs → dist/event-bus.js} +146 -129
- package/dist/feature-thin.d.ts +12 -0
- package/{feature-thin.mjs → dist/feature-thin.js} +208 -191
- package/dist/function-budget.d.ts +7 -0
- package/{function-budget.mjs → dist/function-budget.js} +24 -27
- package/dist/health-ready.d.ts +10 -0
- package/dist/health-ready.js +703 -0
- package/dist/idempotency.d.ts +10 -0
- package/{idempotency.mjs → dist/idempotency.js} +296 -226
- package/dist/index.d.ts +54 -0
- package/{index.mjs → dist/index.js} +91 -109
- package/dist/infra-owner.d.ts +18 -0
- package/{infra-owner.mjs → dist/infra-owner.js} +58 -51
- package/dist/injection.d.ts +30 -0
- package/{injection.mjs → dist/injection.js} +397 -344
- package/dist/input-bounds.d.ts +15 -0
- package/{input-bounds.mjs → dist/input-bounds.js} +143 -132
- package/dist/jobs.d.ts +17 -0
- package/{jobs.mjs → dist/jobs.js} +116 -99
- package/dist/jwt-alg.d.ts +6 -0
- package/dist/jwt-alg.js +49 -0
- package/dist/lib/app-roles.d.ts +25 -0
- package/{lib/app-roles.mjs → dist/lib/app-roles.js} +15 -17
- package/dist/lib/ast.d.ts +40 -0
- package/dist/lib/ast.js +100 -0
- package/dist/lib/client-idempotency.d.ts +3 -0
- package/{lib/client-idempotency.mjs → dist/lib/client-idempotency.js} +316 -268
- package/dist/lib/config.d.ts +36 -0
- package/{lib/config.mjs → dist/lib/config.js} +24 -27
- package/dist/lib/db-writes.d.ts +29 -0
- package/{lib/db-writes.mjs → dist/lib/db-writes.js} +27 -27
- package/dist/lib/declared.d.ts +77 -0
- package/{lib/declared.mjs → dist/lib/declared.js} +83 -78
- package/dist/lib/doors.d.ts +50 -0
- package/{lib/doors.mjs → dist/lib/doors.js} +45 -50
- package/dist/lib/edition.d.ts +25 -0
- package/{lib/edition.mjs → dist/lib/edition.js} +15 -20
- package/dist/lib/entity-writes.d.ts +7 -0
- package/{lib/entity-writes.mjs → dist/lib/entity-writes.js} +31 -22
- package/dist/lib/hfs.d.ts +34 -0
- package/{lib/hfs.mjs → dist/lib/hfs.js} +31 -29
- package/dist/lib/import-source.d.ts +63 -0
- package/dist/lib/import-source.js +134 -0
- package/dist/lib/path.d.ts +8 -0
- package/{lib/path.mjs → dist/lib/path.js} +4 -7
- package/dist/lib/persistence.d.ts +115 -0
- package/{lib/persistence.mjs → dist/lib/persistence.js} +55 -66
- package/dist/lib/ports.d.ts +70 -0
- package/{lib/ports.mjs → dist/lib/ports.js} +62 -66
- package/dist/lib/rule.d.ts +31 -0
- package/dist/lib/rule.js +2 -0
- package/dist/lib/runtime-root.d.ts +4 -0
- package/dist/lib/runtime-root.js +10 -0
- package/dist/lib/schema-values.d.ts +13 -0
- package/{lib/schema-values.mjs → dist/lib/schema-values.js} +67 -59
- package/dist/lib/specifier.d.ts +12 -0
- package/dist/lib/specifier.js +29 -0
- package/dist/lib/subject-values.d.ts +4 -0
- package/dist/lib/subject-values.js +61 -0
- package/dist/lib/transactions.d.ts +18 -0
- package/{lib/transactions.mjs → dist/lib/transactions.js} +17 -15
- package/dist/lib/transport-slots.d.ts +17 -0
- package/{lib/transport-slots.mjs → dist/lib/transport-slots.js} +2 -4
- package/dist/lib/types.d.ts +96 -0
- package/dist/lib/types.js +150 -0
- package/dist/lib/unit-spec.d.ts +48 -0
- package/{lib/unit-spec.mjs → dist/lib/unit-spec.js} +35 -44
- package/dist/lib/webhook-ack.d.ts +4 -0
- package/{lib/webhook-ack.mjs → dist/lib/webhook-ack.js} +78 -62
- package/dist/lib/webhook-handoff-inbox.d.ts +3 -0
- package/{lib/webhook-handoff-inbox.mjs → dist/lib/webhook-handoff-inbox.js} +195 -167
- package/dist/lib/webhook-handoff-intake.d.ts +4 -0
- package/{lib/webhook-handoff-intake.mjs → dist/lib/webhook-handoff-intake.js} +125 -102
- package/dist/lib/webhook-handoff.d.ts +15 -0
- package/{lib/webhook-handoff.mjs → dist/lib/webhook-handoff.js} +107 -100
- package/dist/lib/webhook-verifiers.d.ts +20 -0
- package/{lib/webhook-verifiers.mjs → dist/lib/webhook-verifiers.js} +108 -104
- package/dist/lib/why.d.ts +9 -0
- package/{lib/why.mjs → dist/lib/why.js} +6 -6
- package/dist/log-safety.d.ts +20 -0
- package/dist/log-safety.js +308 -0
- package/dist/module-layering.d.ts +42 -0
- package/dist/module-layering.js +257 -0
- package/dist/module-shape.d.ts +47 -0
- package/{module-shape.mjs → dist/module-shape.js} +278 -236
- package/dist/naming.d.ts +49 -0
- package/dist/naming.js +246 -0
- package/dist/observability.d.ts +25 -0
- package/dist/observability.js +245 -0
- package/dist/operations.d.ts +23 -0
- package/{operations.mjs → dist/operations.js} +410 -332
- package/dist/project-graph.d.ts +9 -0
- package/{project-graph.mjs → dist/project-graph.js} +8 -13
- package/dist/projections.d.ts +11 -0
- package/{projections.mjs → dist/projections.js} +60 -57
- package/dist/query-safety.d.ts +21 -0
- package/{query-safety.mjs → dist/query-safety.js} +92 -108
- package/dist/realtime.d.ts +15 -0
- package/{realtime.mjs → dist/realtime.js} +108 -106
- package/dist/resilience.d.ts +15 -0
- package/dist/resilience.js +319 -0
- package/dist/schema-authority.d.ts +15 -0
- package/{schema-authority.mjs → dist/schema-authority.js} +256 -220
- package/dist/service-deps.d.ts +17 -0
- package/{service-deps.mjs → dist/service-deps.js} +133 -130
- package/dist/service-isolation.d.ts +8 -0
- package/dist/service-isolation.js +76 -0
- package/dist/size-budget.d.ts +9 -0
- package/{size-budget.mjs → dist/size-budget.js} +27 -27
- package/dist/sonar-parity.d.ts +5 -0
- package/{sonar-parity.mjs → dist/sonar-parity.js} +7 -7
- package/dist/spec-no-skip.d.ts +9 -0
- package/dist/spec-no-skip.js +81 -0
- package/dist/spec-quality.d.ts +10 -0
- package/{spec-quality.mjs → dist/spec-quality.js} +103 -87
- package/dist/suppression.d.ts +26 -0
- package/{suppression.mjs → dist/suppression.js} +30 -45
- package/dist/temporal.d.ts +9 -0
- package/{temporal.mjs → dist/temporal.js} +75 -65
- package/dist/test-world.d.ts +28 -0
- package/{test-world.mjs → dist/test-world.js} +102 -83
- package/dist/testing.d.ts +51 -0
- package/dist/testing.js +368 -0
- package/dist/third-party-owner.d.ts +8 -0
- package/{third-party-owner.mjs → dist/third-party-owner.js} +105 -91
- package/dist/transport.d.ts +24 -0
- package/{transport.mjs → dist/transport.js} +279 -240
- package/dist/type-safety.d.ts +25 -0
- package/dist/type-safety.js +297 -0
- package/dist/unit-spec.d.ts +30 -0
- package/{unit-spec.mjs → dist/unit-spec.js} +473 -399
- package/dist/uploads.d.ts +9 -0
- package/{uploads.mjs → dist/uploads.js} +62 -54
- package/dist/user-copy.d.ts +9 -0
- package/{user-copy.mjs → dist/user-copy.js} +76 -67
- package/dist/webhooks.d.ts +16 -0
- package/{webhooks.mjs → dist/webhooks.js} +243 -231
- package/package.json +17 -8
- package/runtime/knowledge/hfs/canon-pins.yaml +17 -10
- package/async-discipline.mjs +0 -83
- package/comments.mjs +0 -416
- package/edge-hardening.mjs +0 -139
- package/health-ready.mjs +0 -539
- package/jwt-alg.mjs +0 -45
- package/lib/ast.mjs +0 -88
- package/lib/import-source.mjs +0 -104
- package/lib/specifier.mjs +0 -27
- package/lib/subject-values.mjs +0 -55
- package/lib/types.mjs +0 -133
- package/log-safety.mjs +0 -303
- package/module-layering.mjs +0 -270
- package/naming.mjs +0 -251
- package/observability.mjs +0 -241
- package/resilience.mjs +0 -304
- package/service-isolation.mjs +0 -75
- package/spec-no-skip.mjs +0 -85
- package/testing.mjs +0 -384
- 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())
|
package/lib/import-source.mjs
DELETED
|
@@ -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
|
-
}
|
package/lib/subject-values.mjs
DELETED
|
@@ -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
|