@cplieger/deadset-ts 8.0.0-dev.7 → 8.0.0-dev.9

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.
Files changed (137) hide show
  1. package/dist/bin/node-host.js +21 -3
  2. package/dist/src/analysis.js +94 -299
  3. package/dist/src/config.js +6 -83
  4. package/dist/src/configuration-files.js +2 -9
  5. package/dist/src/configured-declarations.js +15 -20
  6. package/dist/src/consumers.js +3 -30
  7. package/dist/src/contract/config.generated.js +544 -0
  8. package/dist/src/contract/contract.generated.js +20 -10
  9. package/dist/src/contract/report.generated.js +3 -2
  10. package/dist/src/conventions.js +3 -8
  11. package/dist/src/edges.js +36 -112
  12. package/dist/src/exempt-classes.js +0 -2
  13. package/dist/src/exempt.js +1 -1
  14. package/dist/src/failure.js +36 -0
  15. package/dist/src/findings/completion.js +5 -4
  16. package/dist/src/findings/emitters.js +1 -1
  17. package/dist/src/findings/intra-function.js +1 -1
  18. package/dist/src/findings/non-code-artifacts.js +2 -2
  19. package/dist/src/findings/unused-declarations.js +1 -1
  20. package/dist/src/findings/visibility-narrowing.js +2 -2
  21. package/dist/src/findings-pass.js +1 -1
  22. package/dist/src/framework-lifecycle.js +7 -7
  23. package/dist/src/fs/document.js +52 -0
  24. package/dist/src/fs/manifest.js +7 -0
  25. package/dist/src/injection-container.js +1 -1
  26. package/dist/src/json-document.js +194 -67
  27. package/dist/src/kinds.js +0 -7
  28. package/dist/src/load/classify.js +236 -0
  29. package/dist/src/load/diagnostics.js +79 -0
  30. package/dist/src/load/discover.js +230 -0
  31. package/dist/src/{emit-map.js → load/emit-map.js} +1 -1
  32. package/dist/src/load/expected-module.js +136 -0
  33. package/dist/src/load/load.js +22 -0
  34. package/dist/src/load/module-lookup.js +37 -0
  35. package/dist/src/load/plan.js +155 -0
  36. package/dist/src/{test-globals.js → load/test-globals.js} +51 -56
  37. package/dist/src/load/test-joins.js +82 -0
  38. package/dist/src/{workspace-manifests.js → load/workspace-manifests.js} +3 -3
  39. package/dist/src/{workspace-programs.js → load/workspace-programs.js} +37 -40
  40. package/dist/src/{workspace-resolution.js → load/workspace-resolution.js} +21 -59
  41. package/dist/src/{workspace-run.js → load/workspace-run.js} +4 -3
  42. package/dist/src/{workspace.js → load/workspace.js} +21 -17
  43. package/dist/src/outside-tests.js +1 -1
  44. package/dist/src/print.js +10 -79
  45. package/dist/src/report-schema.js +3 -224
  46. package/dist/src/resolve.js +245 -715
  47. package/dist/src/run.js +59 -72
  48. package/dist/src/schema.js +320 -344
  49. package/dist/src/scope.js +37 -113
  50. package/dist/src/serialization-contract.js +1 -1
  51. package/dist/src/session.js +22 -28
  52. package/dist/src/source-files.js +1 -1
  53. package/dist/src/suppress-file.js +15 -17
  54. package/dist/src/suppress.js +3 -3
  55. package/dist/src/template-field.js +4 -4
  56. package/dist/src/type-errors.js +11 -216
  57. package/dist/src/usage.js +10 -0
  58. package/dist/src/verbs/analyze.js +100 -122
  59. package/dist/src/verbs/describe.js +5 -12
  60. package/dist/src/verbs/explain.js +3 -3
  61. package/dist/src/verbs/print-projects.js +2 -2
  62. package/dist/src/verbs/version.js +12 -2
  63. package/jsr.json +1 -1
  64. package/package.json +1 -1
  65. package/src/analysis.ts +95 -460
  66. package/src/config.ts +31 -298
  67. package/src/configuration-files.ts +8 -10
  68. package/src/configured-declarations.ts +18 -23
  69. package/src/consumers.ts +6 -57
  70. package/src/contract/config.generated.ts +798 -0
  71. package/src/contract/contract.generated.ts +20 -10
  72. package/src/contract/report.generated.ts +3 -2
  73. package/src/conventions.ts +6 -12
  74. package/src/edges.ts +39 -124
  75. package/src/exempt-classes.ts +0 -5
  76. package/src/exempt.ts +4 -4
  77. package/src/failure.ts +63 -0
  78. package/src/findings/completion.ts +5 -6
  79. package/src/findings/emitter.ts +2 -2
  80. package/src/findings/emitters.ts +1 -1
  81. package/src/findings/intra-function.ts +1 -1
  82. package/src/findings/non-code-artifacts.ts +4 -4
  83. package/src/findings/unused-declarations.ts +1 -1
  84. package/src/findings/visibility-narrowing.ts +2 -2
  85. package/src/findings-pass.ts +3 -3
  86. package/src/framework-lifecycle.ts +7 -7
  87. package/src/fs/document.ts +70 -0
  88. package/src/fs/manifest.ts +8 -0
  89. package/src/host.ts +12 -5
  90. package/src/injection-container.ts +1 -1
  91. package/src/json-document.ts +236 -76
  92. package/src/kinds.ts +0 -8
  93. package/src/load/classify.ts +352 -0
  94. package/src/load/diagnostics.ts +141 -0
  95. package/src/load/discover.ts +378 -0
  96. package/src/{emit-map.ts → load/emit-map.ts} +1 -1
  97. package/src/load/expected-module.ts +188 -0
  98. package/src/load/load.ts +61 -0
  99. package/src/load/module-lookup.ts +50 -0
  100. package/src/load/plan.ts +258 -0
  101. package/src/{test-globals.ts → load/test-globals.ts} +59 -65
  102. package/src/load/test-joins.ts +98 -0
  103. package/src/{workspace-manifests.ts → load/workspace-manifests.ts} +4 -4
  104. package/src/{workspace-programs.ts → load/workspace-programs.ts} +59 -53
  105. package/src/{workspace-resolution.ts → load/workspace-resolution.ts} +31 -78
  106. package/src/{workspace-run.ts → load/workspace-run.ts} +14 -11
  107. package/src/{workspace.ts → load/workspace.ts} +28 -21
  108. package/src/outside-tests.ts +1 -1
  109. package/src/partial.ts +1 -1
  110. package/src/print.ts +11 -92
  111. package/src/report-schema.ts +3 -299
  112. package/src/resolve.ts +281 -1105
  113. package/src/run.ts +68 -80
  114. package/src/schema.ts +391 -389
  115. package/src/scope.ts +45 -131
  116. package/src/serialization-contract.ts +1 -1
  117. package/src/session.ts +52 -53
  118. package/src/source-files.ts +1 -1
  119. package/src/suppress-file.ts +15 -20
  120. package/src/suppress.ts +3 -3
  121. package/src/template-field.ts +10 -7
  122. package/src/type-errors.ts +18 -288
  123. package/src/usage.ts +10 -0
  124. package/src/verbs/analyze.ts +139 -157
  125. package/src/verbs/describe.ts +4 -11
  126. package/src/verbs/explain.ts +3 -7
  127. package/src/verbs/print-projects.ts +2 -2
  128. package/src/verbs/verb.ts +2 -2
  129. package/src/verbs/version.ts +13 -2
  130. package/dist/src/base-url.js +0 -18
  131. package/dist/src/discover.js +0 -374
  132. package/dist/src/setup-failure.js +0 -19
  133. package/dist/src/test-joins.js +0 -101
  134. package/src/base-url.ts +0 -24
  135. package/src/discover.ts +0 -548
  136. package/src/setup-failure.ts +0 -37
  137. package/src/test-joins.ts +0 -118
