@cplieger/deadset-ts 8.0.0-dev.8 → 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.
- package/dist/src/analysis.js +84 -288
- package/dist/src/configuration-files.js +2 -9
- package/dist/src/consumers.js +1 -28
- package/dist/src/contract/contract.generated.js +20 -10
- package/dist/src/conventions.js +3 -8
- package/dist/src/failure.js +36 -0
- package/dist/src/fs/document.js +21 -28
- package/dist/src/fs/manifest.js +7 -0
- package/dist/src/load/classify.js +236 -0
- package/dist/src/load/diagnostics.js +79 -0
- package/dist/src/load/discover.js +230 -0
- package/dist/src/{emit-map.js → load/emit-map.js} +1 -1
- package/dist/src/load/expected-module.js +136 -0
- package/dist/src/load/load.js +22 -0
- package/dist/src/load/module-lookup.js +37 -0
- package/dist/src/load/plan.js +155 -0
- package/dist/src/{test-globals.js → load/test-globals.js} +51 -56
- package/dist/src/load/test-joins.js +82 -0
- package/dist/src/{workspace-manifests.js → load/workspace-manifests.js} +3 -3
- package/dist/src/{workspace-programs.js → load/workspace-programs.js} +37 -40
- package/dist/src/{workspace-resolution.js → load/workspace-resolution.js} +21 -59
- package/dist/src/{workspace-run.js → load/workspace-run.js} +4 -3
- package/dist/src/{workspace.js → load/workspace.js} +21 -17
- package/dist/src/outside-tests.js +1 -1
- package/dist/src/run.js +20 -26
- package/dist/src/scope.js +11 -19
- package/dist/src/session.js +22 -28
- package/dist/src/source-files.js +1 -1
- package/dist/src/type-errors.js +11 -216
- package/dist/src/verbs/analyze.js +90 -109
- package/dist/src/verbs/describe.js +5 -12
- package/dist/src/verbs/version.js +12 -2
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/src/analysis.ts +68 -437
- package/src/configuration-files.ts +8 -10
- package/src/consumers.ts +2 -53
- package/src/contract/contract.generated.ts +20 -10
- package/src/conventions.ts +6 -12
- package/src/failure.ts +63 -0
- package/src/fs/document.ts +23 -27
- package/src/fs/manifest.ts +8 -0
- package/src/load/classify.ts +352 -0
- package/src/load/diagnostics.ts +141 -0
- package/src/load/discover.ts +378 -0
- package/src/{emit-map.ts → load/emit-map.ts} +1 -1
- package/src/load/expected-module.ts +188 -0
- package/src/load/load.ts +61 -0
- package/src/load/module-lookup.ts +50 -0
- package/src/load/plan.ts +258 -0
- package/src/{test-globals.ts → load/test-globals.ts} +59 -65
- package/src/load/test-joins.ts +98 -0
- package/src/{workspace-manifests.ts → load/workspace-manifests.ts} +4 -4
- package/src/{workspace-programs.ts → load/workspace-programs.ts} +59 -53
- package/src/{workspace-resolution.ts → load/workspace-resolution.ts} +31 -78
- package/src/{workspace-run.ts → load/workspace-run.ts} +14 -11
- package/src/{workspace.ts → load/workspace.ts} +28 -21
- package/src/outside-tests.ts +1 -1
- package/src/partial.ts +1 -1
- package/src/run.ts +26 -25
- package/src/scope.ts +15 -21
- package/src/session.ts +52 -53
- package/src/source-files.ts +1 -1
- package/src/type-errors.ts +18 -288
- package/src/verbs/analyze.ts +129 -136
- package/src/verbs/describe.ts +4 -11
- package/src/verbs/version.ts +13 -2
- package/dist/src/base-url.js +0 -18
- package/dist/src/discover.js +0 -374
- package/dist/src/setup-failure.js +0 -19
- package/dist/src/test-joins.js +0 -101
- package/src/base-url.ts +0 -24
- package/src/discover.ts +0 -548
- package/src/setup-failure.ts +0 -37
- package/src/test-joins.ts +0 -118
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import type { SourceFile } from "@typescript/native/ast";
|
|
2
|
+
import { conventionFailuresFor } from "../conventions.ts";
|
|
3
|
+
import type { Host } from "../host.ts";
|
|
4
|
+
import { relativePath, resolvePath } from "../paths.ts";
|
|
5
|
+
import { diagnosticsOf, ownedAs, type ProjectView } from "../session.ts";
|
|
6
|
+
import { skipAt, type Skip } from "../type-errors.ts";
|
|
7
|
+
import {
|
|
8
|
+
classOf,
|
|
9
|
+
MISSING_GLOBAL_TYPE,
|
|
10
|
+
projectErrors,
|
|
11
|
+
renderDiagnostic,
|
|
12
|
+
type LocatedDiagnostic,
|
|
13
|
+
} from "./diagnostics.ts";
|
|
14
|
+
import { targetRelative, type LoadedConsumer, type NotBuilt, type Origin } from "./discover.ts";
|
|
15
|
+
import { missingModule, pathsOf, type ModuleContext } from "./expected-module.ts";
|
|
16
|
+
import { FailureExit, failureLine, type Failure } from "../failure.ts";
|
|
17
|
+
import type { ConsumerWorkspace, ProgramPlan, RunPlan } from "./plan.ts";
|
|
18
|
+
import { ownErrors } from "./workspace-run.ts";
|
|
19
|
+
|
|
20
|
+
/** What keeps one configuration from being analyzed. */
|
|
21
|
+
type Failed =
|
|
22
|
+
/** Its project carries an error that leaves it unloaded. */
|
|
23
|
+
| { readonly kind: "load"; readonly errors: readonly [LocatedDiagnostic, ...LocatedDiagnostic[]] }
|
|
24
|
+
| { readonly kind: "setup"; readonly failures: readonly [Failure, ...Failure[]] }
|
|
25
|
+
/** The snapshot opened no project for it. */
|
|
26
|
+
| { readonly kind: "unopened" };
|
|
27
|
+
|
|
28
|
+
/** What a failure ends or drops, by where the configuration comes from. */
|
|
29
|
+
const POLICY: Readonly<Record<Origin, Readonly<Record<Failed["kind"], "drop" | "end">>>> = {
|
|
30
|
+
declared: { load: "end", setup: "end", unopened: "end" },
|
|
31
|
+
scope: { load: "end", setup: "end", unopened: "end" },
|
|
32
|
+
derived: { load: "drop", setup: "drop", unopened: "drop" },
|
|
33
|
+
consumer: { load: "end", setup: "end", unopened: "end" },
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** One library an analyzed configuration names that the compiler cannot find. */
|
|
37
|
+
export interface LibraryUnfound {
|
|
38
|
+
readonly configFile: string;
|
|
39
|
+
/** The compiler's error, rendered as a dropped configuration's is. */
|
|
40
|
+
readonly error: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** One target project the run analyzes, as classification leaves it. */
|
|
44
|
+
interface Analyzed<Brand> {
|
|
45
|
+
readonly plan: ProgramPlan;
|
|
46
|
+
/** The project, its own files the target's. */
|
|
47
|
+
readonly project: ProjectView<Brand>;
|
|
48
|
+
/** Every type error of its own files, with the unit it skips. */
|
|
49
|
+
readonly skips: readonly Skip[];
|
|
50
|
+
/** Whether it holds one file only for the references that file makes. */
|
|
51
|
+
readonly referenceOnly: (file: string) => boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** One consumer project the run reads the references of. */
|
|
55
|
+
interface Consumed<Brand> {
|
|
56
|
+
readonly project: ProjectView<Brand>;
|
|
57
|
+
readonly consumer: LoadedConsumer;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Every opened project of a run that may be read, and what the run notes about the others. */
|
|
61
|
+
export interface Classified {
|
|
62
|
+
readonly analyzed: readonly Analyzed<unknown>[];
|
|
63
|
+
readonly consumed: readonly Consumed<unknown>[];
|
|
64
|
+
/** The derived configurations dropped, discovery's first, then in configuration order. */
|
|
65
|
+
readonly notBuilt: readonly NotBuilt[];
|
|
66
|
+
readonly librariesUnfound: readonly LibraryUnfound[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The errors of one project's own files: an import of a module the project expects and
|
|
71
|
+
* nothing provides is a setup failure, every other error a type error skipping its unit.
|
|
72
|
+
*/
|
|
73
|
+
function readErrors<Brand>(
|
|
74
|
+
project: ProjectView<Brand>,
|
|
75
|
+
errors: readonly LocatedDiagnostic[],
|
|
76
|
+
context: ModuleContext,
|
|
77
|
+
): { readonly missing: readonly Failure[]; readonly skips: readonly Skip[] } {
|
|
78
|
+
const files = new Map<string, SourceFile>(
|
|
79
|
+
project.ownSourceFiles().map((file) => [file.fileName, file]),
|
|
80
|
+
);
|
|
81
|
+
const missing: Failure[] = [];
|
|
82
|
+
const skips: Skip[] = [];
|
|
83
|
+
for (const { diagnostic, at } of errors) {
|
|
84
|
+
const file = diagnostic.fileName === undefined ? undefined : files.get(diagnostic.fileName);
|
|
85
|
+
const kind = classOf(diagnostic, true);
|
|
86
|
+
if (file === undefined || kind === "ignored") {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const line = at?.line ?? 1;
|
|
90
|
+
const path = relativePath(context.targetRoot, file.fileName) ?? file.fileName;
|
|
91
|
+
const expected =
|
|
92
|
+
kind === "module-not-found" ? missingModule(context, file, diagnostic.pos) : undefined;
|
|
93
|
+
if (expected !== undefined) {
|
|
94
|
+
missing.push({
|
|
95
|
+
failureClass: "missing-module",
|
|
96
|
+
detail: `${path}:${String(line)}: ${path} imports ${JSON.stringify(expected.specifier)}, ${expected.reason}`,
|
|
97
|
+
});
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const skip = skipAt(file, diagnostic.pos, line, diagnostic.text, context.targetRoot);
|
|
101
|
+
if (skip !== undefined) {
|
|
102
|
+
skips.push(skip);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return { missing, skips };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function nonEmpty<T>(items: readonly T[]): readonly [T, ...T[]] | undefined {
|
|
109
|
+
const [first, ...rest] = items;
|
|
110
|
+
return first === undefined ? undefined : [first, ...rest];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** One target project read for what keeps it from being analyzed, or the skips it is analyzed with. */
|
|
114
|
+
function classifyTarget<Brand>(
|
|
115
|
+
host: Host,
|
|
116
|
+
run: RunPlan,
|
|
117
|
+
plan: ProgramPlan,
|
|
118
|
+
view: ProjectView<Brand>,
|
|
119
|
+
unfound: LibraryUnfound[],
|
|
120
|
+
): Failed | Analyzed<Brand> {
|
|
121
|
+
const { workspace, root } = run;
|
|
122
|
+
const unbuildable = workspace?.unbuildable(plan.configFile);
|
|
123
|
+
if (unbuildable !== undefined) {
|
|
124
|
+
return {
|
|
125
|
+
kind: "setup",
|
|
126
|
+
failures: [{ failureClass: "workspace-member-without-source", detail: unbuildable }],
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const referenceOnly =
|
|
130
|
+
workspace === undefined
|
|
131
|
+
? () => false
|
|
132
|
+
: workspace.referenceOnly(plan.configFile, view.rootFiles);
|
|
133
|
+
const project =
|
|
134
|
+
workspace === undefined
|
|
135
|
+
? view
|
|
136
|
+
: ownedAs(view, (file, external) => workspace.ownFile(file, external));
|
|
137
|
+
const raw = projectErrors(diagnosticsOf(project));
|
|
138
|
+
const load = nonEmpty(raw.load);
|
|
139
|
+
if (load !== undefined) {
|
|
140
|
+
return { kind: "load", errors: load };
|
|
141
|
+
}
|
|
142
|
+
const reading = readErrors(project, ownErrors(raw.check, referenceOnly), {
|
|
143
|
+
host,
|
|
144
|
+
targetRoot: root,
|
|
145
|
+
paths: pathsOf(plan.configFile, plan.parsed.options),
|
|
146
|
+
member: (manifest) => workspace?.workspace.memberOf(manifest) !== undefined,
|
|
147
|
+
});
|
|
148
|
+
const failures =
|
|
149
|
+
nonEmpty(reading.missing) ??
|
|
150
|
+
nonEmpty(
|
|
151
|
+
conventionFailuresFor(
|
|
152
|
+
run.conventions,
|
|
153
|
+
plan.configFile,
|
|
154
|
+
project.ownSourceFiles().map((file) => file.fileName),
|
|
155
|
+
),
|
|
156
|
+
);
|
|
157
|
+
if (failures !== undefined) {
|
|
158
|
+
return { kind: "setup", failures };
|
|
159
|
+
}
|
|
160
|
+
const display = (path: string): string => relativePath(root, path) ?? path;
|
|
161
|
+
unfound.push(
|
|
162
|
+
...raw.unfound.map((one) => ({
|
|
163
|
+
configFile: plan.configFile,
|
|
164
|
+
error: renderDiagnostic(one, display),
|
|
165
|
+
})),
|
|
166
|
+
);
|
|
167
|
+
const missingGlobal = raw.unfound.find(
|
|
168
|
+
({ diagnostic }) => diagnostic.code === MISSING_GLOBAL_TYPE,
|
|
169
|
+
);
|
|
170
|
+
// The checker types every use of a missing global against nothing and places no error in
|
|
171
|
+
// a file, so every file of the project is skipped whole.
|
|
172
|
+
const skips =
|
|
173
|
+
missingGlobal === undefined
|
|
174
|
+
? reading.skips
|
|
175
|
+
: [
|
|
176
|
+
...reading.skips,
|
|
177
|
+
...project.ownSourceFiles().map((file) => ({
|
|
178
|
+
record: {
|
|
179
|
+
path: display(file.fileName),
|
|
180
|
+
line: 1,
|
|
181
|
+
message: missingGlobal.diagnostic.text,
|
|
182
|
+
},
|
|
183
|
+
file,
|
|
184
|
+
unit: file,
|
|
185
|
+
})),
|
|
186
|
+
];
|
|
187
|
+
return { plan, project, skips, referenceOnly };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* One consumer project read for what keeps its references from being read. A workspace
|
|
192
|
+
* declaration that cannot be read fails it where an import asks whether a package is a member.
|
|
193
|
+
*/
|
|
194
|
+
function classifyConsumer<Brand>(
|
|
195
|
+
host: Host,
|
|
196
|
+
place: ConsumerWorkspace | undefined,
|
|
197
|
+
plan: ProgramPlan,
|
|
198
|
+
consumer: LoadedConsumer,
|
|
199
|
+
project: ProjectView<Brand>,
|
|
200
|
+
): Failed | Consumed<Brand> {
|
|
201
|
+
const raw = projectErrors(diagnosticsOf(project));
|
|
202
|
+
const load = nonEmpty(raw.load);
|
|
203
|
+
if (load !== undefined) {
|
|
204
|
+
return { kind: "load", errors: load };
|
|
205
|
+
}
|
|
206
|
+
let faulted: string | undefined;
|
|
207
|
+
const reading = readErrors(project, raw.check, {
|
|
208
|
+
host,
|
|
209
|
+
targetRoot: resolvePath(host.workingDirectory(), consumer.path),
|
|
210
|
+
paths: pathsOf(plan.configFile, plan.parsed.options),
|
|
211
|
+
member: (manifest) => {
|
|
212
|
+
if (place !== undefined && "fault" in place) {
|
|
213
|
+
faulted = place.fault;
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
return place?.workspace?.memberOf(manifest) !== undefined;
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
const details =
|
|
220
|
+
faulted === undefined
|
|
221
|
+
? reading.missing.map(
|
|
222
|
+
(one) =>
|
|
223
|
+
`the consumer ${consumer.path} does not build, its dependencies not installed: ${one.detail}`,
|
|
224
|
+
)
|
|
225
|
+
: [
|
|
226
|
+
`the consumer ${consumer.path} imports a dependency a package below it declares, and its workspace declaration cannot be read to tell whether that package is a member: ${faulted}`,
|
|
227
|
+
];
|
|
228
|
+
const failures = nonEmpty(
|
|
229
|
+
details.map((detail) => ({ failureClass: "missing-consumer" as const, detail })),
|
|
230
|
+
);
|
|
231
|
+
return failures === undefined ? { project, consumer } : { kind: "setup", failures };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The setup failure one failed configuration ends the run with, its located errors printed first. */
|
|
235
|
+
function ending(plan: ProgramPlan, failed: Failed, root: string): readonly Failure[] {
|
|
236
|
+
if (failed.kind === "setup") {
|
|
237
|
+
return failed.failures;
|
|
238
|
+
}
|
|
239
|
+
const what =
|
|
240
|
+
failed.kind === "load"
|
|
241
|
+
? `the configuration ${relativePath(root, plan.configFile) ?? plan.configFile} does not load, its errors printed above`
|
|
242
|
+
: `the compiler opened no project for ${plan.configFile}`;
|
|
243
|
+
return plan.consumer === undefined
|
|
244
|
+
? [
|
|
245
|
+
{
|
|
246
|
+
failureClass: "no-program",
|
|
247
|
+
detail: `${what}: correct the configuration, or name the configurations to analyze`,
|
|
248
|
+
},
|
|
249
|
+
]
|
|
250
|
+
: [
|
|
251
|
+
{
|
|
252
|
+
failureClass: "missing-consumer",
|
|
253
|
+
detail: `${what}, for the consumer ${plan.consumer.path}: check the consumer out at ${plan.consumer.path} and install its dependencies`,
|
|
254
|
+
},
|
|
255
|
+
];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The first line of the error that drops one derived configuration. */
|
|
259
|
+
function droppedBy(failed: Failed, root: string): string {
|
|
260
|
+
return failed.kind === "load"
|
|
261
|
+
? targetRelative(
|
|
262
|
+
renderDiagnostic(failed.errors[0], (path) => relativePath(root, path) ?? path),
|
|
263
|
+
root,
|
|
264
|
+
)
|
|
265
|
+
: failed.kind === "setup"
|
|
266
|
+
? targetRelative(failureLine(failed.failures[0]), root)
|
|
267
|
+
: "the compiler opened no project for the configuration";
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Every opened project of the run classified as analyzed, dropped or failing by the policy
|
|
272
|
+
* table, before any stage reads one. A failure that ends the run is one {@link FailureExit}, by
|
|
273
|
+
* this precedence: a configuration the snapshot opened nothing for, then the setup failures,
|
|
274
|
+
* then the configurations that did not load with their errors, then, where every derived
|
|
275
|
+
* configuration was dropped, their setup failures or the first reason.
|
|
276
|
+
*/
|
|
277
|
+
export function classify(
|
|
278
|
+
host: Host,
|
|
279
|
+
run: RunPlan,
|
|
280
|
+
views: ReadonlyMap<string, ProjectView<unknown>>,
|
|
281
|
+
): Classified {
|
|
282
|
+
const { root } = run;
|
|
283
|
+
const notBuilt: NotBuilt[] = [...run.discovery.notBuilt];
|
|
284
|
+
const librariesUnfound: LibraryUnfound[] = [];
|
|
285
|
+
const analyzed: Analyzed<unknown>[] = [];
|
|
286
|
+
const consumed: Consumed<unknown>[] = [];
|
|
287
|
+
const ended = new Map<Failed["kind"], { plan: ProgramPlan; failed: Failed }[]>();
|
|
288
|
+
const dropped: Failed[] = [];
|
|
289
|
+
const unopened = run.plans.filter((plan) => !views.has(plan.configFile));
|
|
290
|
+
const outcomes = [
|
|
291
|
+
...run.plans.flatMap((plan) => {
|
|
292
|
+
const view = views.get(plan.configFile);
|
|
293
|
+
return view === undefined
|
|
294
|
+
? []
|
|
295
|
+
: [
|
|
296
|
+
{
|
|
297
|
+
plan,
|
|
298
|
+
outcome:
|
|
299
|
+
plan.consumer === undefined
|
|
300
|
+
? classifyTarget(host, run, plan, view, librariesUnfound)
|
|
301
|
+
: classifyConsumer(
|
|
302
|
+
host,
|
|
303
|
+
run.consumerWorkspaces.get(plan.consumer.path),
|
|
304
|
+
plan,
|
|
305
|
+
plan.consumer,
|
|
306
|
+
view,
|
|
307
|
+
),
|
|
308
|
+
},
|
|
309
|
+
];
|
|
310
|
+
}),
|
|
311
|
+
...unopened.map((plan) => ({ plan, outcome: { kind: "unopened" } as const })),
|
|
312
|
+
];
|
|
313
|
+
for (const { plan, outcome } of outcomes) {
|
|
314
|
+
if (!("kind" in outcome)) {
|
|
315
|
+
if ("consumer" in outcome) {
|
|
316
|
+
consumed.push(outcome);
|
|
317
|
+
} else {
|
|
318
|
+
analyzed.push(outcome);
|
|
319
|
+
}
|
|
320
|
+
} else if (POLICY[plan.origin][outcome.kind] === "end") {
|
|
321
|
+
ended.set(outcome.kind, [...(ended.get(outcome.kind) ?? []), { plan, failed: outcome }]);
|
|
322
|
+
} else {
|
|
323
|
+
dropped.push(outcome);
|
|
324
|
+
notBuilt.push({ id: plan.id, configFile: plan.configFile, error: droppedBy(outcome, root) });
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
for (const kind of ["unopened", "setup", "load"] as const) {
|
|
328
|
+
const all = ended.get(kind) ?? [];
|
|
329
|
+
const failures = nonEmpty(all.flatMap(({ plan, failed }) => ending(plan, failed, root)));
|
|
330
|
+
if (failures !== undefined) {
|
|
331
|
+
throw new FailureExit(
|
|
332
|
+
kind === "unopened" ? [failures[0]] : failures,
|
|
333
|
+
all.flatMap(({ failed }) => (failed.kind === "load" ? failed.errors : [])),
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const setupDropped = nonEmpty(
|
|
338
|
+
dropped.flatMap((one) => (one.kind === "setup" ? one.failures : [])),
|
|
339
|
+
);
|
|
340
|
+
const [first] = notBuilt;
|
|
341
|
+
if (analyzed.length === 0 && first !== undefined) {
|
|
342
|
+
throw new FailureExit(
|
|
343
|
+
setupDropped ?? [
|
|
344
|
+
{
|
|
345
|
+
failureClass: "no-program",
|
|
346
|
+
detail: `no configuration discovery derived could be built; ${first.id}: ${first.error}`,
|
|
347
|
+
},
|
|
348
|
+
],
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
return { analyzed, consumed, notBuilt, librariesUnfound };
|
|
352
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { DiagnosticCategory } from "@typescript/native/sync";
|
|
2
|
+
import type { Diagnostic } from "@typescript/native/sync";
|
|
3
|
+
import type { ProjectDiagnostics } from "../session.ts";
|
|
4
|
+
|
|
5
|
+
/** What one error diagnostic of an opened project does to the run. */
|
|
6
|
+
type DiagnosticClass =
|
|
7
|
+
/** The project did not load, whatever its files say. */
|
|
8
|
+
| "load"
|
|
9
|
+
/** A library the configuration names that the compiler cannot find; the project loads. */
|
|
10
|
+
| "unfound-library"
|
|
11
|
+
/** An import of a module the compiler cannot find, a setup failure where the project expects it. */
|
|
12
|
+
| "module-not-found"
|
|
13
|
+
/** A type error: the unit holding it is skipped. */
|
|
14
|
+
| "type-error"
|
|
15
|
+
/** No error of the program's: the compiler checks the whole program all the same. */
|
|
16
|
+
| "ignored";
|
|
17
|
+
|
|
18
|
+
/** One row of the diagnostic table: the codes it covers, for errors placed in a file or in none. */
|
|
19
|
+
interface DiagnosticRow {
|
|
20
|
+
readonly placed: boolean;
|
|
21
|
+
readonly codes: ReadonlySet<number> | { readonly from: number; readonly below: number };
|
|
22
|
+
readonly class: DiagnosticClass;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The code of a global type the checker finds in no library the program holds. */
|
|
26
|
+
export const MISSING_GLOBAL_TYPE = 2318;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What each error diagnostic does, first matching row first. In no file: an unfound type library
|
|
30
|
+
* or lib (https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/compiler/processingDiagnostic.go),
|
|
31
|
+
* then the semantic band from 2000; the option band from 5000 leaves the program whole
|
|
32
|
+
* (https://github.com/microsoft/TypeScript/wiki/Coding-guidelines#diagnostic-message-codes).
|
|
33
|
+
* In a file: an unfound module, and where the configuration places the file (5055, 5056, 6059,
|
|
34
|
+
* 6307), which changes no type a statement carries.
|
|
35
|
+
*/
|
|
36
|
+
const DIAGNOSTIC_TABLE: readonly DiagnosticRow[] = [
|
|
37
|
+
{
|
|
38
|
+
placed: false,
|
|
39
|
+
codes: new Set([MISSING_GLOBAL_TYPE, 2688, 2726, 2727]),
|
|
40
|
+
class: "unfound-library",
|
|
41
|
+
},
|
|
42
|
+
{ placed: false, codes: { from: 2000, below: 3000 }, class: "load" },
|
|
43
|
+
{ placed: true, codes: new Set([2307, 2792]), class: "module-not-found" },
|
|
44
|
+
{ placed: true, codes: new Set([5055, 5056, 6059, 6307]), class: "ignored" },
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
/** The category an error diagnostic carries, which the protocol sends as a number. */
|
|
48
|
+
const ERROR_CATEGORY: number = DiagnosticCategory.Error;
|
|
49
|
+
|
|
50
|
+
function isError(diagnostic: Diagnostic): boolean {
|
|
51
|
+
return diagnostic.category === ERROR_CATEGORY;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What one diagnostic does, placed in one of the project's own files or in none. A diagnostic
|
|
56
|
+
* the compiler marks as reporting unnecessary code in a file is no error of the program's.
|
|
57
|
+
*/
|
|
58
|
+
export function classOf(diagnostic: Diagnostic, placed: boolean): DiagnosticClass {
|
|
59
|
+
if (!isError(diagnostic) || (placed && diagnostic.reportsUnnecessary === true)) {
|
|
60
|
+
return "ignored";
|
|
61
|
+
}
|
|
62
|
+
const row = DIAGNOSTIC_TABLE.find(
|
|
63
|
+
({ placed: where, codes }) =>
|
|
64
|
+
where === placed &&
|
|
65
|
+
("has" in codes
|
|
66
|
+
? codes.has(diagnostic.code)
|
|
67
|
+
: diagnostic.code >= codes.from && diagnostic.code < codes.below),
|
|
68
|
+
);
|
|
69
|
+
return row?.class ?? (placed ? "type-error" : "ignored");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** One diagnostic, with the line and column its offset falls on where it names a file. */
|
|
73
|
+
export interface LocatedDiagnostic {
|
|
74
|
+
readonly diagnostic: Diagnostic;
|
|
75
|
+
/** Where the diagnostic starts, counted from one; absent where it names no position. */
|
|
76
|
+
readonly at?: { readonly line: number; readonly column: number } | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* One diagnostic as `path:line:column: TScode: message`, the position left out where it names
|
|
81
|
+
* none and the file where it names none. `display` spells the file's path.
|
|
82
|
+
*/
|
|
83
|
+
export function renderDiagnostic(
|
|
84
|
+
located: LocatedDiagnostic,
|
|
85
|
+
display: (path: string) => string = (path) => path,
|
|
86
|
+
): string {
|
|
87
|
+
const { diagnostic, at } = located;
|
|
88
|
+
const message = `TS${String(diagnostic.code)}: ${diagnostic.text}`;
|
|
89
|
+
if (diagnostic.fileName === undefined) {
|
|
90
|
+
return message;
|
|
91
|
+
}
|
|
92
|
+
const where = display(diagnostic.fileName);
|
|
93
|
+
return at === undefined
|
|
94
|
+
? `${where}: ${message}`
|
|
95
|
+
: `${where}:${String(at.line)}:${String(at.column)}: ${message}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function locatedIn(
|
|
99
|
+
diagnostics: readonly Diagnostic[],
|
|
100
|
+
locate: ProjectDiagnostics["locate"],
|
|
101
|
+
): readonly LocatedDiagnostic[] {
|
|
102
|
+
return diagnostics.map((diagnostic) => ({
|
|
103
|
+
diagnostic,
|
|
104
|
+
at: diagnostic.fileName === undefined ? undefined : locate(diagnostic.fileName, diagnostic.pos),
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Every error a project's three diagnostic sets carry, in the order the compiler reported
|
|
110
|
+
* them. A resolved configuration carries no diagnostics of its own, so a configuration written
|
|
111
|
+
* with an unreadable option resolves and is refused here, once its project is open.
|
|
112
|
+
*/
|
|
113
|
+
export function diagnosticErrors(sets: {
|
|
114
|
+
readonly syntactic: readonly Diagnostic[];
|
|
115
|
+
readonly semantic: readonly Diagnostic[];
|
|
116
|
+
readonly configParsing: readonly Diagnostic[];
|
|
117
|
+
}): readonly Diagnostic[] {
|
|
118
|
+
return [...sets.configParsing, ...sets.syntactic, ...sets.semantic].filter(isError);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Why one open project cannot be analyzed as it stands, split by whether it loaded at all. */
|
|
122
|
+
interface ProjectErrors {
|
|
123
|
+
/** The configuration's own errors and the errors in no file the table reads as a load failure. */
|
|
124
|
+
readonly load: readonly LocatedDiagnostic[];
|
|
125
|
+
/** The errors in the files of a project that loaded. */
|
|
126
|
+
readonly check: readonly LocatedDiagnostic[];
|
|
127
|
+
/** The libraries the configuration names that the compiler cannot find, of a project that loaded. */
|
|
128
|
+
readonly unfound: readonly LocatedDiagnostic[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The errors one open project carries, each located in the file it names. */
|
|
132
|
+
export function projectErrors(sets: ProjectDiagnostics): ProjectErrors {
|
|
133
|
+
const unplaced = (kind: DiagnosticClass): readonly Diagnostic[] =>
|
|
134
|
+
sets.unplaced.filter((diagnostic) => classOf(diagnostic, false) === kind);
|
|
135
|
+
const load = [...sets.configParsing.filter(isError), ...unplaced("load")];
|
|
136
|
+
return {
|
|
137
|
+
load: locatedIn(load, sets.locate),
|
|
138
|
+
check: load.length > 0 ? [] : locatedIn(diagnosticErrors(sets), sets.locate),
|
|
139
|
+
unfound: load.length > 0 ? [] : locatedIn(unplaced("unfound-library"), sets.locate),
|
|
140
|
+
};
|
|
141
|
+
}
|