@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.
- package/CHANGELOG.md +21 -0
- package/README.md +13 -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-view.d.ts +127 -0
- package/dist/lib/hfs-view.js +1 -0
- 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 +23 -8
- package/runtime/knowledge/hfs/canon-pins.yaml +18 -11
- package/runtime/scripts/hfs/repo-identity.mjs +5 -4
- 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
|
@@ -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.
|
|
29
|
-
import { hfsOf } from "./lib/hfs.
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import {
|
|
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))
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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)
|
|
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")
|
|
65
|
-
|
|
66
|
-
const
|
|
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
|
-
|
|
73
|
-
|
|
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
|
|
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)
|
|
83
|
-
|
|
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
|
|
90
|
-
* @param
|
|
91
|
-
* @returns
|
|
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))
|
|
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)
|
|
101
|
-
|
|
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)
|
|
116
|
-
|
|
117
|
-
|
|
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")
|
|
132
|
-
|
|
133
|
-
|
|
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)
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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))
|
|
153
|
-
|
|
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)
|
|
160
|
-
|
|
161
|
-
if (owner.type === "
|
|
162
|
-
|
|
163
|
-
|
|
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")
|
|
189
|
-
|
|
190
|
-
const
|
|
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))
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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)
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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")
|
|
222
|
-
|
|
223
|
-
|
|
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) =>
|
|
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)
|
|
233
|
-
|
|
234
|
-
if (declaration.type
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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 = (
|
|
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 = (
|
|
248
|
-
|
|
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())
|
|
253
|
-
|
|
254
|
-
if (type.
|
|
255
|
-
|
|
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"))
|
|
264
|
-
|
|
265
|
-
|
|
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)
|
|
292
|
-
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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))
|
|
301
|
-
|
|
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(
|
|
307
|
-
|
|
331
|
+
if (!looksLikeInjector(entry))
|
|
332
|
+
continue;
|
|
333
|
+
checkInjector(context, hfs, statement, entry);
|
|
308
334
|
}
|
|
309
|
-
if (statement.declaration?.type !== "VariableDeclaration")
|
|
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")
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
const
|
|
317
|
-
|
|
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))
|
|
331
|
-
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
|
|
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)
|
|
346
|
-
|
|
347
|
-
const
|
|
348
|
-
|
|
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")
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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 =
|
|
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
|
-
|
|
395
|
-
|
|
396
|
-
|
|
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)
|
|
402
|
-
|
|
403
|
-
|
|
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")
|
|
412
|
-
|
|
413
|
-
|
|
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))
|
|
427
|
-
|
|
428
|
-
if (
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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))
|
|
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)
|
|
458
|
-
|
|
459
|
-
|
|
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")
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
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))
|
|
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")
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
const
|
|
504
|
-
if (!
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
if (
|
|
508
|
-
|
|
509
|
-
|
|
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")
|
|
577
|
+
if (node.source.value !== "@nestjs/core")
|
|
578
|
+
return;
|
|
535
579
|
for (const specifier of node.specifiers) {
|
|
536
|
-
if (specifier.type === "ImportSpecifier" && (specifier.imported
|
|
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")
|
|
585
|
+
if (node.source?.value !== "@nestjs/core")
|
|
586
|
+
return;
|
|
541
587
|
for (const specifier of node.specifiers) {
|
|
542
|
-
if ((specifier.local
|
|
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))
|
|
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) ?? ""))
|
|
552
|
-
|
|
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))
|
|
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"))
|
|
599
|
-
|
|
600
|
-
if (
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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))
|
|
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)
|
|
614
|
-
|
|
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))
|
|
617
|
-
|
|
618
|
-
|
|
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
|
+
};
|