@@ -1,58 +1,22 @@
1
1
  /**
2
- * What the errors a project's own files carry do to the run. An import of a module the
3
- * project expects and nothing provides is a setup failure. Any other error in an own file
4
- * is a type error: the function, method or file-level statement holding it is skipped,
5
- * which only ever withholds a finding. `projectErrors` decides, before any of these, which
6
- * errors the compiler places in no file leave the project unloaded.
2
+ * What a type error in a project's own file does: the function, method or file-level statement
3
+ * holding it is skipped, which only ever withholds a finding.
7
4
  */
8
5
 
9
6
  import {
10
7
  isClassDeclaration,
11
8
  isFunctionLikeDeclaration,
12
- isNoSubstitutionTemplateLiteral,
13
9
  isPropertyAccessExpression,
14
- isStringLiteral,
15
10
  type Node,
16
11
  type PropertyAccessExpression,
17
12
  type SourceFile,
18
13
  } from "@typescript/native/ast";
19
14
  import type { Symbol as TSSymbol, Type } from "@typescript/native/sync";
20
- import { packageOfSpecifier } from "./dependencies.ts";
21
- import type { LocatedDiagnostic } from "./discover.ts";
22
- import type { Host } from "./host.ts";
23
15
  import { nodeKey, type Inventory } from "./inventory.ts";
