@tayomi/deterministic-gates 0.7.1 → 0.8.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 +23 -4
- package/catalog/index.html +22 -8
- package/dist/src/core/watch.js +8 -5
- package/dist/src/helpers/angular-errors.js +183 -0
- package/package.json +1 -1
- package/src/core/watch.ts +8 -5
- package/src/helpers/angular-errors.ts +200 -0
- package/dist/src/helpers/template-errors.js +0 -85
- package/src/helpers/template-errors.ts +0 -101
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/claude-code-plugin-manifest.json",
|
|
3
3
|
"name": "deterministic-gates",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.8.1",
|
|
5
5
|
"description": "Deterministic backpressure for coding agents: gates that run on what each turn changed and send the agent back until they pass.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Pierre Moati",
|
package/catalog/angular.json
CHANGED
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
}
|
|
86
86
|
},
|
|
87
87
|
{
|
|
88
|
-
"description": "Fails on the strict
|
|
88
|
+
"description": "Fails on the strict errors the turn added, in the TypeScript and in the templates, never on the ones already there, for a project that turned strict options off: it compiles the application through the API of the Angular compiler, with strictTemplates and each option of TypeScript its run lists forced on top of the project's own. Add or remove an option there, written as tsc takes it: one TypeScript does not know is refused. On an Angular project, it stands for the no-new-strict-errors of the TypeScript catalog, which reads no template. The compiler stays between turns and checks again when asked, reusing what it compiled: it is asked through its tsconfig, the one of -p. The build gate checks with the project's own options. An error is not new when moved, or reported with the same message in another file the turn did not change, or on a changed line as long as its file has no more errors of its code than at the turn start. It checks tsconfig.app.json: give its -p, and its watch.sync, the tsconfig of the application.",
|
|
89
89
|
"requires": [
|
|
90
90
|
{
|
|
91
91
|
"tool": "@angular/compiler-cli",
|
|
@@ -97,12 +97,31 @@
|
|
|
97
97
|
}
|
|
98
98
|
],
|
|
99
99
|
"gate": {
|
|
100
|
-
"name": "no-new-strict-
|
|
100
|
+
"name": "no-new-strict-errors",
|
|
101
101
|
"severity": "error",
|
|
102
102
|
"files": "(\\.(ts|html)|(^|/)tsconfig[^/]*\\.json)$",
|
|
103
|
-
"run": [
|
|
103
|
+
"run": [
|
|
104
|
+
"{engine}/src/helpers/angular-errors.ts",
|
|
105
|
+
"-p",
|
|
106
|
+
"tsconfig.app.json",
|
|
107
|
+
"--strictNullChecks",
|
|
108
|
+
"--strictPropertyInitialization"
|
|
109
|
+
],
|
|
104
110
|
"compare": "turn-start",
|
|
105
|
-
"lineKey": "^((?<file>.+?): error (?:TS|NG)\\d+) at [^\\t]*"
|
|
111
|
+
"lineKey": "^((?<file>.+?): error (?:TS|NG)\\d+) at [^\\t]*",
|
|
112
|
+
"watch": {
|
|
113
|
+
"run": [
|
|
114
|
+
"{engine}/src/helpers/angular-errors.ts",
|
|
115
|
+
"--watch",
|
|
116
|
+
"-p",
|
|
117
|
+
"tsconfig.app.json",
|
|
118
|
+
"--strictNullChecks",
|
|
119
|
+
"--strictPropertyInitialization"
|
|
120
|
+
],
|
|
121
|
+
"cycleStart": "^Type check started$",
|
|
122
|
+
"cycleEnd": "^Type check done$",
|
|
123
|
+
"sync": "tsconfig.app.json"
|
|
124
|
+
}
|
|
106
125
|
}
|
|
107
126
|
}
|
|
108
127
|
]
|
package/catalog/index.html
CHANGED
|
@@ -128,26 +128,40 @@ summary { cursor: pointer; color: var(--muted); }
|
|
|
128
128
|
]
|
|
129
129
|
}</code></pre></details>
|
|
130
130
|
</article>
|
|
131
|
-
<article id="angular/no-new-strict-
|
|
132
|
-
<h3><code>angular/no-new-strict-
|
|
133
|
-
<p>Fails on the strict
|
|
131
|
+
<article id="angular/no-new-strict-errors">
|
|
132
|
+
<h3><code>angular/no-new-strict-errors</code></h3>
|
|
133
|
+
<p>Fails on the strict errors the turn added, in the TypeScript and in the templates, never on the ones already there, for a project that turned strict options off: it compiles the application through the API of the Angular compiler, with strictTemplates and each option of TypeScript its run lists forced on top of the project's own. Add or remove an option there, written as tsc takes it: one TypeScript does not know is refused. On an Angular project, it stands for the no-new-strict-errors of the TypeScript catalog, which reads no template. The compiler stays between turns and checks again when asked, reusing what it compiled: it is asked through its tsconfig, the one of -p. The build gate checks with the project's own options. An error is not new when moved, or reported with the same message in another file the turn did not change, or on a changed line as long as its file has no more errors of its code than at the turn start. It checks tsconfig.app.json: give its -p, and its watch.sync, the tsconfig of the application.</p>
|
|
134
134
|
<dl>
|
|
135
|
-
<dt>Runs</dt><dd><code>{engine}/src/helpers/
|
|
135
|
+
<dt>Runs</dt><dd><code>{engine}/src/helpers/angular-errors.ts -p tsconfig.app.json --strictNullChecks --strictPropertyInitialization</code></dd>
|
|
136
136
|
<dt>When the turn changes</dt><dd><code>(\.(ts|html)|(^|/)tsconfig[^/]*\.json)$</code></dd>
|
|
137
137
|
<dt>Fails on</dt><dd>only the output lines the turn added</dd><dt>Requires</dt><dd><ul><li><code>@angular/compiler-cli</code>: npm install --save-dev @angular/compiler-cli</li><li><code>typescript</code>: npm install --save-dev typescript</li></ul></dd>
|
|
138
138
|
</dl>
|
|
139
139
|
<details><summary>Definition to copy into .gates.json</summary><pre><code>{
|
|
140
|
-
"name": "no-new-strict-
|
|
140
|
+
"name": "no-new-strict-errors",
|
|
141
141
|
"severity": "error",
|
|
142
142
|
"files": "(\\.(ts|html)|(^|/)tsconfig[^/]*\\.json)$",
|
|
143
143
|
"run": [
|
|
144
|
-
"{engine}/src/helpers/
|
|
144
|
+
"{engine}/src/helpers/angular-errors.ts",
|
|
145
145
|
"-p",
|
|
146
146
|
"tsconfig.app.json",
|
|
147
|
-
"--strictNullChecks"
|
|
147
|
+
"--strictNullChecks",
|
|
148
|
+
"--strictPropertyInitialization"
|
|
148
149
|
],
|
|
149
150
|
"compare": "turn-start",
|
|
150
|
-
"lineKey": "^((?<file>.+?): error (?:TS|NG)\\d+) at [^\\t]*"
|
|
151
|
+
"lineKey": "^((?<file>.+?): error (?:TS|NG)\\d+) at [^\\t]*",
|
|
152
|
+
"watch": {
|
|
153
|
+
"run": [
|
|
154
|
+
"{engine}/src/helpers/angular-errors.ts",
|
|
155
|
+
"--watch",
|
|
156
|
+
"-p",
|
|
157
|
+
"tsconfig.app.json",
|
|
158
|
+
"--strictNullChecks",
|
|
159
|
+
"--strictPropertyInitialization"
|
|
160
|
+
],
|
|
161
|
+
"cycleStart": "^Type check started$",
|
|
162
|
+
"cycleEnd": "^Type check done$",
|
|
163
|
+
"sync": "tsconfig.app.json"
|
|
164
|
+
}
|
|
151
165
|
}</code></pre></details>
|
|
152
166
|
</article>
|
|
153
167
|
</section>
|
package/dist/src/core/watch.js
CHANGED
|
@@ -223,11 +223,14 @@ async function restartOnFrozenChange(session, root, gate, folder) {
|
|
|
223
223
|
* Asks the watch of a gate for a cycle that read the files of the turn, and waits for it, with no guess on how long a
|
|
224
224
|
* watch takes to see a change. Each round touches files, then waits for the watch to open a cycle after the touch,
|
|
225
225
|
* which only a watch that saw the touch does:
|
|
226
|
-
* 1. the
|
|
227
|
-
*
|
|
228
|
-
*
|
|
226
|
+
* 1. the sync file alone. The cycle that follows proves the watch now looks for changes, whoever made it open: a
|
|
227
|
+
* watch may look for them only once its first cycle is over, as Angular and tsc do, and a touch made before is
|
|
228
|
+
* lost. Touched with the sync file in this round, the files of the turn could be lost with it, and a cycle the
|
|
229
|
+
* watch then opens for a change of someone else, another gate or another session, would answer for them;
|
|
230
|
+
* 2. the files of the turn, so that a change it missed reaches it, then the sync file;
|
|
231
|
+
* 3. the sync file, once that cycle has closed: the cycle of round 2 may have been on its way before the touch, and
|
|
229
232
|
* this one opens after every change the watch then had waiting;
|
|
230
|
-
*
|
|
233
|
+
* 4. the sync file once more, since the cycle it opens ends the one of round 3, the one that judges, whose last lines
|
|
231
234
|
* may come after the line that closes it.
|
|
232
235
|
* A watch that opens no cycle however often it is touched does not look at its sync file, and is said so. A watch no
|
|
233
236
|
* hook started, as when the hooks were installed mid-session, starts now, and so does one the engine
|
|
@@ -244,7 +247,7 @@ async function settle(session, root, gate, files, limitMs) {
|
|
|
244
247
|
}
|
|
245
248
|
await restartOnFrozenChange(session, root, gate, folder);
|
|
246
249
|
const turn = files.map((file) => join(root, file)).filter((path) => existsSync(path));
|
|
247
|
-
const rounds = [[...turn, sync], [sync], [sync]];
|
|
250
|
+
const rounds = [[sync], ...(turn.length ? [[...turn, sync]] : []), [sync], [sync]];
|
|
248
251
|
let asked;
|
|
249
252
|
let round = 0;
|
|
250
253
|
// The cycles the watch had opened when the round touched its files, when it last did, and how many times: none
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { statSync, watchFile } from "node:fs";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import { join, relative } from "node:path";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
import { configOf, nothingChecked } from "./tsc.js";
|
|
14
|
+
import { strictErrorLines } from "./type-errors.js";
|
|
15
|
+
/** A code as Angular prints it: its own codes are negative, -99 then their number, as -998001 for NG8001. */
|
|
16
|
+
function codeOf(code) {
|
|
17
|
+
return code < 0 ? `NG${String(-code).slice(2)}` : `TS${code}`;
|
|
18
|
+
}
|
|
19
|
+
/** The errors of the project, each on one line, as the gate compares them. */
|
|
20
|
+
export function angularErrors(root, diagnostics) {
|
|
21
|
+
return strictErrorLines(diagnostics.map(({ file, start = 0, code, messageText }) => {
|
|
22
|
+
const message = typeof messageText === "string" ? messageText : messageText.messageText;
|
|
23
|
+
if (file === undefined)
|
|
24
|
+
return message;
|
|
25
|
+
const line = file.text.slice(0, start).split("\n").length - 1;
|
|
26
|
+
return {
|
|
27
|
+
file: relative(root, file.fileName),
|
|
28
|
+
code: codeOf(code),
|
|
29
|
+
source: file.text.split("\n")[line] ?? "",
|
|
30
|
+
message: message.split("\n")[0] ?? "",
|
|
31
|
+
};
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
/** The arguments that are options of the compiler: not the tsconfig, nor --watch, which are the helper's own. */
|
|
35
|
+
function compilerArgs(args) {
|
|
36
|
+
const own = (arg) => arg === "-p" || arg === "--project";
|
|
37
|
+
return args.filter((arg, at) => arg !== "--watch" && !own(arg) && !own(args[at - 1]));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The options forced on top of the project's: strict templates, and each option of TypeScript the gate gives, as tsc
|
|
41
|
+
* reads them. One TypeScript does not know is refused, since a misspelt option would otherwise check nothing more and
|
|
42
|
+
* leave the gate green.
|
|
43
|
+
*/
|
|
44
|
+
function forced(ts, args) {
|
|
45
|
+
const { options, errors, fileNames } = ts.parseCommandLine(compilerArgs(args));
|
|
46
|
+
const [error] = errors;
|
|
47
|
+
if (error !== undefined)
|
|
48
|
+
return { refused: ts.flattenDiagnosticMessageText(error.messageText, "\n") };
|
|
49
|
+
const [stray] = fileNames;
|
|
50
|
+
if (stray !== undefined)
|
|
51
|
+
return { refused: `${stray} is not an option of the compiler.` };
|
|
52
|
+
return { options: { strictTemplates: true, ...options } };
|
|
53
|
+
}
|
|
54
|
+
function changedAt(file) {
|
|
55
|
+
return statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? 0;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The Angular compiler of the repo, once it is known to check files with the API this helper calls: otherwise, why
|
|
59
|
+
* not. That API is not a public one of Angular, so a version that changed it refuses the gate, never passes it green.
|
|
60
|
+
*/
|
|
61
|
+
export async function compilerOf(root, args) {
|
|
62
|
+
const nothing = nothingChecked(root, args);
|
|
63
|
+
if (nothing)
|
|
64
|
+
return { refused: nothing };
|
|
65
|
+
const required = createRequire(join(root, "package.json"));
|
|
66
|
+
const given = forced(required("typescript"), args);
|
|
67
|
+
if ("refused" in given)
|
|
68
|
+
return given;
|
|
69
|
+
const ng = await import(__rewriteRelativeImportExtension(pathToFileURL(required.resolve("@angular/compiler-cli")).href));
|
|
70
|
+
const { version } = required("@angular/compiler-cli/package.json");
|
|
71
|
+
const api = ng.NgtscProgram?.prototype;
|
|
72
|
+
const missing = [
|
|
73
|
+
["NgtscProgram", ng.NgtscProgram],
|
|
74
|
+
["NgtscProgram.loadNgStructureAsync", api?.loadNgStructureAsync],
|
|
75
|
+
["NgtscProgram.getTsSyntacticDiagnostics", api?.getTsSyntacticDiagnostics],
|
|
76
|
+
["NgtscProgram.getTsSemanticDiagnostics", api?.getTsSemanticDiagnostics],
|
|
77
|
+
["NgtscProgram.getNgSemanticDiagnostics", api?.getNgSemanticDiagnostics],
|
|
78
|
+
["readConfiguration", ng.readConfiguration],
|
|
79
|
+
["createCompilerHost", ng.createCompilerHost],
|
|
80
|
+
].find(([, found]) => typeof found !== "function")?.[0];
|
|
81
|
+
if (missing !== undefined)
|
|
82
|
+
return {
|
|
83
|
+
refused: `The API of @angular/compiler-cli ${version} is not the one this gate knows: it has no ${missing}. Update deterministic-gates.`,
|
|
84
|
+
};
|
|
85
|
+
// What the check before this one read, by the date it had: a file whose date is the same is given as it was, which
|
|
86
|
+
// is how the compiler knows it need not check it again.
|
|
87
|
+
const sources = new Map();
|
|
88
|
+
const resources = new Map();
|
|
89
|
+
let last;
|
|
90
|
+
return {
|
|
91
|
+
compiler: {
|
|
92
|
+
async diagnostics() {
|
|
93
|
+
const config = ng.readConfiguration(configOf(root, args));
|
|
94
|
+
const options = { ...config.options, ...given.options };
|
|
95
|
+
// A program checked with other options is no help to this one.
|
|
96
|
+
if (last?.options !== JSON.stringify(options)) {
|
|
97
|
+
last = undefined;
|
|
98
|
+
sources.clear();
|
|
99
|
+
resources.clear();
|
|
100
|
+
}
|
|
101
|
+
const base = ng.createCompilerHost({ options });
|
|
102
|
+
const host = {
|
|
103
|
+
...base,
|
|
104
|
+
getSourceFile(file, ...rest) {
|
|
105
|
+
const at = changedAt(file);
|
|
106
|
+
const kept = sources.get(file);
|
|
107
|
+
if (kept?.at === at)
|
|
108
|
+
return kept.source;
|
|
109
|
+
const source = base.getSourceFile(file, ...rest);
|
|
110
|
+
if (source === undefined)
|
|
111
|
+
sources.delete(file);
|
|
112
|
+
else
|
|
113
|
+
sources.set(file, { at, source });
|
|
114
|
+
return source;
|
|
115
|
+
},
|
|
116
|
+
// A template or a style, which the compiler reads as text.
|
|
117
|
+
readFile(file) {
|
|
118
|
+
if (!/\.(ts|json)$/.test(file) && !resources.has(file))
|
|
119
|
+
resources.set(file, changedAt(file));
|
|
120
|
+
return base.readFile(file);
|
|
121
|
+
},
|
|
122
|
+
getModifiedResourceFiles() {
|
|
123
|
+
const modified = new Set();
|
|
124
|
+
for (const [file, at] of resources) {
|
|
125
|
+
const now = changedAt(file);
|
|
126
|
+
if (now === at)
|
|
127
|
+
continue;
|
|
128
|
+
modified.add(file);
|
|
129
|
+
resources.set(file, now);
|
|
130
|
+
}
|
|
131
|
+
return modified;
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
const program = new ng.NgtscProgram(config.rootNames, options, host, last?.program);
|
|
135
|
+
await program.loadNgStructureAsync();
|
|
136
|
+
last = { program, options: JSON.stringify(options) };
|
|
137
|
+
return [
|
|
138
|
+
...config.errors,
|
|
139
|
+
...program.getTsSyntacticDiagnostics(),
|
|
140
|
+
...program.getTsSemanticDiagnostics(),
|
|
141
|
+
...program.getNgSemanticDiagnostics(),
|
|
142
|
+
];
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Checks now, then each time the date of the tsconfig changes, saying when a check starts and when it is done. */
|
|
148
|
+
async function watch(root, args, compiler) {
|
|
149
|
+
let asked = false;
|
|
150
|
+
let checking = false;
|
|
151
|
+
const check = async () => {
|
|
152
|
+
asked = true;
|
|
153
|
+
if (checking)
|
|
154
|
+
return;
|
|
155
|
+
checking = true;
|
|
156
|
+
// Asked again while it checked: the files may have changed since it read them.
|
|
157
|
+
while (asked) {
|
|
158
|
+
asked = false;
|
|
159
|
+
console.log("Type check started");
|
|
160
|
+
console.log([...angularErrors(root, await compiler.diagnostics()), "Type check done"].join("\n"));
|
|
161
|
+
}
|
|
162
|
+
checking = false;
|
|
163
|
+
};
|
|
164
|
+
// Looked at rather than watched: a date read ten times a second misses no change, whatever the file system says.
|
|
165
|
+
watchFile(configOf(root, args), { interval: 100 }, (now, before) => {
|
|
166
|
+
if (now.mtimeMs !== before.mtimeMs)
|
|
167
|
+
void check();
|
|
168
|
+
});
|
|
169
|
+
await check();
|
|
170
|
+
}
|
|
171
|
+
async function main(root, args) {
|
|
172
|
+
const found = await compilerOf(root, args);
|
|
173
|
+
if ("refused" in found) {
|
|
174
|
+
console.log(found.refused);
|
|
175
|
+
process.exit(1);
|
|
176
|
+
}
|
|
177
|
+
if (args.includes("--watch"))
|
|
178
|
+
await watch(root, args, found.compiler);
|
|
179
|
+
else
|
|
180
|
+
console.log(angularErrors(root, await found.compiler.diagnostics()).join("\n"));
|
|
181
|
+
}
|
|
182
|
+
if (import.meta.main)
|
|
183
|
+
await main(process.cwd(), process.argv.slice(2));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tayomi/deterministic-gates",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Deterministic backpressure for coding agents: gates that run on what each turn changed and send the agent back until they pass.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/core/watch.ts
CHANGED
|
@@ -249,11 +249,14 @@ async function restartOnFrozenChange(session: string, root: string, gate: Watche
|
|
|
249
249
|
* Asks the watch of a gate for a cycle that read the files of the turn, and waits for it, with no guess on how long a
|
|
250
250
|
* watch takes to see a change. Each round touches files, then waits for the watch to open a cycle after the touch,
|
|
251
251
|
* which only a watch that saw the touch does:
|
|
252
|
-
* 1. the
|
|
253
|
-
*
|
|
254
|
-
*
|
|
252
|
+
* 1. the sync file alone. The cycle that follows proves the watch now looks for changes, whoever made it open: a
|
|
253
|
+
* watch may look for them only once its first cycle is over, as Angular and tsc do, and a touch made before is
|
|
254
|
+
* lost. Touched with the sync file in this round, the files of the turn could be lost with it, and a cycle the
|
|
255
|
+
* watch then opens for a change of someone else, another gate or another session, would answer for them;
|
|
256
|
+
* 2. the files of the turn, so that a change it missed reaches it, then the sync file;
|
|
257
|
+
* 3. the sync file, once that cycle has closed: the cycle of round 2 may have been on its way before the touch, and
|
|
255
258
|
* this one opens after every change the watch then had waiting;
|
|
256
|
-
*
|
|
259
|
+
* 4. the sync file once more, since the cycle it opens ends the one of round 3, the one that judges, whose last lines
|
|
257
260
|
* may come after the line that closes it.
|
|
258
261
|
* A watch that opens no cycle however often it is touched does not look at its sync file, and is said so. A watch no
|
|
259
262
|
* hook started, as when the hooks were installed mid-session, starts now, and so does one the engine
|
|
@@ -276,7 +279,7 @@ async function settle(
|
|
|
276
279
|
}
|
|
277
280
|
await restartOnFrozenChange(session, root, gate, folder);
|
|
278
281
|
const turn = files.map((file) => join(root, file)).filter((path) => existsSync(path));
|
|
279
|
-
const rounds = [[...turn, sync], [sync], [sync]];
|
|
282
|
+
const rounds = [[sync], ...(turn.length ? [[...turn, sync]] : []), [sync], [sync]];
|
|
280
283
|
let asked: string | undefined;
|
|
281
284
|
let round = 0;
|
|
282
285
|
// The cycles the watch had opened when the round touched its files, when it last did, and how many times: none
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { statSync, watchFile } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import type * as Ts from "typescript";
|
|
6
|
+
import { configOf, nothingChecked } from "./tsc.ts";
|
|
7
|
+
import { strictErrorLines } from "./type-errors.ts";
|
|
8
|
+
|
|
9
|
+
// The Angular compiler of the project, with strict templates and the options it is given forced on top of the
|
|
10
|
+
// project's own, printing the errors of its TypeScript and of its templates so that a gate can compare them with the
|
|
11
|
+
// ones of the turn start: one program gives both, where tsc reads no template, and a compiler of the templates alone
|
|
12
|
+
// would build the same program beside it. It goes through the compiler's API rather than ngc: ngc stops at the errors
|
|
13
|
+
// of the TypeScript, and a strict option a project turned off brings many, so it would report no error of a template.
|
|
14
|
+
// Each error prints on the line the strict TypeScript errors of tsc print on.
|
|
15
|
+
//
|
|
16
|
+
// With --watch, it stays, and checks again each time the date of its tsconfig changes, which is how the engine asks
|
|
17
|
+
// a watch for a cycle. It follows no other file: a check reads the files as they are when it starts, the ones whose
|
|
18
|
+
// date changed since the check before it, so it never misses a change, and it reuses the program of that check.
|
|
19
|
+
|
|
20
|
+
/** A diagnostic of the Angular compiler, as its API gives it. */
|
|
21
|
+
export interface Diagnostic {
|
|
22
|
+
file?: { fileName: string; text: string };
|
|
23
|
+
start?: number;
|
|
24
|
+
code: number;
|
|
25
|
+
messageText: string | { messageText: string };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A code as Angular prints it: its own codes are negative, -99 then their number, as -998001 for NG8001. */
|
|
29
|
+
function codeOf(code: number): string {
|
|
30
|
+
return code < 0 ? `NG${String(-code).slice(2)}` : `TS${code}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The errors of the project, each on one line, as the gate compares them. */
|
|
34
|
+
export function angularErrors(root: string, diagnostics: readonly Diagnostic[]): string[] {
|
|
35
|
+
return strictErrorLines(
|
|
36
|
+
diagnostics.map(({ file, start = 0, code, messageText }) => {
|
|
37
|
+
const message = typeof messageText === "string" ? messageText : messageText.messageText;
|
|
38
|
+
if (file === undefined) return message;
|
|
39
|
+
const line = file.text.slice(0, start).split("\n").length - 1;
|
|
40
|
+
return {
|
|
41
|
+
file: relative(root, file.fileName),
|
|
42
|
+
code: codeOf(code),
|
|
43
|
+
source: file.text.split("\n")[line] ?? "",
|
|
44
|
+
message: message.split("\n")[0] ?? "",
|
|
45
|
+
};
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The arguments that are options of the compiler: not the tsconfig, nor --watch, which are the helper's own. */
|
|
51
|
+
function compilerArgs(args: string[]): string[] {
|
|
52
|
+
const own = (arg: string | undefined) => arg === "-p" || arg === "--project";
|
|
53
|
+
return args.filter((arg, at) => arg !== "--watch" && !own(arg) && !own(args[at - 1]));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The options forced on top of the project's: strict templates, and each option of TypeScript the gate gives, as tsc
|
|
58
|
+
* reads them. One TypeScript does not know is refused, since a misspelt option would otherwise check nothing more and
|
|
59
|
+
* leave the gate green.
|
|
60
|
+
*/
|
|
61
|
+
function forced(ts: typeof Ts, args: string[]): { options: Ts.CompilerOptions } | { refused: string } {
|
|
62
|
+
const { options, errors, fileNames } = ts.parseCommandLine(compilerArgs(args));
|
|
63
|
+
const [error] = errors;
|
|
64
|
+
if (error !== undefined) return { refused: ts.flattenDiagnosticMessageText(error.messageText, "\n") };
|
|
65
|
+
const [stray] = fileNames;
|
|
66
|
+
if (stray !== undefined) return { refused: `${stray} is not an option of the compiler.` };
|
|
67
|
+
return { options: { strictTemplates: true, ...options } };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The Angular compiler of the repo, with the options of the gate. */
|
|
71
|
+
interface Compiler {
|
|
72
|
+
/** What it says of the project as it is now. Each call reuses what the one before it compiled. */
|
|
73
|
+
diagnostics(): Promise<readonly Diagnostic[]>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function changedAt(file: string): number {
|
|
77
|
+
return statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? 0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The Angular compiler of the repo, once it is known to check files with the API this helper calls: otherwise, why
|
|
82
|
+
* not. That API is not a public one of Angular, so a version that changed it refuses the gate, never passes it green.
|
|
83
|
+
*/
|
|
84
|
+
export async function compilerOf(root: string, args: string[]): Promise<{ compiler: Compiler } | { refused: string }> {
|
|
85
|
+
const nothing = nothingChecked(root, args);
|
|
86
|
+
if (nothing) return { refused: nothing };
|
|
87
|
+
const required = createRequire(join(root, "package.json"));
|
|
88
|
+
const given = forced(required("typescript") as typeof Ts, args);
|
|
89
|
+
if ("refused" in given) return given;
|
|
90
|
+
const ng = await import(pathToFileURL(required.resolve("@angular/compiler-cli")).href);
|
|
91
|
+
const { version } = required("@angular/compiler-cli/package.json") as { version: string };
|
|
92
|
+
const api = ng.NgtscProgram?.prototype;
|
|
93
|
+
const missing = (
|
|
94
|
+
[
|
|
95
|
+
["NgtscProgram", ng.NgtscProgram],
|
|
96
|
+
["NgtscProgram.loadNgStructureAsync", api?.loadNgStructureAsync],
|
|
97
|
+
["NgtscProgram.getTsSyntacticDiagnostics", api?.getTsSyntacticDiagnostics],
|
|
98
|
+
["NgtscProgram.getTsSemanticDiagnostics", api?.getTsSemanticDiagnostics],
|
|
99
|
+
["NgtscProgram.getNgSemanticDiagnostics", api?.getNgSemanticDiagnostics],
|
|
100
|
+
["readConfiguration", ng.readConfiguration],
|
|
101
|
+
["createCompilerHost", ng.createCompilerHost],
|
|
102
|
+
] as const
|
|
103
|
+
).find(([, found]) => typeof found !== "function")?.[0];
|
|
104
|
+
if (missing !== undefined)
|
|
105
|
+
return {
|
|
106
|
+
refused: `The API of @angular/compiler-cli ${version} is not the one this gate knows: it has no ${missing}. Update deterministic-gates.`,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// What the check before this one read, by the date it had: a file whose date is the same is given as it was, which
|
|
110
|
+
// is how the compiler knows it need not check it again.
|
|
111
|
+
const sources = new Map<string, { at: number; source: unknown }>();
|
|
112
|
+
const resources = new Map<string, number>();
|
|
113
|
+
let last: { program: unknown; options: string } | undefined;
|
|
114
|
+
return {
|
|
115
|
+
compiler: {
|
|
116
|
+
async diagnostics() {
|
|
117
|
+
const config = ng.readConfiguration(configOf(root, args));
|
|
118
|
+
const options = { ...config.options, ...given.options };
|
|
119
|
+
// A program checked with other options is no help to this one.
|
|
120
|
+
if (last?.options !== JSON.stringify(options)) {
|
|
121
|
+
last = undefined;
|
|
122
|
+
sources.clear();
|
|
123
|
+
resources.clear();
|
|
124
|
+
}
|
|
125
|
+
const base = ng.createCompilerHost({ options });
|
|
126
|
+
const host = {
|
|
127
|
+
...base,
|
|
128
|
+
getSourceFile(file: string, ...rest: unknown[]) {
|
|
129
|
+
const at = changedAt(file);
|
|
130
|
+
const kept = sources.get(file);
|
|
131
|
+
if (kept?.at === at) return kept.source;
|
|
132
|
+
const source: unknown = base.getSourceFile(file, ...rest);
|
|
133
|
+
if (source === undefined) sources.delete(file);
|
|
134
|
+
else sources.set(file, { at, source });
|
|
135
|
+
return source;
|
|
136
|
+
},
|
|
137
|
+
// A template or a style, which the compiler reads as text.
|
|
138
|
+
readFile(file: string) {
|
|
139
|
+
if (!/\.(ts|json)$/.test(file) && !resources.has(file)) resources.set(file, changedAt(file));
|
|
140
|
+
return base.readFile(file);
|
|
141
|
+
},
|
|
142
|
+
getModifiedResourceFiles() {
|
|
143
|
+
const modified = new Set<string>();
|
|
144
|
+
for (const [file, at] of resources) {
|
|
145
|
+
const now = changedAt(file);
|
|
146
|
+
if (now === at) continue;
|
|
147
|
+
modified.add(file);
|
|
148
|
+
resources.set(file, now);
|
|
149
|
+
}
|
|
150
|
+
return modified;
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
const program = new ng.NgtscProgram(config.rootNames, options, host, last?.program);
|
|
154
|
+
await program.loadNgStructureAsync();
|
|
155
|
+
last = { program, options: JSON.stringify(options) };
|
|
156
|
+
return [
|
|
157
|
+
...config.errors,
|
|
158
|
+
...program.getTsSyntacticDiagnostics(),
|
|
159
|
+
...program.getTsSemanticDiagnostics(),
|
|
160
|
+
...program.getNgSemanticDiagnostics(),
|
|
161
|
+
];
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Checks now, then each time the date of the tsconfig changes, saying when a check starts and when it is done. */
|
|
168
|
+
async function watch(root: string, args: string[], compiler: Compiler): Promise<void> {
|
|
169
|
+
let asked = false;
|
|
170
|
+
let checking = false;
|
|
171
|
+
const check = async () => {
|
|
172
|
+
asked = true;
|
|
173
|
+
if (checking) return;
|
|
174
|
+
checking = true;
|
|
175
|
+
// Asked again while it checked: the files may have changed since it read them.
|
|
176
|
+
while (asked) {
|
|
177
|
+
asked = false;
|
|
178
|
+
console.log("Type check started");
|
|
179
|
+
console.log([...angularErrors(root, await compiler.diagnostics()), "Type check done"].join("\n"));
|
|
180
|
+
}
|
|
181
|
+
checking = false;
|
|
182
|
+
};
|
|
183
|
+
// Looked at rather than watched: a date read ten times a second misses no change, whatever the file system says.
|
|
184
|
+
watchFile(configOf(root, args), { interval: 100 }, (now, before) => {
|
|
185
|
+
if (now.mtimeMs !== before.mtimeMs) void check();
|
|
186
|
+
});
|
|
187
|
+
await check();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function main(root: string, args: string[]): Promise<void> {
|
|
191
|
+
const found = await compilerOf(root, args);
|
|
192
|
+
if ("refused" in found) {
|
|
193
|
+
console.log(found.refused);
|
|
194
|
+
process.exit(1);
|
|
195
|
+
}
|
|
196
|
+
if (args.includes("--watch")) await watch(root, args, found.compiler);
|
|
197
|
+
else console.log(angularErrors(root, await found.compiler.diagnostics()).join("\n"));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (import.meta.main) await main(process.cwd(), process.argv.slice(2));
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
-
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
-
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
-
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
return path;
|
|
8
|
-
};
|
|
9
|
-
import { createRequire } from "node:module";
|
|
10
|
-
import { join, relative } from "node:path";
|
|
11
|
-
import { pathToFileURL } from "node:url";
|
|
12
|
-
import { configOf, nothingChecked } from "./tsc.js";
|
|
13
|
-
import { strictErrorLines } from "./type-errors.js";
|
|
14
|
-
/** A code as Angular prints it: its own codes are negative, -99 then their number, as -998001 for NG8001. */
|
|
15
|
-
function codeOf(code) {
|
|
16
|
-
return code < 0 ? `NG${String(-code).slice(2)}` : `TS${code}`;
|
|
17
|
-
}
|
|
18
|
-
/** The errors of the templates, each on one line, as the gate compares them. */
|
|
19
|
-
export function templateErrors(root, diagnostics) {
|
|
20
|
-
return strictErrorLines(diagnostics.map(({ file, start = 0, code, messageText }) => {
|
|
21
|
-
const message = typeof messageText === "string" ? messageText : messageText.messageText;
|
|
22
|
-
if (file === undefined)
|
|
23
|
-
return message;
|
|
24
|
-
const line = file.text.slice(0, start).split("\n").length - 1;
|
|
25
|
-
return {
|
|
26
|
-
file: relative(root, file.fileName),
|
|
27
|
-
code: codeOf(code),
|
|
28
|
-
source: file.text.split("\n")[line] ?? "",
|
|
29
|
-
message: message.split("\n")[0] ?? "",
|
|
30
|
-
};
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
/** The options forced on top of the project's: strict templates, and each --option the gate gives. */
|
|
34
|
-
function forced(args) {
|
|
35
|
-
const options = { strictTemplates: true };
|
|
36
|
-
for (const arg of args)
|
|
37
|
-
if (arg.startsWith("--") && arg !== "--project")
|
|
38
|
-
options[arg.slice(2)] = true;
|
|
39
|
-
return options;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* The Angular compiler of the repo, once it is known to check files with the API this helper calls: otherwise, why
|
|
43
|
-
* not. That API is not a public one of Angular, so a version that changed it refuses the gate, never passes it green.
|
|
44
|
-
*/
|
|
45
|
-
export async function compilerOf(root, args) {
|
|
46
|
-
const nothing = nothingChecked(root, args);
|
|
47
|
-
if (nothing)
|
|
48
|
-
return { refused: nothing };
|
|
49
|
-
const required = createRequire(join(root, "package.json"));
|
|
50
|
-
const ng = await import(__rewriteRelativeImportExtension(pathToFileURL(required.resolve("@angular/compiler-cli")).href));
|
|
51
|
-
const { version } = required("@angular/compiler-cli/package.json");
|
|
52
|
-
const program = ng.NgtscProgram?.prototype;
|
|
53
|
-
const missing = [
|
|
54
|
-
["NgtscProgram", ng.NgtscProgram],
|
|
55
|
-
["NgtscProgram.loadNgStructureAsync", program?.loadNgStructureAsync],
|
|
56
|
-
["NgtscProgram.getNgSemanticDiagnostics", program?.getNgSemanticDiagnostics],
|
|
57
|
-
["readConfiguration", ng.readConfiguration],
|
|
58
|
-
["createCompilerHost", ng.createCompilerHost],
|
|
59
|
-
].find(([, found]) => typeof found !== "function")?.[0];
|
|
60
|
-
if (missing !== undefined)
|
|
61
|
-
return {
|
|
62
|
-
refused: `The API of @angular/compiler-cli ${version} is not the one this gate knows: it has no ${missing}. Update deterministic-gates.`,
|
|
63
|
-
};
|
|
64
|
-
return {
|
|
65
|
-
compiler: {
|
|
66
|
-
async diagnostics() {
|
|
67
|
-
const config = ng.readConfiguration(configOf(root, args));
|
|
68
|
-
const options = { ...config.options, ...forced(args) };
|
|
69
|
-
const program = new ng.NgtscProgram(config.rootNames, options, ng.createCompilerHost({ options }));
|
|
70
|
-
await program.loadNgStructureAsync();
|
|
71
|
-
return program.getNgSemanticDiagnostics();
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
async function main(root, args) {
|
|
77
|
-
const found = await compilerOf(root, args);
|
|
78
|
-
if ("refused" in found) {
|
|
79
|
-
console.log(found.refused);
|
|
80
|
-
process.exit(1);
|
|
81
|
-
}
|
|
82
|
-
console.log(templateErrors(root, await found.compiler.diagnostics()).join("\n"));
|
|
83
|
-
}
|
|
84
|
-
if (import.meta.main)
|
|
85
|
-
await main(process.cwd(), process.argv.slice(2));
|
|
@@ -1,101 +0,0 @@
|
|
|
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));
|