@shivaedev/quality 0.77.0 → 0.78.1
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 +32 -0
- package/README.md +10 -14
- package/biome/preset.json +1 -1
- package/dist/adapters/declarationTagParameter.d.ts.map +1 -1
- package/dist/adapters/declarationTagParameter.js +4 -0
- package/dist/adapters/declarationTagParameter.js.map +1 -1
- package/dist/adapters/imports/resolve.d.ts.map +1 -1
- package/dist/adapters/imports/resolve.js +29 -5
- package/dist/adapters/imports/resolve.js.map +1 -1
- package/dist/adapters/presenceCheck.d.ts +3 -0
- package/dist/adapters/presenceCheck.d.ts.map +1 -0
- package/dist/adapters/presenceCheck.js +15 -0
- package/dist/adapters/presenceCheck.js.map +1 -0
- package/dist/engine/generateFirst.d.ts +5 -0
- package/dist/engine/generateFirst.d.ts.map +1 -0
- package/dist/engine/generateFirst.js +25 -0
- package/dist/engine/generateFirst.js.map +1 -0
- package/dist/engine/projectScripts.d.ts +11 -0
- package/dist/engine/projectScripts.d.ts.map +1 -0
- package/dist/engine/projectScripts.js +18 -0
- package/dist/engine/projectScripts.js.map +1 -0
- package/dist/engine/session.d.ts.map +1 -1
- package/dist/engine/session.js +2 -0
- package/dist/engine/session.js.map +1 -1
- package/dist/hooks/preCommit.d.ts.map +1 -1
- package/dist/hooks/preCommit.js +6 -19
- package/dist/hooks/preCommit.js.map +1 -1
- package/dist/imports/callsDefineModule.d.ts +3 -0
- package/dist/imports/callsDefineModule.d.ts.map +1 -0
- package/dist/imports/callsDefineModule.js +36 -0
- package/dist/imports/callsDefineModule.js.map +1 -0
- package/dist/imports/fences/check.d.ts.map +1 -1
- package/dist/imports/fences/check.js +3 -1
- package/dist/imports/fences/check.js.map +1 -1
- package/dist/imports/generatedFolders.d.ts +7 -0
- package/dist/imports/generatedFolders.d.ts.map +1 -0
- package/dist/imports/generatedFolders.js +21 -0
- package/dist/imports/generatedFolders.js.map +1 -0
- package/dist/imports/runtime.d.ts +1 -1
- package/dist/imports/runtime.d.ts.map +1 -1
- package/dist/imports/runtime.js +3 -2
- package/dist/imports/runtime.js.map +1 -1
- package/dist/naming/isOperationFile.d.ts +3 -0
- package/dist/naming/isOperationFile.d.ts.map +1 -0
- package/dist/naming/isOperationFile.js +42 -0
- package/dist/naming/isOperationFile.js.map +1 -0
- package/dist/naming/repeatedFolder.d.ts +2 -0
- package/dist/naming/repeatedFolder.d.ts.map +1 -0
- package/dist/naming/repeatedFolder.js +13 -0
- package/dist/naming/repeatedFolder.js.map +1 -0
- package/dist/rules/builtIn.d.ts +2 -4
- 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/effect/serviceParameters.js +1 -1
- package/dist/rules/effect/serviceParameters.js.map +1 -1
- package/dist/rules/files/namedAfterExport.d.ts.map +1 -1
- package/dist/rules/files/namedAfterExport.js +11 -12
- package/dist/rules/files/namedAfterExport.js.map +1 -1
- package/dist/rules/imports/resolvable.d.ts +1 -3
- package/dist/rules/imports/resolvable.d.ts.map +1 -1
- package/dist/rules/imports/resolvable.js +12 -10
- package/dist/rules/imports/resolvable.js.map +1 -1
- package/dist/rules/imports/runtime.d.ts.map +1 -1
- package/dist/rules/imports/runtime.js +26 -13
- package/dist/rules/imports/runtime.js.map +1 -1
- package/dist/rules/stories/rawServers.d.ts +2 -0
- package/dist/rules/stories/rawServers.d.ts.map +1 -0
- package/dist/rules/stories/rawServers.js +100 -0
- package/dist/rules/stories/rawServers.js.map +1 -0
- package/docs/north-star.md +1 -1
- package/package.json +4 -4
- package/src/adapters/declarationTagParameter.ts +4 -0
- package/src/adapters/imports/resolve.ts +31 -5
- package/src/adapters/presenceCheck.ts +17 -0
- package/src/engine/generateFirst.ts +32 -0
- package/src/engine/projectScripts.ts +25 -0
- package/src/engine/session.ts +2 -0
- package/src/hooks/preCommit.ts +10 -24
- package/src/imports/callsDefineModule.ts +43 -0
- package/src/imports/fences/check.ts +3 -1
- package/src/imports/generatedFolders.ts +28 -0
- package/src/imports/runtime.ts +7 -2
- package/src/naming/isOperationFile.ts +52 -0
- package/src/naming/repeatedFolder.ts +14 -0
- package/src/rules/builtIn.ts +2 -0
- package/src/rules/effect/serviceParameters.ts +1 -1
- package/src/rules/files/namedAfterExport.ts +14 -13
- package/src/rules/imports/resolvable.ts +13 -11
- package/src/rules/imports/runtime.ts +38 -14
- package/src/rules/stories/rawServers.ts +118 -0
- package/dist/imports/withoutGenerated.d.ts +0 -8
- package/dist/imports/withoutGenerated.d.ts.map +0 -1
- package/dist/imports/withoutGenerated.js +0 -65
- package/dist/imports/withoutGenerated.js.map +0 -1
- package/src/imports/withoutGenerated.ts +0 -82
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { unwrapExpression } from "#adapters/serviceAssemblySyntax.ts";
|
|
3
|
+
import { ownExports } from "#naming/ownExports.ts";
|
|
4
|
+
|
|
5
|
+
const OPERATION_MODULE = "@shivaedev/contract/operation.ts";
|
|
6
|
+
|
|
7
|
+
const OPERATIONS = new Set(["command", "query"]);
|
|
8
|
+
|
|
9
|
+
function operationImports(statement: ts.Statement): readonly string[] {
|
|
10
|
+
if (
|
|
11
|
+
!(ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier))
|
|
12
|
+
|| statement.moduleSpecifier.text !== OPERATION_MODULE
|
|
13
|
+
|| statement.importClause === undefined
|
|
14
|
+
|| statement.importClause.isTypeOnly
|
|
15
|
+
) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
const bindings = statement.importClause.namedBindings;
|
|
19
|
+
if (bindings === undefined || !ts.isNamedImports(bindings)) {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
return bindings.elements.flatMap((element) =>
|
|
23
|
+
!element.isTypeOnly && OPERATIONS.has((element.propertyName ?? element.name).text) ? [element.name.text] : [],
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function declares(statement: ts.Statement, name: string, operations: ReadonlySet<string>): boolean {
|
|
28
|
+
return (
|
|
29
|
+
ts.isVariableStatement(statement)
|
|
30
|
+
&& statement.declarationList.declarations.some((declaration) => {
|
|
31
|
+
if (!ts.isIdentifier(declaration.name) || declaration.name.text !== name || declaration.initializer === undefined) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const value = unwrapExpression(declaration.initializer);
|
|
35
|
+
if (!ts.isCallExpression(value)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
const called = unwrapExpression(value.expression);
|
|
39
|
+
return ts.isIdentifier(called) && operations.has(called.text);
|
|
40
|
+
})
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isOperationFile(source: ts.SourceFile): boolean {
|
|
45
|
+
const exports = ownExports(source);
|
|
46
|
+
const [only] = exports;
|
|
47
|
+
if (exports.length !== 1 || only === undefined || only.kind !== "value") {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const operations = new Set(source.statements.flatMap(operationImports));
|
|
51
|
+
return operations.size > 0 && source.statements.some((statement) => declares(statement, only.name, operations));
|
|
52
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { sameWord, wordsOf } from "#naming/words.ts";
|
|
2
|
+
|
|
3
|
+
function letters(name: string): string {
|
|
4
|
+
return name.replace(/[^a-z0-9]/giu, "").toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function repeatedFolder(stem: string, folders: readonly string[]): string | undefined {
|
|
8
|
+
const own = folders.at(-1);
|
|
9
|
+
if (own !== undefined && letters(own) === letters(stem)) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const words = wordsOf(stem);
|
|
13
|
+
return folders.find((folder) => wordsOf(folder).some((word) => words.some((stemWord) => sameWord(stemWord, word))));
|
|
14
|
+
}
|
package/src/rules/builtIn.ts
CHANGED
|
@@ -50,6 +50,7 @@ import { noLimitImports } from "#rules/stories/noLimitImports.ts";
|
|
|
50
50
|
import { positiveRefusals } from "#rules/stories/positiveRefusals.ts";
|
|
51
51
|
import { queueReadouts } from "#rules/stories/queueReadouts.ts";
|
|
52
52
|
import { queueSql } from "#rules/stories/queueSql.ts";
|
|
53
|
+
import { rawServers } from "#rules/stories/rawServers.ts";
|
|
53
54
|
import { storySetup } from "#rules/stories/setup.ts";
|
|
54
55
|
import { specHelpers } from "#rules/stories/specHelpers.ts";
|
|
55
56
|
import { storySpecs } from "#rules/stories/specs.ts";
|
|
@@ -112,6 +113,7 @@ export const builtInRules = [
|
|
|
112
113
|
adapterOnlyFakes,
|
|
113
114
|
globalRetries,
|
|
114
115
|
databaseUrl,
|
|
116
|
+
rawServers,
|
|
115
117
|
storyEngine,
|
|
116
118
|
queueSql,
|
|
117
119
|
queueReadouts,
|
|
@@ -5,7 +5,7 @@ export const serviceParameters = defineRule({
|
|
|
5
5
|
check: (inputs) => packageFindings(inputs, "effect/service-parameters"),
|
|
6
6
|
depth: "deep",
|
|
7
7
|
description:
|
|
8
|
-
"Exported and local functions and public methods take services from Effect's environment, including aliases, constraints, inferred/destructured parameters and factory returns. Service keys/classes, non-empty Contexts and implementation shapes are services; Effect/Stream requirements are computations. Service implementation methods, framework callbacks, module-level React components and their props, owned initializer state, foreign host callbacks in object literals and classes, owned foreign resources, Layer constructors, registries and RPC clients held by plain functions outside Effect bodies, and tags only handed to middleware declarations or Context.pick are accepted. Within Effect code, test-support/ folders are exempt. adapters/ folders still follow this rule and the ambient runtime bans. No config exceptions or inline suppressions; baseline existing debt.",
|
|
8
|
+
"Exported and local functions and public methods take services from Effect's environment, including aliases, constraints, inferred/destructured parameters and factory returns. Service keys/classes, non-empty Contexts and implementation shapes are services; Effect/Stream requirements are computations. Service implementation methods, framework callbacks, module-level React components and their props, owned initializer state, foreign host callbacks in object literals and classes, owned foreign resources, Layer constructors, registries and RPC clients held by plain functions outside Effect bodies, and tags only handed to middleware declarations or Context.pick, besides comparing them with undefined or null, are accepted. Within Effect code, test-support/ folders are exempt. adapters/ folders still follow this rule and the ambient runtime bans. No config exceptions or inline suppressions; baseline existing debt.",
|
|
9
9
|
id: "effect/service-parameters",
|
|
10
10
|
registrable: false,
|
|
11
11
|
});
|
|
@@ -11,11 +11,13 @@ import { knownDataExports } from "#naming/adapters/plainData.ts";
|
|
|
11
11
|
import { constantModule } from "#naming/constantModule.ts";
|
|
12
12
|
import { exportFits } from "#naming/exportFits.ts";
|
|
13
13
|
import { isMigrationFile } from "#naming/isMigrationFile.ts";
|
|
14
|
+
import { isOperationFile } from "#naming/isOperationFile.ts";
|
|
14
15
|
import { mainExport } from "#naming/mainExport.ts";
|
|
15
16
|
import { moduleRoot } from "#naming/moduleRoot.ts";
|
|
16
17
|
import { ownExports } from "#naming/ownExports.ts";
|
|
17
18
|
import { type PackageLayout, packageLayout } from "#naming/packageLayout.ts";
|
|
18
|
-
import {
|
|
19
|
+
import { repeatedFolder } from "#naming/repeatedFolder.ts";
|
|
20
|
+
import { CAMEL, KEBAB } from "#naming/words.ts";
|
|
19
21
|
import { defineRule, type Finding, type SourceFile } from "#rule.ts";
|
|
20
22
|
import { parse } from "#rules/parse.ts";
|
|
21
23
|
|
|
@@ -36,14 +38,6 @@ function stemOf(path: string): string {
|
|
|
36
38
|
return posix.basename(path).replace(/\.[^.]+$/u, "");
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
function repeated(stem: string, folders: readonly string[]): string | undefined {
|
|
40
|
-
if (folders.at(-1) === stem) {
|
|
41
|
-
return undefined;
|
|
42
|
-
}
|
|
43
|
-
const words = wordsOf(stem);
|
|
44
|
-
return folders.find((folder) => wordsOf(folder).some((word) => words.some((own) => sameWord(own, word))));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
41
|
function nameFinding(file: SourceFile, stem: string, folders: readonly string[]): string | undefined {
|
|
48
42
|
const source = parse(file);
|
|
49
43
|
const exports = source === undefined ? [] : ownExports(source);
|
|
@@ -85,8 +79,12 @@ function nameFinding(file: SourceFile, stem: string, folders: readonly string[])
|
|
|
85
79
|
}
|
|
86
80
|
|
|
87
81
|
function findingsOf(file: SourceFile, layout: PackageLayout): readonly Finding[] {
|
|
88
|
-
const source =
|
|
89
|
-
if (
|
|
82
|
+
const source = parse(file);
|
|
83
|
+
if (
|
|
84
|
+
posix.basename(file.path) === "service.ts"
|
|
85
|
+
&& source !== undefined
|
|
86
|
+
&& directServiceDefinitions(source, serviceAssemblyImports(source).definitions).length > 0
|
|
87
|
+
) {
|
|
90
88
|
return [];
|
|
91
89
|
}
|
|
92
90
|
const stem = stemOf(file.path);
|
|
@@ -103,7 +101,10 @@ function findingsOf(file: SourceFile, layout: PackageLayout): readonly Finding[]
|
|
|
103
101
|
}
|
|
104
102
|
const root = moduleRoot(file.path);
|
|
105
103
|
const admin = root === undefined ? undefined : /^(?:client|server)\/(?<path>admin\/.+)$/u.exec(file.path.slice(root.length + 1))?.groups?.path;
|
|
106
|
-
const folder =
|
|
104
|
+
const folder =
|
|
105
|
+
source !== undefined && isOperationFile(source)
|
|
106
|
+
? undefined
|
|
107
|
+
: repeatedFolder(stem, admin === undefined ? folders : posix.dirname(admin).split("/"));
|
|
107
108
|
const messages = [
|
|
108
109
|
nameFinding(file, stem, folders),
|
|
109
110
|
folder === undefined ? undefined : `"${stem}" repeats its folder "${folder}". The folder is the prefix: items/listItems.ts is items/list.ts.`,
|
|
@@ -134,7 +135,7 @@ export const namedAfterExport = defineRule({
|
|
|
134
135
|
.flatMap((file) => findingsOf(file, layout));
|
|
135
136
|
}),
|
|
136
137
|
description:
|
|
137
|
-
"A file is named after its main runtime export, ignoring private constants and plain-data constants beside it, with its folders as the prefix, including in test-support; module client/admin and server/admin files may repeat names from above the admin folder. defineService declarations use service.ts; TanStack Router configuration owns route filenames; programmatic bundler entries and tool configs keep their role names. A constants module holds only plain data and derived types, in a camelCase file named for words every constant shares (timeouts.ts), limits for max/min/limit constants, or defaults for default constants. Split unrelated constants. A file of several non-constant exports is a camelCase topic file, and a script is kebab-case. Rename the file or the export; nothing renames them for you.",
|
|
138
|
+
"A file is named after its main runtime export, ignoring private constants and plain-data constants beside it, with its folders as the prefix, including in test-support; module client/admin and server/admin files may repeat names from above the admin folder. A file named like its own folder, ignoring case and separators, does not repeat it: notes/Notes.ts exports Notes and reading-lists/ReadingLists.ts exports ReadingLists. A file whose one export is a query or command from @shivaedev/contract/operation.ts may repeat words of the folders above it: reading-lists/server/List.ts exports List. defineService declarations use service.ts; TanStack Router configuration owns route filenames; programmatic bundler entries and tool configs keep their role names. A constants module holds only plain data and derived types, in a camelCase file named for words every constant shares (timeouts.ts), limits for max/min/limit constants, or defaults for default constants. Split unrelated constants. A file of several non-constant exports is a camelCase topic file, and a script is kebab-case. Rename the file or the export; nothing renames them for you.",
|
|
138
139
|
id: "files/named-after-export",
|
|
139
140
|
options: Schema.toStandardSchemaV1(NamedAfterExportOptions, {
|
|
140
141
|
parseOptions: { errors: "all", onExcessProperty: "error" },
|
|
@@ -1,27 +1,29 @@
|
|
|
1
|
-
import { Effect
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { generatedFolders, generatedFrom } from "#imports/generatedFolders.ts";
|
|
3
|
+
import { importGraph, type UnresolvedImport } from "#imports/graph.ts";
|
|
4
4
|
import { defineRule } from "#rule.ts";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
function repair(folders: readonly string[], request: UnresolvedImport): string {
|
|
7
|
+
const module = request.missing === undefined ? undefined : generatedFrom(folders, request.missing);
|
|
8
|
+
return module === undefined
|
|
9
|
+
? "Fix the path, install or declare the package, or declare the module in a .d.ts file among the sources."
|
|
10
|
+
: `The project's generate script writes ${request.missing} only when ${module} defines a module. Fix the path, or define the module there.`;
|
|
11
|
+
}
|
|
9
12
|
|
|
10
13
|
export const importsResolvable = defineRule({
|
|
11
|
-
check: Effect.fn("Imports.check")(function* (
|
|
14
|
+
check: Effect.fn("Imports.check")(function* (inputs) {
|
|
12
15
|
const graph = yield* importGraph(inputs);
|
|
13
|
-
const
|
|
16
|
+
const folders = generatedFolders(graph.packages);
|
|
14
17
|
return [
|
|
15
18
|
...graph.configFindings,
|
|
16
|
-
...unresolved.map((request) => ({
|
|
19
|
+
...graph.unresolved.map((request) => ({
|
|
17
20
|
file: request.from,
|
|
18
21
|
line: request.line,
|
|
19
|
-
message: `Cannot resolve "${request.specifier}".
|
|
22
|
+
message: `Cannot resolve "${request.specifier}". ${repair(folders, request)}`,
|
|
20
23
|
subject: request.specifier,
|
|
21
24
|
})),
|
|
22
25
|
];
|
|
23
26
|
}),
|
|
24
27
|
description: "Every import resolves to a file, a package or a declared module, so no edge of the import graph goes unchecked.",
|
|
25
28
|
id: "imports/resolvable",
|
|
26
|
-
options: Schema.toStandardSchemaV1(ResolvableOptions, { parseOptions: { errors: "all", onExcessProperty: "error" } }),
|
|
27
29
|
});
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { builtinName } from "#adapters/builtinName.ts";
|
|
3
|
+
import { callsDefineModule } from "#imports/callsDefineModule.ts";
|
|
3
4
|
import type { Endpoint } from "#imports/Endpoint.ts";
|
|
4
5
|
import { outgoing, walkFrom } from "#imports/fences/reach.ts";
|
|
6
|
+
import { followedEdges, generatedFolders } from "#imports/generatedFolders.ts";
|
|
5
7
|
import { type ImportEdge, type ImportGraph, importGraph } from "#imports/graph.ts";
|
|
6
8
|
import { nodePlatformBarrels } from "#imports/nodePlatformBarrels.ts";
|
|
7
9
|
import { type Runtime, runtimeDeclarationProblems, runtimeOf } from "#imports/runtime.ts";
|
|
@@ -17,14 +19,31 @@ function forbiddenImport(from: Runtime, to: Runtime | undefined, builtin: boolea
|
|
|
17
19
|
|
|
18
20
|
const REPAIR = "Move runtime-dependent code into src/client/ or src/server/ and keep shared code independent of both runtimes.";
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
const MODULE_REPAIR = "Import the module's generated client from its generated folder instead; it holds no server code.";
|
|
23
|
+
|
|
24
|
+
interface Classified {
|
|
25
|
+
readonly definedModules: ReadonlySet<string>;
|
|
26
|
+
readonly runtime: (path: string) => Runtime | undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Forbidden {
|
|
30
|
+
readonly repair: string;
|
|
31
|
+
readonly target: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function forbiddenTarget(from: Runtime, { definedModules, runtime }: Classified) {
|
|
35
|
+
return (endpoint: Endpoint): Forbidden | undefined => {
|
|
22
36
|
const to = endpoint.kind === "file" ? runtime(endpoint.path) : undefined;
|
|
23
37
|
const builtin = endpoint.kind === "external" && builtinName(endpoint.specifier) !== undefined;
|
|
24
38
|
if (!forbiddenImport(from, to, builtin)) {
|
|
25
39
|
return undefined;
|
|
26
40
|
}
|
|
27
|
-
|
|
41
|
+
if (builtin) {
|
|
42
|
+
return { repair: REPAIR, target: "Node built-in" };
|
|
43
|
+
}
|
|
44
|
+
return endpoint.kind === "file" && definedModules.has(endpoint.path)
|
|
45
|
+
? { repair: MODULE_REPAIR, target: "a defined module, which is server code," }
|
|
46
|
+
: { repair: REPAIR, target: `${to} code` };
|
|
28
47
|
};
|
|
29
48
|
}
|
|
30
49
|
|
|
@@ -38,34 +57,37 @@ interface IgnoredReach {
|
|
|
38
57
|
}
|
|
39
58
|
|
|
40
59
|
// An ignored module, such as generated output, is never reported itself, so its forbidden imports land on the checked file that reaches them.
|
|
41
|
-
function ignoredFindings(ignored: IgnoredReach, edge: ImportEdge, from: Runtime,
|
|
60
|
+
function ignoredFindings(ignored: IgnoredReach, edge: ImportEdge, from: Runtime, classified: Classified) {
|
|
42
61
|
if (edge.to.kind !== "file" || !ignored.modules.has(edge.to.path)) {
|
|
43
62
|
return [];
|
|
44
63
|
}
|
|
45
|
-
return walkFrom(edge.to.path, ignored.edgesFrom, forbiddenTarget(from,
|
|
64
|
+
return walkFrom(edge.to.path, ignored.edgesFrom, forbiddenTarget(from, classified)).reached.map(({ match, path }) => ({
|
|
46
65
|
file: edge.from,
|
|
47
66
|
line: edge.line,
|
|
48
|
-
message: `${importer(from)} code reaches ${
|
|
67
|
+
message: `${importer(from)} code reaches ${match.target} through "${edge.specifier}", which git ignores: ${path.join(" -> ")}. ${match.repair}`,
|
|
49
68
|
subject: edge.specifier,
|
|
50
69
|
}));
|
|
51
70
|
}
|
|
52
71
|
|
|
53
|
-
function runtimeFindings(graph: ImportGraph,
|
|
54
|
-
const ignored = {
|
|
72
|
+
function runtimeFindings(graph: ImportGraph, classified: Classified) {
|
|
73
|
+
const ignored = {
|
|
74
|
+
edgesFrom: outgoing(followedEdges(generatedFolders(graph.packages), graph.ignoredEdges)),
|
|
75
|
+
modules: new Set(graph.ignoredModules),
|
|
76
|
+
};
|
|
55
77
|
return graph.edges.flatMap((edge) => {
|
|
56
|
-
const from = runtime(edge.from);
|
|
78
|
+
const from = classified.runtime(edge.from);
|
|
57
79
|
if (from === undefined || isTestCode(edge.from)) {
|
|
58
80
|
return [];
|
|
59
81
|
}
|
|
60
|
-
const forbidden = forbiddenTarget(from,
|
|
82
|
+
const forbidden = forbiddenTarget(from, classified)(edge.to);
|
|
61
83
|
if (forbidden === undefined) {
|
|
62
|
-
return ignoredFindings(ignored, edge, from,
|
|
84
|
+
return ignoredFindings(ignored, edge, from, classified);
|
|
63
85
|
}
|
|
64
86
|
return [
|
|
65
87
|
{
|
|
66
88
|
file: edge.from,
|
|
67
89
|
line: edge.line,
|
|
68
|
-
message: `${importer(from)} code imports ${forbidden} through "${edge.specifier}". ${
|
|
90
|
+
message: `${importer(from)} code imports ${forbidden.target} through "${edge.specifier}". ${forbidden.repair}`,
|
|
69
91
|
subject: edge.specifier,
|
|
70
92
|
},
|
|
71
93
|
];
|
|
@@ -76,8 +98,10 @@ export const importsRuntime = defineRule({
|
|
|
76
98
|
check: (inputs) =>
|
|
77
99
|
Effect.gen(function* () {
|
|
78
100
|
const graph = yield* importGraph(inputs);
|
|
79
|
-
const
|
|
80
|
-
const
|
|
101
|
+
const definedModules = new Set(inputs.sources.filter(callsDefineModule).map((file) => file.path));
|
|
102
|
+
const runtime = yield* runtimeOf(inputs, graph.packages, definedModules);
|
|
103
|
+
const classified = { definedModules, runtime };
|
|
104
|
+
const imports = [...runtimeFindings(graph, classified), ...inputs.sources.flatMap(nodePlatformBarrels)];
|
|
81
105
|
const declarations = (yield* runtimeDeclarationProblems(inputs, graph.packages)).map(({ directory, message }) => ({
|
|
82
106
|
file: directory === "." ? "package.json" : `${directory}/package.json`,
|
|
83
107
|
line: 1,
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { isTestSupport } from "#naming/testName.ts";
|
|
3
|
+
import { defineRule, type Finding, type SourceFile } from "#rule.ts";
|
|
4
|
+
import { parse } from "#rules/parse.ts";
|
|
5
|
+
import { type ImportBinding, importBindings } from "#rules/stories/importBindings.ts";
|
|
6
|
+
import { memberName, nodesWhere, unwrapped } from "#rules/stories/syntax.ts";
|
|
7
|
+
|
|
8
|
+
const SERVER_MODULES = new Set(["http", "https", "net"]);
|
|
9
|
+
const CONSTRUCTORS: ReadonlyMap<string, ts.SyntaxKind> = new Map([
|
|
10
|
+
["createServer", ts.SyntaxKind.CallExpression],
|
|
11
|
+
["Server", ts.SyntaxKind.NewExpression],
|
|
12
|
+
]);
|
|
13
|
+
const HELPERS =
|
|
14
|
+
"loopbackServer from @shivaedev/test-story/server/adapters/loopback.ts, which listens on a loopback port and closes it with the story's scope, or fake an outside HTTP API with fakeHttpService from @shivaedev/test-story/server/fakeHttpService.ts.";
|
|
15
|
+
const REPAIR = `Hand the server to ${HELPERS}`;
|
|
16
|
+
const NET_REPAIR = `For a free port or a listening endpoint, hand a node:http server to ${HELPERS}`;
|
|
17
|
+
|
|
18
|
+
function serverModule(binding: ImportBinding | undefined): string | undefined {
|
|
19
|
+
const name = binding?.module.replace(/^node:/u, "");
|
|
20
|
+
return name !== undefined && SERVER_MODULES.has(name) ? `node:${name}` : undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type Construction = ts.CallExpression | ts.NewExpression;
|
|
24
|
+
|
|
25
|
+
function isConstruction(node: ts.Node): node is Construction {
|
|
26
|
+
return ts.isCallExpression(node) || ts.isNewExpression(node);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function constructedFrom(node: Construction, bindings: ReadonlyMap<string, ImportBinding>): string | undefined {
|
|
30
|
+
const callee = unwrapped(node.expression);
|
|
31
|
+
if (ts.isIdentifier(callee)) {
|
|
32
|
+
const binding = bindings.get(callee.text);
|
|
33
|
+
return binding !== undefined && CONSTRUCTORS.get(binding.imported) === node.kind ? serverModule(binding) : undefined;
|
|
34
|
+
}
|
|
35
|
+
const receiver = ts.isPropertyAccessExpression(callee) ? unwrapped(callee.expression) : undefined;
|
|
36
|
+
const binding = receiver !== undefined && ts.isIdentifier(receiver) ? bindings.get(receiver.text) : undefined;
|
|
37
|
+
return CONSTRUCTORS.get(memberName(callee) ?? "") === node.kind ? serverModule(binding) : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function outermost(node: ts.Node): ts.Node {
|
|
41
|
+
const parent = node.parent;
|
|
42
|
+
const wraps =
|
|
43
|
+
ts.isParenthesizedExpression(parent)
|
|
44
|
+
|| ts.isAsExpression(parent)
|
|
45
|
+
|| ts.isSatisfiesExpression(parent)
|
|
46
|
+
|| ts.isNonNullExpression(parent)
|
|
47
|
+
|| ts.isAwaitExpression(parent);
|
|
48
|
+
return wraps ? outermost(parent) : node;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function boundName(node: ts.Node): string | undefined {
|
|
52
|
+
const parent = node.parent;
|
|
53
|
+
if (ts.isVariableDeclaration(parent) && parent.initializer === node && ts.isIdentifier(parent.name)) {
|
|
54
|
+
return parent.name.text;
|
|
55
|
+
}
|
|
56
|
+
const assigned = ts.isBinaryExpression(parent) && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken && parent.right === node;
|
|
57
|
+
return assigned && ts.isIdentifier(parent.left) ? parent.left.text : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// A node:net server that handles its own sockets speaks a protocol other than HTTP, which neither shared helper serves.
|
|
61
|
+
function handlesSockets(created: Construction): boolean {
|
|
62
|
+
return (created.arguments ?? [])
|
|
63
|
+
.map(unwrapped)
|
|
64
|
+
.some((value) => ts.isArrowFunction(value) || ts.isFunctionExpression(value) || ts.isIdentifier(value));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function socketPath(argument: ts.Expression | undefined): boolean {
|
|
68
|
+
const value = argument === undefined ? undefined : unwrapped(argument);
|
|
69
|
+
if (value === undefined || !ts.isObjectLiteralExpression(value)) {
|
|
70
|
+
return value !== undefined && (ts.isStringLiteralLike(value) || ts.isTemplateExpression(value));
|
|
71
|
+
}
|
|
72
|
+
return value.properties.some((property) => property.name !== undefined && ts.isIdentifier(property.name) && property.name.text === "path");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// loopbackServer serves a loopback TCP port, so a server listening on a Unix socket or pipe path has no shared replacement.
|
|
76
|
+
function portListeners(source: ts.SourceFile): readonly ts.Node[] {
|
|
77
|
+
return nodesWhere(
|
|
78
|
+
source,
|
|
79
|
+
(node) => ts.isCallExpression(node) && memberName(node.expression) === "listen" && !socketPath(node.arguments[0]),
|
|
80
|
+
).flatMap((call) => {
|
|
81
|
+
const callee = ts.isCallExpression(call) ? unwrapped(call.expression) : undefined;
|
|
82
|
+
return callee !== undefined && ts.isPropertyAccessExpression(callee) ? [unwrapped(callee.expression)] : [];
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function findings(file: SourceFile): readonly Finding[] {
|
|
87
|
+
const source = isTestSupport(file.path) && file.text.includes("listen") ? parse(file) : undefined;
|
|
88
|
+
if (source === undefined) {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const bindings = importBindings(source);
|
|
92
|
+
const listeners = portListeners(source);
|
|
93
|
+
const names = new Set(listeners.flatMap((listener) => (ts.isIdentifier(listener) ? [listener.text] : [])));
|
|
94
|
+
return nodesWhere(source, isConstruction)
|
|
95
|
+
.filter(isConstruction)
|
|
96
|
+
.flatMap((created) => {
|
|
97
|
+
const module = constructedFrom(created, bindings);
|
|
98
|
+
const name = boundName(outermost(created));
|
|
99
|
+
const listened = listeners.includes(created) || (name !== undefined && names.has(name));
|
|
100
|
+
if (module === undefined || !listened || (module === "node:net" && handlesSockets(created))) {
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
return [
|
|
104
|
+
{
|
|
105
|
+
file: file.path,
|
|
106
|
+
line: source.getLineAndCharacterOfPosition(created.getStart(source)).line + 1,
|
|
107
|
+
message: `Starts a ${module} server with listen in test-support. ${module === "node:net" ? NET_REPAIR : REPAIR}`,
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const rawServers = defineRule({
|
|
114
|
+
check: ({ sources }) => sources.flatMap(findings),
|
|
115
|
+
description:
|
|
116
|
+
"Test-support listens on no loopback port with a node:http, node:https or node:net server of its own: hand a Node HTTP server to test-story's loopbackServer, or fake an outside HTTP API with fakeHttpService. Unix socket listeners and node:net servers that handle their own sockets are accepted.",
|
|
117
|
+
id: "tests/raw-servers",
|
|
118
|
+
});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
|
-
import { SetupFailure } from "#failure.ts";
|
|
3
|
-
import type { ImportGraph } from "#imports/graph.ts";
|
|
4
|
-
import type { RuleInputs } from "#rule.ts";
|
|
5
|
-
type Reader = Pick<RuleInputs, "readText" | "root">;
|
|
6
|
-
export declare const withoutGenerated: (reader: Reader, graph: ImportGraph, declared: readonly string[]) => Effect.Effect<import("#imports/graph.ts").UnresolvedImport[], import("../inventory/filesystem.ts").FilesystemFailure | SetupFailure, import("effect/process/ChildProcessSpawner").ChildProcessSpawner>;
|
|
7
|
-
export {};
|
|
8
|
-
//# sourceMappingURL=withoutGenerated.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"withoutGenerated.d.ts","sourceRoot":"","sources":["../../src/imports/withoutGenerated.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,KAAK,MAAM,GAAG,IAAI,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAAC,CAAC;AAgEpD,eAAO,MAAM,gBAAgB,6QAS3B,CAAC"}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { basename, posix, relative } from "node:path";
|
|
2
|
-
import { Effect } from "effect";
|
|
3
|
-
import { SetupFailure } from "#failure.ts";
|
|
4
|
-
import { emptyScope, verdictFor, withIgnoreFile } from "#inventory/ignoreScope.ts";
|
|
5
|
-
import { trackedPaths } from "#inventory/trackedPaths.ts";
|
|
6
|
-
const IGNORE_FILE = ".gitignore";
|
|
7
|
-
const LEADING_DOT = /^\.\//u;
|
|
8
|
-
const TRAILING_SLASHES = /\/+$/u;
|
|
9
|
-
const NODE_MODULES = "node_modules";
|
|
10
|
-
const PLACEHOLDERS = new Set([".gitkeep", ".keep", "README.md"]);
|
|
11
|
-
const withIgnoreFileIn = Effect.fn("Imports.withIgnoreFileIn")(function* (reader, scope, directory) {
|
|
12
|
-
const contents = yield* reader.readText(posix.join(directory, IGNORE_FILE));
|
|
13
|
-
return contents === undefined || contents === "" ? scope : withIgnoreFile(scope, posix.join(reader.root, directory), contents);
|
|
14
|
-
});
|
|
15
|
-
const ignored = Effect.fn("Imports.ignored")(function* (reader, file) {
|
|
16
|
-
const parts = file.split("/");
|
|
17
|
-
let scope = emptyScope;
|
|
18
|
-
for (const index of parts.keys()) {
|
|
19
|
-
scope = yield* withIgnoreFileIn(reader, scope, parts.slice(0, index).join("/") || ".");
|
|
20
|
-
if (verdictFor(scope, posix.join(reader.root, ...parts.slice(0, index + 1)), index < parts.length - 1) === "ignored") {
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return false;
|
|
25
|
-
});
|
|
26
|
-
function under(folder, path) {
|
|
27
|
-
return path?.startsWith(`${folder}/`) === true;
|
|
28
|
-
}
|
|
29
|
-
const contentIssue = Effect.fn("Imports.contentIssue")(function* (reader, folder, named) {
|
|
30
|
-
const verdicts = yield* Effect.forEach(named, (file) => ignored(reader, file), { concurrency: "unbounded" });
|
|
31
|
-
if (verdicts.includes(false)) {
|
|
32
|
-
return `"${folder}" is not ignored by git, so it holds source, not generated output`;
|
|
33
|
-
}
|
|
34
|
-
const kept = (yield* trackedPaths(reader.root, folder)).find((file) => !PLACEHOLDERS.has(basename(file)));
|
|
35
|
-
return kept === undefined ? undefined : `"${folder}" holds files git tracks, such as ${relative(reader.root, kept)}, so it is not generated output`;
|
|
36
|
-
});
|
|
37
|
-
function placeIssue(graph, folder) {
|
|
38
|
-
if (folder === "" || folder.startsWith("../") || posix.isAbsolute(folder)) {
|
|
39
|
-
return `"${folder}" is no folder inside the repository`;
|
|
40
|
-
}
|
|
41
|
-
if (folder.split("/").includes(NODE_MODULES)) {
|
|
42
|
-
return `"${folder}" lies in node_modules, which holds installed packages, not generated output`;
|
|
43
|
-
}
|
|
44
|
-
return graph.edges.some((edge) => edge.to.kind === "file" && under(folder, edge.to.path))
|
|
45
|
-
? undefined
|
|
46
|
-
: `"${folder}" holds no file that an import names`;
|
|
47
|
-
}
|
|
48
|
-
const issueOf = Effect.fn("Imports.issueOf")(function* (reader, graph, folder) {
|
|
49
|
-
const misplaced = placeIssue(graph, folder);
|
|
50
|
-
if (misplaced !== undefined) {
|
|
51
|
-
return misplaced;
|
|
52
|
-
}
|
|
53
|
-
const named = [...new Set(graph.edges.flatMap((edge) => (edge.to.kind === "file" && under(folder, edge.to.path) ? [edge.to.path] : [])))];
|
|
54
|
-
return yield* contentIssue(reader, folder, named);
|
|
55
|
-
});
|
|
56
|
-
// A generated folder may be empty until its generator runs, so a relative import of a missing file in it resolves.
|
|
57
|
-
export const withoutGenerated = Effect.fn("Imports.withoutGenerated")(function* (reader, graph, declared) {
|
|
58
|
-
const folders = declared.map((folder) => posix.normalize(folder).replace(LEADING_DOT, "").replace(TRAILING_SLASHES, ""));
|
|
59
|
-
const issues = (yield* Effect.forEach(folders, (folder) => issueOf(reader, graph, folder), { concurrency: "unbounded" })).flatMap((issue) => issue ?? []);
|
|
60
|
-
if (issues.length > 0) {
|
|
61
|
-
return yield* new SetupFailure({ message: `the generated folders are invalid:\n${issues.map((issue) => ` - ${issue}`).join("\n")}` });
|
|
62
|
-
}
|
|
63
|
-
return graph.unresolved.filter((request) => !folders.some((folder) => under(folder, request.missing)));
|
|
64
|
-
});
|
|
65
|
-
//# sourceMappingURL=withoutGenerated.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"withoutGenerated.js","sourceRoot":"","sources":["../../src/imports/withoutGenerated.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,UAAU,EAAoB,UAAU,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AACrG,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAK1D,MAAM,WAAW,GAAG,YAAY,CAAC;AAEjC,MAAM,WAAW,GAAG,QAAQ,CAAC;AAE7B,MAAM,gBAA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|
1
|
-
import { basename, posix, relative } from "node:path";
|
|
2
|
-
import { Effect } from "effect";
|
|
3
|
-
import { SetupFailure } from "#failure.ts";
|
|
4
|
-
import type { ImportGraph } from "#imports/graph.ts";
|
|
5
|
-
import { emptyScope, type IgnoreScope, verdictFor, withIgnoreFile } from "#inventory/ignoreScope.ts";
|
|
6
|
-
import { trackedPaths } from "#inventory/trackedPaths.ts";
|
|
7
|
-
import type { RuleInputs } from "#rule.ts";
|
|
8
|
-
|
|
9
|
-
type Reader = Pick<RuleInputs, "readText" | "root">;
|
|
10
|
-
|
|
11
|
-
const IGNORE_FILE = ".gitignore";
|
|
12
|
-
|
|
13
|
-
const LEADING_DOT = /^\.\//u;
|
|
14
|
-
|
|
15
|
-
const TRAILING_SLASHES = /\/+$/u;
|
|
16
|
-
|
|
17
|
-
const NODE_MODULES = "node_modules";
|
|
18
|
-
|
|
19
|
-
const PLACEHOLDERS = new Set([".gitkeep", ".keep", "README.md"]);
|
|
20
|
-
|
|
21
|
-
const withIgnoreFileIn = Effect.fn("Imports.withIgnoreFileIn")(function* (reader: Reader, scope: IgnoreScope, directory: string) {
|
|
22
|
-
const contents = yield* reader.readText(posix.join(directory, IGNORE_FILE));
|
|
23
|
-
return contents === undefined || contents === "" ? scope : withIgnoreFile(scope, posix.join(reader.root, directory), contents);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const ignored = Effect.fn("Imports.ignored")(function* (reader: Reader, file: string) {
|
|
27
|
-
const parts = file.split("/");
|
|
28
|
-
let scope = emptyScope;
|
|
29
|
-
for (const index of parts.keys()) {
|
|
30
|
-
scope = yield* withIgnoreFileIn(reader, scope, parts.slice(0, index).join("/") || ".");
|
|
31
|
-
if (verdictFor(scope, posix.join(reader.root, ...parts.slice(0, index + 1)), index < parts.length - 1) === "ignored") {
|
|
32
|
-
return true;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return false;
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
function under(folder: string, path: string | undefined): boolean {
|
|
39
|
-
return path?.startsWith(`${folder}/`) === true;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const contentIssue = Effect.fn("Imports.contentIssue")(function* (reader: Reader, folder: string, named: readonly string[]) {
|
|
43
|
-
const verdicts = yield* Effect.forEach(named, (file) => ignored(reader, file), { concurrency: "unbounded" });
|
|
44
|
-
if (verdicts.includes(false)) {
|
|
45
|
-
return `"${folder}" is not ignored by git, so it holds source, not generated output`;
|
|
46
|
-
}
|
|
47
|
-
const kept = (yield* trackedPaths(reader.root, folder)).find((file) => !PLACEHOLDERS.has(basename(file)));
|
|
48
|
-
return kept === undefined ? undefined : `"${folder}" holds files git tracks, such as ${relative(reader.root, kept)}, so it is not generated output`;
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
function placeIssue(graph: ImportGraph, folder: string): string | undefined {
|
|
52
|
-
if (folder === "" || folder.startsWith("../") || posix.isAbsolute(folder)) {
|
|
53
|
-
return `"${folder}" is no folder inside the repository`;
|
|
54
|
-
}
|
|
55
|
-
if (folder.split("/").includes(NODE_MODULES)) {
|
|
56
|
-
return `"${folder}" lies in node_modules, which holds installed packages, not generated output`;
|
|
57
|
-
}
|
|
58
|
-
return graph.edges.some((edge) => edge.to.kind === "file" && under(folder, edge.to.path))
|
|
59
|
-
? undefined
|
|
60
|
-
: `"${folder}" holds no file that an import names`;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const issueOf = Effect.fn("Imports.issueOf")(function* (reader: Reader, graph: ImportGraph, folder: string) {
|
|
64
|
-
const misplaced = placeIssue(graph, folder);
|
|
65
|
-
if (misplaced !== undefined) {
|
|
66
|
-
return misplaced;
|
|
67
|
-
}
|
|
68
|
-
const named = [...new Set(graph.edges.flatMap((edge) => (edge.to.kind === "file" && under(folder, edge.to.path) ? [edge.to.path] : [])))];
|
|
69
|
-
return yield* contentIssue(reader, folder, named);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
// A generated folder may be empty until its generator runs, so a relative import of a missing file in it resolves.
|
|
73
|
-
export const withoutGenerated = Effect.fn("Imports.withoutGenerated")(function* (reader: Reader, graph: ImportGraph, declared: readonly string[]) {
|
|
74
|
-
const folders = declared.map((folder) => posix.normalize(folder).replace(LEADING_DOT, "").replace(TRAILING_SLASHES, ""));
|
|
75
|
-
const issues = (yield* Effect.forEach(folders, (folder) => issueOf(reader, graph, folder), { concurrency: "unbounded" })).flatMap(
|
|
76
|
-
(issue) => issue ?? [],
|
|
77
|
-
);
|
|
78
|
-
if (issues.length > 0) {
|
|
79
|
-
return yield* new SetupFailure({ message: `the generated folders are invalid:\n${issues.map((issue) => ` - ${issue}`).join("\n")}` });
|
|
80
|
-
}
|
|
81
|
-
return graph.unresolved.filter((request) => !folders.some((folder) => under(folder, request.missing)));
|
|
82
|
-
});
|