@shivaedev/quality 0.53.0 → 0.54.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 +30 -0
- package/README.md +10 -7
- package/dist/adapters/knip/entryPointFindings.d.ts.map +1 -1
- package/dist/adapters/knip/entryPointFindings.js +14 -8
- package/dist/adapters/knip/entryPointFindings.js.map +1 -1
- package/dist/baseline/cleanupMergeFiles.d.ts +4 -0
- package/dist/baseline/cleanupMergeFiles.d.ts.map +1 -0
- package/dist/baseline/cleanupMergeFiles.js +23 -0
- package/dist/baseline/cleanupMergeFiles.js.map +1 -0
- package/dist/biome/recognizeDiagnostics.d.ts.map +1 -1
- package/dist/biome/recognizeDiagnostics.js +4 -5
- package/dist/biome/recognizeDiagnostics.js.map +1 -1
- package/dist/cli/changedRanges.d.ts +2 -0
- package/dist/cli/changedRanges.d.ts.map +1 -0
- package/dist/cli/changedRanges.js +23 -0
- package/dist/cli/changedRanges.js.map +1 -0
- package/dist/cli/fix.d.ts.map +1 -1
- package/dist/cli/fix.js +12 -12
- package/dist/cli/fix.js.map +1 -1
- package/dist/cli/program.d.ts.map +1 -1
- package/dist/cli/program.js +2 -0
- package/dist/cli/program.js.map +1 -1
- package/dist/imports/adminBindings.d.ts +8 -0
- package/dist/imports/adminBindings.d.ts.map +1 -0
- package/dist/imports/adminBindings.js +36 -0
- package/dist/imports/adminBindings.js.map +1 -0
- package/dist/imports/adminEvidence.d.ts +6 -0
- package/dist/imports/adminEvidence.d.ts.map +1 -0
- package/dist/imports/adminEvidence.js +46 -0
- package/dist/imports/adminEvidence.js.map +1 -0
- package/dist/imports/adminFindings.d.ts +4 -0
- package/dist/imports/adminFindings.d.ts.map +1 -0
- package/dist/imports/adminFindings.js +55 -0
- package/dist/imports/adminFindings.js.map +1 -0
- package/dist/imports/departures.d.ts.map +1 -1
- package/dist/imports/departures.js +13 -0
- package/dist/imports/departures.js.map +1 -1
- package/dist/imports/nodePlatformBarrels.d.ts +3 -0
- package/dist/imports/nodePlatformBarrels.d.ts.map +1 -0
- package/dist/imports/nodePlatformBarrels.js +46 -0
- package/dist/imports/nodePlatformBarrels.js.map +1 -0
- package/dist/naming/ownExports.d.ts +1 -0
- package/dist/naming/ownExports.d.ts.map +1 -1
- package/dist/naming/ownExports.js +4 -1
- package/dist/naming/ownExports.js.map +1 -1
- package/dist/rules/builtIn.d.ts +1 -1
- package/dist/rules/builtIn.d.ts.map +1 -1
- package/dist/rules/builtIn.js +2 -0
- package/dist/rules/builtIn.js.map +1 -1
- package/dist/rules/files/namedAfterExport.d.ts.map +1 -1
- package/dist/rules/files/namedAfterExport.js +10 -3
- package/dist/rules/files/namedAfterExport.js.map +1 -1
- package/dist/rules/imports/adminPaths.d.ts +2 -0
- package/dist/rules/imports/adminPaths.d.ts.map +1 -0
- package/dist/rules/imports/adminPaths.js +11 -0
- package/dist/rules/imports/adminPaths.js.map +1 -0
- package/dist/rules/imports/aliased.js +1 -1
- package/dist/rules/imports/aliased.js.map +1 -1
- package/dist/rules/imports/runtime.d.ts.map +1 -1
- package/dist/rules/imports/runtime.js +3 -2
- package/dist/rules/imports/runtime.js.map +1 -1
- package/dist/rules/stories/httpStatus.d.ts.map +1 -1
- package/dist/rules/stories/httpStatus.js +11 -2
- package/dist/rules/stories/httpStatus.js.map +1 -1
- package/package.json +3 -3
- package/src/adapters/knip/entryPointFindings.ts +15 -8
- package/src/baseline/cleanupMergeFiles.ts +27 -0
- package/src/biome/recognizeDiagnostics.ts +6 -7
- package/src/cli/changedRanges.ts +24 -0
- package/src/cli/fix.ts +13 -14
- package/src/cli/program.ts +2 -0
- package/src/imports/adminBindings.ts +45 -0
- package/src/imports/adminEvidence.ts +52 -0
- package/src/imports/adminFindings.ts +65 -0
- package/src/imports/departures.ts +16 -0
- package/src/imports/nodePlatformBarrels.ts +51 -0
- package/src/naming/ownExports.ts +5 -1
- package/src/rules/builtIn.ts +2 -0
- package/src/rules/files/namedAfterExport.ts +10 -3
- package/src/rules/imports/adminPaths.ts +12 -0
- package/src/rules/imports/aliased.ts +1 -1
- package/src/rules/imports/runtime.ts +3 -2
- package/src/rules/stories/httpStatus.ts +13 -2
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { Clock, Effect, FileSystem, Option } from "effect";
|
|
3
|
+
import { SetupFailure } from "#failure.ts";
|
|
4
|
+
import { gitOrFail } from "#git/command.ts";
|
|
5
|
+
|
|
6
|
+
const STALE_AFTER_MS = 5 * 60_000;
|
|
7
|
+
|
|
8
|
+
export const cleanupMergeFiles = Effect.fn("Baseline.cleanupMergeFiles")(
|
|
9
|
+
function* (cwd: string, inputs: readonly string[]) {
|
|
10
|
+
const fs = yield* FileSystem.FileSystem;
|
|
11
|
+
const root = (yield* gitOrFail(cwd, ["rev-parse", "--show-toplevel"])).trim();
|
|
12
|
+
const active = new Set(inputs.map((path) => resolve(cwd, path)));
|
|
13
|
+
const untracked = yield* gitOrFail(root, ["ls-files", "--others", "--full-name", "-z", "--", ":(top,glob).merge_file_*"]);
|
|
14
|
+
const cutoff = (yield* Clock.currentTimeMillis) - STALE_AFTER_MS;
|
|
15
|
+
for (const name of untracked.split("\0").filter(Boolean)) {
|
|
16
|
+
const path = resolve(root, name);
|
|
17
|
+
if (active.has(path)) {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const info = yield* fs.stat(path);
|
|
21
|
+
if (info.type === "File" && Option.exists(info.mtime, (mtime) => mtime.getTime() < cutoff)) {
|
|
22
|
+
yield* fs.remove(path);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
Effect.mapError((failure) => new SetupFailure({ cause: failure, message: failure.message })),
|
|
27
|
+
);
|
|
@@ -21,17 +21,16 @@ function generatorParameter(node: ts.Node): boolean {
|
|
|
21
21
|
|
|
22
22
|
function tagHandler(node: ts.Node, syntax: Syntax): boolean {
|
|
23
23
|
const property = node.parent;
|
|
24
|
-
if (
|
|
24
|
+
if (
|
|
25
|
+
!(ts.isPropertyAssignment(property) || ts.isMethodDeclaration(property))
|
|
26
|
+
|| property.name !== node
|
|
27
|
+
|| !ts.isObjectLiteralExpression(property.parent)
|
|
28
|
+
) {
|
|
25
29
|
return false;
|
|
26
30
|
}
|
|
27
31
|
const handlers = property.parent;
|
|
28
32
|
const call = handlers.parent;
|
|
29
|
-
return (
|
|
30
|
-
nativeEffectCall(call, "catchTags", syntax)
|
|
31
|
-
&& call.arguments.includes(handlers)
|
|
32
|
-
&& ts.isArrowFunction(property.initializer)
|
|
33
|
-
&& nativeEffectCall(property.initializer.body, "fail", syntax)
|
|
34
|
-
);
|
|
33
|
+
return nativeEffectCall(call, "catchTags", syntax) && call.arguments.includes(handlers);
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
function misclassified(diagnostic: Diagnostic, syntax: Syntax): boolean {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function position(text: string, offset: number): string {
|
|
2
|
+
const lines = text.slice(0, offset).split("\n");
|
|
3
|
+
return `${lines.length}:${[...(lines.at(-1) ?? "")].length + 1}`;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
function range(before: string, after: string): string {
|
|
7
|
+
let start = 0;
|
|
8
|
+
while (start < before.length && start < after.length && before[start] === after[start]) {
|
|
9
|
+
start += 1;
|
|
10
|
+
}
|
|
11
|
+
let end = before.length;
|
|
12
|
+
let otherEnd = after.length;
|
|
13
|
+
while (end > start && otherEnd > start && before[end - 1] === after[otherEnd - 1]) {
|
|
14
|
+
end -= 1;
|
|
15
|
+
otherEnd -= 1;
|
|
16
|
+
}
|
|
17
|
+
return `${position(before, start)}-${position(before, end)}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function changedRanges(before: ReadonlyMap<string, string>, after: ReadonlyMap<string, string>, pass: string): readonly string[] {
|
|
21
|
+
return [...new Set([...before.keys(), ...after.keys()])]
|
|
22
|
+
.filter((path) => before.get(path) !== after.get(path))
|
|
23
|
+
.map((path) => `${path}:${range(before.get(path) ?? "", after.get(path) ?? "")} (${pass})`);
|
|
24
|
+
}
|
package/src/cli/fix.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { relative } from "node:path";
|
|
2
2
|
import { Console, Effect, FileSystem } from "effect";
|
|
3
3
|
import { biomeReport } from "#biome/report.ts";
|
|
4
|
+
import { changedRanges } from "#cli/changedRanges.ts";
|
|
4
5
|
import { loadConfig } from "#config/load.ts";
|
|
5
6
|
import type { ExceptionDeclaration } from "#exceptions/declaration.ts";
|
|
6
7
|
import { SetupFailure } from "#failure.ts";
|
|
@@ -66,7 +67,7 @@ function biomePasses(root: string, exceptions: readonly ExceptionDeclaration[]):
|
|
|
66
67
|
const round = Effect.fn("Cli.round")(function* <TFailure, TRequirements>(
|
|
67
68
|
passes: readonly Pass<TFailure, TRequirements>[],
|
|
68
69
|
index: number,
|
|
69
|
-
afterEachPass: Effect.Effect<void, TFailure, TRequirements>,
|
|
70
|
+
afterEachPass: (label: string) => Effect.Effect<void, TFailure, TRequirements>,
|
|
70
71
|
): Effect.fn.Return<number, TFailure, TRequirements> {
|
|
71
72
|
let total = 0;
|
|
72
73
|
const parts: string[] = [];
|
|
@@ -74,22 +75,18 @@ const round = Effect.fn("Cli.round")(function* <TFailure, TRequirements>(
|
|
|
74
75
|
const changed = yield* pass.run;
|
|
75
76
|
total += changed;
|
|
76
77
|
parts.push(`${plural(changed, "file")} ${pass.label}`);
|
|
77
|
-
yield* afterEachPass;
|
|
78
|
+
yield* afterEachPass(pass.label);
|
|
78
79
|
}
|
|
79
80
|
yield* Console.log(`quality: Biome round ${index} rewrote ${parts.join(" and ")}.`);
|
|
80
81
|
return total;
|
|
81
82
|
});
|
|
82
83
|
|
|
83
|
-
function changedBetween(before: ReadonlyMap<string, string>, after: ReadonlyMap<string, string>): readonly string[] {
|
|
84
|
-
return [...new Set([...before.keys(), ...after.keys()])].filter((path) => before.get(path) !== after.get(path));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
84
|
function unsettled(changed: ReadonlySet<string>): SetupFailure {
|
|
88
85
|
const paths = [...changed].sort((left, right) => left.localeCompare(right));
|
|
89
86
|
const stopped = `Biome still rewrote files after ${MAX_FIX_ROUNDS} rounds, so quality fix stopped.`;
|
|
90
87
|
const named =
|
|
91
88
|
paths.length > 0
|
|
92
|
-
? [`${stopped} These
|
|
89
|
+
? [`${stopped} These ranges changed in the last round (before each pass):`, ...paths.map((path) => ` ${path}`)]
|
|
93
90
|
: [
|
|
94
91
|
`${stopped} The files it changed in the last round are ones quality fix does not read, such as files .gitignore lists.`,
|
|
95
92
|
"Run `biome check --write --unsafe --verbose` to name them.",
|
|
@@ -104,18 +101,20 @@ export const settle = Effect.fn("Cli.settle")(function* <TFailure, TRequirements
|
|
|
104
101
|
snapshot,
|
|
105
102
|
}: Settling<TFailure, TRequirements>): Effect.fn.Return<void, TFailure | SetupFailure, TRequirements> {
|
|
106
103
|
for (let index = 1; index < MAX_FIX_ROUNDS; index += 1) {
|
|
107
|
-
if ((yield* round(passes, index, Effect.void)) === 0) {
|
|
104
|
+
if ((yield* round(passes, index, () => Effect.void)) === 0) {
|
|
108
105
|
return;
|
|
109
106
|
}
|
|
110
107
|
}
|
|
111
108
|
const changed = new Set<string>();
|
|
112
109
|
let previous = yield* snapshot;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
function record(label: string) {
|
|
111
|
+
return Effect.map(snapshot, (current) => {
|
|
112
|
+
for (const path of changedRanges(previous, current, label)) {
|
|
113
|
+
changed.add(path);
|
|
114
|
+
}
|
|
115
|
+
previous = current;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
119
118
|
if ((yield* round(passes, MAX_FIX_ROUNDS, record)) === 0) {
|
|
120
119
|
return;
|
|
121
120
|
}
|
package/src/cli/program.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Console, Effect } from "effect";
|
|
2
|
+
import { cleanupMergeFiles } from "#baseline/cleanupMergeFiles.ts";
|
|
2
3
|
import { installDriver } from "#baseline/installDriver.ts";
|
|
3
4
|
import { mergeBaseline } from "#baseline/merge.ts";
|
|
4
5
|
import { parseCommand } from "#cli/args.ts";
|
|
@@ -42,6 +43,7 @@ export const program = Effect.fn("Cli.program")(function* (args: readonly string
|
|
|
42
43
|
case "Fix":
|
|
43
44
|
return yield* command.rules.length === 0 ? fix(cwd, command.config) : fixRule(cwd, command.config, command.rules);
|
|
44
45
|
case "BaselineMerge":
|
|
46
|
+
yield* cleanupMergeFiles(cwd, [command.base, command.ours, command.theirs]);
|
|
45
47
|
return yield* mergeBaseline(command.base, command.ours, command.theirs);
|
|
46
48
|
case "BaselineInstallDriver":
|
|
47
49
|
return yield* installDriver(cwd, launch);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export interface AdminBindings {
|
|
4
|
+
readonly factories: ReadonlySet<string>;
|
|
5
|
+
readonly guards: ReadonlySet<string>;
|
|
6
|
+
readonly permissions: ReadonlySet<string>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function namedImports(source: ts.SourceFile) {
|
|
10
|
+
return source.statements.filter(ts.isImportDeclaration).flatMap((statement) => {
|
|
11
|
+
const bindings = statement.importClause?.namedBindings;
|
|
12
|
+
if (
|
|
13
|
+
!(ts.isStringLiteral(statement.moduleSpecifier) && bindings !== undefined && ts.isNamedImports(bindings))
|
|
14
|
+
|| statement.importClause?.isTypeOnly
|
|
15
|
+
) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
const origin = statement.moduleSpecifier.text;
|
|
19
|
+
return bindings.elements
|
|
20
|
+
.filter((element) => !element.isTypeOnly)
|
|
21
|
+
.map((binding) => ({
|
|
22
|
+
local: binding.name.text,
|
|
23
|
+
name: (binding.propertyName ?? binding.name).text,
|
|
24
|
+
origin,
|
|
25
|
+
}));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function adminBindings(source: ts.SourceFile): AdminBindings {
|
|
30
|
+
const factories = new Set<string>();
|
|
31
|
+
const permissions = new Set<string>();
|
|
32
|
+
const guards = new Set<string>(["middleware"]);
|
|
33
|
+
for (const { local, name, origin } of namedImports(source)) {
|
|
34
|
+
if (origin.startsWith("@shivaedev/admin/") && ["adminPage", "registerAdmin"].includes(name)) {
|
|
35
|
+
factories.add(local);
|
|
36
|
+
}
|
|
37
|
+
if (/(?:^|[/#])admin\//u.test(origin)) {
|
|
38
|
+
permissions.add(local);
|
|
39
|
+
}
|
|
40
|
+
if (origin.startsWith("@shivaedev/") && ["requirePermission", "permissionMiddleware", "permissionReadPolicy"].includes(name)) {
|
|
41
|
+
guards.add(local);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return { factories, guards, permissions };
|
|
45
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { SourceFile } from "#rule.ts";
|
|
3
|
+
import { parse } from "#rules/parse.ts";
|
|
4
|
+
import { type AdminBindings, adminBindings } from "./adminBindings.ts";
|
|
5
|
+
|
|
6
|
+
export function sourceRoot(path: string): string | undefined {
|
|
7
|
+
return /^(?:.*\/)?src\//u.exec(path)?.[0];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function adminPath(path: string): boolean {
|
|
11
|
+
const root = sourceRoot(path);
|
|
12
|
+
return root !== undefined && path.slice(root.length).split("/").includes("admin");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function composition(path: string): boolean {
|
|
16
|
+
return /(?:^|\/)(?:registration|modules)\.[cm]?[jt]sx?$/u.test(path);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function adminCall(node: ts.CallExpression, bindings: AdminBindings): boolean {
|
|
20
|
+
if (ts.isIdentifier(node.expression) && bindings.factories.has(node.expression.text)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
const expression = ts.isCallExpression(node.expression) ? node.expression.expression : node.expression;
|
|
24
|
+
const callee = ts.isPropertyAccessExpression(expression) ? expression.name.text : expression.getText();
|
|
25
|
+
return bindings.guards.has(callee) && node.arguments.some((argument) => ts.isIdentifier(argument) && bindings.permissions.has(argument.text));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function exportedRegistration(node: ts.Node): boolean {
|
|
29
|
+
if (!(ts.isFunctionDeclaration(node) || ts.isVariableStatement(node))) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const exported = node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
33
|
+
const names = ts.isFunctionDeclaration(node)
|
|
34
|
+
? [node.name?.text]
|
|
35
|
+
: node.declarationList.declarations.map((declaration) => declaration.name.getText());
|
|
36
|
+
return exported === true && names.includes("adminRegistration");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function adminEvidence(file: SourceFile): boolean {
|
|
40
|
+
const source = parse(file);
|
|
41
|
+
if (source === undefined) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const bindings = adminBindings(source);
|
|
45
|
+
let found = false;
|
|
46
|
+
function visit(node: ts.Node): void {
|
|
47
|
+
found ||= (ts.isCallExpression(node) && adminCall(node, bindings)) || exportedRegistration(node);
|
|
48
|
+
ts.forEachChild(node, visit);
|
|
49
|
+
}
|
|
50
|
+
visit(source);
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
import { isTestCode } from "#naming/testName.ts";
|
|
3
|
+
import type { Finding, SourceFile } from "#rule.ts";
|
|
4
|
+
import { adminEvidence, adminPath, composition, sourceRoot } from "./adminEvidence.ts";
|
|
5
|
+
import type { ImportGraph } from "./graph.ts";
|
|
6
|
+
|
|
7
|
+
function reachable(seeds: readonly string[], next: (file: string) => readonly string[]): Set<string> {
|
|
8
|
+
const reached = new Set<string>();
|
|
9
|
+
const pending = [...seeds];
|
|
10
|
+
for (const file of pending) {
|
|
11
|
+
if (!reached.has(file)) {
|
|
12
|
+
reached.add(file);
|
|
13
|
+
pending.push(...next(file));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return reached;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function destination(file: string, admin: boolean): string {
|
|
20
|
+
if (!admin) {
|
|
21
|
+
const root = sourceRoot(file) ?? "";
|
|
22
|
+
return posix.dirname(root + file.slice(root.length).replace(/(?:^|\/)admin\//u, (segment) => (segment.startsWith("/") ? "/" : "")));
|
|
23
|
+
}
|
|
24
|
+
const runtime = /^(?<root>.*\/(?:client|server))\/(?<local>.*)$/u.exec(file)?.groups;
|
|
25
|
+
if (runtime?.root !== undefined && runtime.local !== undefined) {
|
|
26
|
+
return posix.dirname(`${runtime.root}/admin/${runtime.local}`);
|
|
27
|
+
}
|
|
28
|
+
return `${posix.dirname(file)}/admin`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function adminFindings(graph: ImportGraph, sources: readonly SourceFile[]): readonly Finding[] {
|
|
32
|
+
const files = sources.filter((file) => sourceRoot(file.path) !== undefined && !isTestCode(file.path));
|
|
33
|
+
const paths = new Set(files.map((file) => file.path));
|
|
34
|
+
const seeds = new Set(files.filter((file) => adminPath(file.path) || adminEvidence(file)).map((file) => file.path));
|
|
35
|
+
const edges = graph.edges.filter((edge) => edge.kind === "import" && paths.has(edge.from));
|
|
36
|
+
const adjacency = new Map<string, string[]>();
|
|
37
|
+
const imported = new Set<string>();
|
|
38
|
+
for (const edge of edges) {
|
|
39
|
+
if (edge.to.kind === "file" && paths.has(edge.to.path) && sourceRoot(edge.from) === sourceRoot(edge.to.path)) {
|
|
40
|
+
adjacency.set(edge.from, [...(adjacency.get(edge.from) ?? []), edge.to.path]);
|
|
41
|
+
imported.add(edge.to.path);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function next(file: string): readonly string[] {
|
|
45
|
+
return adjacency.get(file) ?? [];
|
|
46
|
+
}
|
|
47
|
+
const admin = reachable([...seeds], next);
|
|
48
|
+
const members = files.filter(({ path }) => !seeds.has(path) && (!(admin.has(path) && imported.has(path)) || composition(path)));
|
|
49
|
+
const member = reachable(
|
|
50
|
+
members.map((file) => file.path),
|
|
51
|
+
(file) =>
|
|
52
|
+
next(file).filter(
|
|
53
|
+
(target) => !(composition(file) && seeds.has(target) && /\/(?:[^/]*[Rr]egistration|pages|make)\.[cm]?[jt]sx?$/u.test(target)),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
return files.flatMap(({ path }): Finding[] => {
|
|
57
|
+
if (adminPath(path) && member.has(path)) {
|
|
58
|
+
return [{ file: path, message: `Member code reaches this admin/ file. Move it to ${destination(path, false)}/.` }];
|
|
59
|
+
}
|
|
60
|
+
if (!adminPath(path) && admin.has(path) && !member.has(path)) {
|
|
61
|
+
return [{ file: path, message: `Only admin permissions or registrations reach this file. Move it to ${destination(path, true)}/.` }];
|
|
62
|
+
}
|
|
63
|
+
return [];
|
|
64
|
+
});
|
|
65
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { realpathSync } from "node:fs";
|
|
2
2
|
import { posix } from "node:path";
|
|
3
|
+
import { moduleRoot } from "#naming/moduleRoot.ts";
|
|
3
4
|
import type { RuleInputs } from "#rule.ts";
|
|
4
5
|
import { parse } from "#rules/parse.ts";
|
|
5
6
|
import type { ImportRequest } from "./extract.ts";
|
|
@@ -39,6 +40,18 @@ interface FileContext {
|
|
|
39
40
|
readonly root: string;
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
function registersAdmin(from: string, specifier: string, target: string | undefined): boolean {
|
|
44
|
+
const root = moduleRoot(from);
|
|
45
|
+
const folder = posix.dirname(from);
|
|
46
|
+
return (
|
|
47
|
+
root !== undefined
|
|
48
|
+
&& (folder === `${root}/client` || folder === `${root}/server`)
|
|
49
|
+
&& /^registration\.[cm]?[jt]sx?$/u.test(posix.basename(from))
|
|
50
|
+
&& specifier.startsWith("./admin/")
|
|
51
|
+
&& target?.startsWith(`${folder}/admin/`) === true
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
42
55
|
// A relative import leaves its folder when its path climbs or descends, or when it names a folder whose index file lies below.
|
|
43
56
|
function departureAt(context: FileContext, site: SpecifierSite): Departure | undefined {
|
|
44
57
|
const path = site.specifier.replace(QUERY, "");
|
|
@@ -48,6 +61,9 @@ function departureAt(context: FileContext, site: SpecifierSite): Departure | und
|
|
|
48
61
|
const request: ImportRequest = { kind: "import", line: site.line, specifier: site.specifier, type: site.type };
|
|
49
62
|
const target = resolveImport(context.root, noAmbient, request, context.from, context.project);
|
|
50
63
|
const local = target?.kind === "file" ? target.path : undefined;
|
|
64
|
+
if (registersAdmin(context.path, path, local)) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
51
67
|
if (!(leavesLexically(path) || (local !== undefined && posix.dirname(local) !== posix.dirname(context.path)))) {
|
|
52
68
|
return undefined;
|
|
53
69
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { isTestCode } from "#naming/testName.ts";
|
|
3
|
+
import type { Finding, SourceFile } from "#rule.ts";
|
|
4
|
+
import { parse } from "#rules/parse.ts";
|
|
5
|
+
import { specifierSites } from "./specifiers.ts";
|
|
6
|
+
|
|
7
|
+
const BARREL = "@effect/platform-node";
|
|
8
|
+
|
|
9
|
+
function subpaths(source: ts.SourceFile, start: number): readonly string[] | undefined {
|
|
10
|
+
const statement = source.statements.find((node) => node.pos <= start && node.end > start);
|
|
11
|
+
const bindings = statement !== undefined && ts.isImportDeclaration(statement) ? statement.importClause?.namedBindings : undefined;
|
|
12
|
+
if (bindings !== undefined && ts.isNamedImports(bindings)) {
|
|
13
|
+
const values = bindings.elements.filter((element) => !element.isTypeOnly);
|
|
14
|
+
return values.length === 0 && bindings.elements.length > 0
|
|
15
|
+
? undefined
|
|
16
|
+
: values.map((element) => `${BARREL}/${(element.propertyName ?? element.name).text}`);
|
|
17
|
+
}
|
|
18
|
+
if (
|
|
19
|
+
statement !== undefined
|
|
20
|
+
&& ts.isExportDeclaration(statement)
|
|
21
|
+
&& statement.exportClause !== undefined
|
|
22
|
+
&& ts.isNamedExports(statement.exportClause)
|
|
23
|
+
&& statement.exportClause.elements.every((element) => element.isTypeOnly)
|
|
24
|
+
) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function nodePlatformBarrels(file: SourceFile): readonly Finding[] {
|
|
31
|
+
const source = isTestCode(file.path) ? undefined : parse(file);
|
|
32
|
+
if (source === undefined || source.isDeclarationFile) {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
return specifierSites(source)
|
|
36
|
+
.filter((site) => site.specifier === BARREL && !site.type)
|
|
37
|
+
.flatMap((site) => {
|
|
38
|
+
const paths = subpaths(source, site.start);
|
|
39
|
+
if (paths === undefined) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
file: file.path,
|
|
45
|
+
line: site.line,
|
|
46
|
+
message: `The ${BARREL} barrel loads optional integrations such as redis. Import module subpaths instead: ${paths.length > 0 ? paths.map((path) => `\`import * as ${path.slice(path.lastIndexOf("/") + 1)} from "${path}"\``).join("; ") : `\`import * as NodeServices from "${BARREL}/NodeServices"\` (choose the module you use)`}.`,
|
|
47
|
+
subject: BARREL,
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
});
|
|
51
|
+
}
|
package/src/naming/ownExports.ts
CHANGED
|
@@ -61,8 +61,12 @@ function declared(statement: ts.Statement): readonly OwnExport[] {
|
|
|
61
61
|
return named !== undefined && ts.isIdentifier(named) ? [{ constant: false, kind: "value", name: named.text }] : [];
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
export function ownDeclarations(source: ts.SourceFile): readonly OwnExport[] {
|
|
65
|
+
return source.statements.flatMap(declared);
|
|
66
|
+
}
|
|
67
|
+
|
|
64
68
|
function locals(source: ts.SourceFile): ReadonlyMap<string, OwnExport> {
|
|
65
|
-
const all = source
|
|
69
|
+
const all = ownDeclarations(source);
|
|
66
70
|
return new Map(all.map((local) => [local.name, local]));
|
|
67
71
|
}
|
|
68
72
|
|
package/src/rules/builtIn.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { distinctCase } from "#rules/files/distinctCase.ts";
|
|
|
14
14
|
import { folderNames } from "#rules/files/folderNames.ts";
|
|
15
15
|
import { namedAfterExport } from "#rules/files/namedAfterExport.ts";
|
|
16
16
|
import { otherNames } from "#rules/files/otherNames.ts";
|
|
17
|
+
import { adminPaths } from "#rules/imports/adminPaths.ts";
|
|
17
18
|
import { importsAliased } from "#rules/imports/aliased.ts";
|
|
18
19
|
import { importCycles } from "#rules/imports/cycles.ts";
|
|
19
20
|
import { importFences } from "#rules/imports/fences.ts";
|
|
@@ -83,6 +84,7 @@ export const builtInRules = [
|
|
|
83
84
|
importsResolvable,
|
|
84
85
|
importsManifest,
|
|
85
86
|
importsAliased,
|
|
87
|
+
adminPaths,
|
|
86
88
|
noTestCode,
|
|
87
89
|
manifestsValid,
|
|
88
90
|
manifestsSorted,
|
|
@@ -5,7 +5,8 @@ import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts"
|
|
|
5
5
|
import { exportFits } from "#naming/exportFits.ts";
|
|
6
6
|
import { mainExport } from "#naming/mainExport.ts";
|
|
7
7
|
import { isMigrationFile } from "#naming/migrationFile.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { moduleRoot } from "#naming/moduleRoot.ts";
|
|
9
|
+
import { ownDeclarations, ownExports } from "#naming/ownExports.ts";
|
|
9
10
|
import { type PackageLayout, packageLayout } from "#naming/packageLayout.ts";
|
|
10
11
|
import { CAMEL, KEBAB, sameWord, wordsOf } from "#naming/words.ts";
|
|
11
12
|
import { defineRule, type Finding, type SourceFile } from "#rule.ts";
|
|
@@ -47,6 +48,10 @@ function repeated(stem: string, folders: readonly string[]): string | undefined
|
|
|
47
48
|
function nameFinding(file: SourceFile, stem: string, folders: readonly string[]): string | undefined {
|
|
48
49
|
const source = parse(file);
|
|
49
50
|
const exports = source === undefined ? [] : ownExports(source);
|
|
51
|
+
const constants = source === undefined ? [] : ownDeclarations(source).filter((entry) => entry.constant);
|
|
52
|
+
if (constants.some((entry) => exportFits(entry.name, stem, folders))) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
50
55
|
const [only] = exports;
|
|
51
56
|
if (only === undefined) {
|
|
52
57
|
return CAMEL.test(stem)
|
|
@@ -73,7 +78,9 @@ function findingsOf(file: SourceFile, layout: PackageLayout): readonly Finding[]
|
|
|
73
78
|
? []
|
|
74
79
|
: [{ file: file.path, message: `"${stem}" is run as a script, so it is named in kebab-case, such as build-docs.ts.` }];
|
|
75
80
|
}
|
|
76
|
-
const
|
|
81
|
+
const root = moduleRoot(file.path);
|
|
82
|
+
const admin = root === undefined ? undefined : /^(?:client|server)\/(?<path>admin\/.+)$/u.exec(file.path.slice(root.length + 1))?.groups?.path;
|
|
83
|
+
const folder = repeated(stem, admin === undefined ? folders : posix.dirname(admin).split("/"));
|
|
77
84
|
const messages = [
|
|
78
85
|
nameFinding(file, stem, folders),
|
|
79
86
|
folder === undefined ? undefined : `"${stem}" repeats its folder "${folder}". The folder is the prefix: items/listItems.ts is items/list.ts.`,
|
|
@@ -90,7 +97,7 @@ export const namedAfterExport = defineRule({
|
|
|
90
97
|
.flatMap((file) => findingsOf(file, layout));
|
|
91
98
|
},
|
|
92
99
|
description:
|
|
93
|
-
"A file is named after its main export, including a
|
|
100
|
+
"A file is named after its main export or a module-level constant, including a private constant, with its folders as the prefix; module client/admin and server/admin files may repeat names from above the admin folder. A file of several exports is a camelCase topic file, and a script is kebab-case. Rename the file or the export; nothing renames them for you.",
|
|
94
101
|
id: "files/named-after-export",
|
|
95
102
|
options: Schema.toStandardSchemaV1(NamedAfterExportOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
96
103
|
validate: validateGlobs,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { adminFindings } from "#imports/adminFindings.ts";
|
|
3
|
+
import { importGraph } from "#imports/graph.ts";
|
|
4
|
+
import { defineRule } from "#rule.ts";
|
|
5
|
+
|
|
6
|
+
export const adminPaths = defineRule({
|
|
7
|
+
check: (inputs) => Effect.promise(() => importGraph(inputs)).pipe(Effect.map((graph) => adminFindings(graph, inputs.sources))),
|
|
8
|
+
description:
|
|
9
|
+
"Admin-only source files live under an admin/ segment; member code cannot reach admin/ implementations. Registration and module composition may import admin registrations and declarations. Move the file to the folder named by the finding.",
|
|
10
|
+
id: "imports/admin-paths",
|
|
11
|
+
registrable: false,
|
|
12
|
+
});
|
|
@@ -21,6 +21,6 @@ export const importsAliased = defineRule({
|
|
|
21
21
|
subject: departure.specifier,
|
|
22
22
|
})),
|
|
23
23
|
description:
|
|
24
|
-
"A relative import names only a file in its own folder. Every other import goes through an alias: the package.json `imports` of its package or another workspace package's name.",
|
|
24
|
+
"A relative import names only a file in its own folder, except a module's client or server registration may import its own ./admin/ subtree. Every other import goes through an alias: the package.json `imports` of its package or another workspace package's name.",
|
|
25
25
|
id: "imports/aliased",
|
|
26
26
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { type ImportGraph, importGraph } from "#imports/graph.ts";
|
|
3
|
+
import { nodePlatformBarrels } from "#imports/nodePlatformBarrels.ts";
|
|
3
4
|
import { builtinName } from "#imports/packageName.ts";
|
|
4
5
|
import { type Runtime, runtimeDeclarationProblems, runtimeOf } from "#imports/runtime.ts";
|
|
5
6
|
import { isTestCode } from "#naming/testName.ts";
|
|
@@ -39,7 +40,7 @@ export const importsRuntime = defineRule({
|
|
|
39
40
|
Effect.gen(function* () {
|
|
40
41
|
const graph = yield* Effect.promise(() => importGraph(inputs));
|
|
41
42
|
const runtime = yield* runtimeOf(inputs, graph.packages);
|
|
42
|
-
const imports = runtimeFindings(graph, runtime);
|
|
43
|
+
const imports = [...runtimeFindings(graph, runtime), ...inputs.sources.flatMap(nodePlatformBarrels)];
|
|
43
44
|
const declarations = (yield* runtimeDeclarationProblems(inputs, graph.packages)).map(({ directory, message }) => ({
|
|
44
45
|
file: directory === "." ? "package.json" : `${directory}/package.json`,
|
|
45
46
|
line: 1,
|
|
@@ -54,6 +55,6 @@ export const importsRuntime = defineRule({
|
|
|
54
55
|
return [...imports, ...declarations, ...shared.flatMap(browserGlobals)];
|
|
55
56
|
}),
|
|
56
57
|
description:
|
|
57
|
-
"Runtime folders define client, server and shared code. A manifest's quality.runtime declares a single-runtime package whole; executable packages without runtime folders and package-root tooling are server code.",
|
|
58
|
+
"Runtime folders define client, server and shared code. A manifest's quality.runtime declares a single-runtime package whole; executable packages without runtime folders and package-root tooling are server code. Production imports use @effect/platform-node module subpaths so its barrel does not load optional integrations.",
|
|
58
59
|
id: "imports/runtime",
|
|
59
60
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { matchesGlob } from "node:path";
|
|
2
2
|
import { Schema } from "effect";
|
|
3
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
3
4
|
import ts from "typescript";
|
|
4
5
|
import { GlobDeclarations, globPatterns, validateGlobs } from "#config/globs.ts";
|
|
5
6
|
import { defineRule } from "#rule.ts";
|
|
@@ -25,8 +26,18 @@ const COMPARISONS = new Set([
|
|
|
25
26
|
]);
|
|
26
27
|
|
|
27
28
|
function isErrorStatus(node: ts.Expression): boolean {
|
|
28
|
-
const
|
|
29
|
-
|
|
29
|
+
const value = unwrapped(node);
|
|
30
|
+
const literal =
|
|
31
|
+
ts.isCallExpression(value)
|
|
32
|
+
&& ts.isPropertyAccessExpression(value.expression)
|
|
33
|
+
&& ts.isIdentifier(value.expression.expression)
|
|
34
|
+
&& value.expression.expression.text === "HttpStatus"
|
|
35
|
+
&& value.expression.name.text === "fromLiteral"
|
|
36
|
+
? value.arguments[0]
|
|
37
|
+
: undefined;
|
|
38
|
+
const number: unknown =
|
|
39
|
+
literal !== undefined && ts.isStringLiteralLike(literal) ? Reflect.apply(HttpStatus.fromLiteral, undefined, [literal.text]) : numericValue(value);
|
|
40
|
+
return typeof number === "number" && number >= 400 && number <= 599;
|
|
30
41
|
}
|
|
31
42
|
|
|
32
43
|
function statusBranch(node: ts.Node): boolean {
|