@starci/eslint-canon-be 3.4.57 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +11 -2
- package/dist/ambient-id.d.ts +9 -0
- package/{ambient-id.mjs → dist/ambient-id.js} +43 -39
- package/dist/app-composition.d.ts +21 -0
- package/{app-composition.mjs → dist/app-composition.js} +396 -300
- package/dist/async-discipline.d.ts +9 -0
- package/dist/async-discipline.js +77 -0
- package/dist/authorization.d.ts +12 -0
- package/{authorization.mjs → dist/authorization.js} +53 -59
- package/dist/boundary-schema.d.ts +15 -0
- package/{boundary-schema.mjs → dist/boundary-schema.js} +120 -105
- package/dist/builders.d.ts +21 -0
- package/{builders.mjs → dist/builders.js} +162 -146
- package/dist/cli.d.ts +15 -0
- package/{cli.mjs → dist/cli.js} +97 -82
- package/dist/comments.d.ts +52 -0
- package/dist/comments.js +412 -0
- package/dist/config-owner.d.ts +24 -0
- package/{config-owner.mjs → dist/config-owner.js} +256 -219
- package/dist/connections.d.ts +12 -0
- package/{connections.mjs → dist/connections.js} +138 -140
- package/dist/cookies.d.ts +9 -0
- package/{cookies.mjs → dist/cookies.js} +58 -58
- package/dist/cqrs.d.ts +27 -0
- package/{cqrs.mjs → dist/cqrs.js} +341 -282
- package/dist/data-access.d.ts +50 -0
- package/{data-access.mjs → dist/data-access.js} +238 -243
- package/dist/default-deny.d.ts +15 -0
- package/{default-deny.mjs → dist/default-deny.js} +103 -88
- package/dist/e2e-flow.d.ts +25 -0
- package/{e2e-flow.mjs → dist/e2e-flow.js} +113 -112
- package/dist/edge-hardening.d.ts +10 -0
- package/dist/edge-hardening.js +173 -0
- package/dist/error-handling.d.ts +21 -0
- package/{error-handling.mjs → dist/error-handling.js} +289 -239
- package/dist/event-bus.d.ts +14 -0
- package/{event-bus.mjs → dist/event-bus.js} +146 -129
- package/dist/feature-thin.d.ts +12 -0
- package/{feature-thin.mjs → dist/feature-thin.js} +208 -191
- package/dist/function-budget.d.ts +7 -0
- package/{function-budget.mjs → dist/function-budget.js} +24 -27
- package/dist/health-ready.d.ts +10 -0
- package/dist/health-ready.js +703 -0
- package/dist/idempotency.d.ts +10 -0
- package/{idempotency.mjs → dist/idempotency.js} +296 -226
- package/dist/index.d.ts +54 -0
- package/{index.mjs → dist/index.js} +91 -109
- package/dist/infra-owner.d.ts +18 -0
- package/{infra-owner.mjs → dist/infra-owner.js} +58 -51
- package/dist/injection.d.ts +30 -0
- package/{injection.mjs → dist/injection.js} +397 -344
- package/dist/input-bounds.d.ts +15 -0
- package/{input-bounds.mjs → dist/input-bounds.js} +143 -132
- package/dist/jobs.d.ts +17 -0
- package/{jobs.mjs → dist/jobs.js} +116 -99
- package/dist/jwt-alg.d.ts +6 -0
- package/dist/jwt-alg.js +49 -0
- package/dist/lib/app-roles.d.ts +25 -0
- package/{lib/app-roles.mjs → dist/lib/app-roles.js} +15 -17
- package/dist/lib/ast.d.ts +40 -0
- package/dist/lib/ast.js +100 -0
- package/dist/lib/client-idempotency.d.ts +3 -0
- package/{lib/client-idempotency.mjs → dist/lib/client-idempotency.js} +316 -268
- package/dist/lib/config.d.ts +36 -0
- package/{lib/config.mjs → dist/lib/config.js} +24 -27
- package/dist/lib/db-writes.d.ts +29 -0
- package/{lib/db-writes.mjs → dist/lib/db-writes.js} +27 -27
- package/dist/lib/declared.d.ts +77 -0
- package/{lib/declared.mjs → dist/lib/declared.js} +83 -78
- package/dist/lib/doors.d.ts +50 -0
- package/{lib/doors.mjs → dist/lib/doors.js} +45 -50
- package/dist/lib/edition.d.ts +25 -0
- package/{lib/edition.mjs → dist/lib/edition.js} +15 -20
- package/dist/lib/entity-writes.d.ts +7 -0
- package/{lib/entity-writes.mjs → dist/lib/entity-writes.js} +31 -22
- package/dist/lib/hfs.d.ts +34 -0
- package/{lib/hfs.mjs → dist/lib/hfs.js} +31 -29
- package/dist/lib/import-source.d.ts +63 -0
- package/dist/lib/import-source.js +134 -0
- package/dist/lib/path.d.ts +8 -0
- package/{lib/path.mjs → dist/lib/path.js} +4 -7
- package/dist/lib/persistence.d.ts +115 -0
- package/{lib/persistence.mjs → dist/lib/persistence.js} +55 -66
- package/dist/lib/ports.d.ts +70 -0
- package/{lib/ports.mjs → dist/lib/ports.js} +62 -66
- package/dist/lib/rule.d.ts +31 -0
- package/dist/lib/rule.js +2 -0
- package/dist/lib/runtime-root.d.ts +4 -0
- package/dist/lib/runtime-root.js +10 -0
- package/dist/lib/schema-values.d.ts +13 -0
- package/{lib/schema-values.mjs → dist/lib/schema-values.js} +67 -59
- package/dist/lib/specifier.d.ts +12 -0
- package/dist/lib/specifier.js +29 -0
- package/dist/lib/subject-values.d.ts +4 -0
- package/dist/lib/subject-values.js +61 -0
- package/dist/lib/transactions.d.ts +18 -0
- package/{lib/transactions.mjs → dist/lib/transactions.js} +17 -15
- package/dist/lib/transport-slots.d.ts +17 -0
- package/{lib/transport-slots.mjs → dist/lib/transport-slots.js} +2 -4
- package/dist/lib/types.d.ts +96 -0
- package/dist/lib/types.js +150 -0
- package/dist/lib/unit-spec.d.ts +48 -0
- package/{lib/unit-spec.mjs → dist/lib/unit-spec.js} +35 -44
- package/dist/lib/webhook-ack.d.ts +4 -0
- package/{lib/webhook-ack.mjs → dist/lib/webhook-ack.js} +78 -62
- package/dist/lib/webhook-handoff-inbox.d.ts +3 -0
- package/{lib/webhook-handoff-inbox.mjs → dist/lib/webhook-handoff-inbox.js} +195 -167
- package/dist/lib/webhook-handoff-intake.d.ts +4 -0
- package/{lib/webhook-handoff-intake.mjs → dist/lib/webhook-handoff-intake.js} +125 -102
- package/dist/lib/webhook-handoff.d.ts +15 -0
- package/{lib/webhook-handoff.mjs → dist/lib/webhook-handoff.js} +107 -100
- package/dist/lib/webhook-verifiers.d.ts +20 -0
- package/{lib/webhook-verifiers.mjs → dist/lib/webhook-verifiers.js} +108 -104
- package/dist/lib/why.d.ts +9 -0
- package/{lib/why.mjs → dist/lib/why.js} +6 -6
- package/dist/log-safety.d.ts +20 -0
- package/dist/log-safety.js +308 -0
- package/dist/module-layering.d.ts +42 -0
- package/dist/module-layering.js +257 -0
- package/dist/module-shape.d.ts +47 -0
- package/{module-shape.mjs → dist/module-shape.js} +278 -236
- package/dist/naming.d.ts +49 -0
- package/dist/naming.js +246 -0
- package/dist/observability.d.ts +25 -0
- package/dist/observability.js +245 -0
- package/dist/operations.d.ts +23 -0
- package/{operations.mjs → dist/operations.js} +410 -332
- package/dist/project-graph.d.ts +9 -0
- package/{project-graph.mjs → dist/project-graph.js} +8 -13
- package/dist/projections.d.ts +11 -0
- package/{projections.mjs → dist/projections.js} +60 -57
- package/dist/query-safety.d.ts +21 -0
- package/{query-safety.mjs → dist/query-safety.js} +92 -108
- package/dist/realtime.d.ts +15 -0
- package/{realtime.mjs → dist/realtime.js} +108 -106
- package/dist/resilience.d.ts +15 -0
- package/dist/resilience.js +319 -0
- package/dist/schema-authority.d.ts +15 -0
- package/{schema-authority.mjs → dist/schema-authority.js} +256 -220
- package/dist/service-deps.d.ts +17 -0
- package/{service-deps.mjs → dist/service-deps.js} +133 -130
- package/dist/service-isolation.d.ts +8 -0
- package/dist/service-isolation.js +76 -0
- package/dist/size-budget.d.ts +9 -0
- package/{size-budget.mjs → dist/size-budget.js} +27 -27
- package/dist/sonar-parity.d.ts +5 -0
- package/{sonar-parity.mjs → dist/sonar-parity.js} +7 -7
- package/dist/spec-no-skip.d.ts +9 -0
- package/dist/spec-no-skip.js +81 -0
- package/dist/spec-quality.d.ts +10 -0
- package/{spec-quality.mjs → dist/spec-quality.js} +103 -87
- package/dist/suppression.d.ts +26 -0
- package/{suppression.mjs → dist/suppression.js} +30 -45
- package/dist/temporal.d.ts +9 -0
- package/{temporal.mjs → dist/temporal.js} +75 -65
- package/dist/test-world.d.ts +28 -0
- package/{test-world.mjs → dist/test-world.js} +102 -83
- package/dist/testing.d.ts +51 -0
- package/dist/testing.js +368 -0
- package/dist/third-party-owner.d.ts +8 -0
- package/{third-party-owner.mjs → dist/third-party-owner.js} +105 -91
- package/dist/transport.d.ts +24 -0
- package/{transport.mjs → dist/transport.js} +279 -240
- package/dist/type-safety.d.ts +25 -0
- package/dist/type-safety.js +297 -0
- package/dist/unit-spec.d.ts +30 -0
- package/{unit-spec.mjs → dist/unit-spec.js} +473 -399
- package/dist/uploads.d.ts +9 -0
- package/{uploads.mjs → dist/uploads.js} +62 -54
- package/dist/user-copy.d.ts +9 -0
- package/{user-copy.mjs → dist/user-copy.js} +76 -67
- package/dist/webhooks.d.ts +16 -0
- package/{webhooks.mjs → dist/webhooks.js} +243 -231
- package/package.json +17 -8
- package/runtime/knowledge/hfs/canon-pins.yaml +22 -14
- package/runtime/knowledge/hfs/rules.yaml +23 -0
- package/runtime/scripts/lib/package-at.mjs +5 -5
- package/async-discipline.mjs +0 -83
- package/comments.mjs +0 -416
- package/edge-hardening.mjs +0 -139
- package/health-ready.mjs +0 -539
- package/jwt-alg.mjs +0 -45
- package/lib/ast.mjs +0 -88
- package/lib/import-source.mjs +0 -104
- package/lib/specifier.mjs +0 -27
- package/lib/subject-values.mjs +0 -55
- package/lib/types.mjs +0 -133
- package/log-safety.mjs +0 -303
- package/module-layering.mjs +0 -270
- package/naming.mjs +0 -251
- package/observability.mjs +0 -241
- package/resilience.mjs +0 -304
- package/service-isolation.mjs +0 -75
- package/spec-no-skip.mjs +0 -85
- package/testing.mjs +0 -384
- package/type-safety.mjs +0 -323
package/dist/testing.js
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rules that hold `knowledge/patterns/be/test.yaml` (catalog R47 `BE_TEST_TOPOLOGY` and R48 `BE_SPEC_QUALITY`).
|
|
3
|
+
*
|
|
4
|
+
* MOST OF THAT LAW IS NOT MACHINE-CHECKABLE, and pretending otherwise would be worse than checking nothing. No rule
|
|
5
|
+
* can tell whether a file represents a business flow, whether the unhappy path it covers drags a critical flow
|
|
6
|
+
* behind it, or whether the decision branches are covered - those are read by a person. What a rule CAN see is a
|
|
7
|
+
* shape that is wrong on its face regardless of intent:
|
|
8
|
+
*
|
|
9
|
+
* - a spec whose every assertion is about a call rather than a result (`no-call-only-spec`);
|
|
10
|
+
* - a unit spec of anything but a service, a service with no `<name>.service.spec.ts` beside it, a service spec with no
|
|
11
|
+
* service beside it, and a file of a kind the convention bans (`.test.ts`, `.int-spec.ts`, `.harness-spec.ts`)
|
|
12
|
+
* (`unit-test-colocated`);
|
|
13
|
+
* - an e2e that never reads persisted state back, and an e2e that reaches a model provider
|
|
14
|
+
* (`e2e-asserts-persisted-state`, `no-model-call-in-e2e`);
|
|
15
|
+
* - an e2e filename that names an API shape instead of a business flow (`no-api-shaped-e2e-filename`);
|
|
16
|
+
* - a model stub that returns a bare marker instead of a payload the production parser can parse
|
|
17
|
+
* (`no-marker-model-stub`).
|
|
18
|
+
*
|
|
19
|
+
* Every path question is asked of the slot manifest (`hfsOf(context)`): a rule never tests a path with a regular
|
|
20
|
+
* expression. A file name is a role vocabulary of the slots (`*.spec.ts`, `*.e2e-spec.ts`, `*.handler.ts`) and is read
|
|
21
|
+
* from the basename. A state read is recognised by the TYPE of the receiver (`EntityManager`, `DataSource`,
|
|
22
|
+
* `QueryRunner` of `typeorm`), never by its variable name.
|
|
23
|
+
*/
|
|
24
|
+
import { statSync } from "node:fs";
|
|
25
|
+
import { basename, dirname, join } from "node:path";
|
|
26
|
+
import { hfsOf, inTestWorld } from "./lib/hfs.js";
|
|
27
|
+
import { isPackageType, typeOrigins } from "./lib/types.js";
|
|
28
|
+
import { doorOfSpec, isUnitSpecFile, unitRoleOfSpec, unitRoleOfSubject } from "./lib/unit-spec.js";
|
|
29
|
+
/** The file name of a linted path, in forward-slash form. */
|
|
30
|
+
const baseOf = (filename) => basename(String(filename || "").replace(/\\/g, "/"));
|
|
31
|
+
/** Test kind `spec`: a unit spec, `<name>.service.spec.ts` beside its service. */
|
|
32
|
+
const isUnitSpec = (filename) => /\.spec\.ts$/.test(baseOf(filename));
|
|
33
|
+
/** Test kind `e2e-spec`: `*.e2e-spec.ts` under `src/tests/e2e/` (`integration-spec` and `contract-spec` are the other two kinds of the tests slots). */
|
|
34
|
+
const isE2eSpec = (filename) => /\.e2e-spec\.ts$/.test(baseOf(filename));
|
|
35
|
+
/**
|
|
36
|
+
* Matchers that assert a CALL happened rather than what came out of it.
|
|
37
|
+
*
|
|
38
|
+
* Each is legitimate as a second assertion - "the mail went out" is a real observable effect. The
|
|
39
|
+
* rule fires only when a whole file has nothing else.
|
|
40
|
+
*/
|
|
41
|
+
const CALL_MATCHERS = new Set([
|
|
42
|
+
"toHaveBeenCalled",
|
|
43
|
+
"toHaveBeenCalledWith",
|
|
44
|
+
"toHaveBeenCalledTimes",
|
|
45
|
+
"toHaveBeenLastCalledWith",
|
|
46
|
+
"toHaveBeenNthCalledWith",
|
|
47
|
+
"toBeCalled",
|
|
48
|
+
"toBeCalledWith",
|
|
49
|
+
"toBeCalledTimes",
|
|
50
|
+
"toHaveReturned",
|
|
51
|
+
]);
|
|
52
|
+
/**
|
|
53
|
+
* The matcher a given `expect(...)` call ends in.
|
|
54
|
+
*
|
|
55
|
+
* Climbs the member chain so modifiers pass through: `expect(x).not.toHaveBeenCalled()` answers
|
|
56
|
+
* `toHaveBeenCalled`, and `expect(p).resolves.toBe(1)` answers `toBe`.
|
|
57
|
+
*/
|
|
58
|
+
const matcherOf = (expectCall) => {
|
|
59
|
+
let cursor = expectCall;
|
|
60
|
+
let last = null;
|
|
61
|
+
while (cursor.parent && cursor.parent.type === "MemberExpression" && cursor.parent.object === cursor) {
|
|
62
|
+
const property = cursor.parent.property;
|
|
63
|
+
last = property && ("name" in property ? property.name : undefined);
|
|
64
|
+
cursor = cursor.parent;
|
|
65
|
+
}
|
|
66
|
+
return last;
|
|
67
|
+
};
|
|
68
|
+
// -- TESTING-6 -------------------------------------------------------------------------------------
|
|
69
|
+
/** A spec whose every assertion is about a call restates the source instead of testing it. */
|
|
70
|
+
export const noCallOnlySpec = {
|
|
71
|
+
meta: {
|
|
72
|
+
type: "problem",
|
|
73
|
+
docs: { description: "A unit spec asserts a result or a state change, not only that a call happened." },
|
|
74
|
+
schema: [],
|
|
75
|
+
messages: {
|
|
76
|
+
callOnly: "Every assertion in this spec is `{{matchers}}` - it restates the handler's own source. Rename a collaborator's method and this file goes red; change the business rule to a wrong value and it stays green. Assert what came back, or what changed. A call assertion is legitimate as a SECOND assertion, where the call itself is the observable effect.",
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
create(context) {
|
|
80
|
+
if (!isUnitSpec(context.filename || context.getFilename()))
|
|
81
|
+
return {};
|
|
82
|
+
let assertions = 0;
|
|
83
|
+
let callAssertions = 0;
|
|
84
|
+
const seen = new Set();
|
|
85
|
+
return {
|
|
86
|
+
CallExpression(node) {
|
|
87
|
+
if (!node.callee || node.callee.type !== "Identifier" || node.callee.name !== "expect")
|
|
88
|
+
return;
|
|
89
|
+
const matcher = matcherOf(node);
|
|
90
|
+
if (matcher === null)
|
|
91
|
+
return;
|
|
92
|
+
assertions += 1;
|
|
93
|
+
if (matcher === undefined || !CALL_MATCHERS.has(matcher))
|
|
94
|
+
return;
|
|
95
|
+
callAssertions += 1;
|
|
96
|
+
seen.add(matcher);
|
|
97
|
+
},
|
|
98
|
+
"Program:exit"(node) {
|
|
99
|
+
// the file asserts nothing at all, or asserts something other than a call somewhere
|
|
100
|
+
if (assertions === 0 || callAssertions !== assertions)
|
|
101
|
+
return;
|
|
102
|
+
context.report({ node, messageId: "callOnly", data: { matchers: [...seen].join(", ") } });
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
// -- R47 unit spec topology ------------------------------------------------------------------------
|
|
108
|
+
/** Whether a file exists (a spec is beside its subject on disk, so this is the one question the disk answers). */
|
|
109
|
+
const exists = (path) => {
|
|
110
|
+
try {
|
|
111
|
+
return statSync(path).isFile();
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
/** Kinds of test file the convention bans: `.test.ts`, `*.int-spec.ts`, `*.harness-spec.ts` (any spec kind other than `spec`, `e2e-spec`, `integration-spec` and `contract-spec`). */
|
|
118
|
+
const BANNED_TEST_FILE = /(?:\.test|\.int-spec|\.harness-spec)\.[cm]?[jt]sx?$/;
|
|
119
|
+
/**
|
|
120
|
+
* Unit specs are for services only: every `<name>.service.ts` has its `<name>.service.spec.ts` beside it, every unit spec is
|
|
121
|
+
* one of those, and there are no kinds of test file beyond `spec`, `e2e-spec`, `integration-spec` and `contract-spec`.
|
|
122
|
+
*/
|
|
123
|
+
export const unitTestColocated = {
|
|
124
|
+
meta: {
|
|
125
|
+
type: "problem",
|
|
126
|
+
docs: { description: "Only the unit-tested roles (`ruleParams.be.unitRoles`: a `<name>.service.ts`, a cli command `<name>.cli.ts` of `src/features/cli`) own a REQUIRED unit spec, each beside it as `<name>.<role>.spec.ts`; any other logic role (`ruleParams.be.logicRoles`: policy, client, guard, mapper, ...) inside a slot whose coverage is required may have one; any other unit spec is a finding, and there are only the spec kinds `spec`, `e2e-spec`, `integration-spec` and `contract-spec`." },
|
|
127
|
+
schema: [],
|
|
128
|
+
messages: {
|
|
129
|
+
suffix: "`{{name}}` is a banned kind of test file. A test is a `<name>.service.spec.ts` unit beside its service, or a `*.e2e-spec.ts`, `*.integration-spec.ts` or `*.contract-spec.ts` under its `src/tests/` folder; there is no `.test.ts`, `.int-spec.ts` or `.harness-spec.ts`.",
|
|
130
|
+
notService: "`{{name}}` is a unit spec of something that carries no unit-tested logic here. A unit spec sits beside a `<name>.<role>.ts` of a spec-required role (a service, a cli command of `src/features/cli`) or of a logic role (`ruleParams.be.logicRoles`: policy, client, guard, mapper, ...) inside the logic of `src/modules/**`, where every file owes 100 per file; a feature file (handler, resolver, controller, consumer, processor, step, ...) is thin (BE_FEATURE_THIN) and is proven by the integration and e2e flows, and an entity, module or composition is declaration. Delete this spec and move any business rule it checks into a service, policy or mapper of `src/modules/**`.",
|
|
131
|
+
orphan: "`{{name}}` has nothing beside it to test. A unit spec is `<name>.<role>.spec.ts` next to the `<name>.<role>.ts` it tests (a service, or a cli command of `src/features/cli`; the roles are `ruleParams.be.unitRoles`); move it beside its subject, or rename it after the subject it tests.",
|
|
132
|
+
missing: "This unit-tested subject has no spec. Add `{{spec}}` beside it: it builds the subject with `Test.createTestingModule`, provides only its constructor dependencies as typed doubles and asserts results or state.",
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
create(context) {
|
|
136
|
+
const filename = context.filename || context.getFilename();
|
|
137
|
+
const name = baseOf(filename);
|
|
138
|
+
const hfs = hfsOf(context);
|
|
139
|
+
if (/\.d\.[cm]?ts$/.test(name))
|
|
140
|
+
return {};
|
|
141
|
+
if (BANNED_TEST_FILE.test(name)) {
|
|
142
|
+
return { Program(node) { context.report({ node, messageId: "suffix", data: { name } }); } };
|
|
143
|
+
}
|
|
144
|
+
if (isUnitSpecFile(hfs, filename)) {
|
|
145
|
+
const door = doorOfSpec(hfs, filename);
|
|
146
|
+
if (door !== null)
|
|
147
|
+
return exists(join(dirname(filename), door)) ? {} : { Program(node) { context.report({ node, messageId: "orphan", data: { name } }); } };
|
|
148
|
+
const role = unitRoleOfSpec(hfs, filename);
|
|
149
|
+
if (!role)
|
|
150
|
+
return { Program(node) { context.report({ node, messageId: "notService", data: { name } }); } };
|
|
151
|
+
const subject = `${name.slice(0, -`.${role.spec}.ts`.length)}.${role.role}.ts`;
|
|
152
|
+
if (exists(join(dirname(filename), subject)))
|
|
153
|
+
return {};
|
|
154
|
+
return { Program(node) { context.report({ node, messageId: "orphan", data: { name } }); } };
|
|
155
|
+
}
|
|
156
|
+
// a unit-tested subject outside the test tree (ruleParams.be.unitRoles): its spec sits beside it
|
|
157
|
+
const role = unitRoleOfSubject(hfs, filename);
|
|
158
|
+
if (!role)
|
|
159
|
+
return {};
|
|
160
|
+
const spec = `${name.slice(0, -`.${role.role}.ts`.length)}.${role.spec}.ts`;
|
|
161
|
+
if (exists(join(dirname(filename), spec)))
|
|
162
|
+
return {};
|
|
163
|
+
return { Program(node) { context.report({ node, messageId: "missing", data: { spec } }); } };
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
// -- TESTING-2 -------------------------------------------------------------------------------------
|
|
167
|
+
/** The typeorm types a flow reads persisted state through. */
|
|
168
|
+
const STATE_TYPES = ["EntityManager", "DataSource", "QueryRunner"];
|
|
169
|
+
/** Whether an expression is a receiver of persisted state, by the TYPE it has and the package that declares it. */
|
|
170
|
+
const isStateReader = (context, node) => STATE_TYPES.some((name) => isPackageType(context, node, name, "typeorm"));
|
|
171
|
+
/** The handle `useTestWorld` answers, by where its type is declared: the `@starci/test-world` package or the repository's world slot. */
|
|
172
|
+
const isWorldHandle = (context, node) => {
|
|
173
|
+
const hfs = hfsOf(context);
|
|
174
|
+
return typeOrigins(context, node).some((origin) => origin.name === "TestWorld" && (origin.module === "@starci/test-world" || inTestWorld(hfs, origin.file)));
|
|
175
|
+
};
|
|
176
|
+
/** The name a non-computed member access reads, or null. */
|
|
177
|
+
const propertyNameOf = (node) => (node.type === "MemberExpression" && !node.computed && node.property.type === "Identifier" ? node.property.name : null);
|
|
178
|
+
/**
|
|
179
|
+
* Whether a member expression is the world's own state read: `<world>.db` (its connections are EntityManagers) or
|
|
180
|
+
* `<world>.services.<name>.api` (a sibling service's own API). The receiver is judged by its type, not its name.
|
|
181
|
+
*/
|
|
182
|
+
const isWorldStateRead = (context, node) => {
|
|
183
|
+
if (propertyNameOf(node) === "db")
|
|
184
|
+
return isWorldHandle(context, node.object);
|
|
185
|
+
if (propertyNameOf(node) !== "api")
|
|
186
|
+
return false;
|
|
187
|
+
const service = node.object;
|
|
188
|
+
const services = service.type === "MemberExpression" ? service.object : null;
|
|
189
|
+
// a non-null `services` that answers `services` is a member expression: the guard below only names that for the types
|
|
190
|
+
return services?.type === "MemberExpression" && propertyNameOf(services) === "services" && isWorldHandle(context, services.object);
|
|
191
|
+
};
|
|
192
|
+
/** An end-to-end spec that never reads state back proves only that the server replied. */
|
|
193
|
+
export const e2eAssertsPersistedState = {
|
|
194
|
+
meta: {
|
|
195
|
+
type: "problem",
|
|
196
|
+
docs: { description: "An e2e spec reads state back rather than asserting only on the response." },
|
|
197
|
+
schema: [],
|
|
198
|
+
messages: {
|
|
199
|
+
noState: "This e2e never reads any state back - it asserts on responses alone, so the flow can stop persisting and this file stays green. Read the row, the balance or the entitlement out of the database through the `EntityManager` and assert THAT.",
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
create(context) {
|
|
203
|
+
if (!isE2eSpec(context.filename || context.getFilename()))
|
|
204
|
+
return {};
|
|
205
|
+
let readsState = false;
|
|
206
|
+
return {
|
|
207
|
+
MemberExpression(node) {
|
|
208
|
+
if (!readsState && (isStateReader(context, node.object) || isWorldStateRead(context, node)))
|
|
209
|
+
readsState = true;
|
|
210
|
+
},
|
|
211
|
+
CallExpression(node) {
|
|
212
|
+
if (!readsState && node.arguments.some((argument) => argument.type !== "SpreadElement" && isStateReader(context, argument)))
|
|
213
|
+
readsState = true;
|
|
214
|
+
},
|
|
215
|
+
"Program:exit"(node) {
|
|
216
|
+
if (readsState)
|
|
217
|
+
return;
|
|
218
|
+
context.report({ node, messageId: "noState" });
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
// -- TESTING-9 -------------------------------------------------------------------------------------
|
|
224
|
+
/** Provider SDKs. Importing one into a flow test is a real model call by any other name. */
|
|
225
|
+
const PROVIDER_PACKAGES = /^(?:@anthropic-ai\/|openai$|openai\/|ollama$|@google\/generative-ai|@google\/genai|@mistralai\/|cohere-ai)/;
|
|
226
|
+
/** An e2e never calls a model; only a contract spec (`src/tests/contract/`, suffix `.contract-spec.ts`) may reach a provider. */
|
|
227
|
+
export const noModelCallInE2e = {
|
|
228
|
+
meta: {
|
|
229
|
+
type: "problem",
|
|
230
|
+
docs: { description: "An e2e spec never reaches a model provider." },
|
|
231
|
+
schema: [],
|
|
232
|
+
messages: {
|
|
233
|
+
provider: "`{{source}}` reaches a model provider from an e2e. A model call costs money, takes seconds and answers differently every time - so this makes the flow suite expensive, slow and flaky at once, and the assertion has to be loosened until it stops catching anything. Point the model integration at the world's network fake (`world.fake.<provider>`) and assert what can actually break: the entitlement, the quota, the persisted answer. Judging the answer itself belongs in a contract spec under `src/tests/contract/<provider>/`.",
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
create(context) {
|
|
237
|
+
const filename = context.filename || context.getFilename();
|
|
238
|
+
if (!isE2eSpec(filename))
|
|
239
|
+
return {};
|
|
240
|
+
return {
|
|
241
|
+
ImportDeclaration(node) {
|
|
242
|
+
const source = node.source && node.source.value;
|
|
243
|
+
if (typeof source !== "string" || !PROVIDER_PACKAGES.test(source))
|
|
244
|
+
return;
|
|
245
|
+
context.report({ node, messageId: "provider", data: { source } });
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
// -- TESTING-1 (and E2E-1 in e2e-flow.md, the same requirement on the same file) -------------------
|
|
251
|
+
/** Nouns that name an API shape rather than a business promise. `rewards-queries` is the anchor. */
|
|
252
|
+
const API_SHAPED_FILENAME_NOUNS = new Set([
|
|
253
|
+
"queries", "query", "mutations", "mutation",
|
|
254
|
+
"resolvers", "resolver", "endpoints", "endpoint",
|
|
255
|
+
"controllers", "controller", "handlers", "handler",
|
|
256
|
+
"modules", "module", "apis", "api",
|
|
257
|
+
]);
|
|
258
|
+
/** The hyphen-separated words of an e2e filename, with the lane suffix stripped. */
|
|
259
|
+
const e2eFilenameSegments = (filename) => {
|
|
260
|
+
const base = baseOf(filename);
|
|
261
|
+
return base.replace(/\.e2e-spec\.ts$/, "").split("-").filter(Boolean);
|
|
262
|
+
};
|
|
263
|
+
/** An e2e file is one business flow; the filename may not be an API-shape noun wearing its clothes. */
|
|
264
|
+
export const noApiShapedE2eFilename = {
|
|
265
|
+
meta: {
|
|
266
|
+
type: "problem",
|
|
267
|
+
docs: {
|
|
268
|
+
description: "An e2e filename reads as a business sentence (TESTING-1; E2E-1 in e2e-flow.md), not a resolver/endpoint/module group.",
|
|
269
|
+
},
|
|
270
|
+
schema: [],
|
|
271
|
+
messages: {
|
|
272
|
+
apiShaped: "`{{segment}}` names an API shape - a resolver group, an endpoint, a module - not a business promise. TESTING-1 / E2E-1: one e2e file is one business flow and the filename IS that flow; `rewards-queries.e2e-spec.ts` is exactly the shape this refuses. Name the file for the sentence it proves (`course-purchase.e2e-spec.ts`), not the API surface it happens to hit.",
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
create(context) {
|
|
276
|
+
const filename = context.filename || context.getFilename();
|
|
277
|
+
if (!isE2eSpec(filename))
|
|
278
|
+
return {};
|
|
279
|
+
const segments = e2eFilenameSegments(filename);
|
|
280
|
+
if (segments.length === 0)
|
|
281
|
+
return {};
|
|
282
|
+
const lastSegment = segments[segments.length - 1];
|
|
283
|
+
if (lastSegment === undefined)
|
|
284
|
+
return {};
|
|
285
|
+
const last = lastSegment.toLowerCase();
|
|
286
|
+
if (!API_SHAPED_FILENAME_NOUNS.has(last))
|
|
287
|
+
return {};
|
|
288
|
+
return {
|
|
289
|
+
Program(node) {
|
|
290
|
+
context.report({ node, messageId: "apiShaped", data: { segment: last } });
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
// -- TESTING-7 -------------------------------------------------------------------------------------
|
|
296
|
+
/** Test infrastructure: the fixtures and the test world, where the shared model stub lives (slots `be.tests.fixtures`, `be.tests.world`). */
|
|
297
|
+
const isTestInfrastructure = (hfs, filename) => hfs.slotOf(filename) === "be.tests.fixtures" || inTestWorld(hfs, filename);
|
|
298
|
+
/** Bare markers a stub returns when nobody gave it a real answer to stand in for. */
|
|
299
|
+
const MARKER_STRINGS = new Set(["stubbed", "stub", "ok", "test", "mock", "fake", "todo", "tbd", "n/a", "pending", ""]);
|
|
300
|
+
/** A string literal that is nothing but a marker - not an object, not a `JSON.stringify(...)` call. */
|
|
301
|
+
const isMarkerLiteral = (node) => Boolean(node && node.type === "Literal" && typeof node.value === "string" && MARKER_STRINGS.has(node.value.trim().toLowerCase()));
|
|
302
|
+
/** The world's default model stub returns a payload the production parser can actually parse. */
|
|
303
|
+
export const noMarkerModelStub = {
|
|
304
|
+
meta: {
|
|
305
|
+
type: "problem",
|
|
306
|
+
docs: {
|
|
307
|
+
description: "A model stub in test infra returns a parseable payload (TESTING-7), never a bare marker string.",
|
|
308
|
+
},
|
|
309
|
+
schema: [],
|
|
310
|
+
messages: {
|
|
311
|
+
marker: "This stub resolves to `{{value}}`, a marker rather than an answer. TESTING-7: a stub of a model returns a payload the production parser can actually parse - a marker string means the strict-JSON parser the real flow depends on never runs, so the most fragile seam in the flow is never exercised. Return the shape the production parser expects (an object, or a `JSON.stringify(...)` of one), the way `DEFAULT_MODEL_ANSWER` does.",
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
create(context) {
|
|
315
|
+
if (!isTestInfrastructure(hfsOf(context), context.filename || context.getFilename()))
|
|
316
|
+
return {};
|
|
317
|
+
return {
|
|
318
|
+
CallExpression(node) {
|
|
319
|
+
const callee = node.callee;
|
|
320
|
+
if (!callee || callee.type !== "MemberExpression" || callee.computed)
|
|
321
|
+
return;
|
|
322
|
+
const method = callee.property.name;
|
|
323
|
+
if (method === "mockResolvedValue" || method === "mockReturnValue") {
|
|
324
|
+
const argument = node.arguments[0];
|
|
325
|
+
if (isMarkerLiteral(argument)) {
|
|
326
|
+
context.report({ node: argument, messageId: "marker", data: { value: JSON.stringify(argument.value) } });
|
|
327
|
+
}
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (method !== "mockImplementation")
|
|
331
|
+
return;
|
|
332
|
+
const fn = node.arguments[0];
|
|
333
|
+
if (!fn || (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression"))
|
|
334
|
+
return;
|
|
335
|
+
if (fn.body.type !== "BlockStatement") {
|
|
336
|
+
if (isMarkerLiteral(fn.body)) {
|
|
337
|
+
context.report({ node: fn.body, messageId: "marker", data: { value: JSON.stringify(fn.body.value) } });
|
|
338
|
+
}
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
for (const statement of fn.body.body) {
|
|
342
|
+
if (statement.type === "ReturnStatement" && isMarkerLiteral(statement.argument)) {
|
|
343
|
+
context.report({ node: statement.argument, messageId: "marker", data: { value: JSON.stringify(statement.argument.value) } });
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
},
|
|
349
|
+
};
|
|
350
|
+
/** The rules this law contributes to the plugin. */
|
|
351
|
+
/** The rules this law contributes to the plugin. */
|
|
352
|
+
export const rules = {
|
|
353
|
+
"no-call-only-spec": noCallOnlySpec,
|
|
354
|
+
"unit-test-colocated": unitTestColocated,
|
|
355
|
+
"e2e-asserts-persisted-state": e2eAssertsPersistedState,
|
|
356
|
+
"no-model-call-in-e2e": noModelCallInE2e,
|
|
357
|
+
"no-api-shaped-e2e-filename": noApiShapedE2eFilename,
|
|
358
|
+
"no-marker-model-stub": noMarkerModelStub,
|
|
359
|
+
};
|
|
360
|
+
/** Every rule of this law ships at `error`. */
|
|
361
|
+
export const recommended = {
|
|
362
|
+
"starci-be/no-call-only-spec": "error",
|
|
363
|
+
"starci-be/unit-test-colocated": "error",
|
|
364
|
+
"starci-be/e2e-asserts-persisted-state": "error",
|
|
365
|
+
"starci-be/no-model-call-in-e2e": "error",
|
|
366
|
+
"starci-be/no-api-shaped-e2e-filename": "error",
|
|
367
|
+
"starci-be/no-marker-model-stub": "error",
|
|
368
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Recommended, RuleModule } from "./lib/rule.js";
|
|
2
|
+
/** Every third-party SDK import stays in a single integrations/<provider> capability. */
|
|
3
|
+
export declare const thirdPartyImportOwner: RuleModule;
|
|
4
|
+
/** The published rule and its required severity. */
|
|
5
|
+
export declare const rules: {
|
|
6
|
+
"third-party-import-owner": RuleModule;
|
|
7
|
+
};
|
|
8
|
+
export declare const recommended: Recommended;
|
|
@@ -6,106 +6,114 @@
|
|
|
6
6
|
* framework packages here supply composition, transport and persistence types that other canon rules already judge.
|
|
7
7
|
* The HFS view decides every source file's tier and owner, including imports in specs and provider support files.
|
|
8
8
|
*/
|
|
9
|
-
import { existsSync, readFileSync, readdirSync } from "node:fs"
|
|
10
|
-
import { isBuiltin } from "node:module"
|
|
11
|
-
import { dirname, join, resolve } from "node:path"
|
|
12
|
-
import ts from "typescript"
|
|
13
|
-
import { hfsOf, inTestWorld } from "./lib/hfs.
|
|
14
|
-
import { moduleReferences } from "./lib/import-source.
|
|
15
|
-
import { isSpecFile } from "./lib/path.
|
|
16
|
-
|
|
9
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
10
|
+
import { isBuiltin } from "node:module";
|
|
11
|
+
import { dirname, join, resolve } from "node:path";
|
|
12
|
+
import ts from "typescript";
|
|
13
|
+
import { hfsOf, inTestWorld } from "./lib/hfs.js";
|
|
14
|
+
import { moduleReferences } from "./lib/import-source.js";
|
|
15
|
+
import { isSpecFile } from "./lib/path.js";
|
|
17
16
|
const PURE = new Set([
|
|
18
17
|
"class-transformer", "class-validator", "rxjs", "reflect-metadata", "tslib", "zod", "lodash", "lodash-es",
|
|
19
18
|
"ramda", "decimal.js", "big.js", "bignumber.js", "uuid", "nanoid", "graphql", "graphql-scalars", "graphql-tag",
|
|
20
19
|
"dataloader", "fast-json-stable-stringify", "ajv", "yaml", "js-yaml", "semver",
|
|
21
|
-
])
|
|
20
|
+
]);
|
|
22
21
|
const FRAMEWORK = new Set([
|
|
23
22
|
"@nestjs/common", "@nestjs/core", "@nestjs/cqrs", "@nestjs/graphql", "@nestjs/apollo", "@nestjs/platform-express",
|
|
24
23
|
"@nestjs/typeorm", "@nestjs/swagger", "@nestjs/mapped-types", "@nestjs/throttler", "@apollo/server",
|
|
25
24
|
"@as-integrations/express5", "express", "nest-commander", "typeorm",
|
|
26
|
-
])
|
|
27
|
-
const TEST_LIBRARIES = new Set(["@nestjs/testing", "@starci/jest-preset", "@starci/test-world", "@jest/globals", "jest", "jest-mock"])
|
|
28
|
-
const INVENTORIES = new WeakMap()
|
|
29
|
-
const SOURCE = /\.[cm]?[jt]sx
|
|
30
|
-
const GENERATED = new Set(["node_modules", "dist", "coverage", ".git"])
|
|
31
|
-
|
|
25
|
+
]);
|
|
26
|
+
const TEST_LIBRARIES = new Set(["@nestjs/testing", "@starci/jest-preset", "@starci/test-world", "@jest/globals", "jest", "jest-mock"]);
|
|
27
|
+
const INVENTORIES = new WeakMap();
|
|
28
|
+
const SOURCE = /\.[cm]?[jt]sx?$/;
|
|
29
|
+
const GENERATED = new Set(["node_modules", "dist", "coverage", ".git"]);
|
|
32
30
|
/** The package root of a bare module specifier, including a scoped package's subpaths. */
|
|
33
31
|
const packageOf = (specifier) => {
|
|
34
|
-
if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("#") || isBuiltin(specifier))
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("#") || isBuiltin(specifier))
|
|
33
|
+
return null;
|
|
34
|
+
const parts = specifier.split("/");
|
|
35
|
+
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
36
|
+
};
|
|
39
37
|
/** A file that the back-end slot view classifies as executable source or a test. */
|
|
40
38
|
const inSource = (hfs, file) => {
|
|
41
|
-
const tier = hfs.tierOf(file)
|
|
42
|
-
return (tier !== null && tier !== "none") || hfs.slotOf(file)?.startsWith("be.tests.") === true
|
|
43
|
-
}
|
|
44
|
-
|
|
39
|
+
const tier = hfs.tierOf(file);
|
|
40
|
+
return (tier !== null && tier !== "none") || hfs.slotOf(file)?.startsWith("be.tests.") === true;
|
|
41
|
+
};
|
|
45
42
|
/** Every literal module reference in a source file, including dynamic imports, require and re-exports. */
|
|
46
43
|
const referencesOf = (file, text) => {
|
|
47
|
-
const found = new Set()
|
|
48
|
-
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true)
|
|
44
|
+
const found = new Set();
|
|
45
|
+
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true);
|
|
49
46
|
const add = (node) => {
|
|
50
|
-
if (node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)))
|
|
51
|
-
|
|
47
|
+
if (node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)))
|
|
48
|
+
found.add(node.text);
|
|
49
|
+
};
|
|
52
50
|
const visit = (node) => {
|
|
53
|
-
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node))
|
|
54
|
-
|
|
55
|
-
else if (ts.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
51
|
+
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node))
|
|
52
|
+
add(node.moduleSpecifier);
|
|
53
|
+
else if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference))
|
|
54
|
+
add(node.moduleReference.expression);
|
|
55
|
+
else if (ts.isCallExpression(node) && (node.expression.kind === ts.SyntaxKind.ImportKeyword || (ts.isIdentifier(node.expression) && node.expression.text === "require")))
|
|
56
|
+
add(node.arguments[0]);
|
|
57
|
+
ts.forEachChild(node, visit);
|
|
58
|
+
};
|
|
59
|
+
visit(source);
|
|
60
|
+
return found;
|
|
61
|
+
};
|
|
62
62
|
/**
|
|
63
63
|
* The app's dependency declarations and the SDK imports of all integration owners, read once for this HFS view.
|
|
64
64
|
* No ancestor search: be/ has the app-root package.json immediately above it; standalone fixtures may put it at root.
|
|
65
65
|
*/
|
|
66
66
|
const inventoryOf = (hfs) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
67
|
+
const cached = INVENTORIES.get(hfs);
|
|
68
|
+
if (cached)
|
|
69
|
+
return cached;
|
|
70
|
+
const appManifest = join(dirname(hfs.repoRoot), "package.json");
|
|
71
|
+
const manifest = existsSync(appManifest) ? appManifest : join(hfs.repoRoot, "package.json");
|
|
72
|
+
if (!existsSync(manifest))
|
|
73
|
+
throw new Error("third-party-import-owner needs the app-root package.json beside be/ to derive SDK ownership");
|
|
74
|
+
const declaration = JSON.parse(readFileSync(manifest, "utf8"));
|
|
75
|
+
const dependencies = new Map();
|
|
73
76
|
for (const section of ["dependencies", "optionalDependencies", "peerDependencies", "devDependencies"]) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
const declared = typeof declaration === "object" && declaration !== null ? Reflect.get(declaration, section) : undefined;
|
|
78
|
+
for (const [name, version] of Object.entries(declared ?? {})) {
|
|
79
|
+
if (/^(?:workspace:|file:|link:)/.test(String(version)))
|
|
80
|
+
continue;
|
|
81
|
+
const alias = /^npm:((?:@[^/]+\/)?[^@]+)(?:@|$)/.exec(String(version));
|
|
82
|
+
dependencies.set(name, alias?.[1] ?? name);
|
|
78
83
|
}
|
|
79
84
|
}
|
|
80
|
-
const byFile = new Map()
|
|
85
|
+
const byFile = new Map();
|
|
81
86
|
const walk = (directory) => {
|
|
82
87
|
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
83
|
-
if (GENERATED.has(entry.name) || entry.isSymbolicLink())
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
if (GENERATED.has(entry.name) || entry.isSymbolicLink())
|
|
89
|
+
continue;
|
|
90
|
+
const file = join(directory, entry.name);
|
|
91
|
+
if (entry.isDirectory())
|
|
92
|
+
walk(file);
|
|
93
|
+
else if (SOURCE.test(entry.name) && hfs.tierOf(file) === "integrations")
|
|
94
|
+
byFile.set(resolve(file), referencesOf(file, readFileSync(file, "utf8")));
|
|
87
95
|
}
|
|
88
|
-
}
|
|
89
|
-
walk(hfs.repoRoot)
|
|
90
|
-
const result = { dependencies, byFile }
|
|
91
|
-
INVENTORIES.set(hfs, result)
|
|
92
|
-
return result
|
|
93
|
-
}
|
|
94
|
-
|
|
96
|
+
};
|
|
97
|
+
walk(hfs.repoRoot);
|
|
98
|
+
const result = { dependencies, byFile };
|
|
99
|
+
INVENTORIES.set(hfs, result);
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
95
102
|
/** The table already owns this import (including a package subpath); R90 alone judges it. */
|
|
96
|
-
const inInfraTable = (hfs, specifier) => Object.keys(hfs.ruleParams.infraOwners).some((key) => specifier === key || specifier.startsWith(`${key}/`))
|
|
97
|
-
|
|
103
|
+
const inInfraTable = (hfs, specifier) => Object.keys(hfs.ruleParams.infraOwners).some((key) => specifier === key || specifier.startsWith(`${key}/`));
|
|
98
104
|
/** The declared SDK identity, or null for an import that belongs to another existing concern. */
|
|
99
105
|
const sdkOf = (hfs, dependencies, file, specifier) => {
|
|
100
|
-
const name = packageOf(specifier)
|
|
101
|
-
const dependency = dependencies.get(name)
|
|
102
|
-
if (!dependency || inInfraTable(hfs, specifier) || PURE.has(dependency) || FRAMEWORK.has(dependency))
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
const name = packageOf(specifier);
|
|
107
|
+
const dependency = name === null || name === undefined ? undefined : dependencies.get(name);
|
|
108
|
+
if (!dependency || inInfraTable(hfs, specifier) || PURE.has(dependency) || FRAMEWORK.has(dependency))
|
|
109
|
+
return null;
|
|
110
|
+
const isTest = isSpecFile(file) || hfs.slotOf(file)?.startsWith("be.tests.") === true;
|
|
111
|
+
if (isTest && TEST_LIBRARIES.has(dependency))
|
|
112
|
+
return null;
|
|
113
|
+
if (inTestWorld(hfs, file) && (dependency === "testcontainers" || dependency.startsWith("@testcontainers/")))
|
|
114
|
+
return null;
|
|
115
|
+
return dependency;
|
|
116
|
+
};
|
|
109
117
|
/** Every third-party SDK import stays in a single integrations/<provider> capability. */
|
|
110
118
|
export const thirdPartyImportOwner = {
|
|
111
119
|
meta: {
|
|
@@ -118,32 +126,38 @@ export const thirdPartyImportOwner = {
|
|
|
118
126
|
},
|
|
119
127
|
},
|
|
120
128
|
create(context) {
|
|
121
|
-
const hfs = hfsOf(context)
|
|
122
|
-
const file = context.filename
|
|
123
|
-
if (!inSource(hfs, file))
|
|
124
|
-
|
|
129
|
+
const hfs = hfsOf(context);
|
|
130
|
+
const file = context.filename;
|
|
131
|
+
if (!inSource(hfs, file))
|
|
132
|
+
return {};
|
|
133
|
+
let inventory;
|
|
125
134
|
return moduleReferences(({ source, value }) => {
|
|
126
135
|
// Relative paths, node builtins and the infrastructure table need no dependency manifest.
|
|
127
|
-
if (packageOf(value) === null || inInfraTable(hfs, value))
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const
|
|
136
|
+
if (packageOf(value) === null || inInfraTable(hfs, value))
|
|
137
|
+
return;
|
|
138
|
+
inventory ??= inventoryOf(hfs);
|
|
139
|
+
const { dependencies, byFile } = inventory;
|
|
140
|
+
const sdk = sdkOf(hfs, dependencies, file, value);
|
|
141
|
+
if (sdk === null)
|
|
142
|
+
return;
|
|
143
|
+
const here = hfs.ownerOf(file);
|
|
132
144
|
if (hfs.tierOf(file) !== "integrations" || here === null) {
|
|
133
|
-
context.report({ node: source, messageId: "foreign", data: { name: value } })
|
|
134
|
-
return
|
|
145
|
+
context.report({ node: source, messageId: "foreign", data: { name: value } });
|
|
146
|
+
return;
|
|
135
147
|
}
|
|
136
|
-
const owners = new Set([here])
|
|
137
|
-
for (const [other, references] of
|
|
148
|
+
const owners = new Set([here]);
|
|
149
|
+
for (const [other, references] of byFile) {
|
|
138
150
|
// ESLint's current buffer replaces the on-disk text of this file.
|
|
139
|
-
if (other === resolve(file))
|
|
140
|
-
|
|
151
|
+
if (other === resolve(file))
|
|
152
|
+
continue;
|
|
153
|
+
if ([...references].some((reference) => sdkOf(hfs, dependencies, other, reference) === sdk))
|
|
154
|
+
owners.add(hfs.ownerOf(other));
|
|
141
155
|
}
|
|
142
|
-
if (owners.size > 1)
|
|
143
|
-
|
|
156
|
+
if (owners.size > 1)
|
|
157
|
+
context.report({ node: source, messageId: "multiple", data: { name: sdk, owners: [...owners].sort().map((owner) => `\`${owner}\``).join(", ") } });
|
|
158
|
+
});
|
|
144
159
|
},
|
|
145
|
-
}
|
|
146
|
-
|
|
160
|
+
};
|
|
147
161
|
/** The published rule and its required severity. */
|
|
148
|
-
export const rules = { "third-party-import-owner": thirdPartyImportOwner }
|
|
149
|
-
export const recommended = { "starci-be/third-party-import-owner": "error" }
|
|
162
|
+
export const rules = { "third-party-import-owner": thirdPartyImportOwner };
|
|
163
|
+
export const recommended = { "starci-be/third-party-import-owner": "error" };
|