@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
|
@@ -11,75 +11,78 @@
|
|
|
11
11
|
* capability, a bus is a receiver typed `CommandBus` / `QueryBus`, an entity is a class decorated with `Entity` from
|
|
12
12
|
* `typeorm`. No rule matches a variable name, a class name or a directory spelled in a path pattern.
|
|
13
13
|
*/
|
|
14
|
-
import ts from "typescript"
|
|
15
|
-
import { decoratorCallee, importOf, isImportedFrom, moduleReferences } from "./lib/import-source.
|
|
16
|
-
import { hfsOf } from "./lib/hfs.
|
|
17
|
-
import { walk } from "./lib/ast.
|
|
18
|
-
import { isLoggerType } from "./lib/ports.
|
|
19
|
-
import { isTransportSlot } from "./lib/transport-slots.
|
|
20
|
-
import { isPackageType, typeOrigins, typed } from "./lib/types.
|
|
21
|
-
|
|
14
|
+
import ts from "typescript";
|
|
15
|
+
import { decoratorCallee, importOf, isImportedFrom, moduleReferences } from "./lib/import-source.js";
|
|
16
|
+
import { hfsOf } from "./lib/hfs.js";
|
|
17
|
+
import { walk } from "./lib/ast.js";
|
|
18
|
+
import { isLoggerType } from "./lib/ports.js";
|
|
19
|
+
import { isTransportSlot } from "./lib/transport-slots.js";
|
|
20
|
+
import { isPackageType, typeOrigins, typed } from "./lib/types.js";
|
|
21
|
+
import { isNode } from "./lib/rule.js";
|
|
22
22
|
/** The slot that holds messages and handlers. */
|
|
23
|
-
const APPLICATION = "be.feature.application"
|
|
24
|
-
|
|
23
|
+
const APPLICATION = "be.feature.application";
|
|
25
24
|
/** The slots that hold application-layer code (the support folder is the same layer). */
|
|
26
|
-
const APPLICATION_LAYER = new Set([APPLICATION, "be.feature.application.support"])
|
|
27
|
-
|
|
25
|
+
const APPLICATION_LAYER = new Set([APPLICATION, "be.feature.application.support"]);
|
|
28
26
|
/** The package that owns the CQRS types. */
|
|
29
|
-
const CQRS_PACKAGE = "@nestjs/cqrs"
|
|
30
|
-
|
|
27
|
+
const CQRS_PACKAGE = "@nestjs/cqrs";
|
|
31
28
|
/** The base name of a filename. */
|
|
32
|
-
const baseName = (filename) => String(filename).replace(/\\/g, "/").split("/").pop()
|
|
33
|
-
|
|
29
|
+
const baseName = (filename) => String(filename).replace(/\\/g, "/").split("/").pop() ?? "";
|
|
30
|
+
/** Whether a value is a TypeScript type (it carries numeric `flags` and the `getSymbol` method). */
|
|
31
|
+
const isTsType = (value) => typeof value === "object" && value !== null && "flags" in value && typeof value.flags === "number" && "getSymbol" in value;
|
|
32
|
+
/** Whether an object type is a reference to a generic type (`Set<T>`, `Array<T>`), which carries type arguments. */
|
|
33
|
+
const isTypeReference = (type) => (type.flags & ts.TypeFlags.Object) !== 0 && "objectFlags" in type && typeof type.objectFlags === "number" && (type.objectFlags & ts.ObjectFlags.Reference) !== 0;
|
|
34
|
+
/** The key of a class member, or undefined for a member that has none (a static block, an index signature). */
|
|
35
|
+
const memberKeyOf = (member) => ("key" in member ? member.key : undefined);
|
|
34
36
|
/** The bus receivers of `@nestjs/cqrs`. */
|
|
35
|
-
const isBus = (context, node) => isPackageType(context, node, "CommandBus", CQRS_PACKAGE) || isPackageType(context, node, "QueryBus", CQRS_PACKAGE)
|
|
36
|
-
|
|
37
|
+
const isBus = (context, node) => isPackageType(context, node, "CommandBus", CQRS_PACKAGE) || isPackageType(context, node, "QueryBus", CQRS_PACKAGE);
|
|
37
38
|
/** True when a declaration file belongs to the capability `capability` of a platform slot. */
|
|
38
39
|
const declaredByCapability = (hfs, file, capability) => {
|
|
39
|
-
if (hfs.slotOf(file) !== "be.platform")
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
if (hfs.slotOf(file) !== "be.platform")
|
|
41
|
+
return false;
|
|
42
|
+
const owner = hfs.ownerOf(file);
|
|
43
|
+
return owner !== null && owner.split("/").pop() === capability;
|
|
44
|
+
};
|
|
44
45
|
/** The classes a file declares. */
|
|
45
|
-
const classVisitors = (visit) => ({ ClassDeclaration: visit, ClassExpression: visit })
|
|
46
|
-
|
|
46
|
+
const classVisitors = (visit) => ({ ClassDeclaration: visit, ClassExpression: visit });
|
|
47
47
|
/** The constructor of a class body, or undefined. */
|
|
48
|
-
const constructorOf = (node) => node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "constructor")
|
|
49
|
-
|
|
48
|
+
const constructorOf = (node) => node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "constructor");
|
|
50
49
|
/** The message kind (`command` / `query`) of a file in the application slot, or null. */
|
|
51
50
|
const messageKindOf = (context) => {
|
|
52
|
-
const filename = context.filename
|
|
53
|
-
const name = baseName(filename)
|
|
54
|
-
const kind = name.endsWith(".command.ts") ? "command" : name.endsWith(".query.ts") ? "query" : null
|
|
55
|
-
if (kind === null)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
51
|
+
const filename = context.filename;
|
|
52
|
+
const name = baseName(filename);
|
|
53
|
+
const kind = name.endsWith(".command.ts") ? "command" : name.endsWith(".query.ts") ? "query" : null;
|
|
54
|
+
if (kind === null)
|
|
55
|
+
return null;
|
|
56
|
+
return hfsOf(context).slotOf(filename) === APPLICATION ? kind : null;
|
|
57
|
+
};
|
|
59
58
|
// -- handler-overrides-process -----------------------------------------------------------------------------------
|
|
60
|
-
|
|
61
59
|
/** True when the class (transitively) extends the `ICQRSHandler` of `platform/cqrs`. */
|
|
62
60
|
const extendsTemplate = (context, node, hfs) => {
|
|
63
|
-
const { checker, toTs } = typed(context)
|
|
64
|
-
const seen = new Set()
|
|
61
|
+
const { checker, toTs } = typed(context);
|
|
62
|
+
const seen = new Set();
|
|
65
63
|
const reaches = (type) => {
|
|
66
|
-
if (!type || seen.has(type))
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
64
|
+
if (!type || seen.has(type))
|
|
65
|
+
return false;
|
|
66
|
+
seen.add(type);
|
|
67
|
+
const symbol = type.getSymbol?.();
|
|
68
|
+
if (symbol?.getName() === "ICQRSHandler" && (symbol.getDeclarations() ?? []).some((declaration) => declaredByCapability(hfs, declaration.getSourceFile().fileName, "cqrs")))
|
|
69
|
+
return true;
|
|
70
|
+
// `type.target ?? type`: a reference resolves to its generic class or interface, whose base types the checker knows.
|
|
71
|
+
const target = Reflect.get(type, "target");
|
|
72
|
+
const resolved = target ?? type;
|
|
73
|
+
if (!isTsType(resolved) || !resolved.isClassOrInterface())
|
|
74
|
+
return false;
|
|
75
|
+
return (checker.getBaseTypes(resolved) ?? []).some(reaches);
|
|
76
|
+
};
|
|
77
|
+
return reaches(checker.getTypeAtLocation(toTs(node)));
|
|
78
|
+
};
|
|
75
79
|
/** True when `process` is still the template's abstract member (no class of the chain implemented it). */
|
|
76
80
|
const processIsAbstract = (context, node) => {
|
|
77
|
-
const { checker, toTs } = typed(context)
|
|
78
|
-
const symbol = checker.getPropertyOfType(checker.getTypeAtLocation(toTs(node)), "process")
|
|
79
|
-
const declarations = symbol?.getDeclarations() ?? []
|
|
80
|
-
return declarations.length === 0 || declarations.every((declaration) => (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Abstract) !== 0)
|
|
81
|
-
}
|
|
82
|
-
|
|
81
|
+
const { checker, toTs } = typed(context);
|
|
82
|
+
const symbol = checker.getPropertyOfType(checker.getTypeAtLocation(toTs(node)), "process");
|
|
83
|
+
const declarations = symbol?.getDeclarations() ?? [];
|
|
84
|
+
return declarations.length === 0 || declarations.every((declaration) => (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Abstract) !== 0);
|
|
85
|
+
};
|
|
83
86
|
/** A handler implements the template's `process` and never overrides its public `execute`. */
|
|
84
87
|
export const handlerOverridesProcess = {
|
|
85
88
|
meta: {
|
|
@@ -93,50 +96,49 @@ export const handlerOverridesProcess = {
|
|
|
93
96
|
},
|
|
94
97
|
},
|
|
95
98
|
create(context) {
|
|
96
|
-
const hfs = hfsOf(context)
|
|
97
|
-
if (hfs.slotOf(context.filename) !== APPLICATION)
|
|
99
|
+
const hfs = hfsOf(context);
|
|
100
|
+
if (hfs.slotOf(context.filename) !== APPLICATION)
|
|
101
|
+
return {};
|
|
98
102
|
return classVisitors((node) => {
|
|
99
|
-
if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"])))
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
+
if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"])))
|
|
104
|
+
return;
|
|
105
|
+
const name = node.id?.name ?? "this handler";
|
|
106
|
+
const own = (member) => node.body.body.find((candidate) => candidate.type === "MethodDefinition" && candidate.key?.type === "Identifier" && candidate.key.name === member);
|
|
107
|
+
const execute = own("execute");
|
|
103
108
|
if (execute) {
|
|
104
|
-
context.report({ node: execute.key, messageId: "overridesExecute", data: { name } })
|
|
105
|
-
return
|
|
109
|
+
context.report({ node: execute.key, messageId: "overridesExecute", data: { name } });
|
|
110
|
+
return;
|
|
106
111
|
}
|
|
107
112
|
if (!extendsTemplate(context, node, hfs)) {
|
|
108
|
-
context.report({ node: node.id ?? node, messageId: "notTemplate", data: { name } })
|
|
109
|
-
return
|
|
113
|
+
context.report({ node: node.id ?? node, messageId: "notTemplate", data: { name } });
|
|
114
|
+
return;
|
|
110
115
|
}
|
|
111
|
-
if (!own("process") && processIsAbstract(context, node))
|
|
112
|
-
|
|
116
|
+
if (!own("process") && processIsAbstract(context, node))
|
|
117
|
+
context.report({ node: node.id ?? node, messageId: "noProcess", data: { name } });
|
|
118
|
+
});
|
|
113
119
|
},
|
|
114
|
-
}
|
|
115
|
-
|
|
120
|
+
};
|
|
116
121
|
// -- handler-is-thin ---------------------------------------------------------------------------------------------
|
|
117
|
-
|
|
118
122
|
/** Statements and expressions that are a decision or a repetition: none of them belongs in a handler. */
|
|
119
|
-
const DECISION_NODES = new Set(["IfStatement", "SwitchStatement", "ConditionalExpression", "LogicalExpression", "ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement", "TryStatement", "ThrowStatement"])
|
|
120
|
-
|
|
123
|
+
const DECISION_NODES = new Set(["IfStatement", "SwitchStatement", "ConditionalExpression", "LogicalExpression", "ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement", "TryStatement", "ThrowStatement"]);
|
|
121
124
|
/** The one collaborator a handler may hold: a domain service (a class named `*Service` declared in a `*.service.ts` file) or a projection (a class declared in a `*.projection.ts` file of slot `be.projections`). */
|
|
122
125
|
const isServiceType = (context, hfs, annotation) => typeOrigins(context, annotation).some((origin) => origin.module === null
|
|
123
|
-
&& ((origin.name.endsWith("Service") && origin.file.endsWith(".service.ts")) || (origin.file.endsWith(".projection.ts") && hfs.slotOf(origin.file) === "be.projections")))
|
|
124
|
-
|
|
126
|
+
&& ((origin.name.endsWith("Service") && origin.file.endsWith(".service.ts")) || (origin.file.endsWith(".projection.ts") && hfs.slotOf(origin.file) === "be.projections")));
|
|
125
127
|
/** The injected members of a class: constructor parameters and decorated properties, with the type annotation of each. */
|
|
126
128
|
const injectedMembers = (node) => {
|
|
127
|
-
const members = []
|
|
128
|
-
const constructor = constructorOf(node)
|
|
129
|
+
const members = [];
|
|
130
|
+
const constructor = constructorOf(node);
|
|
129
131
|
for (const original of constructor?.value.params ?? []) {
|
|
130
|
-
const param = original.type === "TSParameterProperty" ? original.parameter : original
|
|
131
|
-
members.push({ report: original, param, name: original.type === "TSParameterProperty" && param.type === "Identifier" ? param.name : null, annotation: param.typeAnnotation?.typeAnnotation })
|
|
132
|
+
const param = original.type === "TSParameterProperty" ? original.parameter : original;
|
|
133
|
+
members.push({ report: original, param, name: original.type === "TSParameterProperty" && param.type === "Identifier" ? param.name : null, annotation: param.typeAnnotation?.typeAnnotation });
|
|
132
134
|
}
|
|
133
135
|
for (const member of node.body.body) {
|
|
134
|
-
if (member.type !== "PropertyDefinition" || (member.decorators ?? []).length === 0)
|
|
135
|
-
|
|
136
|
+
if (member.type !== "PropertyDefinition" || (member.decorators ?? []).length === 0)
|
|
137
|
+
continue;
|
|
138
|
+
members.push({ report: member, param: member, name: member.key.type === "Identifier" ? member.key.name : null, annotation: member.typeAnnotation?.typeAnnotation });
|
|
136
139
|
}
|
|
137
|
-
return members
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
+
return members;
|
|
141
|
+
};
|
|
140
142
|
/** A handler maps its input, calls exactly one method of an injected service and returns its result. */
|
|
141
143
|
export const handlerIsThin = {
|
|
142
144
|
meta: {
|
|
@@ -151,43 +153,50 @@ export const handlerIsThin = {
|
|
|
151
153
|
},
|
|
152
154
|
},
|
|
153
155
|
create(context) {
|
|
154
|
-
const hfs = hfsOf(context)
|
|
155
|
-
if (hfs.slotOf(context.filename) !== APPLICATION)
|
|
156
|
-
|
|
156
|
+
const hfs = hfsOf(context);
|
|
157
|
+
if (hfs.slotOf(context.filename) !== APPLICATION)
|
|
158
|
+
return {};
|
|
159
|
+
const sourceCode = context.sourceCode;
|
|
157
160
|
return classVisitors((node) => {
|
|
158
|
-
if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"])))
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
+
if (!(node.decorators ?? []).some((decorator) => isImportedFrom(context, decoratorCallee(decorator), CQRS_PACKAGE, ["CommandHandler", "QueryHandler"])))
|
|
162
|
+
return;
|
|
163
|
+
const name = node.id?.name ?? "this handler";
|
|
164
|
+
const services = new Set();
|
|
161
165
|
for (const injected of injectedMembers(node)) {
|
|
162
166
|
if (injected.annotation && isServiceType(context, hfs, injected.annotation)) {
|
|
163
|
-
if (injected.name !== null)
|
|
164
|
-
|
|
165
|
-
|
|
167
|
+
if (injected.name !== null)
|
|
168
|
+
services.add(injected.name);
|
|
169
|
+
}
|
|
170
|
+
else if (!injected.annotation || !isLoggerType(context, injected.annotation)) {
|
|
171
|
+
context.report({ node: injected.report, messageId: "dependency", data: { what: sourceCode.getText(injected.param) } });
|
|
166
172
|
}
|
|
167
173
|
}
|
|
168
|
-
const method = node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "method" && member.key.type === "Identifier" && member.key.name === "process")
|
|
169
|
-
const body = method?.value.body
|
|
170
|
-
if (!body)
|
|
171
|
-
|
|
174
|
+
const method = node.body.body.find((member) => member.type === "MethodDefinition" && member.kind === "method" && member.key.type === "Identifier" && member.key.name === "process");
|
|
175
|
+
const body = method?.value.body;
|
|
176
|
+
if (!method || !body)
|
|
177
|
+
return;
|
|
178
|
+
let calls = 0;
|
|
172
179
|
walk(body, (child) => {
|
|
173
|
-
if (DECISION_NODES.has(child.type))
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
180
|
+
if (DECISION_NODES.has(child.type))
|
|
181
|
+
context.report({ node: child, messageId: "branch", data: { what: child.type === "LogicalExpression" ? child.operator : child.type.replace(/(Statement|Expression)$/, "").toLowerCase() } });
|
|
182
|
+
else if (child.type === "CallExpression")
|
|
183
|
+
calls += 1;
|
|
184
|
+
});
|
|
185
|
+
const [head] = body.body;
|
|
186
|
+
const only = body.body.length === 1 && head?.type === "ReturnStatement" ? head.argument : null;
|
|
177
187
|
if (only === null) {
|
|
178
|
-
context.report({ node: method.key, messageId: "body", data: { name } })
|
|
179
|
-
return
|
|
188
|
+
context.report({ node: method.key, messageId: "body", data: { name } });
|
|
189
|
+
return;
|
|
180
190
|
}
|
|
181
|
-
const call = only.type === "AwaitExpression" ? only.argument : only
|
|
182
|
-
const callee = call.type === "CallExpression" ? call.callee : null
|
|
183
|
-
const onService = callee?.type === "MemberExpression" && !callee.computed && callee.object.type === "MemberExpression" && !callee.object.computed && callee.object.object.type === "ThisExpression" && callee.object.property.type === "Identifier" && services.has(callee.object.property.name)
|
|
184
|
-
if (calls !== 1 || !onService)
|
|
185
|
-
|
|
191
|
+
const call = only.type === "AwaitExpression" ? only.argument : only;
|
|
192
|
+
const callee = call.type === "CallExpression" ? call.callee : null;
|
|
193
|
+
const onService = callee?.type === "MemberExpression" && !callee.computed && callee.object.type === "MemberExpression" && !callee.object.computed && callee.object.object.type === "ThisExpression" && callee.object.property.type === "Identifier" && services.has(callee.object.property.name);
|
|
194
|
+
if (calls !== 1 || !onService)
|
|
195
|
+
context.report({ node: method.key, messageId: "call", data: { name, count: String(calls) } });
|
|
196
|
+
});
|
|
186
197
|
},
|
|
187
|
-
}
|
|
188
|
-
|
|
198
|
+
};
|
|
189
199
|
// -- message-carries-params-only ---------------------------------------------------------------------------------
|
|
190
|
-
|
|
191
200
|
/** A message carries request context and nothing else. */
|
|
192
201
|
export const messageCarriesParamsOnly = {
|
|
193
202
|
meta: {
|
|
@@ -201,32 +210,36 @@ export const messageCarriesParamsOnly = {
|
|
|
201
210
|
},
|
|
202
211
|
},
|
|
203
212
|
create(context) {
|
|
204
|
-
if (messageKindOf(context) === null)
|
|
213
|
+
if (messageKindOf(context) === null)
|
|
214
|
+
return {};
|
|
205
215
|
return classVisitors((node) => {
|
|
206
|
-
const name = node.id?.name ?? "this message"
|
|
207
|
-
const members = node.body.body
|
|
208
|
-
const constructor = constructorOf(node)
|
|
216
|
+
const name = node.id?.name ?? "this message";
|
|
217
|
+
const members = node.body.body;
|
|
218
|
+
const constructor = constructorOf(node);
|
|
209
219
|
for (const member of members) {
|
|
210
|
-
if (member === constructor)
|
|
211
|
-
|
|
212
|
-
|
|
220
|
+
if (member === constructor)
|
|
221
|
+
continue;
|
|
222
|
+
const memberKey = memberKeyOf(member);
|
|
223
|
+
const key = memberKey?.type === "Identifier" ? memberKey.name : "a member";
|
|
224
|
+
context.report({ node: memberKey ?? member, messageId: "member", data: { name, member: key } });
|
|
213
225
|
}
|
|
214
226
|
if (!constructor) {
|
|
215
|
-
context.report({ node: node.id ?? node, messageId: "shape", data: { name } })
|
|
216
|
-
return
|
|
227
|
+
context.report({ node: node.id ?? node, messageId: "shape", data: { name } });
|
|
228
|
+
return;
|
|
217
229
|
}
|
|
218
|
-
const params = constructor.value.params
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
230
|
+
const params = constructor.value.params;
|
|
231
|
+
const [firstParam] = params;
|
|
232
|
+
const first = firstParam?.type === "TSParameterProperty" ? firstParam.parameter : firstParam;
|
|
233
|
+
if (params.length !== 1 || first?.type !== "Identifier" || first.name !== "params")
|
|
234
|
+
context.report({ node: constructor, messageId: "shape", data: { name } });
|
|
235
|
+
const statements = constructor.value.body?.body ?? [];
|
|
236
|
+
const onlySuper = statements.every((statement) => statement.type === "ExpressionStatement" && statement.expression.type === "CallExpression" && statement.expression.callee.type === "Super");
|
|
237
|
+
if (!onlySuper)
|
|
238
|
+
context.report({ node: constructor, messageId: "body", data: { name } });
|
|
239
|
+
});
|
|
225
240
|
},
|
|
226
|
-
}
|
|
227
|
-
|
|
241
|
+
};
|
|
228
242
|
// -- message-typed-result ----------------------------------------------------------------------------------------
|
|
229
|
-
|
|
230
243
|
/** A message states the type of its result: `Command<R>` / `Query<R>` from `@nestjs/cqrs`, one readonly `params`. */
|
|
231
244
|
export const messageTypedResult = {
|
|
232
245
|
meta: {
|
|
@@ -240,86 +253,99 @@ export const messageTypedResult = {
|
|
|
240
253
|
},
|
|
241
254
|
},
|
|
242
255
|
create(context) {
|
|
243
|
-
const kind = messageKindOf(context)
|
|
244
|
-
if (kind === null)
|
|
245
|
-
|
|
256
|
+
const kind = messageKindOf(context);
|
|
257
|
+
if (kind === null)
|
|
258
|
+
return {};
|
|
259
|
+
const base = kind === "command" ? "Command" : "Query";
|
|
246
260
|
return classVisitors((node) => {
|
|
247
|
-
const name = node.id?.name ?? "this message"
|
|
261
|
+
const name = node.id?.name ?? "this message";
|
|
248
262
|
if (!node.superClass || !isImportedFrom(context, node.superClass, CQRS_PACKAGE, base)) {
|
|
249
|
-
context.report({ node: node.id ?? node, messageId: "base", data: { name, base } })
|
|
250
|
-
}
|
|
251
|
-
|
|
263
|
+
context.report({ node: node.id ?? node, messageId: "base", data: { name, base } });
|
|
264
|
+
}
|
|
265
|
+
else if (node.superTypeArguments?.params.length !== 1) {
|
|
266
|
+
context.report({ node: node.superClass, messageId: "result", data: { name, base } });
|
|
252
267
|
}
|
|
253
|
-
const constructor = constructorOf(node)
|
|
254
|
-
if (!constructor)
|
|
255
|
-
|
|
268
|
+
const constructor = constructorOf(node);
|
|
269
|
+
if (!constructor)
|
|
270
|
+
return;
|
|
271
|
+
const param = constructor.value.params[0];
|
|
256
272
|
const valid = constructor.value.params.length === 1
|
|
257
|
-
&& param
|
|
273
|
+
&& param?.type === "TSParameterProperty"
|
|
258
274
|
&& param.readonly === true
|
|
259
275
|
&& (param.accessibility === undefined || param.accessibility === "public")
|
|
260
276
|
&& param.parameter.type === "Identifier"
|
|
261
277
|
&& param.parameter.name === "params"
|
|
262
|
-
&& param.parameter.optional !== true
|
|
263
|
-
if (!valid)
|
|
264
|
-
|
|
278
|
+
&& param.parameter.optional !== true;
|
|
279
|
+
if (!valid)
|
|
280
|
+
context.report({ node: param ?? constructor, messageId: "params", data: { name } });
|
|
281
|
+
});
|
|
265
282
|
},
|
|
266
|
-
}
|
|
267
|
-
|
|
283
|
+
};
|
|
268
284
|
// -- execute-params-shape ----------------------------------------------------------------------------------------
|
|
269
|
-
|
|
270
285
|
/** True for a class declaration decorated with `Entity` from `typeorm`. */
|
|
271
286
|
const isEntityClass = (checker, declaration) => {
|
|
272
|
-
if (!ts.isClassDeclaration(declaration))
|
|
287
|
+
if (!ts.isClassDeclaration(declaration))
|
|
288
|
+
return false;
|
|
273
289
|
return (ts.getDecorators(declaration) ?? []).some((decorator) => {
|
|
274
|
-
const call = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression
|
|
275
|
-
if (!ts.isIdentifier(call))
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
290
|
+
const call = ts.isCallExpression(decorator.expression) ? decorator.expression.expression : decorator.expression;
|
|
291
|
+
if (!ts.isIdentifier(call))
|
|
292
|
+
return false;
|
|
293
|
+
let symbol = checker.getSymbolAtLocation(call);
|
|
294
|
+
if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0)
|
|
295
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
296
|
+
return symbol?.getName() === "Entity" && (symbol.getDeclarations() ?? []).some((entity) => /\/node_modules\/typeorm\//.test(entity.getSourceFile().fileName.replace(/\\/g, "/")));
|
|
297
|
+
});
|
|
298
|
+
};
|
|
282
299
|
/** The name of an `@Entity` class reachable from `root` through properties, index signatures, unions and type arguments, or null. */
|
|
283
300
|
const reachableEntity = (context, root) => {
|
|
284
|
-
const { checker, program } = typed(context)
|
|
285
|
-
const seen = new Set()
|
|
286
|
-
const stack = [root]
|
|
287
|
-
let steps = 0
|
|
301
|
+
const { checker, program } = typed(context);
|
|
302
|
+
const seen = new Set();
|
|
303
|
+
const stack = [root];
|
|
304
|
+
let steps = 0;
|
|
288
305
|
while (stack.length > 0 && steps < 4000) {
|
|
289
|
-
steps += 1
|
|
290
|
-
const type = stack.pop()
|
|
291
|
-
if (
|
|
292
|
-
|
|
306
|
+
steps += 1;
|
|
307
|
+
const type = stack.pop();
|
|
308
|
+
if (type === undefined)
|
|
309
|
+
break;
|
|
310
|
+
if (seen.has(type))
|
|
311
|
+
continue;
|
|
312
|
+
seen.add(type);
|
|
293
313
|
if (type.isUnionOrIntersection()) {
|
|
294
|
-
stack.push(...type.types)
|
|
295
|
-
continue
|
|
314
|
+
stack.push(...type.types);
|
|
315
|
+
continue;
|
|
296
316
|
}
|
|
297
|
-
if ((type
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
const
|
|
301
|
-
|
|
317
|
+
if (isTypeReference(type))
|
|
318
|
+
stack.push(...checker.getTypeArguments(type));
|
|
319
|
+
const symbol = type.getSymbol() ?? type.aliasSymbol;
|
|
320
|
+
const declarations = symbol?.getDeclarations() ?? [];
|
|
321
|
+
const entity = declarations.find((declaration) => isEntityClass(checker, declaration));
|
|
322
|
+
if (entity && symbol)
|
|
323
|
+
return symbol.getName();
|
|
302
324
|
const own = declarations.some((declaration) => {
|
|
303
|
-
const file = declaration.getSourceFile()
|
|
304
|
-
return !program.isSourceFileFromExternalLibrary(file) && !program.isSourceFileDefaultLibrary(file)
|
|
305
|
-
})
|
|
306
|
-
if (!own)
|
|
307
|
-
|
|
308
|
-
for (const
|
|
325
|
+
const file = declaration.getSourceFile();
|
|
326
|
+
return !program.isSourceFileFromExternalLibrary(file) && !program.isSourceFileDefaultLibrary(file);
|
|
327
|
+
});
|
|
328
|
+
if (!own)
|
|
329
|
+
continue;
|
|
330
|
+
for (const property of checker.getPropertiesOfType(type))
|
|
331
|
+
stack.push(checker.getTypeOfSymbol(property));
|
|
332
|
+
for (const info of checker.getIndexInfosOfType(type))
|
|
333
|
+
stack.push(info.type);
|
|
309
334
|
}
|
|
310
|
-
return null
|
|
311
|
-
}
|
|
312
|
-
|
|
335
|
+
return null;
|
|
336
|
+
};
|
|
313
337
|
/** True when a type annotation is a reference to the `ExecuteParams` or `PublicExecuteParams` that `platform/cqrs` declares (the symbol the name resolves to, so a local alias or lookalike is not it). */
|
|
314
338
|
const namesPlatformParams = (context, hfs, annotation) => {
|
|
315
|
-
if (annotation.type !== "TSTypeReference")
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
if (
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
339
|
+
if (annotation.type !== "TSTypeReference")
|
|
340
|
+
return false;
|
|
341
|
+
const { checker, toTs } = typed(context);
|
|
342
|
+
let symbol = checker.getSymbolAtLocation(toTs(annotation.typeName));
|
|
343
|
+
if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0)
|
|
344
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
345
|
+
if (!symbol || (symbol.getName() !== "ExecuteParams" && symbol.getName() !== "PublicExecuteParams"))
|
|
346
|
+
return false;
|
|
347
|
+
return (symbol.getDeclarations() ?? []).some((declaration) => declaredByCapability(hfs, declaration.getSourceFile().fileName, "cqrs"));
|
|
348
|
+
};
|
|
323
349
|
/** A message's `params` is `ExecuteParams<X>` / `PublicExecuteParams<X>` of `platform/cqrs` and X holds no entity. */
|
|
324
350
|
export const executeParamsShape = {
|
|
325
351
|
meta: {
|
|
@@ -332,41 +358,47 @@ export const executeParamsShape = {
|
|
|
332
358
|
},
|
|
333
359
|
},
|
|
334
360
|
create(context) {
|
|
335
|
-
if (messageKindOf(context) === null)
|
|
336
|
-
|
|
361
|
+
if (messageKindOf(context) === null)
|
|
362
|
+
return {};
|
|
363
|
+
const hfs = hfsOf(context);
|
|
337
364
|
return classVisitors((node) => {
|
|
338
|
-
const name = node.id?.name ?? "this message"
|
|
339
|
-
const constructor = constructorOf(node)
|
|
340
|
-
const param = constructor?.value.params[0]
|
|
341
|
-
const annotation = (param?.type === "TSParameterProperty" ? param.parameter : param)?.typeAnnotation?.typeAnnotation
|
|
342
|
-
if (!annotation)
|
|
365
|
+
const name = node.id?.name ?? "this message";
|
|
366
|
+
const constructor = constructorOf(node);
|
|
367
|
+
const param = constructor?.value.params[0];
|
|
368
|
+
const annotation = (param?.type === "TSParameterProperty" ? param.parameter : param)?.typeAnnotation?.typeAnnotation;
|
|
369
|
+
if (!annotation)
|
|
370
|
+
return;
|
|
343
371
|
if (!namesPlatformParams(context, hfs, annotation)) {
|
|
344
|
-
context.report({ node: annotation, messageId: "shape", data: { name } })
|
|
345
|
-
return
|
|
372
|
+
context.report({ node: annotation, messageId: "shape", data: { name } });
|
|
373
|
+
return;
|
|
346
374
|
}
|
|
347
|
-
const argument =
|
|
348
|
-
if (!argument)
|
|
349
|
-
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
375
|
+
const argument = annotation.typeArguments?.params[0];
|
|
376
|
+
if (!argument)
|
|
377
|
+
return;
|
|
378
|
+
const { checker, toTs } = typed(context);
|
|
379
|
+
const tsArgument = toTs(argument);
|
|
380
|
+
// A type argument maps to a TS type node; the declared map also admits an identifier, read as typeOrigins reads one.
|
|
381
|
+
const entity = reachableEntity(context, ts.isTypeNode(tsArgument) ? checker.getTypeFromTypeNode(tsArgument) : checker.getTypeAtLocation(tsArgument));
|
|
382
|
+
if (entity !== null)
|
|
383
|
+
context.report({ node: argument, messageId: "entity", data: { name, entity } });
|
|
384
|
+
});
|
|
353
385
|
},
|
|
354
|
-
}
|
|
355
|
-
|
|
386
|
+
};
|
|
356
387
|
// -- no-use-case -------------------------------------------------------------------------------------------------
|
|
357
|
-
|
|
358
388
|
/** True when every method body of the class is `return this.<bus>.execute(...)` (at least one method). */
|
|
359
389
|
const isForwarder = (context, node) => {
|
|
360
|
-
const methods = node.body.body.filter((member) => member.type === "MethodDefinition" && member.kind === "method")
|
|
361
|
-
if (methods.length === 0)
|
|
390
|
+
const methods = node.body.body.filter((member) => member.type === "MethodDefinition" && member.kind === "method");
|
|
391
|
+
if (methods.length === 0)
|
|
392
|
+
return false;
|
|
362
393
|
return methods.every((method) => {
|
|
363
|
-
const statements = method.value.body?.body ?? []
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
394
|
+
const statements = method.value.body?.body ?? [];
|
|
395
|
+
const [statement] = statements;
|
|
396
|
+
if (statements.length !== 1 || statement?.type !== "ReturnStatement")
|
|
397
|
+
return false;
|
|
398
|
+
const call = statement.argument?.type === "AwaitExpression" ? statement.argument.argument : statement.argument;
|
|
399
|
+
return call?.type === "CallExpression" && call.callee.type === "MemberExpression" && !call.callee.computed && call.callee.property.type === "Identifier" && call.callee.property.name === "execute" && isBus(context, call.callee.object);
|
|
400
|
+
});
|
|
401
|
+
};
|
|
370
402
|
/** No use-case class and no forwarder service: the transport dispatches on the bus itself. */
|
|
371
403
|
export const noUseCase = {
|
|
372
404
|
meta: {
|
|
@@ -380,52 +412,54 @@ export const noUseCase = {
|
|
|
380
412
|
},
|
|
381
413
|
},
|
|
382
414
|
create(context) {
|
|
383
|
-
const hfs = hfsOf(context)
|
|
384
|
-
const filename = context.filename
|
|
385
|
-
const slot = hfs.slotOf(filename)
|
|
386
|
-
const visitors = {}
|
|
415
|
+
const hfs = hfsOf(context);
|
|
416
|
+
const filename = context.filename;
|
|
417
|
+
const slot = hfs.slotOf(filename);
|
|
418
|
+
const visitors = {};
|
|
387
419
|
if (APPLICATION_LAYER.has(slot)) {
|
|
388
|
-
const name = baseName(filename)
|
|
420
|
+
const name = baseName(filename);
|
|
389
421
|
visitors.Program = (node) => {
|
|
390
|
-
if (name.endsWith(".use-case.ts"))
|
|
391
|
-
|
|
422
|
+
if (name.endsWith(".use-case.ts"))
|
|
423
|
+
context.report({ node, loc: { line: 1, column: 0 }, messageId: "file", data: { file: name } });
|
|
424
|
+
};
|
|
392
425
|
visitors.ClassDeclaration = (node) => {
|
|
393
|
-
if (node.id && node.id.name.endsWith("UseCase"))
|
|
394
|
-
|
|
426
|
+
if (node.id && node.id.name.endsWith("UseCase"))
|
|
427
|
+
context.report({ node: node.id, messageId: "klass", data: { name: node.id.name } });
|
|
428
|
+
};
|
|
395
429
|
}
|
|
396
430
|
if ((slot === APPLICATION || isTransportSlot(slot)) && baseName(filename).endsWith(".service.ts")) {
|
|
397
|
-
const previous = visitors.ClassDeclaration
|
|
431
|
+
const previous = visitors.ClassDeclaration;
|
|
398
432
|
visitors.ClassDeclaration = (node) => {
|
|
399
|
-
previous?.(node)
|
|
400
|
-
if (isForwarder(context, node))
|
|
401
|
-
|
|
433
|
+
previous?.(node);
|
|
434
|
+
if (isForwarder(context, node))
|
|
435
|
+
context.report({ node: node.id ?? node, messageId: "forwarder", data: { name: node.id?.name ?? "this service" } });
|
|
436
|
+
};
|
|
402
437
|
}
|
|
403
|
-
return visitors
|
|
438
|
+
return visitors;
|
|
404
439
|
},
|
|
405
|
-
}
|
|
406
|
-
|
|
440
|
+
};
|
|
407
441
|
// -- no-event-bus ------------------------------------------------------------------------------------------------
|
|
408
|
-
|
|
409
442
|
/** The bus class a constructed or extended expression is declared as (by declaration origin), else null. `classes` is the `ruleParams.be.eventBus.classes` map: module specifier -> class names. */
|
|
410
443
|
const busClassOf = (context, node, classes) => {
|
|
411
|
-
const origin = typeOrigins(context, node).find((entry) => entry.module !== null && classes[entry.module]?.includes(entry.name))
|
|
412
|
-
return origin ? origin.name : null
|
|
413
|
-
}
|
|
414
|
-
|
|
444
|
+
const origin = typeOrigins(context, node).find((entry) => entry.module !== null && classes[entry.module]?.includes(entry.name));
|
|
445
|
+
return origin ? origin.name : null;
|
|
446
|
+
};
|
|
415
447
|
/** The methods that register a subscriber on a collection. */
|
|
416
|
-
const REGISTER_METHODS = new Set(["add", "push", "unshift", "set"])
|
|
417
|
-
|
|
448
|
+
const REGISTER_METHODS = new Set(["add", "push", "unshift", "set"]);
|
|
418
449
|
/** True for a type that is a function: it has a call signature and no construct signature. */
|
|
419
|
-
const isFunctionType = (type) => type.getCallSignatures().length > 0 && type.getConstructSignatures().length === 0
|
|
420
|
-
|
|
450
|
+
const isFunctionType = (type) => type.getCallSignatures().length > 0 && type.getConstructSignatures().length === 0;
|
|
421
451
|
/** True when a collection type (Set, Array, Map) holds functions. */
|
|
422
452
|
const holdsFunctions = (checker, type) => {
|
|
423
|
-
const symbolName = type.getSymbol()?.getName()
|
|
424
|
-
if (!["Set", "Array", "ReadonlyArray", "Map", "WeakSet"].includes(symbolName ?? ""))
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
453
|
+
const symbolName = type.getSymbol()?.getName();
|
|
454
|
+
if (!["Set", "Array", "ReadonlyArray", "Map", "WeakSet"].includes(symbolName ?? ""))
|
|
455
|
+
return false;
|
|
456
|
+
// A collection type that is not a generic reference has no type arguments: the checker answers [] for it.
|
|
457
|
+
if (!isTypeReference(type))
|
|
458
|
+
return false;
|
|
459
|
+
const args = checker.getTypeArguments(type);
|
|
460
|
+
const last = args[args.length - 1];
|
|
461
|
+
return args.length > 0 && last !== undefined && isFunctionType(last);
|
|
462
|
+
};
|
|
429
463
|
/** No in-process events: a side effect that must happen anyway is an event published through the bus. */
|
|
430
464
|
export const noEventBus = {
|
|
431
465
|
meta: {
|
|
@@ -439,67 +473,93 @@ export const noEventBus = {
|
|
|
439
473
|
},
|
|
440
474
|
},
|
|
441
475
|
create(context) {
|
|
442
|
-
const report = (node, name) => context.report({ node, messageId: "event", data: { name } })
|
|
443
|
-
const eventBus = hfsOf(context).ruleParams.eventBus
|
|
444
|
-
const eventNames = new Set(eventBus.imports[CQRS_PACKAGE] ?? [])
|
|
445
|
-
const { checker, toTs } = typed(context)
|
|
476
|
+
const report = (node, name) => context.report({ node, messageId: "event", data: { name } });
|
|
477
|
+
const eventBus = hfsOf(context).ruleParams.eventBus;
|
|
478
|
+
const eventNames = new Set(eventBus.imports[CQRS_PACKAGE] ?? []);
|
|
479
|
+
const { checker, toTs } = typed(context);
|
|
446
480
|
/** Collects, per class, the functions-collection properties and the registrations made on them. */
|
|
447
481
|
const checkClass = (classNode) => {
|
|
448
|
-
const collections = new Map()
|
|
482
|
+
const collections = new Map();
|
|
449
483
|
for (const member of classNode.body.body) {
|
|
450
|
-
if (member.type !== "PropertyDefinition" || member.computed || member.key.type !== "Identifier")
|
|
451
|
-
|
|
452
|
-
|
|
484
|
+
if (member.type !== "PropertyDefinition" || member.computed || member.key.type !== "Identifier")
|
|
485
|
+
continue;
|
|
486
|
+
const tsKey = toTs(member.key);
|
|
487
|
+
if (tsKey && holdsFunctions(checker, checker.getTypeAtLocation(tsKey)))
|
|
488
|
+
collections.set(member.key.name, member.key);
|
|
453
489
|
}
|
|
454
|
-
if (collections.size === 0)
|
|
455
|
-
|
|
490
|
+
if (collections.size === 0)
|
|
491
|
+
return;
|
|
492
|
+
const registered = new Set();
|
|
456
493
|
const visit = (node) => {
|
|
457
|
-
if (!node
|
|
494
|
+
if (!isNode(node))
|
|
495
|
+
return;
|
|
458
496
|
if (node.type === "CallExpression" && node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && REGISTER_METHODS.has(node.callee.property.name)) {
|
|
459
|
-
const holder = node.callee.object
|
|
497
|
+
const holder = node.callee.object;
|
|
460
498
|
if (holder.type === "MemberExpression" && holder.object.type === "ThisExpression" && !holder.computed && holder.property.type === "Identifier" && collections.has(holder.property.name)) {
|
|
461
|
-
const callback = node.arguments[node.arguments.length - 1]
|
|
462
|
-
const tsCallback = callback ? toTs(callback) : null
|
|
463
|
-
if (tsCallback && isFunctionType(checker.getTypeAtLocation(tsCallback)) && callback
|
|
499
|
+
const callback = node.arguments[node.arguments.length - 1];
|
|
500
|
+
const tsCallback = callback ? toTs(callback) : null;
|
|
501
|
+
if (tsCallback && isFunctionType(checker.getTypeAtLocation(tsCallback)) && callback?.type === "Identifier")
|
|
502
|
+
registered.add(holder.property.name);
|
|
464
503
|
}
|
|
465
504
|
}
|
|
466
505
|
for (const key of Object.keys(node)) {
|
|
467
|
-
if (key === "parent")
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
506
|
+
if (key === "parent")
|
|
507
|
+
continue;
|
|
508
|
+
const value = Reflect.get(node, key);
|
|
509
|
+
if (Array.isArray(value)) {
|
|
510
|
+
const children = value;
|
|
511
|
+
for (const child of children)
|
|
512
|
+
visit(child);
|
|
513
|
+
}
|
|
514
|
+
else if (isNode(value))
|
|
515
|
+
visit(value);
|
|
471
516
|
}
|
|
517
|
+
};
|
|
518
|
+
visit(classNode.body);
|
|
519
|
+
for (const name of registered) {
|
|
520
|
+
const key = collections.get(name);
|
|
521
|
+
if (key)
|
|
522
|
+
context.report({ node: key, messageId: "registry", data: { name } });
|
|
472
523
|
}
|
|
473
|
-
|
|
474
|
-
for (const name of registered) context.report({ node: collections.get(name), messageId: "registry", data: { name } })
|
|
475
|
-
}
|
|
524
|
+
};
|
|
476
525
|
return {
|
|
477
526
|
ClassDeclaration(node) {
|
|
478
|
-
checkClass(node)
|
|
527
|
+
checkClass(node);
|
|
479
528
|
if (node.superClass) {
|
|
480
|
-
const name = busClassOf(context, node.superClass, eventBus.classes)
|
|
481
|
-
if (name)
|
|
529
|
+
const name = busClassOf(context, node.superClass, eventBus.classes);
|
|
530
|
+
if (name)
|
|
531
|
+
context.report({ node: node.superClass, messageId: "bus", data: { name } });
|
|
482
532
|
}
|
|
483
533
|
},
|
|
484
|
-
ClassExpression(node) { checkClass(node) },
|
|
534
|
+
ClassExpression(node) { checkClass(node); },
|
|
485
535
|
NewExpression(node) {
|
|
486
|
-
const name = busClassOf(context, node.callee, eventBus.classes)
|
|
487
|
-
if (name)
|
|
536
|
+
const name = busClassOf(context, node.callee, eventBus.classes);
|
|
537
|
+
if (name)
|
|
538
|
+
context.report({ node, messageId: "bus", data: { name } });
|
|
488
539
|
},
|
|
489
540
|
...moduleReferences(({ node, source, value, names }) => {
|
|
490
|
-
const members = eventBus.imports[value]
|
|
491
|
-
if (members === undefined)
|
|
492
|
-
|
|
493
|
-
|
|
541
|
+
const members = eventBus.imports[value];
|
|
542
|
+
if (members === undefined)
|
|
543
|
+
return;
|
|
544
|
+
if (members.length === 0)
|
|
545
|
+
report(source, value);
|
|
546
|
+
else
|
|
547
|
+
for (const name of names)
|
|
548
|
+
if (name !== null && members.includes(name))
|
|
549
|
+
report(node, name);
|
|
494
550
|
}),
|
|
495
551
|
MemberExpression(node) {
|
|
496
|
-
if (node.computed || node.property.type !== "Identifier" || !eventNames.has(node.property.name))
|
|
497
|
-
|
|
552
|
+
if (node.computed || node.property.type !== "Identifier" || !eventNames.has(node.property.name))
|
|
553
|
+
return;
|
|
554
|
+
if (node.object.type !== "Identifier")
|
|
555
|
+
return;
|
|
556
|
+
const binding = importOf(context, node.object);
|
|
557
|
+
if (binding?.source === CQRS_PACKAGE && binding.imported === "*")
|
|
558
|
+
report(node, node.property.name);
|
|
498
559
|
},
|
|
499
|
-
}
|
|
560
|
+
};
|
|
500
561
|
},
|
|
501
|
-
}
|
|
502
|
-
|
|
562
|
+
};
|
|
503
563
|
/** The rules this law contributes to the plugin. */
|
|
504
564
|
export const rules = {
|
|
505
565
|
"handler-overrides-process": handlerOverridesProcess,
|
|
@@ -509,8 +569,7 @@ export const rules = {
|
|
|
509
569
|
"execute-params-shape": executeParamsShape,
|
|
510
570
|
"no-use-case": noUseCase,
|
|
511
571
|
"no-event-bus": noEventBus,
|
|
512
|
-
}
|
|
513
|
-
|
|
572
|
+
};
|
|
514
573
|
/** Every rule of this law at `error`. */
|
|
515
574
|
export const recommended = {
|
|
516
575
|
"starci-be/handler-overrides-process": "error",
|
|
@@ -520,4 +579,4 @@ export const recommended = {
|
|
|
520
579
|
"starci-be/execute-params-shape": "error",
|
|
521
580
|
"starci-be/no-use-case": "error",
|
|
522
581
|
"starci-be/no-event-bus": "error",
|
|
523
|
-
}
|
|
582
|
+
};
|