@milkenjoyer/toolchain 0.14.1 → 0.15.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/README.md +13 -17
- package/dist/effect-plugin-BsrW5_nI.mjs +79 -0
- package/dist/effect-plugin.d.mts +2 -3966
- package/dist/effect-plugin.mjs +1 -1
- package/dist/fmt-BaECz64m.mjs +8 -0
- package/dist/fmt.mjs +1 -8
- package/dist/formal.mjs +1 -1
- package/dist/guard-Doqk4Fju.mjs +143 -0
- package/dist/guard.mjs +1 -96
- package/dist/{lint-IhmtnBbx.mjs → lint-CfztB4u8.mjs} +1 -1
- package/dist/lint.mjs +1 -1
- package/dist/standard.mjs +557 -4
- package/package.json +4 -9
- package/src/arithmetic.ts +60 -0
- package/src/compatible.ts +512 -0
- package/src/conformance.ts +56 -0
- package/src/effect-plugin.ts +24 -0
- package/src/effect-tsgo.ts +44 -0
- package/src/exceptions.ts +17 -0
- package/src/fmt.ts +13 -0
- package/src/foldkit-plugin.ts +51 -0
- package/src/guard.ts +307 -0
- package/src/hooks.ts +205 -0
- package/src/index.ts +1 -0
- package/src/knip.ts +25 -0
- package/src/lint.ts +170 -0
- package/src/plugin.ts +21 -0
- package/src/rules/no-comments.ts +36 -0
- package/src/rules/no-process-env.ts +68 -0
- package/src/rules/prefer-accessible-scene-locators.ts +227 -0
- package/src/rules/require-command-answer-names.ts +144 -0
- package/src/rules/require-past-tense-messages.ts +201 -0
- package/src/rules/use-temp-file-scoped.ts +132 -0
- package/src/rules.ts +121 -0
- package/src/shared/foldkit-api.ts +51 -0
- package/src/shared/member-path.ts +106 -0
- package/src/shared/syntax.ts +47 -0
- package/src/standard/alchemy.ts +70 -0
- package/src/standard/checks.ts +10 -1
- package/src/standard/config.ts +84 -0
- package/src/standard/domain-rules.ts +204 -0
- package/src/standard/domain.ts +296 -0
- package/src/standard/infra.ts +486 -0
- package/src/versions.ts +221 -0
- package/dist/effect-plugin-BXUx9PPu.mjs +0 -3561
- package/dist/manifest-xQwgnYI1.mjs +0 -53
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
|
|
3
|
+
import { manifestRanges, PLAIN, type Ranges } from "./manifest.ts";
|
|
4
|
+
import { read, type Finding, type Rule } from "./repo.ts";
|
|
5
|
+
|
|
6
|
+
interface AlchemyEntry {
|
|
7
|
+
readonly file: string;
|
|
8
|
+
readonly field: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly version: string;
|
|
11
|
+
}
|
|
12
|
+
const resolve = (
|
|
13
|
+
name: string,
|
|
14
|
+
written: string,
|
|
15
|
+
groups: ReadonlyMap<string, Ranges>
|
|
16
|
+
): string => {
|
|
17
|
+
if (!written.startsWith("catalog:")) return written;
|
|
18
|
+
const catalog = written.slice(8);
|
|
19
|
+
return (
|
|
20
|
+
(catalog === ""
|
|
21
|
+
? (groups.get("workspaces.catalog")?.[name] ??
|
|
22
|
+
groups.get("catalog")?.[name])
|
|
23
|
+
: (groups.get(`workspaces.catalogs.${catalog}`)?.[name] ??
|
|
24
|
+
groups.get(`catalogs.${catalog}`)?.[name])) ?? written
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
export const alchemyPins: Rule = (repo) =>
|
|
28
|
+
Effect.gen(function* () {
|
|
29
|
+
const root = yield* read(repo, "package.json", manifestRanges);
|
|
30
|
+
if (!root.ok) return root.findings;
|
|
31
|
+
const groups = new Map(root.value);
|
|
32
|
+
const entries: AlchemyEntry[] = [];
|
|
33
|
+
const findings: Finding[] = [];
|
|
34
|
+
for (const file of repo.tracked
|
|
35
|
+
.filter((name) => /(?:^|\/)package\.json$/u.test(name))
|
|
36
|
+
.toSorted()) {
|
|
37
|
+
const input =
|
|
38
|
+
file === "package.json"
|
|
39
|
+
? root
|
|
40
|
+
: yield* read(repo, file, manifestRanges);
|
|
41
|
+
if (!input.ok) {
|
|
42
|
+
findings.push(...input.findings);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
for (const [field, ranges] of input.value) {
|
|
46
|
+
for (const [name, written] of Object.entries(ranges).toSorted(
|
|
47
|
+
([a], [b]) => a.localeCompare(b)
|
|
48
|
+
)) {
|
|
49
|
+
if (name === "alchemy" || name.startsWith("@alchemy.run/"))
|
|
50
|
+
entries.push({
|
|
51
|
+
file,
|
|
52
|
+
field,
|
|
53
|
+
name,
|
|
54
|
+
version: resolve(name, written, groups),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const expected =
|
|
60
|
+
entries.find((entry) => entry.name === "alchemy")?.version ??
|
|
61
|
+
entries[0]?.version;
|
|
62
|
+
for (const entry of entries) {
|
|
63
|
+
if (!PLAIN.test(entry.version) || entry.version !== expected)
|
|
64
|
+
findings.push({
|
|
65
|
+
file: entry.file,
|
|
66
|
+
message: `${entry.field} ${entry.name}: ${entry.version} must match the exact Alchemy version ${expected}`,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return findings;
|
|
70
|
+
});
|
package/src/standard/checks.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { skillNames, taskAgents } from "./agents.ts";
|
|
2
|
-
import {
|
|
2
|
+
import { alchemyPins } from "./alchemy.ts";
|
|
3
|
+
import { lintConfig, lintFmt, tsconfig } from "./config.ts";
|
|
4
|
+
import { domainImports, domainLayout, domainNoThrow } from "./domain-rules.ts";
|
|
3
5
|
import { readyFormal, specs } from "./formal.ts";
|
|
6
|
+
import { infra } from "./infra.ts";
|
|
4
7
|
import type { Rule } from "./repo.ts";
|
|
5
8
|
import { exact, lintPackage, nodeVersion, pin, renovate } from "./toolchain.ts";
|
|
6
9
|
|
|
@@ -16,4 +19,10 @@ export const rules = {
|
|
|
16
19
|
"ready-formal": readyFormal,
|
|
17
20
|
"task-agents": taskAgents,
|
|
18
21
|
"skill-names": skillNames,
|
|
22
|
+
"infra-through-alchemy": infra,
|
|
23
|
+
"alchemy-pins": alchemyPins,
|
|
24
|
+
"domain-layout": domainLayout,
|
|
25
|
+
"domain-imports": domainImports,
|
|
26
|
+
"domain-no-throw": domainNoThrow,
|
|
27
|
+
"lint-config": lintConfig,
|
|
19
28
|
} satisfies Record<string, Rule>;
|
package/src/standard/config.ts
CHANGED
|
@@ -1,9 +1,93 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
|
|
1
3
|
import * as Effect from "effect/Effect";
|
|
2
4
|
import { Path } from "effect/Path";
|
|
3
5
|
import * as Schema from "effect/Schema";
|
|
6
|
+
import type { UserConfig } from "vite-plus";
|
|
4
7
|
|
|
8
|
+
import { fmt } from "../fmt.ts";
|
|
9
|
+
import { ruleFindings } from "../guard.ts";
|
|
5
10
|
import { read, type Finding, type Rule } from "./repo.ts";
|
|
6
11
|
|
|
12
|
+
type StandardConfig = Pick<UserConfig, "lint" | "fmt">;
|
|
13
|
+
const ConfigValue = Schema.declare(
|
|
14
|
+
(value): value is StandardConfig =>
|
|
15
|
+
typeof value === "object" && value !== null
|
|
16
|
+
);
|
|
17
|
+
const ConfigFactory = Schema.declare(
|
|
18
|
+
(
|
|
19
|
+
value
|
|
20
|
+
): value is (env: { command: "build"; mode: "production" }) => unknown =>
|
|
21
|
+
typeof value === "function"
|
|
22
|
+
);
|
|
23
|
+
const parseConfig = Schema.decodeUnknownSync(ConfigValue);
|
|
24
|
+
const parseModule = Schema.decodeUnknownSync(
|
|
25
|
+
Schema.Struct({ default: Schema.Unknown })
|
|
26
|
+
);
|
|
27
|
+
export const lintConfig: Rule = (repo) =>
|
|
28
|
+
Effect.gen(function* () {
|
|
29
|
+
const path = yield* Path;
|
|
30
|
+
const configs = repo.tracked.filter((file) =>
|
|
31
|
+
/^vite\.config\.(?:ts|mts|js|mjs)$/u.test(file)
|
|
32
|
+
);
|
|
33
|
+
const findings: Finding[] = [];
|
|
34
|
+
for (const file of configs) {
|
|
35
|
+
const input = yield* read(repo, file, (text) => text);
|
|
36
|
+
if (!input.ok) {
|
|
37
|
+
findings.push(...input.findings);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const loaded = yield* Effect.tryPromise({
|
|
41
|
+
try: async () => {
|
|
42
|
+
const imported: unknown = await import(path.join(repo.root, file));
|
|
43
|
+
const module = parseModule(imported);
|
|
44
|
+
const exported = await module.default;
|
|
45
|
+
const config = parseConfig(
|
|
46
|
+
Schema.is(ConfigFactory)(exported)
|
|
47
|
+
? await exported({ command: "build", mode: "production" })
|
|
48
|
+
: exported
|
|
49
|
+
);
|
|
50
|
+
const messages =
|
|
51
|
+
config.lint === undefined
|
|
52
|
+
? ["configure lint with the shared lint() preset"]
|
|
53
|
+
: [...ruleFindings(config.lint)];
|
|
54
|
+
const expected = fmt();
|
|
55
|
+
const actual = config.fmt;
|
|
56
|
+
const keys = new Set([
|
|
57
|
+
...Object.keys(expected),
|
|
58
|
+
...Object.keys(actual ?? {}),
|
|
59
|
+
]);
|
|
60
|
+
keys.delete("ignorePatterns");
|
|
61
|
+
for (const key of keys) {
|
|
62
|
+
if (
|
|
63
|
+
!isDeepStrictEqual(
|
|
64
|
+
Reflect.get(expected, key),
|
|
65
|
+
actual === undefined ? undefined : Reflect.get(actual, key)
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
messages.push(`set fmt.${key} to the shared fmt() value`);
|
|
69
|
+
}
|
|
70
|
+
if (
|
|
71
|
+
!(expected.ignorePatterns ?? []).every(
|
|
72
|
+
(pattern) => actual?.ignorePatterns?.includes(pattern) === true
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
messages.push("include every shared fmt() ignore pattern");
|
|
76
|
+
return messages;
|
|
77
|
+
},
|
|
78
|
+
catch: (error) =>
|
|
79
|
+
`cannot import config: ${String(error instanceof Error ? error.message : error).split("\n")[0]}`,
|
|
80
|
+
}).pipe(
|
|
81
|
+
Effect.match({
|
|
82
|
+
onFailure: (message) => [message],
|
|
83
|
+
onSuccess: (messages) => messages,
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
findings.push(...loaded.map((message) => ({ file, message })));
|
|
87
|
+
}
|
|
88
|
+
return findings;
|
|
89
|
+
});
|
|
90
|
+
|
|
7
91
|
export const lintFmt: Rule = (repo) =>
|
|
8
92
|
Effect.gen(function* () {
|
|
9
93
|
const path = yield* Path;
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Schema from "effect/Schema";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
BLOCK_KINDS,
|
|
8
|
+
domain,
|
|
9
|
+
isRelative,
|
|
10
|
+
targetFile,
|
|
11
|
+
within,
|
|
12
|
+
type DomainFile,
|
|
13
|
+
} from "./domain.ts";
|
|
14
|
+
import { read, type Finding, type Rule } from "./repo.ts";
|
|
15
|
+
|
|
16
|
+
const KINDS = [
|
|
17
|
+
...new Set([...BLOCK_KINDS.values()].flat()),
|
|
18
|
+
"middleware",
|
|
19
|
+
"id",
|
|
20
|
+
];
|
|
21
|
+
const KIND_FILE = new RegExp(
|
|
22
|
+
`^[A-Z][A-Za-z0-9]*\\.(?<kind>${KINDS.join("|")})\\.ts$`,
|
|
23
|
+
"u"
|
|
24
|
+
);
|
|
25
|
+
const SUFFIXED = /^[^.]+\..+\.ts$/u;
|
|
26
|
+
const ALLOWED_IMPORT = /^(?:@alchemy\.run\/domain|effect)(?:\/|$)/u;
|
|
27
|
+
const DomainImports = Schema.fromJsonString(
|
|
28
|
+
Schema.Struct({
|
|
29
|
+
standard: Schema.optionalKey(
|
|
30
|
+
Schema.Struct({
|
|
31
|
+
domainImports: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
32
|
+
})
|
|
33
|
+
),
|
|
34
|
+
})
|
|
35
|
+
);
|
|
36
|
+
const parseDomainImports = Schema.decodeSync(DomainImports);
|
|
37
|
+
const add = (index: Map<string, string[]>, key: string, file: string) =>
|
|
38
|
+
index.set(key, [...(index.get(key) ?? []), file]);
|
|
39
|
+
const packageName = (specifier: string): string =>
|
|
40
|
+
specifier
|
|
41
|
+
.split("/")
|
|
42
|
+
.slice(0, specifier.startsWith("@") ? 2 : 1)
|
|
43
|
+
.join("/");
|
|
44
|
+
|
|
45
|
+
export const layoutFindings = (
|
|
46
|
+
files: ReadonlyArray<DomainFile>,
|
|
47
|
+
paths: ReadonlyArray<string>
|
|
48
|
+
): ReadonlyArray<Finding> => {
|
|
49
|
+
const sources = files.filter((entry) => entry.source);
|
|
50
|
+
const parsed = new Map(
|
|
51
|
+
sources.map(({ file, parsed: module }) => [file, module])
|
|
52
|
+
);
|
|
53
|
+
const importers = new Map<string, string[]>();
|
|
54
|
+
const recorders = new Map<string, string[]>();
|
|
55
|
+
const rejecters = new Map<string, string[]>();
|
|
56
|
+
const findings: Finding[] = [];
|
|
57
|
+
for (const { root, file, parsed: module } of sources) {
|
|
58
|
+
for (const specifier of module.imports) {
|
|
59
|
+
if (!isRelative(specifier)) continue;
|
|
60
|
+
const target = targetFile(file, specifier, paths);
|
|
61
|
+
if (within(root, target)) add(importers, target, file);
|
|
62
|
+
}
|
|
63
|
+
for (const events of module.recorders)
|
|
64
|
+
for (const target of new Set(events.map((entry) => entry.file)))
|
|
65
|
+
add(recorders, target, file);
|
|
66
|
+
for (const { rejects } of module.commands) {
|
|
67
|
+
for (const target of new Set(rejects.map((entry) => entry.file)))
|
|
68
|
+
add(rejecters, target, file);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (const { file, parsed: module } of sources) {
|
|
72
|
+
const name = posix.basename(file);
|
|
73
|
+
const { kind } = KIND_FILE.exec(name)?.groups ?? {};
|
|
74
|
+
const { blocks } = module;
|
|
75
|
+
if (kind === undefined) {
|
|
76
|
+
if (SUFFIXED.test(name) || blocks.length > 0)
|
|
77
|
+
findings.push({
|
|
78
|
+
file,
|
|
79
|
+
message: "a Domain kind file is named <Noun>.<kind>.ts in PascalCase",
|
|
80
|
+
});
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const misplaced = [
|
|
84
|
+
...new Set(
|
|
85
|
+
blocks.filter((block) => !(BLOCK_KINDS.get(block) ?? []).includes(kind))
|
|
86
|
+
),
|
|
87
|
+
];
|
|
88
|
+
if (misplaced.length > 0)
|
|
89
|
+
findings.push({
|
|
90
|
+
file,
|
|
91
|
+
message: `a .${kind}.ts file does not hold ${misplaced.map((block) => `Domain.${block}`).join(", ")}`,
|
|
92
|
+
});
|
|
93
|
+
const [primary, ...extra] = blocks.filter(
|
|
94
|
+
(block) => BLOCK_KINDS.get(block)?.[0] === kind
|
|
95
|
+
);
|
|
96
|
+
if (primary !== undefined && extra.length > 0)
|
|
97
|
+
findings.push({
|
|
98
|
+
file,
|
|
99
|
+
message: `a .${kind}.ts file holds one Domain.${primary}, not ${extra.length + 1}`,
|
|
100
|
+
});
|
|
101
|
+
const recordedBy = recorders.get(file) ?? [];
|
|
102
|
+
if (kind === "event" && recordedBy.length < 2)
|
|
103
|
+
findings.push({
|
|
104
|
+
file,
|
|
105
|
+
message: `a standalone event is recorded by ${recordedBy.length} block(s); it lives in the file of the block that records it, unless more than one block records it`,
|
|
106
|
+
});
|
|
107
|
+
const [returnedBy, ...others] = rejecters.get(file) ?? [];
|
|
108
|
+
if (
|
|
109
|
+
kind === "rejection" &&
|
|
110
|
+
returnedBy !== undefined &&
|
|
111
|
+
others.length === 0 &&
|
|
112
|
+
(importers.get(file) ?? []).every((user) =>
|
|
113
|
+
(parsed.get(user)?.blocks ?? []).some(
|
|
114
|
+
(block) => block === "Command" || block === "Aggregate"
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
findings.push({
|
|
119
|
+
file,
|
|
120
|
+
message: `a standalone rejection is returned only by ${returnedBy}; it lives in that command's file`,
|
|
121
|
+
});
|
|
122
|
+
if (
|
|
123
|
+
kind === "aggregate" &&
|
|
124
|
+
module.aggregateIds.some(
|
|
125
|
+
(id) =>
|
|
126
|
+
id === undefined ||
|
|
127
|
+
id.file === file ||
|
|
128
|
+
!id.file.endsWith(".id.ts") ||
|
|
129
|
+
!paths.includes(id.file)
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
findings.push({
|
|
133
|
+
file,
|
|
134
|
+
message: "declare the aggregate's id in an .id.ts file and import it",
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return findings;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const importFindings = (
|
|
141
|
+
files: ReadonlyArray<DomainFile>,
|
|
142
|
+
paths: ReadonlyArray<string>,
|
|
143
|
+
allowed: ReadonlyArray<string> = []
|
|
144
|
+
): ReadonlyArray<Finding> =>
|
|
145
|
+
files
|
|
146
|
+
.filter((entry) => entry.source)
|
|
147
|
+
.flatMap(({ root, file, parsed }) =>
|
|
148
|
+
parsed.imports.flatMap((specifier) => {
|
|
149
|
+
const target = isRelative(specifier)
|
|
150
|
+
? targetFile(file, specifier, paths)
|
|
151
|
+
: undefined;
|
|
152
|
+
const ownTarget =
|
|
153
|
+
target !== undefined &&
|
|
154
|
+
within(root, target) &&
|
|
155
|
+
!paths.some(
|
|
156
|
+
(path) =>
|
|
157
|
+
path.endsWith("/package.json") &&
|
|
158
|
+
posix.dirname(path) !== root &&
|
|
159
|
+
within(root, path) &&
|
|
160
|
+
within(posix.dirname(path), target)
|
|
161
|
+
);
|
|
162
|
+
return ownTarget ||
|
|
163
|
+
(target === undefined &&
|
|
164
|
+
(ALLOWED_IMPORT.test(specifier) ||
|
|
165
|
+
allowed.includes(packageName(specifier))))
|
|
166
|
+
? []
|
|
167
|
+
: [
|
|
168
|
+
{
|
|
169
|
+
file,
|
|
170
|
+
message: `imports ${specifier}; a domain package imports only @alchemy.run/domain, effect and its own files${allowed.length > 0 ? ", plus configured domainImports" : ""}`,
|
|
171
|
+
},
|
|
172
|
+
];
|
|
173
|
+
})
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
export const domainLayout: Rule = (repo) =>
|
|
177
|
+
Effect.gen(function* () {
|
|
178
|
+
const model = yield* domain(repo);
|
|
179
|
+
return [...model.findings, ...layoutFindings(model.files, repo.tracked)];
|
|
180
|
+
});
|
|
181
|
+
export const domainImports: Rule = (repo) =>
|
|
182
|
+
Effect.gen(function* () {
|
|
183
|
+
const model = yield* domain(repo);
|
|
184
|
+
if (model.files.length === 0) return [];
|
|
185
|
+
const input = yield* read(repo, "package.json", parseDomainImports);
|
|
186
|
+
if (!input.ok) return input.findings;
|
|
187
|
+
return importFindings(
|
|
188
|
+
model.files,
|
|
189
|
+
repo.tracked,
|
|
190
|
+
input.value.standard?.domainImports ?? []
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
export const domainNoThrow: Rule = (repo) =>
|
|
194
|
+
Effect.gen(function* () {
|
|
195
|
+
const model = yield* domain(repo);
|
|
196
|
+
return model.files
|
|
197
|
+
.filter((entry) => entry.source)
|
|
198
|
+
.flatMap(({ file, parsed }) =>
|
|
199
|
+
parsed.throws.map((line) => ({
|
|
200
|
+
file,
|
|
201
|
+
message: `line ${line}: return a Domain.Rejection or fail with a Schema.TaggedError instead of throwing`,
|
|
202
|
+
}))
|
|
203
|
+
);
|
|
204
|
+
});
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import {
|
|
5
|
+
parseSync,
|
|
6
|
+
Visitor,
|
|
7
|
+
type Expression,
|
|
8
|
+
type ObjectExpression,
|
|
9
|
+
} from "oxc-parser";
|
|
10
|
+
|
|
11
|
+
import { read, type Finding, type Repo } from "./repo.ts";
|
|
12
|
+
|
|
13
|
+
interface Reference {
|
|
14
|
+
readonly file: string;
|
|
15
|
+
}
|
|
16
|
+
interface CommandDeclaration {
|
|
17
|
+
readonly rejects: ReadonlyArray<Reference>;
|
|
18
|
+
}
|
|
19
|
+
export interface ParsedModule {
|
|
20
|
+
readonly blocks: ReadonlyArray<string>;
|
|
21
|
+
readonly imports: ReadonlyArray<string>;
|
|
22
|
+
readonly commands: ReadonlyArray<CommandDeclaration>;
|
|
23
|
+
readonly recorders: ReadonlyArray<ReadonlyArray<Reference>>;
|
|
24
|
+
readonly aggregateIds: ReadonlyArray<Reference | undefined>;
|
|
25
|
+
readonly throws: ReadonlyArray<number>;
|
|
26
|
+
}
|
|
27
|
+
export interface DomainFile {
|
|
28
|
+
readonly root: string;
|
|
29
|
+
readonly file: string;
|
|
30
|
+
readonly source: boolean;
|
|
31
|
+
readonly parsed: ParsedModule;
|
|
32
|
+
}
|
|
33
|
+
interface DomainRepo {
|
|
34
|
+
readonly files: ReadonlyArray<DomainFile>;
|
|
35
|
+
readonly findings: ReadonlyArray<Finding>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const BLOCK_KINDS = new Map([
|
|
39
|
+
["Aggregate", ["aggregate"]],
|
|
40
|
+
["Command", ["command"]],
|
|
41
|
+
["Event", ["event", "command", "view", "stream"]],
|
|
42
|
+
["Rejection", ["rejection", "command"]],
|
|
43
|
+
["View", ["view"]],
|
|
44
|
+
["Policy", ["policy"]],
|
|
45
|
+
["Saga", ["saga"]],
|
|
46
|
+
["Projection", ["projection"]],
|
|
47
|
+
["Stream", ["stream"]],
|
|
48
|
+
["ExternalSystem", ["external"]],
|
|
49
|
+
["Api", ["api"]],
|
|
50
|
+
["Model", ["model"]],
|
|
51
|
+
["RequestContext", ["context"]],
|
|
52
|
+
]);
|
|
53
|
+
const DOMAIN = "@alchemy.run/domain";
|
|
54
|
+
const TEST_FILE = /(?:^|\/)tests?\/|\.(?:test|proof)\.ts$/u;
|
|
55
|
+
export const isRelative = (specifier: string): boolean =>
|
|
56
|
+
specifier.startsWith("./") || specifier.startsWith("../");
|
|
57
|
+
export const within = (directory: string, file: string): boolean =>
|
|
58
|
+
directory === "."
|
|
59
|
+
? file !== ".." && !file.startsWith("../") && !posix.isAbsolute(file)
|
|
60
|
+
: file.startsWith(`${directory}/`);
|
|
61
|
+
export const targetFile = (
|
|
62
|
+
file: string,
|
|
63
|
+
from: string,
|
|
64
|
+
paths: ReadonlyArray<string>
|
|
65
|
+
): string => {
|
|
66
|
+
const target = posix.join(posix.dirname(file), from);
|
|
67
|
+
return (
|
|
68
|
+
[
|
|
69
|
+
target,
|
|
70
|
+
target.replace(/\.js$/u, ".ts"),
|
|
71
|
+
`${target}.ts`,
|
|
72
|
+
`${target}/index.ts`,
|
|
73
|
+
].find((candidate) => paths.includes(candidate)) ?? target
|
|
74
|
+
);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const parseModule = (
|
|
78
|
+
file: string,
|
|
79
|
+
source: string,
|
|
80
|
+
paths: ReadonlyArray<string> = []
|
|
81
|
+
): ParsedModule => {
|
|
82
|
+
const result = parseSync(file, source);
|
|
83
|
+
const roots = new Set<string>();
|
|
84
|
+
const modules = new Map<string, string>();
|
|
85
|
+
const constructors = new Map<string, string>();
|
|
86
|
+
const relatives = new Map<string, Reference>();
|
|
87
|
+
const relativeModules = new Map<string, string>();
|
|
88
|
+
for (const statement of result.program.body) {
|
|
89
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
90
|
+
if (statement.importKind === "type") continue;
|
|
91
|
+
const from = statement.source.value;
|
|
92
|
+
const subpath = from.startsWith(`${DOMAIN}/`)
|
|
93
|
+
? from.slice(DOMAIN.length + 1)
|
|
94
|
+
: undefined;
|
|
95
|
+
const kind =
|
|
96
|
+
subpath !== undefined && BLOCK_KINDS.has(subpath) ? subpath : undefined;
|
|
97
|
+
const target = isRelative(from) ? targetFile(file, from, paths) : undefined;
|
|
98
|
+
for (const specifier of statement.specifiers) {
|
|
99
|
+
const local = specifier.local.name;
|
|
100
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
101
|
+
if (from === DOMAIN) roots.add(local);
|
|
102
|
+
else if (kind !== undefined) modules.set(local, kind);
|
|
103
|
+
else if (target !== undefined) relativeModules.set(local, target);
|
|
104
|
+
} else if (specifier.type === "ImportDefaultSpecifier") {
|
|
105
|
+
if (target !== undefined) relatives.set(local, { file: target });
|
|
106
|
+
} else {
|
|
107
|
+
if (specifier.importKind === "type") continue;
|
|
108
|
+
const imported =
|
|
109
|
+
specifier.imported.type === "Identifier"
|
|
110
|
+
? specifier.imported.name
|
|
111
|
+
: specifier.imported.value;
|
|
112
|
+
if (from === DOMAIN && BLOCK_KINDS.has(imported))
|
|
113
|
+
constructors.set(local, imported);
|
|
114
|
+
else if (kind !== undefined && imported === "make")
|
|
115
|
+
constructors.set(local, kind);
|
|
116
|
+
else if (target !== undefined) relatives.set(local, { file: target });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const imports = [
|
|
121
|
+
...result.module.staticImports.map((entry) => entry.moduleRequest.value),
|
|
122
|
+
...result.module.staticExports.flatMap((entry) =>
|
|
123
|
+
entry.entries.flatMap((binding) =>
|
|
124
|
+
binding.moduleRequest === null ? [] : [binding.moduleRequest.value]
|
|
125
|
+
)
|
|
126
|
+
),
|
|
127
|
+
...result.module.dynamicImports.map(({ moduleRequest }) =>
|
|
128
|
+
source
|
|
129
|
+
.slice(moduleRequest.start, moduleRequest.end)
|
|
130
|
+
.replaceAll(/^["'`]|["'`]$/gu, "")
|
|
131
|
+
),
|
|
132
|
+
];
|
|
133
|
+
const blockOf = (callee: Expression): string | undefined => {
|
|
134
|
+
if (callee.type === "Identifier") return constructors.get(callee.name);
|
|
135
|
+
if (
|
|
136
|
+
callee.type !== "MemberExpression" ||
|
|
137
|
+
callee.computed ||
|
|
138
|
+
callee.object.type !== "Identifier" ||
|
|
139
|
+
callee.property.type !== "Identifier"
|
|
140
|
+
)
|
|
141
|
+
return undefined;
|
|
142
|
+
const { name } = callee.property;
|
|
143
|
+
return roots.has(callee.object.name) && BLOCK_KINDS.has(name)
|
|
144
|
+
? name
|
|
145
|
+
: name === "make"
|
|
146
|
+
? modules.get(callee.object.name)
|
|
147
|
+
: undefined;
|
|
148
|
+
};
|
|
149
|
+
const reference = (entry: Expression): ReadonlyArray<Reference> => {
|
|
150
|
+
if (entry.type === "Identifier")
|
|
151
|
+
return [relatives.get(entry.name) ?? { file }];
|
|
152
|
+
if (
|
|
153
|
+
entry.type !== "MemberExpression" ||
|
|
154
|
+
entry.computed ||
|
|
155
|
+
entry.object.type !== "Identifier" ||
|
|
156
|
+
entry.property.type !== "Identifier"
|
|
157
|
+
)
|
|
158
|
+
return [];
|
|
159
|
+
const target = relativeModules.get(entry.object.name);
|
|
160
|
+
return target === undefined ? [] : [{ file: target }];
|
|
161
|
+
};
|
|
162
|
+
const option = (
|
|
163
|
+
options: ObjectExpression,
|
|
164
|
+
key: string
|
|
165
|
+
): Expression | undefined => {
|
|
166
|
+
const property = options.properties.find(
|
|
167
|
+
(property) =>
|
|
168
|
+
property.type === "Property" &&
|
|
169
|
+
!property.computed &&
|
|
170
|
+
(property.key.type === "Identifier"
|
|
171
|
+
? property.key.name
|
|
172
|
+
: property.key.type === "Literal"
|
|
173
|
+
? property.key.value
|
|
174
|
+
: undefined) === key
|
|
175
|
+
);
|
|
176
|
+
return property?.type === "Property" ? property.value : undefined;
|
|
177
|
+
};
|
|
178
|
+
const declared = (
|
|
179
|
+
options: ObjectExpression,
|
|
180
|
+
key: string
|
|
181
|
+
): ReadonlyArray<Reference> => {
|
|
182
|
+
const value = option(options, key);
|
|
183
|
+
return value?.type === "ArrayExpression"
|
|
184
|
+
? value.elements.flatMap((entry) =>
|
|
185
|
+
entry === null || entry.type === "SpreadElement"
|
|
186
|
+
? []
|
|
187
|
+
: reference(entry)
|
|
188
|
+
)
|
|
189
|
+
: [];
|
|
190
|
+
};
|
|
191
|
+
const blocks: string[] = [];
|
|
192
|
+
const commands: CommandDeclaration[] = [];
|
|
193
|
+
const recorders: Reference[][] = [];
|
|
194
|
+
const aggregateIds: (Reference | undefined)[] = [];
|
|
195
|
+
const throws: number[] = [];
|
|
196
|
+
new Visitor({
|
|
197
|
+
CallExpression(node) {
|
|
198
|
+
const block = blockOf(node.callee);
|
|
199
|
+
const [, options] = node.arguments;
|
|
200
|
+
if (block !== undefined) {
|
|
201
|
+
blocks.push(block);
|
|
202
|
+
if (block === "Aggregate") {
|
|
203
|
+
const id =
|
|
204
|
+
options?.type === "ObjectExpression"
|
|
205
|
+
? option(options, "id")
|
|
206
|
+
: undefined;
|
|
207
|
+
aggregateIds.push(id === undefined ? undefined : reference(id)[0]);
|
|
208
|
+
}
|
|
209
|
+
if (options?.type === "ObjectExpression") {
|
|
210
|
+
if (block === "Command" || block === "View" || block === "Stream")
|
|
211
|
+
recorders.push([
|
|
212
|
+
...declared(options, block === "Stream" ? "entries" : "events"),
|
|
213
|
+
]);
|
|
214
|
+
if (block === "Command")
|
|
215
|
+
commands.push({
|
|
216
|
+
rejects: declared(options, "rejects"),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
ThrowStatement(node) {
|
|
222
|
+
throws.push(source.slice(0, node.start).split("\n").length);
|
|
223
|
+
},
|
|
224
|
+
}).visit(result.program);
|
|
225
|
+
return {
|
|
226
|
+
blocks: blocks.toSorted(),
|
|
227
|
+
imports,
|
|
228
|
+
commands,
|
|
229
|
+
recorders,
|
|
230
|
+
aggregateIds,
|
|
231
|
+
throws,
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
export const sourceFiles = (
|
|
236
|
+
paths: ReadonlyArray<string>,
|
|
237
|
+
parsed: ReadonlyMap<string, ParsedModule>
|
|
238
|
+
): ReadonlyArray<DomainFile> => {
|
|
239
|
+
const manifests = new Set(
|
|
240
|
+
paths
|
|
241
|
+
.filter((file) => posix.basename(file) === "package.json")
|
|
242
|
+
.map((file) => posix.dirname(file))
|
|
243
|
+
);
|
|
244
|
+
const files = paths.flatMap((file): DomainFile[] => {
|
|
245
|
+
const module = parsed.get(file);
|
|
246
|
+
if (module === undefined) return [];
|
|
247
|
+
let root = posix.dirname(file);
|
|
248
|
+
while (root !== "." && !manifests.has(root)) root = posix.dirname(root);
|
|
249
|
+
return manifests.has(root)
|
|
250
|
+
? [
|
|
251
|
+
{
|
|
252
|
+
root,
|
|
253
|
+
file,
|
|
254
|
+
source:
|
|
255
|
+
file.endsWith(".ts") &&
|
|
256
|
+
!TEST_FILE.test(
|
|
257
|
+
root === "." ? file : file.slice(root.length + 1)
|
|
258
|
+
),
|
|
259
|
+
parsed: module,
|
|
260
|
+
},
|
|
261
|
+
]
|
|
262
|
+
: [];
|
|
263
|
+
});
|
|
264
|
+
const roots = new Set(
|
|
265
|
+
files
|
|
266
|
+
.filter((entry) => entry.source && entry.parsed.blocks.length > 0)
|
|
267
|
+
.map((entry) => entry.root)
|
|
268
|
+
);
|
|
269
|
+
return files.filter((entry) => roots.has(entry.root));
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const cache = new WeakMap<Repo, DomainRepo>();
|
|
273
|
+
export const domain = Effect.fn("standard.domain")(function* (repo: Repo) {
|
|
274
|
+
const cached = cache.get(repo);
|
|
275
|
+
if (cached !== undefined) return cached;
|
|
276
|
+
const parsed = new Map<string, ParsedModule>();
|
|
277
|
+
const findings: Finding[] = [];
|
|
278
|
+
for (const file of repo.tracked.filter((name) =>
|
|
279
|
+
/\.[cm]?[jt]sx?$/u.test(name)
|
|
280
|
+
)) {
|
|
281
|
+
const input = yield* read(repo, file, (text) =>
|
|
282
|
+
parseModule(file, text, repo.tracked)
|
|
283
|
+
);
|
|
284
|
+
if (input.ok) parsed.set(file, input.value);
|
|
285
|
+
else findings.push(...input.findings);
|
|
286
|
+
}
|
|
287
|
+
const files = sourceFiles(repo.tracked, parsed);
|
|
288
|
+
const value = {
|
|
289
|
+
files,
|
|
290
|
+
findings: findings.filter((finding) =>
|
|
291
|
+
files.some((entry) => within(entry.root, finding.file))
|
|
292
|
+
),
|
|
293
|
+
};
|
|
294
|
+
cache.set(repo, value);
|
|
295
|
+
return value;
|
|
296
|
+
});
|