@shivaedev/quality 0.74.0 → 0.74.2
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 +16 -0
- package/README.md +4 -4
- package/dist/adapters/buildEntryValue.d.ts +1 -0
- package/dist/adapters/buildEntryValue.d.ts.map +1 -1
- package/dist/adapters/buildEntryValue.js +1 -1
- package/dist/adapters/buildEntryValue.js.map +1 -1
- package/dist/adapters/buildHelper.d.ts +1 -0
- package/dist/adapters/buildHelper.d.ts.map +1 -1
- package/dist/adapters/buildHelper.js +9 -8
- package/dist/adapters/buildHelper.js.map +1 -1
- package/dist/adapters/bundlerExternals.d.ts +4 -0
- package/dist/adapters/bundlerExternals.d.ts.map +1 -0
- package/dist/adapters/bundlerExternals.js +46 -0
- package/dist/adapters/bundlerExternals.js.map +1 -0
- package/dist/adapters/bundlerSources.d.ts +1 -0
- package/dist/adapters/bundlerSources.d.ts.map +1 -1
- package/dist/adapters/bundlerSources.js +17 -4
- package/dist/adapters/bundlerSources.js.map +1 -1
- package/dist/adapters/bundlerTargets.d.ts +8 -0
- package/dist/adapters/bundlerTargets.d.ts.map +1 -0
- package/dist/adapters/{bundlerEntries.js → bundlerTargets.js} +18 -15
- package/dist/adapters/bundlerTargets.js.map +1 -0
- package/dist/adapters/importedCall.d.ts +4 -0
- package/dist/adapters/importedCall.d.ts.map +1 -1
- package/dist/adapters/importedCall.js +6 -3
- package/dist/adapters/importedCall.js.map +1 -1
- package/dist/adapters/imports/departures.d.ts.map +1 -1
- package/dist/adapters/imports/departures.js +7 -1
- package/dist/adapters/imports/departures.js.map +1 -1
- package/dist/adapters/isToolConfig.d.ts.map +1 -1
- package/dist/adapters/isToolConfig.js +5 -1
- package/dist/adapters/isToolConfig.js.map +1 -1
- package/dist/adapters/knip/configuration.d.ts +3 -1
- package/dist/adapters/knip/configuration.d.ts.map +1 -1
- package/dist/adapters/knip/configuration.js +8 -2
- package/dist/adapters/knip/configuration.js.map +1 -1
- package/dist/adapters/knip/run.js +3 -3
- package/dist/adapters/knip/run.js.map +1 -1
- package/dist/adapters/outsideImportFindings.d.ts.map +1 -1
- package/dist/adapters/outsideImportFindings.js +18 -15
- package/dist/adapters/outsideImportFindings.js.map +1 -1
- package/dist/adapters/reachableBuilds.d.ts +9 -0
- package/dist/adapters/reachableBuilds.d.ts.map +1 -0
- package/dist/adapters/{reachableBuildEntries.js → reachableBuilds.js} +15 -11
- package/dist/adapters/reachableBuilds.js.map +1 -0
- package/dist/imports/isTooling.d.ts +1 -0
- package/dist/imports/isTooling.d.ts.map +1 -1
- package/dist/imports/isTooling.js +6 -3
- package/dist/imports/isTooling.js.map +1 -1
- package/dist/inventory/programmaticBuilds.d.ts +4 -0
- package/dist/inventory/programmaticBuilds.d.ts.map +1 -0
- package/dist/inventory/programmaticBuilds.js +13 -0
- package/dist/inventory/programmaticBuilds.js.map +1 -0
- package/dist/rules/effect/adapterImports.js +1 -1
- package/dist/rules/effect/adapterImports.js.map +1 -1
- package/dist/rules/effect/boundary.d.ts.map +1 -1
- package/dist/rules/effect/boundary.js +9 -7
- package/dist/rules/effect/boundary.js.map +1 -1
- package/dist/rules/effect/boundaryReference.d.ts +3 -0
- package/dist/rules/effect/boundaryReference.d.ts.map +1 -1
- package/dist/rules/effect/boundaryReference.js +23 -2
- package/dist/rules/effect/boundaryReference.js.map +1 -1
- package/dist/rules/files/namedAfterExport.js +2 -2
- package/dist/rules/files/namedAfterExport.js.map +1 -1
- package/dist/rules/imports/aliased.js +1 -1
- package/dist/rules/imports/aliased.js.map +1 -1
- package/package.json +1 -1
- package/src/adapters/buildEntryValue.ts +1 -1
- package/src/adapters/buildHelper.ts +10 -9
- package/src/adapters/bundlerExternals.ts +57 -0
- package/src/adapters/bundlerSources.ts +23 -4
- package/src/adapters/{bundlerEntries.ts → bundlerTargets.ts} +39 -15
- package/src/adapters/importedCall.ts +7 -3
- package/src/adapters/imports/departures.ts +7 -1
- package/src/adapters/isToolConfig.ts +7 -3
- package/src/adapters/knip/configuration.ts +13 -2
- package/src/adapters/knip/run.ts +3 -3
- package/src/adapters/outsideImportFindings.ts +18 -15
- package/src/adapters/{reachableBuildEntries.ts → reachableBuilds.ts} +19 -10
- package/src/imports/isTooling.ts +7 -3
- package/src/inventory/{programmaticEntries.ts → programmaticBuilds.ts} +5 -3
- package/src/rules/effect/adapterImports.ts +1 -1
- package/src/rules/effect/boundary.ts +25 -17
- package/src/rules/effect/boundaryReference.ts +30 -2
- package/src/rules/files/namedAfterExport.ts +2 -2
- package/src/rules/imports/aliased.ts +1 -1
- package/dist/adapters/bundlerEntries.d.ts +0 -4
- package/dist/adapters/bundlerEntries.d.ts.map +0 -1
- package/dist/adapters/bundlerEntries.js.map +0 -1
- package/dist/adapters/reachableBuildEntries.d.ts +0 -5
- package/dist/adapters/reachableBuildEntries.d.ts.map +0 -1
- package/dist/adapters/reachableBuildEntries.js.map +0 -1
- package/dist/inventory/programmaticEntries.d.ts +0 -4
- package/dist/inventory/programmaticEntries.d.ts.map +0 -1
- package/dist/inventory/programmaticEntries.js +0 -11
- package/dist/inventory/programmaticEntries.js.map +0 -1
|
@@ -51,17 +51,18 @@ function localHelper(expression: ts.Expression, call: ts.CallExpression): Helper
|
|
|
51
51
|
return undefined;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
if (imported === undefined || !(imported.module.startsWith(".") || imported.module.startsWith("#"))) {
|
|
54
|
+
export function importedSource(node: ts.Node, module: string, sources: ReadonlyMap<string, ts.SourceFile>): ts.SourceFile | undefined {
|
|
55
|
+
if (!(module.startsWith(".") || module.startsWith("#"))) {
|
|
57
56
|
return undefined;
|
|
58
57
|
}
|
|
59
|
-
const modulePath =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
const modulePath = module.startsWith("#") ? module.slice(1) : posix.join(posix.dirname(node.getSourceFile().fileName), module);
|
|
59
|
+
return sources.get(modulePath) ?? [...sources.values()].find((candidate) => candidate.fileName.replaceAll("\\", "/").endsWith(`/${modulePath}`));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function importedHelper(call: ts.CallExpression, sources: ReadonlyMap<string, ts.SourceFile>): Helper | undefined {
|
|
63
|
+
const imported = importedCall(call);
|
|
64
|
+
const source = imported === undefined ? undefined : importedSource(call, imported.module, sources);
|
|
65
|
+
return source === undefined || imported === undefined ? undefined : declared(source, imported.name);
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
export function buildHelper(call: ts.CallExpression, sources: ReadonlyMap<string, ts.SourceFile>, bindings: BuildBindings): Helper | undefined {
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { type BuildBindings, buildEntryValue, buildOption, declaredValue } from "#adapters/buildEntryValue.ts";
|
|
3
|
+
import { importedSource } from "#adapters/buildHelper.ts";
|
|
4
|
+
import { importedName } from "#adapters/importedCall.ts";
|
|
5
|
+
import { packageNameOf } from "#imports/packageName.ts";
|
|
6
|
+
|
|
7
|
+
function importedValue(node: ts.Identifier, sources: ReadonlyMap<string, ts.SourceFile>): ts.Expression | undefined {
|
|
8
|
+
const imported = importedName(node);
|
|
9
|
+
const source = imported === undefined ? undefined : importedSource(node, imported.module, sources);
|
|
10
|
+
return source === undefined || imported === undefined ? undefined : declaredValue(source.statements, imported.name);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function externalNames(
|
|
14
|
+
expression: ts.Expression,
|
|
15
|
+
bindings: BuildBindings,
|
|
16
|
+
sources: ReadonlyMap<string, ts.SourceFile>,
|
|
17
|
+
active: ReadonlySet<ts.Node>,
|
|
18
|
+
): readonly string[] {
|
|
19
|
+
const node = buildEntryValue(expression, new Set(), bindings);
|
|
20
|
+
if (active.has(node)) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
const visited = new Set([...active, node]);
|
|
24
|
+
function names(value: ts.Expression) {
|
|
25
|
+
return externalNames(value, bindings, sources, visited);
|
|
26
|
+
}
|
|
27
|
+
if (ts.isStringLiteralLike(node)) {
|
|
28
|
+
return [node.text];
|
|
29
|
+
}
|
|
30
|
+
if (ts.isSpreadElement(node)) {
|
|
31
|
+
return names(node.expression);
|
|
32
|
+
}
|
|
33
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
34
|
+
return node.elements.flatMap(names);
|
|
35
|
+
}
|
|
36
|
+
if (ts.isIdentifier(node)) {
|
|
37
|
+
const value = importedValue(node, sources);
|
|
38
|
+
return value === undefined ? [] : names(value);
|
|
39
|
+
}
|
|
40
|
+
// A filter keeps only names the list already declares external, so the whole list stands in for its predicate.
|
|
41
|
+
return ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "filter"
|
|
42
|
+
? names(node.expression.expression)
|
|
43
|
+
: [];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function bundlerExternals(
|
|
47
|
+
options: ts.ObjectLiteralExpression,
|
|
48
|
+
bindings: BuildBindings,
|
|
49
|
+
sources: ReadonlyMap<string, ts.SourceFile>,
|
|
50
|
+
): readonly string[] {
|
|
51
|
+
const external = buildOption(options, "external");
|
|
52
|
+
return external === undefined
|
|
53
|
+
? []
|
|
54
|
+
: externalNames(external, bindings, sources, new Set())
|
|
55
|
+
.filter((name) => !(name.startsWith(".") || name.startsWith("/")))
|
|
56
|
+
.map(packageNameOf);
|
|
57
|
+
}
|
|
@@ -30,12 +30,14 @@ function directSources(files: readonly SourceFile[]): Map<string, ts.SourceFile>
|
|
|
30
30
|
return sources;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
function fileEdges(graph: ImportGraph): readonly (readonly [from: string, to: string])[] {
|
|
34
|
+
return graph.edges.flatMap((edge) => (!edge.type && edge.to.kind === "file" ? [[edge.from, edge.to.path] as const] : []));
|
|
35
|
+
}
|
|
36
|
+
|
|
33
37
|
function importedBy(graph: ImportGraph): ReadonlyMap<string, readonly string[]> {
|
|
34
38
|
const importers = new Map<string, string[]>();
|
|
35
|
-
for (const
|
|
36
|
-
|
|
37
|
-
importers.set(edge.to.path, [...(importers.get(edge.to.path) ?? []), edge.from]);
|
|
38
|
-
}
|
|
39
|
+
for (const [from, to] of fileEdges(graph)) {
|
|
40
|
+
importers.set(to, [...(importers.get(to) ?? []), from]);
|
|
39
41
|
}
|
|
40
42
|
return importers;
|
|
41
43
|
}
|
|
@@ -58,3 +60,20 @@ export function bundlerSources(files: readonly SourceFile[], graph: ImportGraph)
|
|
|
58
60
|
}
|
|
59
61
|
return sources;
|
|
60
62
|
}
|
|
63
|
+
|
|
64
|
+
export function bundlerModules(
|
|
65
|
+
sources: ReadonlyMap<string, ts.SourceFile>,
|
|
66
|
+
files: readonly SourceFile[],
|
|
67
|
+
graph: ImportGraph,
|
|
68
|
+
): ReadonlyMap<string, ts.SourceFile> {
|
|
69
|
+
const filesByPath = new Map(files.map((file) => [file.path, file]));
|
|
70
|
+
const modules = new Map(sources);
|
|
71
|
+
for (const [from, to] of fileEdges(graph)) {
|
|
72
|
+
const file = sources.has(from) && !modules.has(to) ? filesByPath.get(to) : undefined;
|
|
73
|
+
const source = file === undefined ? undefined : parse(file);
|
|
74
|
+
if (source !== undefined) {
|
|
75
|
+
modules.set(to, source);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return modules;
|
|
79
|
+
}
|
|
@@ -3,6 +3,7 @@ import ts from "typescript";
|
|
|
3
3
|
import { buildEntryPaths } from "#adapters/buildEntryPaths.ts";
|
|
4
4
|
import { type BuildBindings, buildEntryValue, buildOption } from "#adapters/buildEntryValue.ts";
|
|
5
5
|
import { buildArguments, buildHelper } from "#adapters/buildHelper.ts";
|
|
6
|
+
import { bundlerExternals } from "#adapters/bundlerExternals.ts";
|
|
6
7
|
import { importedCall } from "#adapters/importedCall.ts";
|
|
7
8
|
import { mappedBuildBindings } from "#adapters/mappedBuildBindings.ts";
|
|
8
9
|
import type { SourceFile } from "#rule.ts";
|
|
@@ -28,22 +29,37 @@ function entryOption(call: ts.CallExpression): string | undefined {
|
|
|
28
29
|
return imported?.module === "esbuild" && ESBUILD.has(imported.name) ? "entryPoints" : undefined;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
export interface BundlerTargets {
|
|
33
|
+
readonly entries: readonly string[];
|
|
34
|
+
readonly externals: readonly string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface BuildConfig {
|
|
38
|
+
readonly option: string;
|
|
39
|
+
readonly options: ts.ObjectLiteralExpression;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function buildConfigs(call: ts.CallExpression, bindings: BuildBindings): readonly BuildConfig[] {
|
|
32
43
|
const option = entryOption(call);
|
|
33
44
|
const [argument] = call.arguments;
|
|
34
|
-
const
|
|
35
|
-
if (option === undefined ||
|
|
45
|
+
const value = argument === undefined ? undefined : buildEntryValue(argument, new Set(), bindings);
|
|
46
|
+
if (option === undefined || value === undefined) {
|
|
36
47
|
return [];
|
|
37
48
|
}
|
|
38
|
-
const configs = option === "input" && ts.isArrayLiteralExpression(
|
|
39
|
-
return configs.flatMap((config) =>
|
|
49
|
+
const configs = option === "input" && ts.isArrayLiteralExpression(value) ? value.elements : [value];
|
|
50
|
+
return configs.flatMap((config) => {
|
|
51
|
+
const options = buildEntryValue(config, new Set(), bindings);
|
|
52
|
+
return ts.isObjectLiteralExpression(options) ? [{ option, options }] : [];
|
|
53
|
+
});
|
|
40
54
|
}
|
|
41
55
|
|
|
42
|
-
function configuredEntries(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
56
|
+
function configuredEntries(
|
|
57
|
+
options: ts.ObjectLiteralExpression,
|
|
58
|
+
option: string,
|
|
59
|
+
cwd: string,
|
|
60
|
+
root: string,
|
|
61
|
+
bindings: BuildBindings,
|
|
62
|
+
): readonly string[] {
|
|
47
63
|
const directory = buildOption(options, "absWorkingDir");
|
|
48
64
|
const working = directory === undefined ? cwd : buildEntryPaths(directory, cwd, root, new Set(), bindings)[0];
|
|
49
65
|
if (working === undefined) {
|
|
@@ -53,17 +69,19 @@ function configuredEntries(value: ts.Expression, option: string, cwd: string, ro
|
|
|
53
69
|
return entries === undefined ? [] : buildEntryPaths(entries, cwd, root, new Set(), bindings).map((path) => posix.resolve(cwd, working, path));
|
|
54
70
|
}
|
|
55
71
|
|
|
56
|
-
export function
|
|
72
|
+
export function bundlerTargets(
|
|
57
73
|
file: SourceFile,
|
|
58
74
|
root: string,
|
|
59
75
|
directory: string,
|
|
60
76
|
sources: ReadonlyMap<string, ts.SourceFile> = new Map(),
|
|
61
|
-
|
|
77
|
+
modules: ReadonlyMap<string, ts.SourceFile> = sources,
|
|
78
|
+
): BundlerTargets {
|
|
62
79
|
const source = sources.get(file.path) ?? parse(file);
|
|
63
80
|
if (source === undefined) {
|
|
64
|
-
return [];
|
|
81
|
+
return { entries: [], externals: [] };
|
|
65
82
|
}
|
|
66
83
|
const entries: string[] = [];
|
|
84
|
+
const externals: string[] = [];
|
|
67
85
|
function visit(node: ts.Node, bindings: BuildBindings, active: ReadonlySet<ts.Node>): void {
|
|
68
86
|
if (ts.isFunctionLike(node)) {
|
|
69
87
|
const body = callbackBody(node);
|
|
@@ -75,7 +93,13 @@ export function bundlerEntries(
|
|
|
75
93
|
return;
|
|
76
94
|
}
|
|
77
95
|
if (ts.isCallExpression(node)) {
|
|
78
|
-
|
|
96
|
+
const configs = buildConfigs(node, bindings);
|
|
97
|
+
entries.push(
|
|
98
|
+
...configs.flatMap(({ option, options }) =>
|
|
99
|
+
configuredEntries(options, option, posix.join(root, directory), root, bindings).map((path) => posix.relative(root, path)),
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
externals.push(...configs.flatMap(({ options }) => bundlerExternals(options, bindings, modules)));
|
|
79
103
|
const helper = buildHelper(node, sources, bindings);
|
|
80
104
|
if (helper?.body !== undefined && !active.has(helper)) {
|
|
81
105
|
entries.push(posix.relative(root, posix.resolve(root, helper.getSourceFile().fileName)));
|
|
@@ -85,5 +109,5 @@ export function bundlerEntries(
|
|
|
85
109
|
ts.forEachChild(node, (child) => visit(child, bindings, active));
|
|
86
110
|
}
|
|
87
111
|
visit(source, new Map(), new Set());
|
|
88
|
-
return entries;
|
|
112
|
+
return { entries, externals };
|
|
89
113
|
}
|
|
@@ -13,15 +13,19 @@ function calledName(expression: ts.LeftHandSideExpression, clause: ts.ImportClau
|
|
|
13
13
|
return namespace === expression.expression.text ? expression.name.text : undefined;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export function
|
|
17
|
-
for (const statement of
|
|
16
|
+
export function importedName(expression: ts.LeftHandSideExpression): { readonly module: string; readonly name: string } | undefined {
|
|
17
|
+
for (const statement of expression.getSourceFile().statements) {
|
|
18
18
|
if (!(ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) || statement.importClause === undefined) {
|
|
19
19
|
continue;
|
|
20
20
|
}
|
|
21
|
-
const name = calledName(
|
|
21
|
+
const name = calledName(expression, statement.importClause);
|
|
22
22
|
if (name !== undefined) {
|
|
23
23
|
return { module: statement.moduleSpecifier.text, name };
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
return undefined;
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
export function importedCall(call: ts.CallExpression): { readonly module: string; readonly name: string } | undefined {
|
|
30
|
+
return importedName(call.expression);
|
|
31
|
+
}
|
|
@@ -4,6 +4,7 @@ import { type Project, projectsFor } from "#adapters/imports/projects.ts";
|
|
|
4
4
|
import { resolveImport } from "#adapters/imports/resolve.ts";
|
|
5
5
|
import type { Departure } from "#imports/departures.ts";
|
|
6
6
|
import type { ImportRequest } from "#imports/extract.ts";
|
|
7
|
+
import { isPackageRootConfig } from "#imports/isTooling.ts";
|
|
7
8
|
import { type SpecifierSite, specifierSites } from "#imports/specifiers.ts";
|
|
8
9
|
import { packageOf, type WorkspacePackage } from "#imports/workspace.ts";
|
|
9
10
|
import type { RuleInputs } from "#rule.ts";
|
|
@@ -36,7 +37,12 @@ function departureAt(context: FileContext, site: SpecifierSite): Departure | und
|
|
|
36
37
|
const target = resolveImport(context.root, noAmbient, request, context.from, context.project);
|
|
37
38
|
const local = target?.kind === "file" ? target.path : undefined;
|
|
38
39
|
const owner = local === undefined ? undefined : packageOf(context.packages, local);
|
|
39
|
-
const
|
|
40
|
+
const home = packageOf(context.packages, context.path);
|
|
41
|
+
const directory = home?.directory ?? ".";
|
|
42
|
+
if (local !== undefined && isPackageRootConfig(directory, context.path) && isPackageRootConfig(directory, local)) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const workspace = owner !== undefined && owner !== home ? owner.name : undefined;
|
|
40
46
|
return { ...site, file: context.path, installed: target?.kind === "external" ? target.package : undefined, workspace };
|
|
41
47
|
}
|
|
42
48
|
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
2
|
import preset from "#biome-preset" with { type: "json" };
|
|
3
3
|
|
|
4
|
-
const PATTERNS =
|
|
5
|
-
|
|
6
|
-
)
|
|
4
|
+
const PATTERNS = [
|
|
5
|
+
...preset.overrides.flatMap((override) =>
|
|
6
|
+
override.includes.includes("**/*.config.*") && override.linter?.rules?.style?.noDefaultExport === "off" ? override.includes : [],
|
|
7
|
+
),
|
|
8
|
+
// @shivaedev/deploy loads this module from its default path, so no import reaches it.
|
|
9
|
+
"**/.railway/postgres-schedules.ts",
|
|
10
|
+
];
|
|
7
11
|
|
|
8
12
|
export function isToolConfig(path: string): boolean {
|
|
9
13
|
return PATTERNS.some((pattern) => posix.matchesGlob(path, pattern));
|
|
@@ -4,9 +4,11 @@ import { isToolConfig } from "#adapters/isToolConfig.ts";
|
|
|
4
4
|
import { KNIP_ISSUES } from "#adapters/knip/findings.ts";
|
|
5
5
|
import { mockEntries } from "#adapters/knip/mockEntries.ts";
|
|
6
6
|
import { readPackageManifest } from "#adapters/manifestInventory.ts";
|
|
7
|
+
import type { ReachableBuilds } from "#adapters/reachableBuilds.ts";
|
|
7
8
|
import { scriptPaths } from "#adapters/scriptPaths.ts";
|
|
8
9
|
import { exportedModule, exportTargets } from "#imports/packageExports.ts";
|
|
9
10
|
import { packageOf, workspacePackages } from "#imports/workspace.ts";
|
|
11
|
+
import type { PackageManifest } from "#PackageManifest.ts";
|
|
10
12
|
import type { RuleInputs } from "#rule.ts";
|
|
11
13
|
|
|
12
14
|
const ENTRIES = ["**/*.{test,spec,stories,story}.{ts,tsx,mts,cts,js,jsx,mjs,cjs}", "src/test-support/**", "*.config.{ts,mts,cts,js,mjs,cjs}"];
|
|
@@ -38,10 +40,17 @@ function importPaths(imports: Readonly<Record<string, unknown>>) {
|
|
|
38
40
|
);
|
|
39
41
|
}
|
|
40
42
|
|
|
43
|
+
function externalDependencies(manifest: PackageManifest, externals: ReadonlySet<string>): readonly string[] {
|
|
44
|
+
const dependencies = [manifest.dependencies, manifest.devDependencies, manifest.optionalDependencies, manifest.peerDependencies].flatMap((names) =>
|
|
45
|
+
Object.keys(names ?? {}),
|
|
46
|
+
);
|
|
47
|
+
return dependencies.filter((name) => externals.has(name));
|
|
48
|
+
}
|
|
49
|
+
|
|
41
50
|
export async function knipConfiguration(
|
|
42
51
|
inputs: RuleInputs,
|
|
43
52
|
toolOwned: readonly string[] = [],
|
|
44
|
-
|
|
53
|
+
builds: ReachableBuilds = { entries: new Set(), externals: new Map() },
|
|
45
54
|
declared: readonly string[] = declaredEntries(inputs),
|
|
46
55
|
) {
|
|
47
56
|
const packages = await workspacePackages(inputs);
|
|
@@ -58,7 +67,7 @@ export async function knipConfiguration(
|
|
|
58
67
|
.filter(({ path }) => (packageOf(packages, path)?.directory ?? ".") === directory)
|
|
59
68
|
.map(({ path }) => posix.relative(directory, path));
|
|
60
69
|
const scriptEntries = sources.filter((path) => scripted.has(posix.join(directory, path)));
|
|
61
|
-
const bundled = sources.filter((path) =>
|
|
70
|
+
const bundled = sources.filter((path) => builds.entries.has(posix.join(directory, path)) || mocked.has(posix.join(directory, path)));
|
|
62
71
|
const bins = exportTargets(manifest.bin).map((target) => target.replace(/^\.\//u, ""));
|
|
63
72
|
const binaries = sources.filter(
|
|
64
73
|
(path) => bins.includes(path) || bins.includes(path.replace(/^src\//u, "dist/").replace(/\.(?<mode>[cm]?)tsx?$/u, ".$<mode>js")),
|
|
@@ -90,6 +99,8 @@ export async function knipConfiguration(
|
|
|
90
99
|
...styles,
|
|
91
100
|
...workspaceEntries,
|
|
92
101
|
],
|
|
102
|
+
// A bundle leaves its externals as runtime imports, which the bundling package's own dependencies satisfy.
|
|
103
|
+
ignoreDependencies: externalDependencies(manifest, builds.externals.get(directory === "." ? "" : directory) ?? new Set()),
|
|
93
104
|
paths: importPaths(manifest.imports ?? {}),
|
|
94
105
|
project: [...sources, ...ui, ...styles, ...workspaceEntries],
|
|
95
106
|
vitest: { config: ["vitest*.config.{ts,mts,cts,js,mjs,cjs}", "vitest.{workspace,projects}.{ts,mts,cts,js,mjs,cjs,json}"] },
|
package/src/adapters/knip/run.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { KNIP_ISSUES } from "#adapters/knip/findings.ts";
|
|
|
10
10
|
import { knipReport } from "#adapters/knip/report.ts";
|
|
11
11
|
import { sourceConfig } from "#adapters/knip/sourceConfig.ts";
|
|
12
12
|
import { readPackageManifest } from "#adapters/manifestInventory.ts";
|
|
13
|
-
import {
|
|
13
|
+
import { programmaticBuilds } from "#inventory/programmaticBuilds.ts";
|
|
14
14
|
import { toolOwnedFiles } from "#inventory/toolOwnedFiles.ts";
|
|
15
15
|
import type { RuleInputs } from "#rule.ts";
|
|
16
16
|
|
|
@@ -27,9 +27,9 @@ export const runKnip = Effect.fn("Knip.runKnip")(function* (inputs: RuleInputs)
|
|
|
27
27
|
return [];
|
|
28
28
|
}
|
|
29
29
|
const files = yield* toolOwnedFiles(inputs.root);
|
|
30
|
-
const
|
|
30
|
+
const builds = yield* programmaticBuilds(inputs);
|
|
31
31
|
const declared = declaredEntries(inputs);
|
|
32
|
-
const configuration = yield* Effect.promise(() => knipConfiguration(inputs, files,
|
|
32
|
+
const configuration = yield* Effect.promise(() => knipConfiguration(inputs, files, builds, declared));
|
|
33
33
|
const checked = new Set([
|
|
34
34
|
...inputs.files,
|
|
35
35
|
...declared,
|
|
@@ -2,10 +2,12 @@ import { Effect } from "effect";
|
|
|
2
2
|
import type { ImportAnalysisFailure } from "#adapters/ImportAnalysisFailure.ts";
|
|
3
3
|
import { readPackageManifest } from "#adapters/manifestInventory.ts";
|
|
4
4
|
import { importGraph } from "#imports/graph.ts";
|
|
5
|
+
import { isPackageRootConfig } from "#imports/isTooling.ts";
|
|
5
6
|
import { packageNameOf } from "#imports/packageName.ts";
|
|
6
7
|
import { packageOf } from "#imports/workspace.ts";
|
|
7
8
|
import { testName } from "#naming/testName.ts";
|
|
8
9
|
import type { Finding, RuleInputs } from "#rule.ts";
|
|
10
|
+
import { entryModules } from "#rules/effect/boundaryReference.ts";
|
|
9
11
|
|
|
10
12
|
const CLIENT_RENDERING_LIBRARIES = new Set([
|
|
11
13
|
"react",
|
|
@@ -21,10 +23,11 @@ const CLIENT_RENDERING_LIBRARIES = new Set([
|
|
|
21
23
|
"tailwind-merge",
|
|
22
24
|
]);
|
|
23
25
|
|
|
24
|
-
function exempt(path: string, name: string): boolean {
|
|
26
|
+
function exempt(directory: string, path: string, name: string): boolean {
|
|
25
27
|
return (
|
|
26
28
|
/(?:^|\/)(?:adapters|test-support)\//u.test(path)
|
|
27
|
-
|| (
|
|
29
|
+
|| isPackageRootConfig(directory, path)
|
|
30
|
+
|| (name === "vitest" && testName(path) !== undefined)
|
|
28
31
|
|| name === "effect"
|
|
29
32
|
|| name.startsWith("@effect/")
|
|
30
33
|
|| name.startsWith("@shivaedev/")
|
|
@@ -58,23 +61,23 @@ export const outsideImportFindings = Effect.fn("Imports.outsideImportFindings")(
|
|
|
58
61
|
.filter((edge) => edge.missing === undefined && !/^[.#/]/u.test(edge.specifier))
|
|
59
62
|
.map((edge) => ({ ...edge, name: packageNameOf(edge.specifier) })),
|
|
60
63
|
];
|
|
61
|
-
|
|
64
|
+
const outside = sites.filter((site) => {
|
|
62
65
|
const directory = packageOf(graph.packages, site.from)?.directory ?? ".";
|
|
63
|
-
|
|
66
|
+
return !(
|
|
64
67
|
workspace.has(site.name)
|
|
65
68
|
|| site.type
|
|
66
69
|
|| site.kind !== "import"
|
|
67
|
-
|| exempt(site.from, site.name)
|
|
70
|
+
|| exempt(directory, site.from, site.name)
|
|
68
71
|
|| !(site.name.startsWith("node:") || runtime.get(directory)?.has(site.name))
|
|
69
|
-
)
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
|
-
return [
|
|
73
|
-
{
|
|
74
|
-
file: site.from,
|
|
75
|
-
line: site.line,
|
|
76
|
-
message: `Imports outside library "${site.specifier}" as a value. Move the integration into an adapters/ module with explicit imports that exposes Effect values; use import type for types. Read environment variables with Config instead of process.env, including in adapters/.`,
|
|
77
|
-
},
|
|
78
|
-
];
|
|
72
|
+
);
|
|
79
73
|
});
|
|
74
|
+
const processImporters = new Set(outside.flatMap((site) => (site.name === "node:process" ? [site.from] : [])));
|
|
75
|
+
const entries = entryModules(inputs.sources.filter((file) => processImporters.has(file.path)));
|
|
76
|
+
return outside
|
|
77
|
+
.filter((site) => !(site.name === "node:process" && entries.has(site.from)))
|
|
78
|
+
.map((site) => ({
|
|
79
|
+
file: site.from,
|
|
80
|
+
line: site.line,
|
|
81
|
+
message: `Imports outside library "${site.specifier}" as a value. Move the integration into an adapters/ module with explicit imports that exposes Effect values; use import type for types. Read environment variables with Config instead of process.env, including in adapters/.`,
|
|
82
|
+
}));
|
|
80
83
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { bundlerModules, bundlerSources } from "#adapters/bundlerSources.ts";
|
|
2
|
+
import { bundlerTargets } from "#adapters/bundlerTargets.ts";
|
|
3
3
|
import { spawnedEntries } from "#adapters/spawnedEntries.ts";
|
|
4
4
|
import type { ImportGraph } from "#imports/graph.ts";
|
|
5
5
|
import type { PackageLayout } from "#naming/packageLayout.ts";
|
|
@@ -39,22 +39,31 @@ function importsOf(graph: ImportGraph): ReadonlyMap<string, readonly string[]> {
|
|
|
39
39
|
return imports;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
export
|
|
42
|
+
export interface ReachableBuilds {
|
|
43
|
+
readonly entries: ReadonlySet<string>;
|
|
44
|
+
readonly externals: ReadonlyMap<string, ReadonlySet<string>>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function reachableBuilds(inputs: RuleInputs, graph: ImportGraph, layout: PackageLayout): ReachableBuilds {
|
|
43
48
|
const imports = importsOf(graph);
|
|
44
49
|
const sources = new Map(inputs.sources.map((source) => [source.path, source]));
|
|
45
50
|
const syntax = bundlerSources(inputs.sources, graph);
|
|
51
|
+
const modules = bundlerModules(syntax, inputs.sources, graph);
|
|
46
52
|
const entries = new Set<string>();
|
|
53
|
+
const externals = new Map<string, ReadonlySet<string>>();
|
|
47
54
|
for (const [directory, scripts] of scriptsByPackage(layout)) {
|
|
48
|
-
|
|
55
|
+
const targets = [...reachable(scripts, imports)].flatMap((path) => {
|
|
49
56
|
const source = sources.get(path);
|
|
50
57
|
if (source === undefined) {
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
const bundled = syntax.has(path) ? bundlerEntries(source, inputs.root, directory, syntax) : [];
|
|
54
|
-
for (const entry of [...bundled, ...spawnedEntries(source, inputs.root, directory)].filter((target) => sources.has(target))) {
|
|
55
|
-
entries.add(entry);
|
|
58
|
+
return [];
|
|
56
59
|
}
|
|
60
|
+
const bundled = syntax.has(path) ? bundlerTargets(source, inputs.root, directory, syntax, modules) : { entries: [], externals: [] };
|
|
61
|
+
return [{ entries: [...bundled.entries, ...spawnedEntries(source, inputs.root, directory)], externals: bundled.externals }];
|
|
62
|
+
});
|
|
63
|
+
for (const entry of targets.flatMap((target) => target.entries).filter((target) => sources.has(target))) {
|
|
64
|
+
entries.add(entry);
|
|
57
65
|
}
|
|
66
|
+
externals.set(directory, new Set(targets.flatMap((target) => target.externals)));
|
|
58
67
|
}
|
|
59
|
-
return entries;
|
|
68
|
+
return { entries, externals };
|
|
60
69
|
}
|
package/src/imports/isTooling.ts
CHANGED
|
@@ -4,8 +4,12 @@ const TOOL_FOLDERS = new Set(["script", "scripts", "tooling"]);
|
|
|
4
4
|
|
|
5
5
|
const CONFIG_FILE = /(?:^|\.)config\.[cm]?[jt]sx?$/u;
|
|
6
6
|
|
|
7
|
-
export function
|
|
7
|
+
export function isPackageRootConfig(directory: string, path: string): boolean {
|
|
8
8
|
const local = posix.relative(directory, path);
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
return !local.includes("/") && CONFIG_FILE.test(local);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function isTooling(directory: string, path: string): boolean {
|
|
13
|
+
const [folder = ""] = posix.relative(directory, path).split("/");
|
|
14
|
+
return TOOL_FOLDERS.has(folder) || isPackageRootConfig(directory, path);
|
|
11
15
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import { reachableBuilds } from "#adapters/reachableBuilds.ts";
|
|
3
3
|
import { importGraph } from "#imports/graph.ts";
|
|
4
4
|
import { packageLayout } from "#naming/packageLayout.ts";
|
|
5
5
|
import { isTestCode } from "#naming/testName.ts";
|
|
6
6
|
import type { RuleInputs } from "#rule.ts";
|
|
7
7
|
|
|
8
|
-
export const
|
|
8
|
+
export const programmaticBuilds = Effect.fn("Inventory.programmaticBuilds")(function* (inputs: RuleInputs) {
|
|
9
9
|
const layout = yield* packageLayout(inputs);
|
|
10
10
|
const scripts = new Set([...layout.scripts, ...inputs.sources.filter((file) => isTestCode(file.path)).map((file) => file.path)]);
|
|
11
|
-
return scripts.size === 0
|
|
11
|
+
return scripts.size === 0
|
|
12
|
+
? { entries: new Set<string>(), externals: new Map<string, ReadonlySet<string>>() }
|
|
13
|
+
: reachableBuilds(inputs, yield* importGraph(inputs), { ...layout, scripts });
|
|
12
14
|
});
|
|
@@ -4,7 +4,7 @@ import { defineRule } from "#rule.ts";
|
|
|
4
4
|
export const adapterImports = defineRule({
|
|
5
5
|
check: outsideImportFindings,
|
|
6
6
|
description:
|
|
7
|
-
"Keep value imports of non-Effect runtime dependencies, peers, optional dependencies and Node builtins in adapters/. Type-only imports, workspace modules, test-support/ kits, Effect and @shivaedev packages, supported UI, routing and styling libraries in client/ folders and
|
|
7
|
+
"Keep value imports of non-Effect runtime dependencies, peers, optional dependencies and Node builtins in adapters/. Type-only imports, workspace modules, test-support/ kits, tool configs directly in a package's folder, Effect and @shivaedev packages, supported UI, routing and styling libraries in client/ folders, Vitest in tests and specs, and node:process in entry modules (cli/main/entry files, runMain callers and the import.meta.url === process.argv[1] guard) are accepted. Recharts is the stack's charting library, accepted in client/ folders; build charts with shadcn's Chart component (ChartContainer and friends) rather than raw recharts primitives where shadcn covers the case. Libraries that perform I/O or manage external state still need adapters/. Move integrations into adapters/ and expose Effect values. No config exceptions or inline suppressions; baseline existing debt.",
|
|
8
8
|
id: "effect/adapter-imports",
|
|
9
9
|
registrable: false,
|
|
10
10
|
});
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
|
+
import { Effect } from "effect";
|
|
2
3
|
import ts from "typescript";
|
|
4
|
+
import { isPackageRootConfig } from "#imports/isTooling.ts";
|
|
5
|
+
import { packageOf, workspacePackages } from "#imports/workspace.ts";
|
|
3
6
|
import { defineRule, type Finding, type RuleInputs } from "#rule.ts";
|
|
4
7
|
import { boundaryCalls } from "#rules/effect/boundaryCalls.ts";
|
|
5
8
|
import { boundaryEntryPoint } from "#rules/effect/boundaryReference.ts";
|
|
@@ -54,9 +57,6 @@ function checkBoundary(inputs: RuleInputs): readonly Finding[] {
|
|
|
54
57
|
const checker = program.getTypeChecker();
|
|
55
58
|
const call = boundaryCalls(checker);
|
|
56
59
|
return inputs.sources.flatMap((file) => {
|
|
57
|
-
if (/(?:^|\/)(?:adapters|test-support)\//u.test(file.path)) {
|
|
58
|
-
return [];
|
|
59
|
-
}
|
|
60
60
|
const source = parse(file) === undefined ? undefined : program.getSourceFile(join(inputs.root, file.path));
|
|
61
61
|
const findings: Finding[] = [];
|
|
62
62
|
const entryPoint = source !== undefined && boundaryEntryPoint(file.path, source, checker);
|
|
@@ -79,21 +79,29 @@ function checkBoundary(inputs: RuleInputs): readonly Finding[] {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export const effectBoundary = defineRule({
|
|
82
|
-
check: (inputs) =>
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
82
|
+
check: (inputs) =>
|
|
83
|
+
Effect.map(
|
|
84
|
+
Effect.promise(() => workspacePackages(inputs)),
|
|
85
|
+
(packages) => {
|
|
86
|
+
const groups = new Map<string, RuleInputs["sources"][number][]>();
|
|
87
|
+
for (const source of inputs.sources) {
|
|
88
|
+
if (
|
|
89
|
+
/(?:^|\/)(?:adapters|test-support)\//u.test(source.path)
|
|
90
|
+
|| !/\.[cm]?[jt]sx?$/u.test(source.path)
|
|
91
|
+
|| isPackageRootConfig(packageOf(packages, source.path)?.directory ?? ".", source.path)
|
|
92
|
+
) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const group = /^(?:packages|examples)\/[^/]+/u.exec(source.path)?.[0] ?? ".";
|
|
96
|
+
const sources = groups.get(group) ?? [];
|
|
97
|
+
sources.push(source);
|
|
98
|
+
groups.set(group, sources);
|
|
99
|
+
}
|
|
100
|
+
return [...groups.values()].flatMap((sources) => checkBoundary({ ...inputs, sources }));
|
|
101
|
+
},
|
|
102
|
+
),
|
|
95
103
|
description:
|
|
96
|
-
"Use Effect instead of Promise syntax and parameters, synchronous Node IO, throwing helper calls or Effect.sync with throwing APIs. Runners belong at entry points (including main.tsx and entry.tsx), tool configs and tests. Within Effect code, adapters/ and test-support/ folders are exempt; calls to throwing helpers defined in adapters/ are accepted. Use Effect services and tagged errors; bridge outside libraries in adapters/. No config exceptions or inline suppressions; baseline existing debt.",
|
|
104
|
+
"Use Effect instead of Promise syntax and parameters, synchronous Node IO, throwing helper calls or Effect.sync with throwing APIs. Runners belong at entry points (including main.tsx and entry.tsx), tool configs and tests. Within Effect code, adapters/ and test-support/ folders and tool configs directly in a package's folder, which their tool loads from that fixed path, are exempt; calls to throwing helpers defined in adapters/ are accepted. Use Effect services and tagged errors; bridge outside libraries in adapters/. No config exceptions or inline suppressions; baseline existing debt.",
|
|
97
105
|
id: "effect/boundary",
|
|
98
106
|
registrable: false,
|
|
99
107
|
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
+
import type { SourceFile } from "#rule.ts";
|
|
3
|
+
import { parse } from "#rules/parse.ts";
|
|
2
4
|
|
|
3
5
|
function importedReference(declaration: ts.Declaration): string | undefined {
|
|
4
6
|
if (ts.isImportSpecifier(declaration)) {
|
|
@@ -65,8 +67,15 @@ export function boundaryReference(checker: ts.TypeChecker, node: ts.Node, seen =
|
|
|
65
67
|
return ts.isIdentifier(node) ? symbolReference(checker, node, seen) : undefined;
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
const ENTRY_NAME = /(?:^|\/)(?:cli|main|entry|[^/]+-cli)\.[cm]?[jt]sx?$/u;
|
|
71
|
+
|
|
72
|
+
const RUNNER_HOST = /\.(?:config|test|spec)\.[cm]?[jt]sx?$/u;
|
|
73
|
+
|
|
74
|
+
export function entryModule(path: string, source: ts.SourceFile, checker: ts.TypeChecker): boolean {
|
|
75
|
+
if (RUNNER_HOST.test(path)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (ENTRY_NAME.test(path)) {
|
|
70
79
|
return true;
|
|
71
80
|
}
|
|
72
81
|
function visit(node: ts.Node): boolean {
|
|
@@ -83,3 +92,22 @@ export function boundaryEntryPoint(path: string, source: ts.SourceFile, checker:
|
|
|
83
92
|
}
|
|
84
93
|
return visit(source);
|
|
85
94
|
}
|
|
95
|
+
|
|
96
|
+
export function boundaryEntryPoint(path: string, source: ts.SourceFile, checker: ts.TypeChecker): boolean {
|
|
97
|
+
return RUNNER_HOST.test(path) || entryModule(path, source, checker);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Entry markers name their imports, so each module binds on its own without resolving the program behind it.
|
|
101
|
+
export function entryModules(sources: readonly SourceFile[]): ReadonlySet<string> {
|
|
102
|
+
const options: ts.CompilerOptions = { allowJs: true, noLib: true, noResolve: true, types: [] };
|
|
103
|
+
const host = ts.createCompilerHost(options);
|
|
104
|
+
const parsed = new Map(
|
|
105
|
+
sources.flatMap((file) => {
|
|
106
|
+
const source = parse(file);
|
|
107
|
+
return source === undefined ? [] : [[file.path, source] as const];
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
host.getSourceFile = (path) => parsed.get(path);
|
|
111
|
+
const checker = ts.createProgram([...parsed.keys()], options, host).getTypeChecker();
|
|
112
|
+
return new Set([...parsed].flatMap(([path, source]) => (entryModule(path, source, checker) ? [path] : [])));
|
|
113
|
+
}
|