@tayomi/deterministic-gates 0.6.1 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/catalog/angular.json +28 -4
- package/catalog/index.html +33 -7
- package/catalog/typescript.json +3 -2
- package/dist/src/core/config.js +10 -4
- package/dist/src/core/watch.js +277 -103
- package/dist/src/helpers/template-errors.js +85 -0
- package/dist/src/helpers/tsc.js +1 -1
- package/dist/src/helpers/type-errors.js +22 -14
- package/package.json +1 -1
- package/src/core/config.ts +14 -4
- package/src/core/watch.ts +284 -101
- package/src/helpers/template-errors.ts +101 -0
- package/src/helpers/tsc.ts +1 -1
- package/src/helpers/type-errors.ts +35 -15
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { configOf, nothingChecked } from "./tsc.ts";
|
|
5
|
+
import { strictErrorLines } from "./type-errors.ts";
|
|
6
|
+
|
|
7
|
+
// The Angular compiler of the project, with strict templates and the options it is given forced on top of the
|
|
8
|
+
// project's own, printing the errors of its templates so that a gate can compare them with the ones of the turn start.
|
|
9
|
+
// It goes through the compiler's API rather than ngc: ngc stops at the errors of the TypeScript, and a strict option
|
|
10
|
+
// a project turned off brings many, so it would report no error of a template. Each error prints on the line the
|
|
11
|
+
// strict TypeScript errors print on. The errors of the TypeScript are left to them.
|
|
12
|
+
|
|
13
|
+
/** A diagnostic of the Angular compiler, as its API gives it. */
|
|
14
|
+
export interface Diagnostic {
|
|
15
|
+
file?: { fileName: string; text: string };
|
|
16
|
+
start?: number;
|
|
17
|
+
code: number;
|
|
18
|
+
messageText: string | { messageText: string };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A code as Angular prints it: its own codes are negative, -99 then their number, as -998001 for NG8001. */
|
|
22
|
+
function codeOf(code: number): string {
|
|
23
|
+
return code < 0 ? `NG${String(-code).slice(2)}` : `TS${code}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The errors of the templates, each on one line, as the gate compares them. */
|
|
27
|
+
export function templateErrors(root: string, diagnostics: readonly Diagnostic[]): string[] {
|
|
28
|
+
return strictErrorLines(
|
|
29
|
+
diagnostics.map(({ file, start = 0, code, messageText }) => {
|
|
30
|
+
const message = typeof messageText === "string" ? messageText : messageText.messageText;
|
|
31
|
+
if (file === undefined) return message;
|
|
32
|
+
const line = file.text.slice(0, start).split("\n").length - 1;
|
|
33
|
+
return {
|
|
34
|
+
file: relative(root, file.fileName),
|
|
35
|
+
code: codeOf(code),
|
|
36
|
+
source: file.text.split("\n")[line] ?? "",
|
|
37
|
+
message: message.split("\n")[0] ?? "",
|
|
38
|
+
};
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The options forced on top of the project's: strict templates, and each --option the gate gives. */
|
|
44
|
+
function forced(args: string[]): Record<string, boolean> {
|
|
45
|
+
const options: Record<string, boolean> = { strictTemplates: true };
|
|
46
|
+
for (const arg of args) if (arg.startsWith("--") && arg !== "--project") options[arg.slice(2)] = true;
|
|
47
|
+
return options;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The Angular compiler of the repo, with the options of the gate: what it says of the templates. */
|
|
51
|
+
interface Compiler {
|
|
52
|
+
diagnostics(): Promise<readonly Diagnostic[]>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The Angular compiler of the repo, once it is known to check files with the API this helper calls: otherwise, why
|
|
57
|
+
* not. That API is not a public one of Angular, so a version that changed it refuses the gate, never passes it green.
|
|
58
|
+
*/
|
|
59
|
+
export async function compilerOf(root: string, args: string[]): Promise<{ compiler: Compiler } | { refused: string }> {
|
|
60
|
+
const nothing = nothingChecked(root, args);
|
|
61
|
+
if (nothing) return { refused: nothing };
|
|
62
|
+
const required = createRequire(join(root, "package.json"));
|
|
63
|
+
const ng = await import(pathToFileURL(required.resolve("@angular/compiler-cli")).href);
|
|
64
|
+
const { version } = required("@angular/compiler-cli/package.json") as { version: string };
|
|
65
|
+
const program = ng.NgtscProgram?.prototype;
|
|
66
|
+
const missing = (
|
|
67
|
+
[
|
|
68
|
+
["NgtscProgram", ng.NgtscProgram],
|
|
69
|
+
["NgtscProgram.loadNgStructureAsync", program?.loadNgStructureAsync],
|
|
70
|
+
["NgtscProgram.getNgSemanticDiagnostics", program?.getNgSemanticDiagnostics],
|
|
71
|
+
["readConfiguration", ng.readConfiguration],
|
|
72
|
+
["createCompilerHost", ng.createCompilerHost],
|
|
73
|
+
] as const
|
|
74
|
+
).find(([, found]) => typeof found !== "function")?.[0];
|
|
75
|
+
if (missing !== undefined)
|
|
76
|
+
return {
|
|
77
|
+
refused: `The API of @angular/compiler-cli ${version} is not the one this gate knows: it has no ${missing}. Update deterministic-gates.`,
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
compiler: {
|
|
81
|
+
async diagnostics() {
|
|
82
|
+
const config = ng.readConfiguration(configOf(root, args));
|
|
83
|
+
const options = { ...config.options, ...forced(args) };
|
|
84
|
+
const program = new ng.NgtscProgram(config.rootNames, options, ng.createCompilerHost({ options }));
|
|
85
|
+
await program.loadNgStructureAsync();
|
|
86
|
+
return program.getNgSemanticDiagnostics();
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function main(root: string, args: string[]): Promise<void> {
|
|
93
|
+
const found = await compilerOf(root, args);
|
|
94
|
+
if ("refused" in found) {
|
|
95
|
+
console.log(found.refused);
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
console.log(templateErrors(root, await found.compiler.diagnostics()).join("\n"));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (import.meta.main) await main(process.cwd(), process.argv.slice(2));
|
package/src/helpers/tsc.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { type Command, commandOf } from "../core/run.ts";
|
|
|
10
10
|
// refused, with the tsconfigs it references.
|
|
11
11
|
|
|
12
12
|
/** The tsconfig tsc reads: the one of -p or --project, a folder meaning its tsconfig.json, or the one of the root. */
|
|
13
|
-
function configOf(root: string, args: string[]): string {
|
|
13
|
+
export function configOf(root: string, args: string[]): string {
|
|
14
14
|
const at = args.findIndex((arg) => arg === "-p" || arg === "--project");
|
|
15
15
|
const given = at === -1 ? undefined : args[at + 1];
|
|
16
16
|
if (given === undefined) return join(root, "tsconfig.json");
|
|
@@ -26,27 +26,47 @@ function cut(text: string, chars: number): string {
|
|
|
26
26
|
return text.length > chars ? `${text.slice(0, chars)}…` : text;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/** An error of a file, as a strict gate compares it: its source line is the text of the line it points at. */
|
|
30
|
+
export interface StrictError {
|
|
31
|
+
file: string;
|
|
32
|
+
code: string;
|
|
33
|
+
source: string;
|
|
34
|
+
message: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The errors, each on one line: a key, its file, code and source line, with a count after one its file already has
|
|
39
|
+
* on the same source; then, after a tab, the first line of its message. An error of no file is kept as it is.
|
|
40
|
+
*/
|
|
41
|
+
export function strictErrorLines(errors: (StrictError | string)[]): string[] {
|
|
42
|
+
const seen = new Map<string, number>();
|
|
43
|
+
return errors.map((error) => {
|
|
44
|
+
const source = typeof error === "string" ? "" : error.source.replaceAll("\t", " ").trim();
|
|
45
|
+
const found =
|
|
46
|
+
typeof error === "string" ? error : `${error.file}: error ${error.code} at \`${cut(source, SOURCE_CHARS)}\``;
|
|
47
|
+
const count = (seen.get(found) ?? 0) + 1;
|
|
48
|
+
seen.set(found, count);
|
|
49
|
+
const key = count === 1 ? found : `${found} (${count})`;
|
|
50
|
+
return typeof error === "string" ? key : `${key}\t${cut(error.message, MESSAGE_CHARS)}`;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
29
54
|
/** The errors tsc printed, each on one line, as the gate compares them. */
|
|
30
55
|
export function typeErrors(printed: string[], sources: Sources): string[] {
|
|
31
56
|
const lines = new Map<string, string[]>();
|
|
32
57
|
const sourceLine = (file: string, line: number) => {
|
|
33
58
|
if (!lines.has(file)) lines.set(file, (sources(file) ?? "").split("\n"));
|
|
34
|
-
return
|
|
59
|
+
return lines.get(file)?.[line - 1] ?? "";
|
|
35
60
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
seen.set(found, count);
|
|
46
|
-
const key = count === 1 ? found : `${found} (${count})`;
|
|
47
|
-
errors.push(file === undefined ? key : `${key}\t${cut(message, MESSAGE_CHARS)}`);
|
|
48
|
-
}
|
|
49
|
-
return errors;
|
|
61
|
+
return strictErrorLines(
|
|
62
|
+
printed
|
|
63
|
+
// The lines under an error explain it, and name its first failing member.
|
|
64
|
+
.filter((line) => line.trim() && !/^\s/.test(line))
|
|
65
|
+
.map((line) => {
|
|
66
|
+
const [, file, at, code = "", message = ""] = POSITION.exec(line) ?? [];
|
|
67
|
+
return file === undefined ? line : { file, code, source: sourceLine(file, Number(at)), message };
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
50
70
|
}
|
|
51
71
|
|
|
52
72
|
/** What to print for each line of tsc in watch mode: nothing until a compilation ends, then its errors. */
|