24
- import {
25
- ancestors,
26
- dirnamePath,
27
- isAbsolutePath,
28
- joinPath,
29
- relativePath,
30
- resolvePath,
31
- } from "./paths.ts";
16
+ import { relativePath } from "./paths.ts";
32
17
  import { isAnswered } from "./query.ts";
33
18
  import type { TypeErrorSkip } from "./report.ts";
34
- import type { Engine, ProjectView } from "./session.ts";
35
- import type { SetupFailure } from "./setup-failure.ts";
36
- import { findWorkspace, type Workspace } from "./workspace.ts";
37
- import { entriesOf } from "./fs/tree.ts";
38
- import { INSTALLED_PACKAGES, MANIFEST, manifestAt } from "./fs/manifest.ts";
39
-
40
- /** The compiler's codes for an import whose module it cannot find. */
41
- const MODULE_NOT_FOUND: ReadonlySet<number> = new Set([2307, 2792]);
42
-
43
- /**
44
- * The compiler's codes for where the configuration places a file, which leave every type
45
- * the file's statements carry as it is: an output that would overwrite an input (5055,
46
- * 5056), a file outside `rootDir` (6059), one a composite project does not list (6307).
47
- */
48
- const CONFIGURATION_LEVEL: ReadonlySet<number> = new Set([5055, 5056, 6059, 6307]);
49
-
50
- const MANIFEST_SECTIONS = [
51
- "dependencies",
52
- "devDependencies",
53
- "peerDependencies",
54
- "optionalDependencies",
55
- ] as const;
19
+ import type { ProjectView } from "./session.ts";
56
20
 
57
21
  /** The lines of one file a skip withholds every finding on. */
