@sshkeda/check 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +156 -2
- package/docs/how-to-make-check-pass.md +71 -0
- package/package.json +51 -4
- package/src/index.ts +8 -0
- package/src/internal/ban-abbreviated-words-in-declared-names.ts +165 -0
- package/src/internal/ban-checked-members-outside-calls.ts +151 -0
- package/src/internal/ban-comments.ts +23 -0
- package/src/internal/ban-comparing-or-switching-on-tag.ts +114 -0
- package/src/internal/ban-deadlines-inside-tests.ts +41 -0
- package/src/internal/ban-declared-values.ts +70 -0
- package/src/internal/ban-dropped-failures-detached-fibers-sleeps-and-untraced-functions.ts +67 -0
- package/src/internal/ban-failures-collected-as-results.ts +57 -0
- package/src/internal/ban-fallbacks-of-named-recoveries.ts +118 -0
- package/src/internal/ban-function-overloads.ts +107 -0
- package/src/internal/ban-global-error-in-failure-types.ts +146 -0
- package/src/internal/ban-instanceof-in-effect-recovery-callbacks.ts +63 -0
- package/src/internal/ban-let-and-effect-mutable-ref.ts +79 -0
- package/src/internal/ban-logic-in-index-files.ts +178 -0
- package/src/internal/ban-namespaces-used-without-a-member.ts +176 -0
- package/src/internal/ban-node-builtins-that-effect-replaces.ts +42 -0
- package/src/internal/ban-parsing-outside-data-without-a-schema.ts +299 -0
- package/src/internal/ban-re-exports.ts +172 -0
- package/src/internal/ban-reading-the-clock-and-sleeping-outside-effect.ts +239 -0
- package/src/internal/ban-retired-words-in-names.ts +49 -0
- package/src/internal/ban-scanning-directories-with-bun-glob.ts +79 -0
- package/src/internal/ban-shebang-lines.ts +26 -0
- package/src/internal/ban-timer-globals-and-the-global-object.ts +34 -0
- package/src/internal/ban-timer-schedules-without-deadlines.ts +240 -0
- package/src/internal/ban-type-and-interface-declarations.ts +48 -0
- package/src/internal/ban-type-predicates.ts +82 -0
- package/src/internal/ban-unknown-in-function-signatures.ts +143 -0
- package/src/internal/ban-vague-error-class-names.ts +28 -0
- package/src/internal/ban-vague-file-names.ts +51 -0
- package/src/internal/ban-what-a-file-is-not-granted.ts +512 -0
- package/src/internal/ban-while-do-while-and-for-loops.ts +27 -0
- package/src/internal/confine-service-declarations-to-granted-files.ts +116 -0
- package/src/internal/confine-the-stdio-service-to-terminal-output.ts +190 -0
- package/src/internal/define-the-lint-plugin-and-configs.ts +361 -0
- package/src/internal/describe-and-decode-rule-options.ts +22 -0
- package/src/internal/keep-assertions-in-the-test-home-and-ban-test-runners.ts +128 -0
- package/src/internal/keep-effect-testing-helpers-in-test-files.ts +157 -0
- package/src/internal/match-banned-modules-and-globals.ts +71 -0
- package/src/internal/match-effect-service-factories.ts +20 -0
- package/src/internal/require-a-named-const-for-compound-conditions.ts +44 -0
- package/src/internal/require-capital-snake-case-and-as-const-on-literal-constants.ts +141 -0
- package/src/internal/require-service-keys-to-end-with-their-class-name.ts +64 -0
- package/src/internal/resolve-what-an-expression-imports.ts +295 -0
- package/src/internal/sort-type-constituents.ts +82 -0
- package/src/internal/split-names-into-words.ts +12 -0
- package/src/internal/visit-every-declared-name.ts +264 -0
- package/src/lib/tasks/index.ts +1 -0
- package/src/lib/tasks/internal/declare-the-check-task.ts +29 -0
- package/src/lib/tasks/internal/declare-the-checker-queries-task.ts +41 -0
- package/src/lib/tasks/internal/declare-the-typed-checks-task.ts +25 -0
- package/src/lib/tasks/internal/run-checks-over-one-typescript-project.ts +68 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/index.ts +15 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/check-effect-diagnostics-with-effect-tsgo.ts +211 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/read-effect-tsgo-json-report.ts +39 -0
- package/src/lib/tasks/lib/check-import-boundaries/index.ts +1 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-import-boundary-violations.ts +415 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-subpath-import-targets.ts +117 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-what-hides-an-import.ts +65 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/import-boundary-errors.ts +176 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/index.ts +1 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/declared-use-errors.ts +32 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-declared-use-violations.ts +38 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-sync-calls.ts +23 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-type-only-schemas.ts +375 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/index.ts +1 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/find-layer-provides-outside-program-starts.ts +648 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/layer-provide-errors.ts +27 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/resolve-imported-package-apis.ts +262 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/index.ts +1 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/find-ungranted-stdio-uses.ts +219 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/stdio-use-errors.ts +31 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/index.ts +1 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/internal/find-inferrable-annotations.ts +357 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/internal/inferrable-annotation-errors.ts +33 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/index.ts +1 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/internal/find-repeated-service-yields.ts +162 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/internal/repeated-service-yield-errors.ts +25 -0
- package/src/lib/tasks/lib/enforce-file-layout/index.ts +5 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/check-the-layout-of-each-package.ts +9 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/file-layout-errors.ts +417 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-export-path-mismatches.ts +83 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-file-layout-violations.ts +827 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-module-layout-violations.ts +82 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/resolve-task-imports.ts +178 -0
- package/src/lib/tasks/lib/enforce-file-types/index.ts +5 -0
- package/src/lib/tasks/lib/enforce-file-types/internal/file-type-errors.ts +21 -0
- package/src/lib/tasks/lib/enforce-file-types/internal/find-file-type-violations.ts +78 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/index.ts +1 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/check-formatting-with-bun-fmt.ts +157 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/declare-the-formatting-task.ts +66 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-config-violations.ts +16 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-input-violations.ts +74 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-symbolic-links.ts +14 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/unformatted-file-errors.ts +12 -0
- package/src/lib/tasks/lib/judge-index-re-exports/index.ts +1 -0
- package/src/lib/tasks/lib/judge-index-re-exports/internal/find-index-re-export-violations.ts +353 -0
- package/src/lib/tasks/lib/judge-index-re-exports/internal/index-re-export-errors.ts +21 -0
- package/src/lib/tasks/lib/lint-with-oxlint/index.ts +15 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/check-lint-rules-with-oxlint.ts +123 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/check-unsafe-enum-assignments.ts +152 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/find-tsgolint-executable.ts +21 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/read-oxlint-json-report.ts +82 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/read-tsgolint-rule-messages.ts +68 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/index.ts +1 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/export-placement-errors.ts +135 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-export-placement-violations.ts +507 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-misplaced-files.ts +116 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-subdivided-directories.ts +29 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-the-importer-graph.ts +451 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/list-binding-names.ts +49 -0
- package/src/lib/tasks/lib/refuse-disable-directives/index.ts +15 -0
- package/src/lib/tasks/lib/refuse-disable-directives/internal/disable-directive-errors.ts +45 -0
- package/src/lib/tasks/lib/refuse-disable-directives/internal/find-disable-directives.ts +66 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/index.ts +1 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/error-mapping-errors.ts +21 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/find-error-mappings-to-done.ts +135 -0
- package/src/lib/tasks/lib/require-separate-service-layers/index.ts +1 -0
- package/src/lib/tasks/lib/require-separate-service-layers/internal/find-service-layer-violations.ts +170 -0
- package/src/lib/tasks/lib/require-separate-service-layers/internal/service-layer-errors.ts +21 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/index.ts +14 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/find-linted-files-not-type-checked.ts +117 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/linted-file-errors.ts +51 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/index.ts +11 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/check-types-with-bun-check.ts +54 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/read-bun-check-output.ts +73 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/type-check-errors.ts +10 -0
- package/src/lib/tasks/shared/check-each-nested-package.ts +79 -0
- package/src/lib/tasks/shared/classify-files-by-role.ts +115 -0
- package/src/lib/tasks/shared/count-the-line-of-an-offset.ts +2 -0
- package/src/lib/tasks/shared/create-the-module-resolver.ts +38 -0
- package/src/lib/tasks/shared/declare-lint-config-task.ts +13 -0
- package/src/lib/tasks/shared/declare-typescript-config-task.ts +18 -0
- package/src/lib/tasks/shared/encode-compact-json.ts +5 -0
- package/src/lib/tasks/shared/find-installed-package-binaries.ts +62 -0
- package/src/lib/tasks/shared/find-module-requests.ts +482 -0
- package/src/lib/tasks/shared/find-named-package-manifests.ts +94 -0
- package/src/lib/tasks/shared/find-unmet-typescript-config-requirements.ts +60 -0
- package/src/lib/tasks/shared/list-ignored-files-imported-by-source.ts +79 -0
- package/src/lib/tasks/shared/list-linted-files.ts +28 -0
- package/src/lib/tasks/shared/list-owned-files.ts +13 -0
- package/src/lib/tasks/shared/list-package-files.ts +66 -0
- package/src/lib/tasks/shared/load-the-lint-config.ts +176 -0
- package/src/lib/tasks/shared/match-granted-patch-files.ts +88 -0
- package/src/lib/tasks/shared/parse-config-files-that-import-from-check.ts +310 -0
- package/src/lib/tasks/shared/print-failures-and-keep-detail-in-the-trace.ts +99 -0
- package/src/lib/tasks/shared/project-config-and-tool-errors.ts +485 -0
- package/src/lib/tasks/shared/query-the-typescript-project.ts +635 -0
- package/src/lib/tasks/shared/read-named-package-manifests.ts +53 -0
- package/src/lib/tasks/shared/read-package-manifest-object.ts +28 -0
- package/src/lib/tasks/shared/resolve-absolute-paths.ts +4 -0
- package/src/lib/tasks/shared/resolve-module-requests.ts +121 -0
- package/src/lib/tasks/shared/run-tools-and-decode-their-reports.ts +98 -0
- package/src/lib/tasks/shared/tool-file-locations.ts +38 -0
- package/src/shared/bans-each-permission-lifts.ts +454 -0
- package/src/shared/config-file-names.ts +3 -0
- package/src/shared/find-retired-words.ts +17 -0
- package/src/shared/format-reported-failures.ts +31 -0
- package/src/shared/lint-rule-messages-and-patterns.ts +104 -0
- package/src/shared/match-file-names-and-path-prefixes.ts +18 -0
- package/src/shared/permission-grant-errors.ts +42 -0
- package/src/shared/record-and-grant-permissions-by-path.ts +419 -0
- package/src/shared/test-home-directories.ts +4 -0
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
import { Array as Arrays, Effect, FileSystem, Path, Predicate, Schema } from "effect";
|
|
2
|
+
import type {
|
|
3
|
+
CallExpression,
|
|
4
|
+
Node,
|
|
5
|
+
ShorthandPropertyAssignment,
|
|
6
|
+
SourceFile,
|
|
7
|
+
} from "typescript/unstable/ast";
|
|
8
|
+
import {
|
|
9
|
+
isArrowFunction,
|
|
10
|
+
isCallExpression,
|
|
11
|
+
isCallSignatureDeclaration,
|
|
12
|
+
isClassDeclaration,
|
|
13
|
+
isFunctionDeclaration,
|
|
14
|
+
isFunctionExpression,
|
|
15
|
+
isFunctionTypeNode,
|
|
16
|
+
isIdentifier,
|
|
17
|
+
isInterfaceDeclaration,
|
|
18
|
+
isMethodDeclaration,
|
|
19
|
+
isMethodSignatureDeclaration,
|
|
20
|
+
isPropertyAssignment,
|
|
21
|
+
isPropertySignatureDeclaration,
|
|
22
|
+
isShorthandPropertyAssignment,
|
|
23
|
+
isSourceFile,
|
|
24
|
+
isTypeLiteralNode,
|
|
25
|
+
isVariableDeclaration,
|
|
26
|
+
} from "typescript/unstable/ast/is";
|
|
27
|
+
import { API, SymbolFlags } from "typescript/unstable/async";
|
|
28
|
+
import type { Checker, Program, Symbol as TypeScriptSymbol } from "typescript/unstable/async";
|
|
29
|
+
|
|
30
|
+
import { isDeclarationFile, isScript, packagePathOf } from "./classify-files-by-role.ts";
|
|
31
|
+
import { ownedFilesAmong } from "./list-owned-files.ts";
|
|
32
|
+
import {
|
|
33
|
+
existingPackageFiles,
|
|
34
|
+
isInsideAnyDirectory,
|
|
35
|
+
nestedPackageDirectoriesAmong,
|
|
36
|
+
} from "./list-package-files.ts";
|
|
37
|
+
import {
|
|
38
|
+
ProgramFilesOutsideProject,
|
|
39
|
+
ProjectReferencesUndecodable,
|
|
40
|
+
TypeScriptApiFailed,
|
|
41
|
+
TypeScriptConfigUnparsable,
|
|
42
|
+
TypeScriptProjectFileMissing,
|
|
43
|
+
TypeScriptReturnedNothing,
|
|
44
|
+
} from "./project-config-and-tool-errors.ts";
|
|
45
|
+
import { absolutePathOf } from "./resolve-absolute-paths.ts";
|
|
46
|
+
|
|
47
|
+
export const TYPESCRIPT_PROJECT_FILE = "tsconfig.json";
|
|
48
|
+
|
|
49
|
+
export const CONCURRENT_TYPESCRIPT_REQUESTS = 32;
|
|
50
|
+
|
|
51
|
+
const NODE_MODULES_DIRECTORY = "node_modules";
|
|
52
|
+
|
|
53
|
+
export const packageFileOf = (projectDirectory: string, fileName: string) => {
|
|
54
|
+
const projectPrefix = `${projectDirectory}/`;
|
|
55
|
+
const searchedPath = fileName.toLowerCase().startsWith(projectPrefix.toLowerCase())
|
|
56
|
+
? fileName.slice(projectPrefix.length)
|
|
57
|
+
: fileName;
|
|
58
|
+
const segments = searchedPath.split("/");
|
|
59
|
+
const installedAt = segments.lastIndexOf(NODE_MODULES_DIRECTORY);
|
|
60
|
+
return installedAt === -1
|
|
61
|
+
? undefined
|
|
62
|
+
: segments
|
|
63
|
+
.slice(installedAt + 1)
|
|
64
|
+
.join("/")
|
|
65
|
+
.toLowerCase();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const isInNodeModules = (projectDirectory: string, fileName: string) =>
|
|
69
|
+
packageFileOf(projectDirectory, fileName) !== undefined;
|
|
70
|
+
|
|
71
|
+
const isLibraryFile = (projectDirectory: string, fileName: string) =>
|
|
72
|
+
isInNodeModules(projectDirectory, fileName) || isDeclarationFile(fileName);
|
|
73
|
+
|
|
74
|
+
const COMPILER_OPTIONS = Schema.Record(Schema.String, Schema.Json);
|
|
75
|
+
|
|
76
|
+
export const CUSTOM_CONDITIONS = Schema.Struct({
|
|
77
|
+
customConditions: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const PROJECT_REFERENCES = Schema.Struct({
|
|
81
|
+
references: Schema.optionalKey(Schema.Array(Schema.Struct({ path: Schema.String }))),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
export const typeScriptRequest = <Value>(operation: string, request: () => Promise<Value>) =>
|
|
85
|
+
Effect.tryPromise({
|
|
86
|
+
try: request,
|
|
87
|
+
catch: (cause) => new TypeScriptApiFailed({ operation, reason: String(cause) }),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
export const requiredTypeScriptRequest = <Value>(
|
|
91
|
+
operation: string,
|
|
92
|
+
request: () => Promise<Value | undefined>,
|
|
93
|
+
) =>
|
|
94
|
+
typeScriptRequest(operation, request).pipe(
|
|
95
|
+
Effect.filterOrFail(
|
|
96
|
+
Predicate.isNotUndefined,
|
|
97
|
+
() => new TypeScriptReturnedNothing({ operation }),
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
export const followedImportAliases = (
|
|
102
|
+
checker: Checker,
|
|
103
|
+
symbols: readonly (TypeScriptSymbol | undefined)[],
|
|
104
|
+
) =>
|
|
105
|
+
Effect.forEach(symbols, (symbol) => {
|
|
106
|
+
const isImportAlias = symbol !== undefined && (symbol.flags & SymbolFlags.Alias) !== 0;
|
|
107
|
+
return isImportAlias
|
|
108
|
+
? typeScriptRequest("follow an import alias", () => checker.getAliasedSymbol(symbol))
|
|
109
|
+
: Effect.succeed(symbol);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
export const resolvedSymbols = (checker: Checker, references: readonly Node[]) =>
|
|
113
|
+
Effect.gen(function* () {
|
|
114
|
+
const symbols = yield* typeScriptRequest("resolve referenced symbols", () =>
|
|
115
|
+
checker.getSymbolAtLocation(references),
|
|
116
|
+
);
|
|
117
|
+
return yield* followedImportAliases(checker, symbols);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
export const parentOf = (node: Node) => (isSourceFile(node) ? undefined : node.parent);
|
|
121
|
+
|
|
122
|
+
export const nodePosition = (sourceFile: SourceFile, node: Node) => {
|
|
123
|
+
const location = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
124
|
+
return { line: location.line + 1, column: location.character + 1 };
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const parsedProjectFile = (configFile: string) =>
|
|
128
|
+
Effect.gen(function* () {
|
|
129
|
+
const fileSystem = yield* FileSystem.FileSystem;
|
|
130
|
+
const text = yield* fileSystem
|
|
131
|
+
.readFileString(configFile)
|
|
132
|
+
.pipe(
|
|
133
|
+
Effect.catchReason("PlatformError", "NotFound", () =>
|
|
134
|
+
Effect.fail(new TypeScriptProjectFileMissing({ projectFile: configFile })),
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
return yield* Effect.try({
|
|
138
|
+
try: () => Bun.JSONC.parse(text),
|
|
139
|
+
catch: (cause) =>
|
|
140
|
+
new TypeScriptConfigUnparsable({ projectFile: configFile, reason: String(cause) }),
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const referencesOf = (configFile: string) =>
|
|
145
|
+
Effect.gen(function* () {
|
|
146
|
+
const parsed = yield* parsedProjectFile(configFile);
|
|
147
|
+
const decoded = yield* Schema.decodeUnknownEffect(PROJECT_REFERENCES)(parsed).pipe(
|
|
148
|
+
Effect.mapError(
|
|
149
|
+
(error) =>
|
|
150
|
+
new ProjectReferencesUndecodable({ projectFile: configFile, reason: error.message }),
|
|
151
|
+
),
|
|
152
|
+
);
|
|
153
|
+
return decoded.references ?? [];
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
export const projectConfigFiles = (projectFile: string) =>
|
|
157
|
+
Effect.gen(function* () {
|
|
158
|
+
const path = yield* Path.Path;
|
|
159
|
+
const configFiles = [path.normalize(projectFile)];
|
|
160
|
+
for (const configFile of configFiles) {
|
|
161
|
+
for (const reference of yield* referencesOf(configFile)) {
|
|
162
|
+
const referenced = path.join(path.dirname(configFile), reference.path);
|
|
163
|
+
const referencedConfigFile = referenced.endsWith(".json")
|
|
164
|
+
? referenced
|
|
165
|
+
: path.join(referenced, TYPESCRIPT_PROJECT_FILE);
|
|
166
|
+
if (!configFiles.includes(referencedConfigFile)) {
|
|
167
|
+
configFiles.push(referencedConfigFile);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return configFiles;
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const openedApi = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
175
|
+
Effect.acquireRelease(
|
|
176
|
+
Effect.sync(
|
|
177
|
+
() =>
|
|
178
|
+
new API(
|
|
179
|
+
sourceTextOverrides.size === 0
|
|
180
|
+
? {}
|
|
181
|
+
: { fs: { readFile: (fileName) => sourceTextOverrides.get(fileName) } },
|
|
182
|
+
),
|
|
183
|
+
),
|
|
184
|
+
(openApi) => Effect.promise(() => openApi.close()),
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const parseCompilerOptionsAndInputs = (configFile: string) =>
|
|
188
|
+
Effect.gen(function* () {
|
|
189
|
+
const path = yield* Path.Path;
|
|
190
|
+
const api = yield* openedApi(new Map());
|
|
191
|
+
const absoluteConfigFile = yield* absolutePathOf(configFile);
|
|
192
|
+
const configDirectory = path.dirname(absoluteConfigFile);
|
|
193
|
+
const parsed = yield* typeScriptRequest(`parse ${configFile}`, () =>
|
|
194
|
+
api.parseConfigFile(absoluteConfigFile),
|
|
195
|
+
);
|
|
196
|
+
const options = yield* Schema.decodeUnknownEffect(COMPILER_OPTIONS)(parsed.options);
|
|
197
|
+
const compilesFiles = parsed.fileNames.length > 0;
|
|
198
|
+
const relativeToConfigDirectory = (value: Schema.Json) => {
|
|
199
|
+
const isAbsolutePath = Predicate.isString(value) && path.isAbsolute(value);
|
|
200
|
+
return isAbsolutePath ? path.relative(configDirectory, value) : value;
|
|
201
|
+
};
|
|
202
|
+
const effectiveOptions = Object.fromEntries(
|
|
203
|
+
Object.entries(options).map(([optionName, value]) => [
|
|
204
|
+
optionName,
|
|
205
|
+
Array.isArray(value)
|
|
206
|
+
? value.map((item: Schema.Json) => relativeToConfigDirectory(item))
|
|
207
|
+
: relativeToConfigDirectory(value),
|
|
208
|
+
]),
|
|
209
|
+
);
|
|
210
|
+
return { effectiveOptions, compilesFiles };
|
|
211
|
+
}).pipe(Effect.scoped);
|
|
212
|
+
|
|
213
|
+
const COMPILER_OPTIONS_AND_INPUTS_BY_CONFIG_FILE = new Map<
|
|
214
|
+
string,
|
|
215
|
+
ReturnType<typeof parseCompilerOptionsAndInputs>
|
|
216
|
+
>();
|
|
217
|
+
|
|
218
|
+
const compilerOptionsAndInputs = (configFile: string) =>
|
|
219
|
+
Effect.gen(function* () {
|
|
220
|
+
const cachedParse =
|
|
221
|
+
COMPILER_OPTIONS_AND_INPUTS_BY_CONFIG_FILE.get(configFile) ??
|
|
222
|
+
(yield* Effect.cached(parseCompilerOptionsAndInputs(configFile)));
|
|
223
|
+
COMPILER_OPTIONS_AND_INPUTS_BY_CONFIG_FILE.set(configFile, cachedParse);
|
|
224
|
+
return yield* cachedParse;
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
export const effectiveCompilerOptions = (configFile: string) =>
|
|
228
|
+
compilerOptionsAndInputs(configFile).pipe(Effect.map(({ effectiveOptions }) => effectiveOptions));
|
|
229
|
+
|
|
230
|
+
export const compilesFiles = (configFile: string) =>
|
|
231
|
+
compilerOptionsAndInputs(configFile).pipe(Effect.map((parsed) => parsed.compilesFiles));
|
|
232
|
+
|
|
233
|
+
export const includedFiles = (projectFile: string) =>
|
|
234
|
+
Effect.gen(function* () {
|
|
235
|
+
const path = yield* Path.Path;
|
|
236
|
+
const projectDirectory = path.dirname(yield* absolutePathOf(projectFile));
|
|
237
|
+
const api = yield* openedApi(new Map());
|
|
238
|
+
const parsedConfigs = yield* Effect.forEach(
|
|
239
|
+
yield* projectConfigFiles(projectFile),
|
|
240
|
+
(configFile) =>
|
|
241
|
+
absolutePathOf(configFile).pipe(
|
|
242
|
+
Effect.flatMap((absoluteConfigFile) =>
|
|
243
|
+
typeScriptRequest(`parse ${configFile}`, () => api.parseConfigFile(absoluteConfigFile)),
|
|
244
|
+
),
|
|
245
|
+
),
|
|
246
|
+
);
|
|
247
|
+
return new Set(
|
|
248
|
+
parsedConfigs.flatMap(({ fileNames }) =>
|
|
249
|
+
fileNames.map((fileName) => path.relative(projectDirectory, fileName)),
|
|
250
|
+
),
|
|
251
|
+
);
|
|
252
|
+
}).pipe(Effect.scoped);
|
|
253
|
+
|
|
254
|
+
const openedPrograms = (
|
|
255
|
+
configFiles: readonly string[],
|
|
256
|
+
sourceTextOverrides: ReadonlyMap<string, string>,
|
|
257
|
+
) =>
|
|
258
|
+
Effect.gen(function* () {
|
|
259
|
+
const api = yield* openedApi(sourceTextOverrides);
|
|
260
|
+
const snapshot = yield* typeScriptRequest("load the project", () =>
|
|
261
|
+
api.updateSnapshot({ openProjects: [...configFiles] }),
|
|
262
|
+
);
|
|
263
|
+
return yield* Effect.forEach(configFiles, (configFile) => {
|
|
264
|
+
const project = snapshot.getProject(configFile);
|
|
265
|
+
return project === undefined
|
|
266
|
+
? new TypeScriptApiFailed({
|
|
267
|
+
operation: "load the project",
|
|
268
|
+
reason: `${configFile} is not a project`,
|
|
269
|
+
})
|
|
270
|
+
: Effect.succeed(project);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
const compiledFilesOf = (
|
|
275
|
+
projectFile: string,
|
|
276
|
+
projects: readonly (readonly [configFile: string, project: { readonly program: Program }])[],
|
|
277
|
+
) =>
|
|
278
|
+
Effect.gen(function* () {
|
|
279
|
+
const path = yield* Path.Path;
|
|
280
|
+
const projectDirectory = path.dirname(yield* absolutePathOf(projectFile));
|
|
281
|
+
const listedFilesOfEachProject = yield* Effect.forEach(projects, ([configFile, { program }]) =>
|
|
282
|
+
typeScriptRequest("list the program files", () => program.getSourceFileNames()).pipe(
|
|
283
|
+
Effect.map((fileNames) => ({ configFile, program, fileNames })),
|
|
284
|
+
),
|
|
285
|
+
);
|
|
286
|
+
const everyListedFile = listedFilesOfEachProject.flatMap(({ fileNames }) => fileNames);
|
|
287
|
+
const listedFiles = everyListedFile.filter(
|
|
288
|
+
(fileName) => !isInNodeModules(projectDirectory, fileName),
|
|
289
|
+
);
|
|
290
|
+
const pathsFromProjectDirectory = listedFiles.map((fileName) =>
|
|
291
|
+
path.relative(projectDirectory, fileName),
|
|
292
|
+
);
|
|
293
|
+
const [firstListedFile] = listedFiles;
|
|
294
|
+
const everyListedFileIsOutsideTheProject =
|
|
295
|
+
firstListedFile !== undefined &&
|
|
296
|
+
pathsFromProjectDirectory.every((relativePath) => relativePath.startsWith(".."));
|
|
297
|
+
if (everyListedFileIsOutsideTheProject) {
|
|
298
|
+
return yield* new ProgramFilesOutsideProject({ projectFile, listedFile: firstListedFile });
|
|
299
|
+
}
|
|
300
|
+
const projectFilesOutsideNodeModules = everyListedFile.filter(
|
|
301
|
+
(fileName) => !isInNodeModules(projectDirectory, fileName),
|
|
302
|
+
);
|
|
303
|
+
const nestedPackageDirectories = yield* nestedPackageDirectoriesAmong(
|
|
304
|
+
projectDirectory,
|
|
305
|
+
projectFilesOutsideNodeModules
|
|
306
|
+
.map((fileName) => path.relative(projectDirectory, fileName))
|
|
307
|
+
.filter((relativePath) => !relativePath.startsWith("..")),
|
|
308
|
+
);
|
|
309
|
+
const isOutsidePackage = (relativePath: string) =>
|
|
310
|
+
relativePath.startsWith("..") || isInsideAnyDirectory(relativePath, nestedPackageDirectories);
|
|
311
|
+
const isPackageScript = (relativePath: string) =>
|
|
312
|
+
!isOutsidePackage(relativePath) && isScript(relativePath);
|
|
313
|
+
const packageFiles = pathsFromProjectDirectory.filter((relativePath) =>
|
|
314
|
+
isPackageScript(relativePath),
|
|
315
|
+
);
|
|
316
|
+
return {
|
|
317
|
+
packageFiles: [...new Set(packageFiles)].toSorted(),
|
|
318
|
+
listedFilesOfEachProject,
|
|
319
|
+
isPackageScriptFile: (fileName: string) =>
|
|
320
|
+
!isInNodeModules(projectDirectory, fileName) &&
|
|
321
|
+
isPackageScript(path.relative(projectDirectory, fileName)),
|
|
322
|
+
};
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
const listProgramFiles = (projectFile: string) =>
|
|
326
|
+
Effect.gen(function* () {
|
|
327
|
+
const path = yield* Path.Path;
|
|
328
|
+
const projectDirectory = path.dirname(yield* absolutePathOf(projectFile));
|
|
329
|
+
const configFiles = yield* projectConfigFiles(projectFile);
|
|
330
|
+
const projects = yield* openedPrograms(configFiles, new Map());
|
|
331
|
+
const { listedFilesOfEachProject, isPackageScriptFile, ...compiledFiles } =
|
|
332
|
+
yield* compiledFilesOf(projectFile, Arrays.zip(configFiles, projects));
|
|
333
|
+
const packageSourceFilesOfEachProject = yield* Effect.forEach(
|
|
334
|
+
listedFilesOfEachProject,
|
|
335
|
+
({ configFile, program, fileNames }) =>
|
|
336
|
+
Effect.forEach(
|
|
337
|
+
fileNames.filter((fileName) => isPackageScriptFile(fileName)),
|
|
338
|
+
(fileName) =>
|
|
339
|
+
requiredTypeScriptRequest("read a source file", () =>
|
|
340
|
+
program.getSourceFile(fileName),
|
|
341
|
+
).pipe(
|
|
342
|
+
Effect.map(
|
|
343
|
+
(sourceFile) =>
|
|
344
|
+
[
|
|
345
|
+
path.relative(projectDirectory, fileName),
|
|
346
|
+
{
|
|
347
|
+
isDeclarationFile: sourceFile.isDeclarationFile,
|
|
348
|
+
scriptKind: sourceFile.scriptKind,
|
|
349
|
+
},
|
|
350
|
+
] as const,
|
|
351
|
+
),
|
|
352
|
+
),
|
|
353
|
+
{ concurrency: CONCURRENT_TYPESCRIPT_REQUESTS },
|
|
354
|
+
).pipe(Effect.map((sourceFiles) => ({ configFile, sourceFiles: new Map(sourceFiles) }))),
|
|
355
|
+
);
|
|
356
|
+
return { ...compiledFiles, packageSourceFilesOfEachProject };
|
|
357
|
+
}).pipe(Effect.scoped);
|
|
358
|
+
|
|
359
|
+
const PROGRAM_FILES_BY_PROJECT = new Map<string, ReturnType<typeof listProgramFiles>>();
|
|
360
|
+
|
|
361
|
+
const cachedProgramFileListing = (projectFile: string) =>
|
|
362
|
+
Effect.gen(function* () {
|
|
363
|
+
const cachedListing =
|
|
364
|
+
PROGRAM_FILES_BY_PROJECT.get(projectFile) ??
|
|
365
|
+
(yield* Effect.cached(listProgramFiles(projectFile)));
|
|
366
|
+
PROGRAM_FILES_BY_PROJECT.set(projectFile, cachedListing);
|
|
367
|
+
return yield* cachedListing;
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
export const programFiles = (projectFile: string) =>
|
|
371
|
+
cachedProgramFileListing(projectFile).pipe(Effect.map(({ packageFiles }) => packageFiles));
|
|
372
|
+
|
|
373
|
+
export const packageSourceFilesOfEachProject = (projectFile: string) =>
|
|
374
|
+
cachedProgramFileListing(projectFile).pipe(
|
|
375
|
+
Effect.map((listing) => listing.packageSourceFilesOfEachProject),
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
export const checkedSourceFiles = (projectFile: string) =>
|
|
379
|
+
Effect.gen(function* () {
|
|
380
|
+
const path = yield* Path.Path;
|
|
381
|
+
const packageDirectory = path.dirname(projectFile);
|
|
382
|
+
const tracked = yield* existingPackageFiles(packageDirectory, ["."]);
|
|
383
|
+
const listed = new Set([
|
|
384
|
+
...(yield* programFiles(projectFile)),
|
|
385
|
+
...tracked.filter((file) => isScript(file)),
|
|
386
|
+
]);
|
|
387
|
+
return yield* FileSystem.FileSystem.use((fileSystem) =>
|
|
388
|
+
Effect.gen(function* () {
|
|
389
|
+
const realPackageDirectory = yield* fileSystem.realPath(packageDirectory);
|
|
390
|
+
const realFiles = yield* Effect.forEach(
|
|
391
|
+
listed,
|
|
392
|
+
(file) =>
|
|
393
|
+
fileSystem
|
|
394
|
+
.realPath(path.join(packageDirectory, file))
|
|
395
|
+
.pipe(Effect.map((real) => packagePathOf(path, realPackageDirectory, real))),
|
|
396
|
+
{ concurrency: "unbounded" },
|
|
397
|
+
);
|
|
398
|
+
return [
|
|
399
|
+
...new Set(realFiles.filter((file) => !file.startsWith("..") && listed.has(file))),
|
|
400
|
+
].toSorted();
|
|
401
|
+
}),
|
|
402
|
+
);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const NO_SYMBOL = undefined;
|
|
406
|
+
|
|
407
|
+
const factOf = (projectDirectory: string, symbol: TypeScriptSymbol | undefined) =>
|
|
408
|
+
symbol === undefined
|
|
409
|
+
? undefined
|
|
410
|
+
: {
|
|
411
|
+
id: symbol.id,
|
|
412
|
+
library:
|
|
413
|
+
symbol.declarations.length > 0 &&
|
|
414
|
+
symbol.declarations.every((declaration) =>
|
|
415
|
+
isLibraryFile(projectDirectory, declaration.path),
|
|
416
|
+
),
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
export const valueSymbolAt = <Failure>(
|
|
420
|
+
checker: Checker,
|
|
421
|
+
node: Node | undefined,
|
|
422
|
+
located: Effect.Effect<TypeScriptSymbol | undefined, Failure>,
|
|
423
|
+
) => {
|
|
424
|
+
const isShorthandPropertyName =
|
|
425
|
+
node !== undefined && isShorthandPropertyAssignment(node.parent) && node.parent.name === node;
|
|
426
|
+
return isShorthandPropertyName
|
|
427
|
+
? typeScriptRequest("resolve a shorthand property value", () =>
|
|
428
|
+
checker.getShorthandAssignmentValueSymbol(node.parent),
|
|
429
|
+
)
|
|
430
|
+
: located;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
export const projectSymbolsIn = (
|
|
434
|
+
projectDirectory: string,
|
|
435
|
+
projects: Effect.Success<ReturnType<typeof openTypeScriptProjects>>,
|
|
436
|
+
) => {
|
|
437
|
+
const checkerOf = new Map(
|
|
438
|
+
projects.flatMap(({ checker, allSourceFiles }) =>
|
|
439
|
+
allSourceFiles.map(
|
|
440
|
+
({ sourceFile, displayName }) => [displayName, { checker, sourceFile }] as const,
|
|
441
|
+
),
|
|
442
|
+
),
|
|
443
|
+
);
|
|
444
|
+
return (file: string) => {
|
|
445
|
+
const owner = checkerOf.get(file);
|
|
446
|
+
if (owner === undefined) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
return (
|
|
450
|
+
requests: readonly {
|
|
451
|
+
readonly start: number;
|
|
452
|
+
readonly end: number;
|
|
453
|
+
readonly shorthand: boolean;
|
|
454
|
+
}[],
|
|
455
|
+
) =>
|
|
456
|
+
Effect.gen(function* () {
|
|
457
|
+
if (requests.length === 0) {
|
|
458
|
+
return requests.map(() => NO_SYMBOL);
|
|
459
|
+
}
|
|
460
|
+
const symbols = yield* typeScriptRequest("resolve same-file references", () =>
|
|
461
|
+
owner.checker.getSymbolAtPosition(
|
|
462
|
+
owner.sourceFile.fileName,
|
|
463
|
+
requests.map(({ start }) => start),
|
|
464
|
+
),
|
|
465
|
+
);
|
|
466
|
+
const shorthandByNameEnd = new Map<number, ShorthandPropertyAssignment>();
|
|
467
|
+
if (requests.some(({ shorthand }) => shorthand)) {
|
|
468
|
+
const visit = (node: Node) => {
|
|
469
|
+
if (isShorthandPropertyAssignment(node)) {
|
|
470
|
+
shorthandByNameEnd.set(node.name.end, node);
|
|
471
|
+
}
|
|
472
|
+
node.forEachChild(visit);
|
|
473
|
+
};
|
|
474
|
+
owner.sourceFile.forEachChild(visit);
|
|
475
|
+
}
|
|
476
|
+
return yield* Effect.forEach(requests, (request, index) => {
|
|
477
|
+
const shorthand = request.shorthand ? shorthandByNameEnd.get(request.end) : undefined;
|
|
478
|
+
return valueSymbolAt(
|
|
479
|
+
owner.checker,
|
|
480
|
+
shorthand === undefined ? undefined : shorthand.name,
|
|
481
|
+
Effect.succeed(symbols[index]),
|
|
482
|
+
).pipe(Effect.map((symbol) => factOf(projectDirectory, symbol)));
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
};
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
const isFunctionAssignedToName = (node: Node) =>
|
|
489
|
+
(isArrowFunction(node) || isFunctionExpression(node)) &&
|
|
490
|
+
(isVariableDeclaration(node.parent) || isPropertyAssignment(node.parent));
|
|
491
|
+
|
|
492
|
+
const signatureOwnerOf = (node: Node | undefined): Node | undefined => {
|
|
493
|
+
const isSignatureOrTypeLiteral =
|
|
494
|
+
node !== undefined &&
|
|
495
|
+
(isFunctionTypeNode(node) ||
|
|
496
|
+
isCallSignatureDeclaration(node) ||
|
|
497
|
+
isTypeLiteralNode(node) ||
|
|
498
|
+
isFunctionAssignedToName(node));
|
|
499
|
+
return isSignatureOrTypeLiteral ? signatureOwnerOf(node.parent) : node;
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
const isReturnedBySignature = (node: Node | undefined, member: Node): boolean =>
|
|
503
|
+
node !== undefined &&
|
|
504
|
+
node !== member &&
|
|
505
|
+
(isFunctionTypeNode(node) ||
|
|
506
|
+
isCallSignatureDeclaration(node) ||
|
|
507
|
+
isReturnedBySignature(node.parent, member));
|
|
508
|
+
|
|
509
|
+
const declaredCallee = (declared: Node | undefined) => {
|
|
510
|
+
const member = signatureOwnerOf(declared);
|
|
511
|
+
const isCalleeDeclaration =
|
|
512
|
+
member !== undefined &&
|
|
513
|
+
(isMethodDeclaration(member) ||
|
|
514
|
+
isMethodSignatureDeclaration(member) ||
|
|
515
|
+
isPropertySignatureDeclaration(member) ||
|
|
516
|
+
isPropertyAssignment(member) ||
|
|
517
|
+
isFunctionDeclaration(member) ||
|
|
518
|
+
isVariableDeclaration(member));
|
|
519
|
+
const name = isCalleeDeclaration ? member.name : undefined;
|
|
520
|
+
if (member === undefined) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
if (name === undefined) {
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (!isIdentifier(name)) {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
const owner = member.parent;
|
|
530
|
+
const isNamedClassOrInterface =
|
|
531
|
+
(isClassDeclaration(owner) || isInterfaceDeclaration(owner)) && owner.name !== undefined;
|
|
532
|
+
return {
|
|
533
|
+
declaration: member,
|
|
534
|
+
name: name.text,
|
|
535
|
+
className: isNamedClassOrInterface ? owner.name.text : undefined,
|
|
536
|
+
returnedBySignature: isReturnedBySignature(declared?.parent, member),
|
|
537
|
+
};
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
export const packageCalleeOf = (
|
|
541
|
+
checker: Checker,
|
|
542
|
+
projectDirectory: string,
|
|
543
|
+
call: CallExpression,
|
|
544
|
+
isWantedDeclarationFile: (file: string) => boolean,
|
|
545
|
+
) =>
|
|
546
|
+
Effect.gen(function* () {
|
|
547
|
+
const signature = yield* typeScriptRequest("resolve a call signature", () =>
|
|
548
|
+
checker.getResolvedSignature(call),
|
|
549
|
+
);
|
|
550
|
+
const declaration = signature?.declaration;
|
|
551
|
+
const file =
|
|
552
|
+
declaration === undefined ? undefined : packageFileOf(projectDirectory, declaration.path);
|
|
553
|
+
if (declaration === undefined) {
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (file === undefined) {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (!isWantedDeclarationFile(file)) {
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
const declared = yield* typeScriptRequest("read a call's declaration", () =>
|
|
563
|
+
declaration.resolve(),
|
|
564
|
+
);
|
|
565
|
+
const named = declaredCallee(declared);
|
|
566
|
+
return named === undefined ? undefined : { call, file, ...named };
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
const isAnyPackageFile = () => true;
|
|
570
|
+
|
|
571
|
+
export const packageCalleesIn = (
|
|
572
|
+
checker: Checker,
|
|
573
|
+
projectDirectory: string,
|
|
574
|
+
sourceFile: SourceFile,
|
|
575
|
+
) => {
|
|
576
|
+
const calleeOf = (call: CallExpression) =>
|
|
577
|
+
packageCalleeOf(checker, projectDirectory, call, isAnyPackageFile).pipe(
|
|
578
|
+
Effect.map((callee) => (callee === undefined ? [] : [callee])),
|
|
579
|
+
);
|
|
580
|
+
return Effect.gen(function* () {
|
|
581
|
+
const calls: CallExpression[] = [];
|
|
582
|
+
const visit = (node: Node) => {
|
|
583
|
+
if (isCallExpression(node)) {
|
|
584
|
+
calls.push(node);
|
|
585
|
+
}
|
|
586
|
+
node.forEachChild(visit);
|
|
587
|
+
};
|
|
588
|
+
sourceFile.forEachChild(visit);
|
|
589
|
+
return (yield* Effect.forEach(calls, calleeOf, {
|
|
590
|
+
concurrency: CONCURRENT_TYPESCRIPT_REQUESTS,
|
|
591
|
+
})).flat();
|
|
592
|
+
});
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
export const openTypeScriptProjects = (
|
|
596
|
+
projectFile: string,
|
|
597
|
+
sourceTextOverrides: ReadonlyMap<string, string> = new Map(),
|
|
598
|
+
) =>
|
|
599
|
+
Effect.gen(function* () {
|
|
600
|
+
const path = yield* Path.Path;
|
|
601
|
+
const projectDirectory = path.dirname(yield* absolutePathOf(projectFile));
|
|
602
|
+
const configFiles = yield* projectConfigFiles(projectFile);
|
|
603
|
+
const opened = yield* openedPrograms(configFiles, sourceTextOverrides);
|
|
604
|
+
const { packageFiles } = yield* compiledFilesOf(projectFile, Arrays.zip(configFiles, opened));
|
|
605
|
+
const projects = opened.map(({ program, checker }) => ({
|
|
606
|
+
program,
|
|
607
|
+
checker,
|
|
608
|
+
sourceFiles: new Array<{ readonly sourceFile: SourceFile; readonly displayName: string }>(),
|
|
609
|
+
}));
|
|
610
|
+
for (const displayName of packageFiles.filter((file) => !isDeclarationFile(file))) {
|
|
611
|
+
const fileName = path.resolve(projectDirectory, displayName);
|
|
612
|
+
const candidates = yield* Effect.forEach(projects, (project) =>
|
|
613
|
+
typeScriptRequest("read a source file", () => project.program.getSourceFile(fileName)).pipe(
|
|
614
|
+
Effect.map((sourceFile) => ({ project, sourceFile })),
|
|
615
|
+
),
|
|
616
|
+
);
|
|
617
|
+
const owned = candidates.find(
|
|
618
|
+
({ sourceFile }) => sourceFile !== undefined && !sourceFile.isDeclarationFile,
|
|
619
|
+
);
|
|
620
|
+
if (owned?.sourceFile === undefined) {
|
|
621
|
+
return yield* new TypeScriptApiFailed({
|
|
622
|
+
operation: "read a source file",
|
|
623
|
+
reason: `${displayName} is in no project`,
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
owned.project.sourceFiles.push({ sourceFile: owned.sourceFile, displayName });
|
|
627
|
+
}
|
|
628
|
+
const owned = new Set(yield* ownedFilesAmong(projectDirectory, packageFiles));
|
|
629
|
+
return projects.map(({ program, checker, sourceFiles }) => ({
|
|
630
|
+
program,
|
|
631
|
+
checker,
|
|
632
|
+
allSourceFiles: sourceFiles,
|
|
633
|
+
sourceFiles: sourceFiles.filter(({ displayName }) => owned.has(displayName)),
|
|
634
|
+
}));
|
|
635
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Effect, FileSystem, Option } from "effect";
|
|
2
|
+
|
|
3
|
+
import { directoryHolding } from "../../../shared/match-file-names-and-path-prefixes.ts";
|
|
4
|
+
import {
|
|
5
|
+
namedPackageManifestIn,
|
|
6
|
+
namesASymlinkLoop,
|
|
7
|
+
namesNoManifest,
|
|
8
|
+
unreadableManifestReason,
|
|
9
|
+
} from "./find-named-package-manifests.ts";
|
|
10
|
+
import { PackageManifestUnreadable } from "./project-config-and-tool-errors.ts";
|
|
11
|
+
|
|
12
|
+
const manifestBytesAt = (manifestFile: string) =>
|
|
13
|
+
FileSystem.FileSystem.use((fileSystem) =>
|
|
14
|
+
Effect.gen(function* () {
|
|
15
|
+
const isManifestFile = yield* fileSystem.stat(manifestFile).pipe(
|
|
16
|
+
Effect.map((information) => information.type === "File"),
|
|
17
|
+
Effect.catchTag("PlatformError", (error) => {
|
|
18
|
+
if (namesNoManifest(error.reason.cause)) {
|
|
19
|
+
return Effect.succeed(false);
|
|
20
|
+
}
|
|
21
|
+
if (!namesASymlinkLoop(error.reason.cause)) {
|
|
22
|
+
return Effect.fail(error);
|
|
23
|
+
}
|
|
24
|
+
return fileSystem.stat(directoryHolding(manifestFile)).pipe(
|
|
25
|
+
Effect.as(true),
|
|
26
|
+
Effect.catchTag("PlatformError", (directoryError) =>
|
|
27
|
+
namesASymlinkLoop(directoryError.reason.cause)
|
|
28
|
+
? Effect.succeed(false)
|
|
29
|
+
: Effect.fail(error),
|
|
30
|
+
),
|
|
31
|
+
Effect.filterOrFail(
|
|
32
|
+
(isDirectoryReadable) => !isDirectoryReadable,
|
|
33
|
+
() => error,
|
|
34
|
+
),
|
|
35
|
+
);
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
return isManifestFile
|
|
39
|
+
? Option.some(yield* fileSystem.readFile(manifestFile))
|
|
40
|
+
: Option.none<Uint8Array>();
|
|
41
|
+
}),
|
|
42
|
+
).pipe(Effect.mapError((error) => unreadableManifestReason(error.reason.cause)));
|
|
43
|
+
|
|
44
|
+
export const namedPackageManifestAt = (manifestFile: string) =>
|
|
45
|
+
manifestBytesAt(manifestFile).pipe(
|
|
46
|
+
Effect.flatMap(
|
|
47
|
+
Option.match({
|
|
48
|
+
onNone: () => Effect.succeedNone,
|
|
49
|
+
onSome: (bytes) => Effect.fromResult(namedPackageManifestIn(bytes)),
|
|
50
|
+
}),
|
|
51
|
+
),
|
|
52
|
+
Effect.mapError((reason) => new PackageManifestUnreadable({ location: manifestFile, reason })),
|
|
53
|
+
);
|