architecture-harness 0.4.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/package.json +80 -0
- package/src/cli/check.mjs +535 -0
- package/src/cli/cli.test.mjs +120 -0
- package/src/cli/commit-attribution.mjs +68 -0
- package/src/cli/commit-attribution.test.mjs +47 -0
- package/src/cli/decisions.mjs +223 -0
- package/src/cli/doctor.mjs +135 -0
- package/src/cli/doctor.test.mjs +57 -0
- package/src/cli/init.mjs +98 -0
- package/src/cli/install-hooks.mjs +26 -0
- package/src/cli/qc.mjs +89 -0
- package/src/cli/scaffold.mjs +194 -0
- package/src/cli/sync-enforcement-map.mjs +28 -0
- package/src/cli/work-order-guard.mjs +0 -0
- package/src/cli/work-order-guard.test.mjs +79 -0
- package/src/cli/worktree-isolation.mjs +94 -0
- package/src/cli/worktree-isolation.test.mjs +82 -0
- package/src/config.mjs +338 -0
- package/src/config.test.mjs +59 -0
- package/src/decisions.mjs +198 -0
- package/src/decisions.test.mjs +183 -0
- package/src/descriptions.mjs +28 -0
- package/src/descriptions.test.mjs +27 -0
- package/src/enforcement-map.mjs +46 -0
- package/src/enforcement-map.test.mjs +60 -0
- package/src/eslint/index.mjs +37 -0
- package/src/eslint/options.mjs +53 -0
- package/src/eslint/preset.mjs +192 -0
- package/src/eslint/preset.test.mjs +96 -0
- package/src/eslint/rules/durable-idempotency-key.mjs +120 -0
- package/src/eslint/rules/external-service-only-in-adapter.mjs +52 -0
- package/src/eslint/rules/module-boundary.mjs +12 -0
- package/src/eslint/rules/name-the-pattern.mjs +111 -0
- package/src/eslint/rules/no-comment-paragraph.mjs +101 -0
- package/src/eslint/rules/no-cross-feature-internals.mjs +57 -0
- package/src/eslint/rules/no-number-in-comment.mjs +89 -0
- package/src/eslint/rules/no-offset-pagination.mjs +29 -0
- package/src/eslint/rules/no-orchestration-in-trigger.mjs +56 -0
- package/src/eslint/rules/no-promise-then.mjs +43 -0
- package/src/eslint/rules/no-promise-then.test.mjs +63 -0
- package/src/eslint/rules/no-raw-fetch.mjs +27 -0
- package/src/eslint/rules/no-status-literal.mjs +34 -0
- package/src/eslint/rules/no-supersession-trail.mjs +51 -0
- package/src/eslint/rules/scoped-repository.mjs +43 -0
- package/src/eslint/rules/signal-last-param.mjs +43 -0
- package/src/eslint/rules/storage-only-in-resource.mjs +52 -0
- package/src/eslint/rules/tenant-scoped-table.mjs +71 -0
- package/src/eslint/rules.test.mjs +627 -0
- package/src/gates/adr-format.mjs +87 -0
- package/src/gates/adr-format.test.mjs +54 -0
- package/src/gates/audit-append-only.mjs +34 -0
- package/src/gates/audit-append-only.test.mjs +36 -0
- package/src/gates/citations.mjs +93 -0
- package/src/gates/citations.test.mjs +115 -0
- package/src/gates/claimed-requirements.mjs +53 -0
- package/src/gates/claimed-requirements.test.mjs +60 -0
- package/src/gates/comment-style.mjs +76 -0
- package/src/gates/comment-style.test.mjs +46 -0
- package/src/gates/config-floor.mjs +68 -0
- package/src/gates/config-floor.test.mjs +52 -0
- package/src/gates/contract-compose.mjs +38 -0
- package/src/gates/contract-compose.test.mjs +40 -0
- package/src/gates/eight-blocks.mjs +223 -0
- package/src/gates/eight-blocks.test.mjs +359 -0
- package/src/gates/enforcement-map.mjs +62 -0
- package/src/gates/enforcement-map.test.mjs +41 -0
- package/src/gates/english-source.mjs +176 -0
- package/src/gates/english-source.test.mjs +122 -0
- package/src/gates/frontend-boundaries.mjs +154 -0
- package/src/gates/frontend-boundaries.test.mjs +72 -0
- package/src/gates/gate-tests.mjs +44 -0
- package/src/gates/gate-tests.test.mjs +41 -0
- package/src/gates/headless-sagas.mjs +54 -0
- package/src/gates/headless-sagas.test.mjs +71 -0
- package/src/gates/internal-routes.mjs +76 -0
- package/src/gates/internal-routes.test.mjs +53 -0
- package/src/gates/public-routes.mjs +67 -0
- package/src/gates/public-routes.test.mjs +38 -0
- package/src/gates/registry-agreement.mjs +37 -0
- package/src/gates/registry-agreement.test.mjs +36 -0
- package/src/gates/saga-tests.mjs +94 -0
- package/src/gates/saga-tests.test.mjs +92 -0
- package/src/gates/sql-identifiers.mjs +244 -0
- package/src/gates/sql-identifiers.test.mjs +197 -0
- package/src/gates/tenant-predicate.mjs +175 -0
- package/src/gates/tenant-predicate.test.mjs +161 -0
- package/src/gates/tenant-tables.mjs +65 -0
- package/src/gates/tenant-tables.test.mjs +56 -0
- package/src/gates/test-mirror.mjs +47 -0
- package/src/gates/test-mirror.test.mjs +31 -0
- package/src/index.mjs +9 -0
- package/templates/.jscpd.json +15 -0
- package/templates/CLAUDE.md +54 -0
- package/templates/decisions.examples.md +25 -0
- package/templates/docs/adr-template.md +58 -0
- package/templates/docs/architecture.md +179 -0
- package/templates/docs/decisions.md +72 -0
- package/templates/docs/enforcement.md +94 -0
- package/templates/docs/glossary.md +16 -0
- package/templates/docs/guards.md +23 -0
- package/templates/docs/pagination.md +113 -0
- package/templates/docs/performance.md +35 -0
- package/templates/docs/ui.md +32 -0
- package/templates/githooks/commit-msg +4 -0
- package/templates/githooks/pre-commit +18 -0
- package/templates/githooks/pre-push +6 -0
- package/templates/github/workflows/ci.yml +21 -0
- package/templates/qc.config.json +6 -0
- package/templates/quality-thresholds.json +114 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// A UI trigger is a thin presenter. Multi-step orchestration belongs in a headless
|
|
2
|
+
// pipeline. docs/architecture.md.
|
|
3
|
+
|
|
4
|
+
import { filenameOf, optionsOf, pathGlob, schemaOf, TEST_FILE } from "../options.mjs";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_PRESENTERS = "frontend/src/features/*/trigger";
|
|
7
|
+
const DEFAULT_MAX_AWAITS = 1;
|
|
8
|
+
|
|
9
|
+
export default {
|
|
10
|
+
meta: {
|
|
11
|
+
type: "problem",
|
|
12
|
+
docs: { description: "UI triggers cannot orchestrate multi-step asynchronous business flows" },
|
|
13
|
+
schema: schemaOf({ presenters: { type: "string" }, maxAwaits: { type: "integer", minimum: 1 } }),
|
|
14
|
+
messages: {
|
|
15
|
+
orchestration:
|
|
16
|
+
"Multi-step orchestration in a UI trigger. Encapsulate the workflow as a headless saga or pipeline command.",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
create(context) {
|
|
20
|
+
const options = optionsOf(context);
|
|
21
|
+
const filename = filenameOf(context);
|
|
22
|
+
if (!pathGlob(options.presenters ?? DEFAULT_PRESENTERS).test(filename)) return {};
|
|
23
|
+
if (TEST_FILE.test(filename)) return {};
|
|
24
|
+
|
|
25
|
+
const maxAwaits = options.maxAwaits ?? DEFAULT_MAX_AWAITS;
|
|
26
|
+
const functionStack = [];
|
|
27
|
+
|
|
28
|
+
function enterFunction(node) {
|
|
29
|
+
functionStack.push({ node, awaitNodes: [] });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function exitFunction() {
|
|
33
|
+
const current = functionStack.pop();
|
|
34
|
+
if (!current) return;
|
|
35
|
+
if (current.awaitNodes.length > maxAwaits) {
|
|
36
|
+
for (const node of current.awaitNodes.slice(maxAwaits)) {
|
|
37
|
+
context.report({ node, messageId: "orchestration" });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
FunctionDeclaration: enterFunction,
|
|
44
|
+
FunctionExpression: enterFunction,
|
|
45
|
+
ArrowFunctionExpression: enterFunction,
|
|
46
|
+
"FunctionDeclaration:exit": exitFunction,
|
|
47
|
+
"FunctionExpression:exit": exitFunction,
|
|
48
|
+
"ArrowFunctionExpression:exit": exitFunction,
|
|
49
|
+
AwaitExpression(node) {
|
|
50
|
+
if (functionStack.length > 0) {
|
|
51
|
+
functionStack[functionStack.length - 1].awaitNodes.push(node);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// A promise is awaited. `.finally()` carries no value and has no await form, so it is left alone.
|
|
2
|
+
|
|
3
|
+
import { filenameOf, optionsOf, pathSuffix, schemaOf } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_METHODS = ["then", "catch"];
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: { description: "a promise is awaited, never continued with .then() or .catch()" },
|
|
11
|
+
schema: schemaOf({
|
|
12
|
+
methods: { type: "array", items: { type: "string" } },
|
|
13
|
+
allow: { type: "array", items: { type: "string" } },
|
|
14
|
+
}),
|
|
15
|
+
messages: {
|
|
16
|
+
chained: "Await the promise instead of chaining .{{method}}(). Use try/catch for the failure path.",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
create(context) {
|
|
20
|
+
const options = optionsOf(context);
|
|
21
|
+
const methods = options.methods ?? DEFAULT_METHODS;
|
|
22
|
+
const allowed = (options.allow ?? []).some((file) => pathSuffix(file).test(filenameOf(context)));
|
|
23
|
+
if (allowed) return {};
|
|
24
|
+
return {
|
|
25
|
+
CallExpression(node) {
|
|
26
|
+
const callee = node.callee;
|
|
27
|
+
if (callee.type !== "MemberExpression") return;
|
|
28
|
+
// `p["then"](fn)` is `p.then(fn)` spelled to evade a rule that only reads dot access.
|
|
29
|
+
const name = callee.computed
|
|
30
|
+
? (typeof callee.property.value === "string" ? callee.property.value : undefined)
|
|
31
|
+
: callee.property.type === "Identifier"
|
|
32
|
+
? callee.property.name
|
|
33
|
+
: undefined;
|
|
34
|
+
if (name === undefined || !methods.includes(name)) return;
|
|
35
|
+
// `x.catch` with no callback is some other api borrowing the name.
|
|
36
|
+
const [first] = node.arguments;
|
|
37
|
+
if (first === undefined) return;
|
|
38
|
+
if (first.type !== "ArrowFunctionExpression" && first.type !== "FunctionExpression" && first.type !== "Identifier") return;
|
|
39
|
+
context.report({ node: callee.property, messageId: "chained", data: { method: name } });
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { RuleTester } from "eslint";
|
|
4
|
+
import rule from "./no-promise-then.mjs";
|
|
5
|
+
|
|
6
|
+
const tester = new RuleTester({ languageOptions: { ecmaVersion: 2023, sourceType: "module" } });
|
|
7
|
+
|
|
8
|
+
test("an awaited promise passes, and a chained one fails", () => {
|
|
9
|
+
tester.run("no-promise-then", rule, {
|
|
10
|
+
valid: [
|
|
11
|
+
{ code: "async function f() { const a = await g(); return a; }" },
|
|
12
|
+
{ code: "async function f() { try { await g(); } catch (error) { report(error); } }" },
|
|
13
|
+
// .finally carries no value and has no await form, so it is left alone.
|
|
14
|
+
{ code: "async function f() { await g().finally(() => close()); }" },
|
|
15
|
+
// A member named `then` with no callback is some other api borrowing the word.
|
|
16
|
+
{ code: "const when = schedule.then;" },
|
|
17
|
+
// A computed key that is not a literal cannot be read, so it is left alone.
|
|
18
|
+
{ code: "async function f() { await queue[key](handler); }" },
|
|
19
|
+
],
|
|
20
|
+
invalid: [
|
|
21
|
+
{
|
|
22
|
+
code: "function f() { return g().then((value) => value + 1); }",
|
|
23
|
+
errors: [{ messageId: "chained", data: { method: "then" } }],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
code: "function f() { return g().catch(handle); }",
|
|
27
|
+
errors: [{ messageId: "chained", data: { method: "catch" } }],
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
code: "function f() { return g().then(function (v) { return v; }); }",
|
|
31
|
+
errors: [{ messageId: "chained" }],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
code: "function f() { return g()[\"then\"](h); }",
|
|
35
|
+
errors: [{ messageId: "chained", data: { method: "then" } }],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
code: "function f() { return g()?.then(h); }",
|
|
39
|
+
errors: [{ messageId: "chained" }],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a module named in `allow` may still chain", () => {
|
|
46
|
+
tester.run("no-promise-then", rule, {
|
|
47
|
+
valid: [
|
|
48
|
+
{
|
|
49
|
+
code: "function f() { return g().then(h); }",
|
|
50
|
+
filename: "/repo/packages/kernel/src/abortable.ts",
|
|
51
|
+
options: [{ allow: ["packages/kernel/src/abortable.ts"] }],
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
invalid: [
|
|
55
|
+
{
|
|
56
|
+
code: "function f() { return g().then(h); }",
|
|
57
|
+
filename: "/repo/frontend/src/features/photos/slices/list-photos.ts",
|
|
58
|
+
options: [{ allow: ["packages/kernel/src/abortable.ts"] }],
|
|
59
|
+
errors: [{ messageId: "chained" }],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// One module builds a URL and calls fetch. docs/architecture.md.
|
|
2
|
+
|
|
3
|
+
import { filenameOf, optionsOf, pathSuffix, schemaOf } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_CLIENT = "platform/api-client.ts";
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: { description: "fetch belongs to the api client" },
|
|
11
|
+
schema: schemaOf({ client: { type: "string" } }),
|
|
12
|
+
messages: { raw: "Call the api client. Only {{client}} may call fetch or build a URL." },
|
|
13
|
+
},
|
|
14
|
+
create(context) {
|
|
15
|
+
const client = optionsOf(context).client ?? DEFAULT_CLIENT;
|
|
16
|
+
if (pathSuffix(client).test(filenameOf(context))) return {};
|
|
17
|
+
const report = (node) => context.report({ node, messageId: "raw", data: { client } });
|
|
18
|
+
return {
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
if (node.callee.type === "Identifier" && node.callee.name === "fetch") report(node);
|
|
21
|
+
},
|
|
22
|
+
NewExpression(node) {
|
|
23
|
+
if (node.callee.type === "Identifier" && node.callee.name === "URL") report(node);
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Domain code throws a DomainError; one handler maps it. docs/architecture.md.
|
|
2
|
+
|
|
3
|
+
import { optionsOf, schemaOf, suffixGroup } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_SETTERS = ["status", "code", "statusCode"];
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: { description: "no hand-written HTTP status" },
|
|
11
|
+
schema: schemaOf({ setters: { type: "array", items: { type: "string" } }, error: { type: "string" } }),
|
|
12
|
+
messages: { literal: "Throw a {{error}}. One error handler owns the status mapping." },
|
|
13
|
+
},
|
|
14
|
+
create(context) {
|
|
15
|
+
const options = optionsOf(context);
|
|
16
|
+
const setters = new Set(options.setters ?? DEFAULT_SETTERS);
|
|
17
|
+
const error = options.error ?? "DomainError";
|
|
18
|
+
return {
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
const callee = node.callee;
|
|
21
|
+
if (
|
|
22
|
+
callee.type === "MemberExpression" &&
|
|
23
|
+
callee.property.type === "Identifier" &&
|
|
24
|
+
setters.has(callee.property.name) &&
|
|
25
|
+
node.arguments.length === 1 &&
|
|
26
|
+
node.arguments[0].type === "Literal" &&
|
|
27
|
+
typeof node.arguments[0].value === "number"
|
|
28
|
+
) {
|
|
29
|
+
context.report({ node, messageId: "literal", data: { error } });
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// A change is rewritten in place, as if it had always been that way. No supersession
|
|
2
|
+
// trail, no deprecation note, no "what this replaces" — the repository states what is
|
|
3
|
+
// true now, and git holds the history. docs/decisions.md.
|
|
4
|
+
//
|
|
5
|
+
// Comments only. A vocabulary word in a string is someone's data, not this repository
|
|
6
|
+
// narrating its own past.
|
|
7
|
+
|
|
8
|
+
import { escape, optionsOf, schemaOf } from "../options.mjs";
|
|
9
|
+
|
|
10
|
+
const DEFAULT_PHRASES = [
|
|
11
|
+
"deprecated",
|
|
12
|
+
"deprecation",
|
|
13
|
+
"superseded",
|
|
14
|
+
"supersedes",
|
|
15
|
+
"legacy",
|
|
16
|
+
"this replaces",
|
|
17
|
+
"replaced by",
|
|
18
|
+
"formerly",
|
|
19
|
+
"used to be",
|
|
20
|
+
"no longer used",
|
|
21
|
+
"kept for backwards compat",
|
|
22
|
+
"backwards compatibility",
|
|
23
|
+
"for backward compat",
|
|
24
|
+
"old implementation",
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
meta: {
|
|
29
|
+
type: "problem",
|
|
30
|
+
docs: { description: "no supersession trail in a comment; the repository states what is true now" },
|
|
31
|
+
schema: schemaOf({ phrases: { type: "array", items: { type: "string" } } }),
|
|
32
|
+
messages: {
|
|
33
|
+
trail:
|
|
34
|
+
"'{{phrase}}' narrates what this used to be. Rewrite in place and delete the trail — git holds the history.",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
create(context) {
|
|
38
|
+
const phrases = optionsOf(context).phrases ?? DEFAULT_PHRASES;
|
|
39
|
+
const pattern = new RegExp(`\\b(${phrases.map(escape).join("|")})\\b`, "i");
|
|
40
|
+
return {
|
|
41
|
+
Program() {
|
|
42
|
+
for (const comment of context.sourceCode.getAllComments()) {
|
|
43
|
+
const found = pattern.exec(comment.value);
|
|
44
|
+
if (found !== null) {
|
|
45
|
+
context.report({ node: comment, messageId: "trail", data: { phrase: found[1].toLowerCase() } });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// A singleton repository captures the first request's tenant. docs/architecture.md.
|
|
2
|
+
|
|
3
|
+
import { optionsOf, schemaOf, suffixGroup } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_SUFFIXES = ["Repository", "Saga", "UnitOfWork", "UnitOfWorkFactory"];
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: { description: "repositories and sagas are scoped, never singleton" },
|
|
11
|
+
schema: schemaOf({
|
|
12
|
+
suffixes: { type: "array", items: { type: "string" } },
|
|
13
|
+
lifetime: { type: "string" },
|
|
14
|
+
}),
|
|
15
|
+
messages: {
|
|
16
|
+
singleton:
|
|
17
|
+
"'{{name}}' must be .{{lifetime}}(). A singleton repository holds one tenant for the process lifetime — a cross-tenant leak.",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
const options = optionsOf(context);
|
|
22
|
+
const scopedName = suffixGroup(options.suffixes ?? DEFAULT_SUFFIXES);
|
|
23
|
+
const lifetime = options.lifetime ?? "scoped";
|
|
24
|
+
return {
|
|
25
|
+
CallExpression(node) {
|
|
26
|
+
const callee = node.callee;
|
|
27
|
+
if (
|
|
28
|
+
callee.type !== "MemberExpression" ||
|
|
29
|
+
callee.property.type !== "Identifier" ||
|
|
30
|
+
callee.property.name !== "singleton"
|
|
31
|
+
) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const inner = callee.object;
|
|
35
|
+
if (inner.type !== "CallExpression" || inner.arguments.length === 0) return;
|
|
36
|
+
const arg = inner.arguments[0];
|
|
37
|
+
if (arg.type === "Identifier" && scopedName.test(arg.name)) {
|
|
38
|
+
context.report({ node, messageId: "singleton", data: { name: arg.name, lifetime } });
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Cancellation is a parameter, not something a developer remembers. docs/architecture.md.
|
|
2
|
+
|
|
3
|
+
import { optionsOf, schemaOf } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_NAME = "signal";
|
|
6
|
+
|
|
7
|
+
function isSignal(param, pattern) {
|
|
8
|
+
const node = param.type === "AssignmentPattern" ? param.left : param;
|
|
9
|
+
if (node.type !== "Identifier") return false;
|
|
10
|
+
return pattern.test(node.name);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
meta: {
|
|
15
|
+
type: "problem",
|
|
16
|
+
docs: { description: "an exported async function takes a cancellation token last" },
|
|
17
|
+
schema: schemaOf({ name: { type: "string" }, type: { type: "string" } }),
|
|
18
|
+
messages: { missing: "'{{name}}' can outlive the interaction: take an {{type}} as the last parameter." },
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
const options = optionsOf(context);
|
|
22
|
+
const pattern = new RegExp(`${options.name ?? DEFAULT_NAME}$`, "i");
|
|
23
|
+
const type = options.type ?? "AbortSignal";
|
|
24
|
+
function check(node, name) {
|
|
25
|
+
if (!node.async) return;
|
|
26
|
+
const params = node.params;
|
|
27
|
+
if (params.length > 0 && isSignal(params[params.length - 1], pattern)) return;
|
|
28
|
+
context.report({ node, messageId: "missing", data: { name, type } });
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
"ExportNamedDeclaration > FunctionDeclaration"(node) {
|
|
32
|
+
check(node, node.id ? node.id.name : "function");
|
|
33
|
+
},
|
|
34
|
+
"ExportNamedDeclaration > VariableDeclaration > VariableDeclarator"(node) {
|
|
35
|
+
const init = node.init;
|
|
36
|
+
if (!init) return;
|
|
37
|
+
if (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression") {
|
|
38
|
+
check(init, node.id.type === "Identifier" ? node.id.name : "function");
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// One block touches storage. docs/architecture.md.
|
|
2
|
+
//
|
|
3
|
+
// The driver binding itself is the exception, and it has to be: something must
|
|
4
|
+
// import the driver for that block to have a pool to query through. It lives in
|
|
5
|
+
// the infrastructure path, which docs/architecture.md already permits to import
|
|
6
|
+
// anything. Recognising the path is what keeps this rule honest — matching
|
|
7
|
+
// filenames alone forced the one file that must import the driver to disable the
|
|
8
|
+
// rule, and a disable comment is how a rule stops meaning anything.
|
|
9
|
+
|
|
10
|
+
import { filenameOf, moduleGroup, optionsOf, schemaOf } from "../options.mjs";
|
|
11
|
+
import { isExemptFile } from "./module-boundary.mjs";
|
|
12
|
+
|
|
13
|
+
const DEFAULT_MODULES = ["drizzle-orm", "pg", "postgres"];
|
|
14
|
+
const DEFAULT_RESOURCE_FILES = ["resource.ts", "schema.ts"];
|
|
15
|
+
const DEFAULT_DRIVER_BINDING = "backend/src/infrastructure/db/";
|
|
16
|
+
|
|
17
|
+
export default {
|
|
18
|
+
meta: {
|
|
19
|
+
type: "problem",
|
|
20
|
+
docs: { description: "storage imports belong in the resource block" },
|
|
21
|
+
schema: schemaOf({
|
|
22
|
+
modules: { type: "array", items: { type: "string" } },
|
|
23
|
+
resourceFiles: { type: "array", items: { type: "string" } },
|
|
24
|
+
driverBinding: { type: "string" },
|
|
25
|
+
}),
|
|
26
|
+
messages: { misplaced: "'{{source}}' belongs in {{allowed}} — the only block that touches storage." },
|
|
27
|
+
},
|
|
28
|
+
create(context) {
|
|
29
|
+
const options = optionsOf(context);
|
|
30
|
+
const storage = moduleGroup(options.modules ?? DEFAULT_MODULES);
|
|
31
|
+
const resourceFiles = options.resourceFiles ?? DEFAULT_RESOURCE_FILES;
|
|
32
|
+
const driverBinding = options.driverBinding ?? DEFAULT_DRIVER_BINDING;
|
|
33
|
+
|
|
34
|
+
const exempt = isExemptFile(filenameOf(context), {
|
|
35
|
+
allowedBasenames: resourceFiles,
|
|
36
|
+
allowedPathPrefixes: driverBinding ? [driverBinding] : [],
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
ImportDeclaration(node) {
|
|
40
|
+
if (exempt) return;
|
|
41
|
+
const source = node.source.value;
|
|
42
|
+
if (typeof source === "string" && storage.test(source)) {
|
|
43
|
+
context.report({
|
|
44
|
+
node,
|
|
45
|
+
messageId: "misplaced",
|
|
46
|
+
data: { source, allowed: resourceFiles.join(" or ") },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Tenant isolation is the security model. docs/architecture.md.
|
|
2
|
+
// The partition-key index the database requires is the one exception.
|
|
3
|
+
|
|
4
|
+
import { optionsOf, schemaOf } from "../options.mjs";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_COLUMN = "tenantId";
|
|
7
|
+
const DEFAULT_TABLE_FACTORY = "pgTable";
|
|
8
|
+
const DEFAULT_INDEX_FACTORY = "index";
|
|
9
|
+
const DEFAULT_EXEMPT = "partition";
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
meta: {
|
|
13
|
+
type: "problem",
|
|
14
|
+
docs: { description: "every business table carries the tenant column" },
|
|
15
|
+
schema: schemaOf({
|
|
16
|
+
column: { type: "string" },
|
|
17
|
+
tableFactory: { type: "string" },
|
|
18
|
+
indexFactory: { type: "string" },
|
|
19
|
+
exemptIndex: { type: "string" },
|
|
20
|
+
}),
|
|
21
|
+
messages: {
|
|
22
|
+
missing: "Table '{{table}}' has no {{column}} column.",
|
|
23
|
+
indexOrder: "Composite index '{{index}}' must lead with {{column}}.",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
create(context) {
|
|
27
|
+
const options = optionsOf(context);
|
|
28
|
+
const column = options.column ?? DEFAULT_COLUMN;
|
|
29
|
+
const tableFactory = options.tableFactory ?? DEFAULT_TABLE_FACTORY;
|
|
30
|
+
const indexFactory = options.indexFactory ?? DEFAULT_INDEX_FACTORY;
|
|
31
|
+
const exemptIndex = new RegExp(options.exemptIndex ?? DEFAULT_EXEMPT, "i");
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
CallExpression(node) {
|
|
35
|
+
if (node.callee.type !== "Identifier" || node.callee.name !== tableFactory) return;
|
|
36
|
+
const columns = node.arguments[1];
|
|
37
|
+
if (!columns || columns.type !== "ObjectExpression") return;
|
|
38
|
+
const tableArg = node.arguments[0];
|
|
39
|
+
const table = tableArg && tableArg.type === "Literal" ? String(tableArg.value) : "table";
|
|
40
|
+
const hasTenant = columns.properties.some(
|
|
41
|
+
(property) =>
|
|
42
|
+
property.type === "Property" &&
|
|
43
|
+
property.key.type === "Identifier" &&
|
|
44
|
+
property.key.name === column,
|
|
45
|
+
);
|
|
46
|
+
if (!hasTenant) {
|
|
47
|
+
context.report({ node, messageId: "missing", data: { table, column } });
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"CallExpression > MemberExpression > CallExpression"(node) {
|
|
51
|
+
if (node.callee.type !== "Identifier" || node.callee.name !== indexFactory) return;
|
|
52
|
+
const parent = node.parent;
|
|
53
|
+
if (parent.type !== "MemberExpression" || parent.property.type !== "Identifier") return;
|
|
54
|
+
if (parent.property.name !== "on") return;
|
|
55
|
+
const nameArg = node.arguments[0];
|
|
56
|
+
const name = nameArg && nameArg.type === "Literal" ? String(nameArg.value) : "";
|
|
57
|
+
if (exemptIndex.test(name)) return;
|
|
58
|
+
const call = parent.parent;
|
|
59
|
+
if (call.type !== "CallExpression" || call.arguments.length < 2) return;
|
|
60
|
+
const first = call.arguments[0];
|
|
61
|
+
const leadsWithTenant =
|
|
62
|
+
first.type === "MemberExpression" &&
|
|
63
|
+
first.property.type === "Identifier" &&
|
|
64
|
+
first.property.name === column;
|
|
65
|
+
if (!leadsWithTenant) {
|
|
66
|
+
context.report({ node: call, messageId: "indexOrder", data: { index: name || "index", column } });
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
};
|