58
22
  export interface SkippedUnit {
@@ -74,184 +38,12 @@ export function insideSkipped(
74
38
  }
75
39
 
76
40
  /** One type error of an own file, with the unit it skips. */
77
- interface Skip {
41
+ export interface Skip {
78
42
  readonly record: TypeErrorSkip;
79
43
  readonly file: SourceFile;
80
44
  readonly unit: Node;
81
45
  }
82
46
 
83
- /** What the errors of one project's own files are. */
84
- interface ProjectErrorReading {
85
- /** The imports of a module the project expects that nothing provides, one failure each. */
86
- readonly missing: readonly SetupFailure[];
87
- /** The type errors, each with the unit it skips. */
88
- readonly skips: readonly Skip[];
89
- }
90
-
91
- /** Where one project reads what its imports may name. */
92
- interface ErrorContext {
93
- readonly host: Host;
94
- /** The target root, absolute. */
95
- readonly targetRoot: string;
96
- /** The project configuration's `paths` mapping. */
97
- readonly paths: PathsMapping;
98
- /** Whether one manifest is a workspace member's, whose dependencies the workspace installs. */
99
- readonly member: (manifest: string) => boolean;
100
- }
101
-
102
- /** One configuration's `paths` mapping, its targets read against `base`. */
103
- interface PathsMapping {
104
- readonly base: string;
105
- readonly entries: readonly { readonly key: string; readonly targets: readonly string[] }[];
106
- }
107
-
108
- /** The string literal holding one offset, innermost first, a doc comment's included. */
109
- function literalAt(file: SourceFile, offset: number): string | undefined {
110
- let found: string | undefined;
111
- const visit = (node: Node): void => {
112
- if (offset < node.pos || offset >= node.end) {
113
- return;
114
- }
115
- if (isStringLiteral(node) || isNoSubstitutionTemplateLiteral(node)) {
116
- found = node.text;
117
- }
118
- // `forEachChild` does not enter a doc comment, where a JavaScript file imports types.
119
- for (const doc of (node as { readonly jsDoc?: readonly Node[] }).jsDoc ?? []) {
120
- visit(doc);
121
- }
122
- node.forEachChild(visit);
123
- };
124
- file.forEachChild(visit);
125
- return found;
126
- }
127
-
128
- /**
129
- * What one `paths` key's `*` matches in one specifier, `""` for a key with no `*` that
130
- * is the specifier, or `undefined` where the key does not match it.
131
- */
132
- function mappedBy(key: string, specifier: string): string | undefined {
133
- const star = key.indexOf("*");
134
- if (star < 0) {
135
- return key === specifier ? "" : undefined;
136
- }
137
- const prefix = key.slice(0, star);
138
- const suffix = key.slice(star + 1);
139
- return specifier.length >= prefix.length + suffix.length &&
140
- specifier.startsWith(prefix) &&
141
- specifier.endsWith(suffix)
142
- ? specifier.slice(prefix.length, specifier.length - suffix.length)
143
- : undefined;
144
- }
145
-
146
- /**
147
- * One `paths` target, its first `*` replaced by `star` taken literally, as the compiler does:
148
- * https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/module/resolver.go#L1282
149
- */
150
- function substituted(target: string, star: string): string {
151
- const at = target.indexOf("*");
152
- return at < 0 ? target : target.slice(0, at) + star + target.slice(at + 1);
153
- }
154
-
155
- /**
156
- * Whether a path names something on disk, as written or with any extension; under
157
- * `fileOnly`, only a file counts.
158
- */
159
- function provided(host: Host, path: string, fileOnly: boolean): boolean {
160
- const counts = (at: string): boolean =>
161
- fileOnly ? host.kindOf(at) === "file" : host.kindOf(at) !== "absent";
162
- if (counts(path)) {
163
- return true;
164
- }
165
- const dir = dirnamePath(path);
166
- const stem = `${path.slice(dir.length + 1)}.`;
167
- return entriesOf(host, dir).some(
168
- (entry) => entry.name.startsWith(stem) && counts(joinPath(dir, entry.name)),
169
- );
170
- }
171
-
172
- /** The manifest at or above `dir`, no higher than the target root, that declares `name`. */
173
- function declaringManifest(
174
- host: Host,
175
- dir: string,
176
- targetRoot: string,
177
- name: string,
178
- ): string | undefined {
179
- const chain = ancestors(dir, targetRoot);
180
- // A directory outside the target root reads its own manifest alone.
181
- for (const at of chain.length > 0 ? chain : [dir]) {
182
- const manifest = manifestAt(host, at);
183
- for (const section of MANIFEST_SECTIONS) {
184
- const declared = manifest?.[section];
185
- if (typeof declared === "object" && declared !== null && Object.hasOwn(declared, name)) {
186
- return joinPath(at, MANIFEST);
187
- }
188
- }
189
- }
190
- return undefined;
191
- }
192
-
193
- /**
194
- * Whether a package is installed for an importer in `dir`: in the `node_modules` of
195
- * `dir` or of any ancestor, above the target root too, as Node and the compiler look.
196
- */
197
- function installed(host: Host, dir: string, name: string): boolean {
198
- return ancestors(dir).some(
199
- (at) => host.kindOf(joinPath(at, INSTALLED_PACKAGES, name)) === "directory",
200
- );
201
- }
202
-
203
- /**
204
- * Why one import that the compiler cannot resolve names a module the project expects,
205
- * with the fix, or undefined where the module is one the project does not expect.
206
- */
207
- function expectedModule(
208
- context: ErrorContext,
209
- importer: string,
210
- specifier: string,
211
- ): string | undefined {
212
- const { host, targetRoot } = context;
213
- const fix = "run the generator or the build that writes it";
214
- if (specifier.startsWith(".") || isAbsolutePath(specifier)) {
215
- return provided(host, resolvePath(dirnamePath(importer), specifier), false)
216
- ? undefined
217
- : `which names no file of the target: ${fix}`;
218
- }
219
- if (specifier.startsWith("#")) {
220
- return `which the manifest's imports map to no file: ${fix}`;
221
- }
222
- const mapped = context.paths.entries.flatMap(({ key, targets }) => {
223
- const star = mappedBy(key, specifier);
224
- return star === undefined
225
- ? []
226
- : targets.map((target) => resolvePath(context.paths.base, substituted(target, star)));
227
- });
228
- const name = packageOfSpecifier(specifier);
229
- // The compiler looks up in `node_modules` what `paths` does not resolve, so the error
230
- // of an installed package's import is the import's own, a subpath its exports omit:
231
- // https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/module/resolver.go#L548-L575
232
- if (name !== undefined && installed(host, dirnamePath(importer), name)) {
233
- return undefined;
234
- }
235
- if (mapped.length > 0) {
236
- return mapped.some((path) => provided(host, path, true))
237
- ? undefined
238
- : `which the compiler configuration's paths map to no file: ${fix}`;
239
- }
240
- const manifest =
241
- name === undefined
242
- ? undefined
243
- : declaringManifest(host, dirnamePath(importer), targetRoot, name);
244
- // No run requires a nested package that is no workspace member to be installed.
245
- if (
246
- manifest === undefined ||
247
- (dirnamePath(manifest) !== targetRoot && !context.member(manifest))
248
- ) {
249
- return undefined;
250
- }
251
- const where = relativePath(targetRoot, manifest) ?? manifest;
252
- return `a dependency ${where} declares that is not installed: run the package manager's install`;
253
- }
254
-
255
47
  /**
256
48
  * The unit a type error at one offset skips: the innermost function holding it, else the
257
49
  * class member holding it where a top-level class does, else the top-level statement.
@@ -284,62 +76,19 @@ function messageOf(text: string, targetRoot: string): string {
284
76
  return line === "" ? "a type error" : line;
285
77
  }
286
78
 
287
- /**
288
- * Reads the errors one project's own files carry. A diagnostic marking unnecessary code
289
- * or about where the configuration places a file is no error here, and one in a file that
290
- * is not the project's own is that file's package's to answer for.
291
- */
292
- export function readErrors<Brand>(
293
- project: ProjectView<Brand>,
294
- errors: readonly LocatedDiagnostic[],
295
- where: Omit<ErrorContext, "member">,
296
- ): ProjectErrorReading {
297
- let workspace: Workspace | undefined | null = null;
298
- const context: ErrorContext = {
299
- ...where,
300
- member: (manifest) => {
301
- if (workspace === null) {
302
- workspace = findWorkspace(where.host, where.targetRoot, () => undefined);
303
- }
304
- return workspace?.memberOf(manifest) !== undefined;
305
- },
306
- };
307
- const files = new Map(project.ownSourceFiles().map((file) => [file.fileName, file]));
308
- const missing: SetupFailure[] = [];
309
- const skips: Skip[] = [];
310
- for (const { diagnostic, at } of errors) {
311
- const file = diagnostic.fileName === undefined ? undefined : files.get(diagnostic.fileName);
312
- if (
313
- file === undefined ||
314
- diagnostic.reportsUnnecessary === true ||
315
- CONFIGURATION_LEVEL.has(diagnostic.code)
316
- ) {
317
- continue;
318
- }
319
- const path = relativePath(context.targetRoot, file.fileName) ?? file.fileName;
320
- const line = at?.line ?? 1;
321
- const specifier = MODULE_NOT_FOUND.has(diagnostic.code)
322
- ? literalAt(file, diagnostic.pos)
323
- : undefined;
324
- const expected =
325
- specifier === undefined ? undefined : expectedModule(context, file.fileName, specifier);
326
- if (specifier !== undefined && expected !== undefined) {
327
- missing.push({
328
- setupClass: "missing-module",
329
- detail: `${path}:${String(line)}: ${path} imports ${JSON.stringify(specifier)}, ${expected}`,
330
- });
331
- continue;
332
- }
333
- const unit = unitAt(file, diagnostic.pos);
334
- if (unit !== undefined) {
335
- skips.push({
336
- record: { path, line, message: messageOf(diagnostic.text, context.targetRoot) },
337
- file,
338
- unit,
339
- });
340
- }
341
- }
342
- return { missing, skips };
79
+ /** The skip one type error at `offset` of an own file makes, the root spelled relative to itself. */
80
+ export function skipAt(
81
+ file: SourceFile,
82
+ offset: number,
83
+ line: number,
84
+ text: string,
85
+ targetRoot: string,
86
+ ): Skip | undefined {
87
+ const unit = unitAt(file, offset);
88
+ const path = relativePath(targetRoot, file.fileName) ?? file.fileName;
89
+ return unit === undefined
90
+ ? undefined
91
+ : { record: { path, line, message: messageOf(text, targetRoot) }, file, unit };
343
92
  }
344
93
 
345
94
  /** The lines each skip withholds, below the target root. */
@@ -411,22 +160,3 @@ export function skippedRoots<Brand>(
411
160
  }
412
161
  return [...kept];
413
162
  }
414
-
415
- /** One compiler configuration's `paths` mapping, empty where it cannot be read. */
416
- export function pathsOf(engine: Engine, configFile: string): PathsMapping {
417
- const base = dirnamePath(configFile);
418
- try {
419
- const { options } = engine.parseConfigFile(configFile);
420
- const { paths } = options;
421
- // The parse answers `pathsBasePath`, the directory of the configuration that writes
422
- // `paths` (an extended one included), though the client's option type omits it:
423
- // https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/tsoptions/tsconfigparsing.go#L1120
424
- const declaredAt = (options as { readonly pathsBasePath?: unknown }).pathsBasePath;
425
- return {
426
- base: typeof declaredAt === "string" ? declaredAt : base,
427
- entries: Object.entries(paths ?? {}).map(([key, targets]) => ({ key, targets })),
428
- };
429
- } catch {
430
- return { base, entries: [] };
431
- }
432
- }
package/src/usage.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A refusal of the invocation: the fix is to the command line or to a document it
3
+ * names, so the run ends with the usage code before any analysis.
4
+ */
5
+ export class UsageError extends Error {
6
+ constructor(message: string) {
7
+ super(message);
8
+ this.name = "UsageError";
9
+ }
10
+ }