@venizia/dev-configs 0.5.0-12 → 0.5.0-13
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/README.md +1 -1
- package/dist/cli/codemod/binding-keys.js +2 -0
- package/dist/cli/codemod/binding-keys.js.map +1 -1
- package/dist/cli/codemod/public-names.d.ts +1 -1
- package/dist/cli/codemod/public-names.js +6 -0
- package/dist/cli/codemod/public-names.js.map +1 -1
- package/dist/cli/codemod/typed-route-context-satisfies.d.ts +85 -0
- package/dist/cli/codemod/typed-route-context-satisfies.js +910 -0
- package/dist/cli/codemod/typed-route-context-satisfies.js.map +1 -0
- package/dist/cli/codemod/typed-route-context.d.ts +17 -2
- package/dist/cli/codemod/typed-route-context.js +148 -37
- package/dist/cli/codemod/typed-route-context.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,910 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SatisfiesRewrite = exports.TextEdits = void 0;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
/** The type whose annotation loses the schemas of a route config. */
|
|
6
|
+
const CONFIG_TYPE = 'IAuthRouteConfig';
|
|
7
|
+
const RULE = 'typed-route-context';
|
|
8
|
+
/** How many times the projects are typechecked with candidates written, at most. */
|
|
9
|
+
const ROUND_LIMIT = 40;
|
|
10
|
+
/** Why a config declared wide is left as it is. */
|
|
11
|
+
class LeftReasons {
|
|
12
|
+
static { this.LET = 'declared with let'; }
|
|
13
|
+
static { this.VAR = 'declared with var'; }
|
|
14
|
+
static { this.NO_INITIALIZER = 'no initializer'; }
|
|
15
|
+
static { this.NOT_LITERAL = 'its value is not an object literal'; }
|
|
16
|
+
static { this.OTHER_ASSERTION = 'its value is asserted to another type'; }
|
|
17
|
+
static { this.PATTERN = 'a destructuring pattern'; }
|
|
18
|
+
static { this.OPTIONAL = 'an optional or definite property'; }
|
|
19
|
+
static { this.MUTABLE_PROPERTY = 'a property that is not readonly'; }
|
|
20
|
+
static { this.DISABLED = 'the lint rule is turned off here'; }
|
|
21
|
+
static { this.IMPORT_TYPE = 'typed through import()'; }
|
|
22
|
+
static { this.OPEN_KEY = 'indexed with a key that is not a literal'; }
|
|
23
|
+
static { this.ABSENT_KEY = 'indexed with a key the object does not hold'; }
|
|
24
|
+
}
|
|
25
|
+
/** The calls that write to their first argument. */
|
|
26
|
+
const WRITING_CALLS = new Set([
|
|
27
|
+
'Object.assign',
|
|
28
|
+
'Object.defineProperty',
|
|
29
|
+
'Object.defineProperties',
|
|
30
|
+
'Reflect.set',
|
|
31
|
+
'Reflect.defineProperty',
|
|
32
|
+
'Reflect.deleteProperty',
|
|
33
|
+
]);
|
|
34
|
+
/** Applies edits to a text, and moves positions between the text before and after them. */
|
|
35
|
+
class TextEdits {
|
|
36
|
+
/** The text of each file once the candidates are written, and the edits that make it. */
|
|
37
|
+
static writeAll(opts) {
|
|
38
|
+
const edits = new Map();
|
|
39
|
+
for (const { file, edits: own } of opts.candidates) {
|
|
40
|
+
edits.set(file, [...(edits.get(file) ?? []), ...own]);
|
|
41
|
+
}
|
|
42
|
+
const contents = new Map([...edits].map(([file, own]) => [
|
|
43
|
+
file,
|
|
44
|
+
TextEdits.apply({ source: opts.sources.get(file) ?? '', edits: own }),
|
|
45
|
+
]));
|
|
46
|
+
return { edits, contents };
|
|
47
|
+
}
|
|
48
|
+
static apply(opts) {
|
|
49
|
+
let output = opts.source;
|
|
50
|
+
for (const edit of [...opts.edits].sort((a, b) => b.start - a.start || b.end - a.end)) {
|
|
51
|
+
output = output.slice(0, edit.start) + edit.text + output.slice(edit.end);
|
|
52
|
+
}
|
|
53
|
+
return output;
|
|
54
|
+
}
|
|
55
|
+
static toSource(opts) {
|
|
56
|
+
let moved = 0;
|
|
57
|
+
for (const edit of [...opts.edits].sort((a, b) => a.start - b.start || a.end - b.end)) {
|
|
58
|
+
const start = edit.start + moved;
|
|
59
|
+
if (opts.position < start) {
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (opts.position < start + edit.text.length) {
|
|
63
|
+
return edit.start;
|
|
64
|
+
}
|
|
65
|
+
moved += edit.text.length - (edit.end - edit.start);
|
|
66
|
+
}
|
|
67
|
+
return opts.position - moved;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.TextEdits = TextEdits;
|
|
71
|
+
/** The configs one file declares wide, in the forms `@venizia/typed-route-context` reports. */
|
|
72
|
+
class ConfigDeclarations {
|
|
73
|
+
constructor(options) {
|
|
74
|
+
this.options = options;
|
|
75
|
+
/** The type aliases of this file that stand for a wide config type. */
|
|
76
|
+
this.aliases = new Map();
|
|
77
|
+
/** The assertions a declaration's own annotation already covers. */
|
|
78
|
+
this.covered = new Set();
|
|
79
|
+
this.result = { candidates: [], left: [] };
|
|
80
|
+
const { typescript: ts, fileName, source } = options;
|
|
81
|
+
const scriptKind = fileName.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
82
|
+
this.file = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, scriptKind);
|
|
83
|
+
}
|
|
84
|
+
scan() {
|
|
85
|
+
const { typescript: ts } = this.options;
|
|
86
|
+
for (const statement of this.file.statements) {
|
|
87
|
+
if (ts.isTypeAliasDeclaration(statement) && !statement.typeParameters) {
|
|
88
|
+
this.aliases.set(statement.name.text, statement.type);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const visit = (node) => {
|
|
92
|
+
if (ts.isVariableDeclaration(node) || ts.isPropertyDeclaration(node)) {
|
|
93
|
+
this.readDeclaration(node);
|
|
94
|
+
}
|
|
95
|
+
if (ts.isAsExpression(node) && !this.covered.has(node)) {
|
|
96
|
+
this.readAssertion(node);
|
|
97
|
+
}
|
|
98
|
+
ts.forEachChild(node, visit);
|
|
99
|
+
};
|
|
100
|
+
visit(this.file);
|
|
101
|
+
return this.result;
|
|
102
|
+
}
|
|
103
|
+
/** Whether a type is `IAuthRouteConfig`, alone, in an `Omit` or `Readonly`, or as the value of a `Record`. */
|
|
104
|
+
isWide(type, seen = new Set()) {
|
|
105
|
+
const { typescript: ts } = this.options;
|
|
106
|
+
if (type && ts.isImportTypeNode(type)) {
|
|
107
|
+
return type.qualifier?.getText(this.file) === CONFIG_TYPE;
|
|
108
|
+
}
|
|
109
|
+
if (!type || !ts.isTypeReferenceNode(type)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const name = type.typeName.getText(this.file);
|
|
113
|
+
const [first, second] = type.typeArguments ?? [];
|
|
114
|
+
if (name === 'Record') {
|
|
115
|
+
return this.isWide(second, seen);
|
|
116
|
+
}
|
|
117
|
+
if (name === 'Omit' || name === 'Readonly') {
|
|
118
|
+
return this.isWide(first, seen);
|
|
119
|
+
}
|
|
120
|
+
const alias = this.aliases.get(name);
|
|
121
|
+
if (alias && !seen.has(name)) {
|
|
122
|
+
return this.isWide(alias, seen.add(name));
|
|
123
|
+
}
|
|
124
|
+
return name === CONFIG_TYPE;
|
|
125
|
+
}
|
|
126
|
+
hasImportType(node) {
|
|
127
|
+
return (this.options.typescript.isImportTypeNode(node) ||
|
|
128
|
+
this.options.typescript.forEachChild(node, (child) => this.hasImportType(child) ? true : undefined) === true);
|
|
129
|
+
}
|
|
130
|
+
isRecord(type, seen = new Set()) {
|
|
131
|
+
const { typescript: ts } = this.options;
|
|
132
|
+
if (!ts.isTypeReferenceNode(type)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const name = type.typeName.getText(this.file);
|
|
136
|
+
const [first] = type.typeArguments ?? [];
|
|
137
|
+
if (name === 'Readonly' && first) {
|
|
138
|
+
return this.isRecord(first, seen);
|
|
139
|
+
}
|
|
140
|
+
const alias = this.aliases.get(name);
|
|
141
|
+
if (alias && !seen.has(name)) {
|
|
142
|
+
return this.isRecord(alias, seen.add(name));
|
|
143
|
+
}
|
|
144
|
+
return name === 'Record';
|
|
145
|
+
}
|
|
146
|
+
unwrapped(expression) {
|
|
147
|
+
return this.options.typescript.isParenthesizedExpression(expression)
|
|
148
|
+
? this.unwrapped(expression.expression)
|
|
149
|
+
: expression;
|
|
150
|
+
}
|
|
151
|
+
/** An empty object is a collection filled later, which the lint rule does not report. */
|
|
152
|
+
isFilledObject(expression) {
|
|
153
|
+
const value = this.unwrapped(expression);
|
|
154
|
+
return this.options.typescript.isObjectLiteralExpression(value) && value.properties.length > 0;
|
|
155
|
+
}
|
|
156
|
+
isConstAssertion(type) {
|
|
157
|
+
return type.getText(this.file) === 'const';
|
|
158
|
+
}
|
|
159
|
+
readDeclaration(declaration) {
|
|
160
|
+
const { typescript: ts } = this.options;
|
|
161
|
+
const { type, initializer } = declaration;
|
|
162
|
+
if (!type || !this.isWide(type)) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const value = initializer ? this.unwrapped(initializer) : undefined;
|
|
166
|
+
const asserted = value && ts.isAsExpression(value) ? value : undefined;
|
|
167
|
+
if (asserted) {
|
|
168
|
+
this.covered.add(asserted);
|
|
169
|
+
}
|
|
170
|
+
const literal = asserted ? asserted.expression : value;
|
|
171
|
+
// `for (const x of list)` takes its value from the list, and an empty object is filled later.
|
|
172
|
+
const isEmpty = literal !== undefined &&
|
|
173
|
+
ts.isObjectLiteralExpression(this.unwrapped(literal)) &&
|
|
174
|
+
!this.isFilledObject(literal);
|
|
175
|
+
if (isEmpty || ts.isForOfStatement(declaration.parent.parent)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const name = declaration.name.getText(this.file);
|
|
179
|
+
const refusal = this.hasImportType(type)
|
|
180
|
+
? LeftReasons.IMPORT_TYPE
|
|
181
|
+
: this.refusalOf(declaration);
|
|
182
|
+
if (refusal) {
|
|
183
|
+
this.leave({ node: declaration, name, reason: refusal });
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (!initializer || !literal || !this.isFilledObject(literal)) {
|
|
187
|
+
this.leave({ node: declaration, name, reason: LeftReasons.NOT_LITERAL });
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const typeText = type.getText(this.file);
|
|
191
|
+
const edits = this.removeAnnotation({ declaration, type });
|
|
192
|
+
if (!asserted || this.isConstAssertion(asserted.type)) {
|
|
193
|
+
const end = initializer.getEnd();
|
|
194
|
+
edits.push({ start: end, end, text: ` satisfies ${typeText}` });
|
|
195
|
+
}
|
|
196
|
+
else if (asserted.type.getText(this.file) === typeText) {
|
|
197
|
+
edits.push(this.assertionToSatisfies(asserted));
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
this.leave({ node: declaration, name, reason: LeftReasons.OTHER_ASSERTION });
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this.add({ node: declaration, declaration, name, type, literal, edits });
|
|
204
|
+
}
|
|
205
|
+
isDeclaration(node) {
|
|
206
|
+
const { typescript: ts } = this.options;
|
|
207
|
+
return ts.isVariableDeclaration(node) || ts.isPropertyDeclaration(node);
|
|
208
|
+
}
|
|
209
|
+
readAssertion(assertion) {
|
|
210
|
+
const { typescript: ts } = this.options;
|
|
211
|
+
if (!this.isWide(assertion.type) || !this.isFilledObject(assertion.expression)) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
let node = assertion.parent;
|
|
215
|
+
// The declaration the assertion is part of the value of; one inside a function belongs to no outer declaration.
|
|
216
|
+
while (!ts.isSourceFile(node) && !ts.isFunctionLike(node) && !this.isDeclaration(node)) {
|
|
217
|
+
node = node.parent;
|
|
218
|
+
}
|
|
219
|
+
const current = this.isDeclaration(node) ? node : undefined;
|
|
220
|
+
const name = current ? current.name.getText(this.file) : 'an assertion';
|
|
221
|
+
const refusal = this.hasImportType(assertion.type)
|
|
222
|
+
? LeftReasons.IMPORT_TYPE
|
|
223
|
+
: current
|
|
224
|
+
? this.refusalOf(current)
|
|
225
|
+
: this.disabledAt(assertion);
|
|
226
|
+
if (refusal) {
|
|
227
|
+
this.leave({ node: assertion, name, reason: refusal });
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
// Only a whole value is the `Record` itself; a member's assertion types that member.
|
|
231
|
+
const isWhole = current?.initializer === assertion;
|
|
232
|
+
this.add({
|
|
233
|
+
node: current ?? assertion,
|
|
234
|
+
declaration: current,
|
|
235
|
+
name,
|
|
236
|
+
type: isWhole ? assertion.type : undefined,
|
|
237
|
+
literal: assertion.expression,
|
|
238
|
+
edits: [this.assertionToSatisfies(assertion)],
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/** What about the declaration itself forbids the rewrite. */
|
|
242
|
+
refusalOf(declaration) {
|
|
243
|
+
const { typescript: ts } = this.options;
|
|
244
|
+
const disabled = this.disabledAt(declaration);
|
|
245
|
+
if (disabled) {
|
|
246
|
+
return disabled;
|
|
247
|
+
}
|
|
248
|
+
if (ts.isPropertyDeclaration(declaration)) {
|
|
249
|
+
const isReadonly = declaration.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword);
|
|
250
|
+
if (!declaration.initializer) {
|
|
251
|
+
return LeftReasons.NO_INITIALIZER;
|
|
252
|
+
}
|
|
253
|
+
if (declaration.questionToken || declaration.exclamationToken) {
|
|
254
|
+
return LeftReasons.OPTIONAL;
|
|
255
|
+
}
|
|
256
|
+
return isReadonly ? undefined : LeftReasons.MUTABLE_PROPERTY;
|
|
257
|
+
}
|
|
258
|
+
const flags = ts.getCombinedNodeFlags(declaration);
|
|
259
|
+
if (!declaration.initializer) {
|
|
260
|
+
return LeftReasons.NO_INITIALIZER;
|
|
261
|
+
}
|
|
262
|
+
if (!(flags & ts.NodeFlags.Const)) {
|
|
263
|
+
return flags & ts.NodeFlags.Let ? LeftReasons.LET : LeftReasons.VAR;
|
|
264
|
+
}
|
|
265
|
+
return ts.isIdentifier(declaration.name) ? undefined : LeftReasons.PATTERN;
|
|
266
|
+
}
|
|
267
|
+
/** Whether an `eslint-disable` comment turns the rule off for this node. */
|
|
268
|
+
disabledAt(node) {
|
|
269
|
+
const lines = this.options.source.split('\n');
|
|
270
|
+
const line = this.lineOf(node) - 1;
|
|
271
|
+
const near = [lines[line - 1] ?? '', lines[line] ?? ''].join('\n');
|
|
272
|
+
const isNear = /eslint-disable(?:-next-line|-line)?\s/.test(near) && near.includes(RULE);
|
|
273
|
+
const isWholeFile = new RegExp(`/\\*\\s*eslint-disable\\s[^*]*${RULE}`).test(this.options.source);
|
|
274
|
+
return isNear || isWholeFile ? LeftReasons.DISABLED : undefined;
|
|
275
|
+
}
|
|
276
|
+
/** Removes `: Type`; a comment between the colon and the type stays. */
|
|
277
|
+
removeAnnotation(opts) {
|
|
278
|
+
const { declaration, type } = opts;
|
|
279
|
+
const { source } = this.options;
|
|
280
|
+
const colon = declaration
|
|
281
|
+
.getChildren(this.file)
|
|
282
|
+
.find((child) => child.kind === this.options.typescript.SyntaxKind.ColonToken);
|
|
283
|
+
const start = colon?.getStart(this.file) ?? type.getStart(this.file);
|
|
284
|
+
const typeStart = type.getStart(this.file);
|
|
285
|
+
const end = type.getEnd();
|
|
286
|
+
if (source.slice(start + 1, typeStart).trim() === '') {
|
|
287
|
+
return [{ start, end, text: '' }];
|
|
288
|
+
}
|
|
289
|
+
// The space before the type goes with it, so no double space stays before `=`.
|
|
290
|
+
const blank = source.slice(0, typeStart).trimEnd().length;
|
|
291
|
+
return [
|
|
292
|
+
{ start, end: start + 1, text: '' },
|
|
293
|
+
{ start: blank, end, text: '' },
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
assertionToSatisfies(assertion) {
|
|
297
|
+
const { typescript: ts, source } = this.options;
|
|
298
|
+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, true);
|
|
299
|
+
scanner.setText(source, assertion.expression.getEnd());
|
|
300
|
+
scanner.scan();
|
|
301
|
+
const start = scanner.getTokenStart();
|
|
302
|
+
return { start, end: scanner.getTokenEnd(), text: 'satisfies' };
|
|
303
|
+
}
|
|
304
|
+
add(opts) {
|
|
305
|
+
const { typescript: ts } = this.options;
|
|
306
|
+
const { node, declaration, name, type, literal, edits } = opts;
|
|
307
|
+
const value = this.unwrapped(literal);
|
|
308
|
+
const keys = type && this.isRecord(type) && ts.isObjectLiteralExpression(value)
|
|
309
|
+
? value.properties.flatMap((property) => {
|
|
310
|
+
const { name: written } = property;
|
|
311
|
+
// `['/a']: ...` writes the same key as `'/a': ...`.
|
|
312
|
+
const key = written && ts.isComputedPropertyName(written) ? written.expression : written;
|
|
313
|
+
// A bare identifier in brackets names a variable, not the key.
|
|
314
|
+
if (key && ts.isIdentifier(key)) {
|
|
315
|
+
return key === written ? [key.text] : [];
|
|
316
|
+
}
|
|
317
|
+
return key && (ts.isStringLiteralLike(key) || ts.isNumericLiteral(key))
|
|
318
|
+
? [key.text]
|
|
319
|
+
: [];
|
|
320
|
+
})
|
|
321
|
+
: undefined;
|
|
322
|
+
this.result.candidates.push({
|
|
323
|
+
file: this.options.fileName,
|
|
324
|
+
line: this.lineOf(node),
|
|
325
|
+
name,
|
|
326
|
+
start: node.getStart(this.file),
|
|
327
|
+
end: node.getEnd(),
|
|
328
|
+
binding: declaration?.name.getStart(this.file),
|
|
329
|
+
isExported: this.isExported(node),
|
|
330
|
+
recordKeys: keys ? new Set(keys) : undefined,
|
|
331
|
+
edits,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/** Exported where it stands, or named by an `export` of its file; nothing declared in a function is. */
|
|
335
|
+
isExported(node) {
|
|
336
|
+
const { typescript: ts } = this.options;
|
|
337
|
+
let statement = node;
|
|
338
|
+
while (!ts.isSourceFile(statement.parent)) {
|
|
339
|
+
statement = statement.parent;
|
|
340
|
+
if (ts.isFunctionLike(statement)) {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const isModified = ts.canHaveModifiers(statement) &&
|
|
345
|
+
ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
346
|
+
if (isModified || ts.isExportAssignment(statement)) {
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
const names = ts.isVariableStatement(statement)
|
|
350
|
+
? statement.declarationList.declarations.map((item) => item.name.getText(this.file))
|
|
351
|
+
: ts.isClassDeclaration(statement) && statement.name
|
|
352
|
+
? [statement.name.text]
|
|
353
|
+
: [];
|
|
354
|
+
return this.file.statements.some((item) => {
|
|
355
|
+
if (ts.isExportAssignment(item)) {
|
|
356
|
+
return names.includes(item.expression.getText(this.file));
|
|
357
|
+
}
|
|
358
|
+
return (ts.isExportDeclaration(item) &&
|
|
359
|
+
!item.moduleSpecifier &&
|
|
360
|
+
item.exportClause !== undefined &&
|
|
361
|
+
ts.isNamedExports(item.exportClause) &&
|
|
362
|
+
item.exportClause.elements.some((element) => names.includes((element.propertyName ?? element.name).text)));
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
leave(opts) {
|
|
366
|
+
const { node, name, reason } = opts;
|
|
367
|
+
this.result.left.push({ file: this.options.fileName, line: this.lineOf(node), name, reason });
|
|
368
|
+
}
|
|
369
|
+
lineOf(node) {
|
|
370
|
+
return this.file.getLineAndCharacterOfPosition(node.getStart(this.file)).line + 1;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Which of the candidates can be written: those whose binding is never written to and, if a `Record`, never indexed
|
|
375
|
+
* by a key the literal may not hold, and whose rewrite adds no type error to any project of the run.
|
|
376
|
+
*/
|
|
377
|
+
class SatisfiesProof {
|
|
378
|
+
constructor(options) {
|
|
379
|
+
this.options = options;
|
|
380
|
+
this.left = new Map();
|
|
381
|
+
this.untried = [];
|
|
382
|
+
/** How many times the projects were typechecked with candidates written. */
|
|
383
|
+
this.rounds = 0;
|
|
384
|
+
this.accepted = new Set(options.candidates);
|
|
385
|
+
}
|
|
386
|
+
run() {
|
|
387
|
+
if (this.accepted.size === 0) {
|
|
388
|
+
return { accepted: [], left: [], untried: [] };
|
|
389
|
+
}
|
|
390
|
+
const programs = this.holding(this.options.programs(new Map()));
|
|
391
|
+
this.dropWritten(programs);
|
|
392
|
+
if (this.accepted.size > 0) {
|
|
393
|
+
this.dropBreaking(new Set(this.errorsOf(programs, new Map()).map((error) => error.key)));
|
|
394
|
+
}
|
|
395
|
+
return { accepted: [...this.accepted], left: [...this.left.values()], untried: this.untried };
|
|
396
|
+
}
|
|
397
|
+
/** The programs that read a file with a candidate: no other can be broken by a rewrite. */
|
|
398
|
+
holding(programs) {
|
|
399
|
+
const files = this.options.candidates.map((candidate) => candidate.file);
|
|
400
|
+
return programs.filter((program) => files.some((file) => program.getSourceFile(file)));
|
|
401
|
+
}
|
|
402
|
+
reject(opts) {
|
|
403
|
+
const { candidate, reason } = opts;
|
|
404
|
+
if (this.accepted.delete(candidate)) {
|
|
405
|
+
const { file, line, name } = candidate;
|
|
406
|
+
this.left.set(candidate, { file, line, name, reason });
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/** Takes a rejection back, so that the candidate is proved again. */
|
|
410
|
+
restore(candidate) {
|
|
411
|
+
this.left.delete(candidate);
|
|
412
|
+
this.accepted.add(candidate);
|
|
413
|
+
}
|
|
414
|
+
shown(file) {
|
|
415
|
+
return (0, node_path_1.relative)(this.options.root, file).split('\\').join('/');
|
|
416
|
+
}
|
|
417
|
+
ownFiles(program) {
|
|
418
|
+
return program
|
|
419
|
+
.getSourceFiles()
|
|
420
|
+
.filter((file) => !file.isDeclarationFile && !file.fileName.includes('/node_modules/'));
|
|
421
|
+
}
|
|
422
|
+
/** The candidates by their binding: the file and the position of its name in the source. */
|
|
423
|
+
bindings() {
|
|
424
|
+
const found = new Map();
|
|
425
|
+
for (const candidate of this.accepted) {
|
|
426
|
+
if (candidate.binding !== undefined) {
|
|
427
|
+
const key = `${candidate.file}:${candidate.binding}`;
|
|
428
|
+
found.set(key, [...(found.get(key) ?? []), candidate]);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return found;
|
|
432
|
+
}
|
|
433
|
+
/** What an identifier stands for, through an import and a shorthand property. */
|
|
434
|
+
symbolOf(opts) {
|
|
435
|
+
const { typescript: ts } = this.options;
|
|
436
|
+
const { checker, name } = opts;
|
|
437
|
+
const { parent } = name;
|
|
438
|
+
const symbol = ts.isShorthandPropertyAssignment(parent) && parent.name === name
|
|
439
|
+
? checker.getShorthandAssignmentValueSymbol(parent)
|
|
440
|
+
: checker.getSymbolAtLocation(name);
|
|
441
|
+
return symbol && symbol.flags & ts.SymbolFlags.Alias
|
|
442
|
+
? checker.getAliasedSymbol(symbol)
|
|
443
|
+
: symbol;
|
|
444
|
+
}
|
|
445
|
+
/** The declaration of the value a member of an object literal holds, through any number of such members. */
|
|
446
|
+
heldBy(opts) {
|
|
447
|
+
const { typescript: ts } = this.options;
|
|
448
|
+
const { checker } = opts;
|
|
449
|
+
const seen = new Set();
|
|
450
|
+
let { declaration } = opts;
|
|
451
|
+
while (declaration && !seen.has(declaration)) {
|
|
452
|
+
seen.add(declaration);
|
|
453
|
+
let held;
|
|
454
|
+
if (ts.isShorthandPropertyAssignment(declaration)) {
|
|
455
|
+
held = checker.getShorthandAssignmentValueSymbol(declaration);
|
|
456
|
+
}
|
|
457
|
+
else if (ts.isPropertyAssignment(declaration) && ts.isIdentifier(declaration.initializer)) {
|
|
458
|
+
held = checker.getSymbolAtLocation(declaration.initializer);
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
return declaration;
|
|
462
|
+
}
|
|
463
|
+
declaration =
|
|
464
|
+
held && held.flags & ts.SymbolFlags.Alias
|
|
465
|
+
? checker.getAliasedSymbol(held).valueDeclaration
|
|
466
|
+
: held?.valueDeclaration;
|
|
467
|
+
}
|
|
468
|
+
return declaration;
|
|
469
|
+
}
|
|
470
|
+
/** Calls `found` for each identifier under `node` that reads the binding of a candidate. */
|
|
471
|
+
forEachReference(opts) {
|
|
472
|
+
const { typescript: ts } = this.options;
|
|
473
|
+
const { program, node, edits, bindings, isThroughMembers = false, found } = opts;
|
|
474
|
+
const checker = program.getTypeChecker();
|
|
475
|
+
const names = new Set([...bindings.values()].flat().map((candidate) => candidate.name));
|
|
476
|
+
const visit = (current) => {
|
|
477
|
+
ts.forEachChild(current, visit);
|
|
478
|
+
if (!ts.isIdentifier(current)) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const read = this.symbolOf({ checker, name: current })?.valueDeclaration;
|
|
482
|
+
const declaration = isThroughMembers ? this.heldBy({ checker, declaration: read }) : read;
|
|
483
|
+
if (!declaration ||
|
|
484
|
+
!(ts.isVariableDeclaration(declaration) || ts.isPropertyDeclaration(declaration)) ||
|
|
485
|
+
!names.has(declaration.name.getText()) ||
|
|
486
|
+
declaration.name === current) {
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const file = declaration.getSourceFile().fileName;
|
|
490
|
+
const position = TextEdits.toSource({
|
|
491
|
+
edits: edits.get(file) ?? [],
|
|
492
|
+
position: declaration.name.getStart(),
|
|
493
|
+
});
|
|
494
|
+
const candidates = bindings.get(`${file}:${position}`);
|
|
495
|
+
if (candidates) {
|
|
496
|
+
found(current, candidates);
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
visit(node);
|
|
500
|
+
}
|
|
501
|
+
dropWritten(programs) {
|
|
502
|
+
const bindings = this.bindings();
|
|
503
|
+
for (const program of programs) {
|
|
504
|
+
const { noImplicitAny, strict } = program.getCompilerOptions();
|
|
505
|
+
const isImplicitAnyRefused = noImplicitAny ?? strict ?? false;
|
|
506
|
+
for (const file of this.ownFiles(program)) {
|
|
507
|
+
this.forEachReference({
|
|
508
|
+
program,
|
|
509
|
+
node: file,
|
|
510
|
+
edits: new Map(),
|
|
511
|
+
bindings,
|
|
512
|
+
found: (reference, candidates) => {
|
|
513
|
+
const line = file.getLineAndCharacterOfPosition(reference.getStart()).line + 1;
|
|
514
|
+
const where = `${this.shown(file.fileName)}:${line}`;
|
|
515
|
+
for (const candidate of candidates) {
|
|
516
|
+
const { isWritten, openIndex } = this.useOf({
|
|
517
|
+
program,
|
|
518
|
+
reference,
|
|
519
|
+
keys: candidate.recordKeys,
|
|
520
|
+
isImplicitAnyRefused,
|
|
521
|
+
});
|
|
522
|
+
if (isWritten) {
|
|
523
|
+
this.reject({ candidate, reason: `written at ${where}` });
|
|
524
|
+
}
|
|
525
|
+
if (openIndex) {
|
|
526
|
+
this.reject({ candidate, reason: `${openIndex} at ${where}` });
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* How one reference uses its binding. An index is open when its key is not a literal; without `noImplicitAny` also
|
|
536
|
+
* when the literal does not hold it, because the read then turns into `any` with no error.
|
|
537
|
+
*/
|
|
538
|
+
useOf(opts) {
|
|
539
|
+
const { typescript: ts } = this.options;
|
|
540
|
+
const { program, reference, keys, isImplicitAnyRefused } = opts;
|
|
541
|
+
const { parent } = reference;
|
|
542
|
+
let current = ts.isPropertyAccessExpression(parent) && parent.name === reference ? parent : reference;
|
|
543
|
+
const base = current;
|
|
544
|
+
let openIndex;
|
|
545
|
+
while (true) {
|
|
546
|
+
const next = current.parent;
|
|
547
|
+
if (ts.isElementAccessExpression(next) && next.expression === current) {
|
|
548
|
+
if (keys && this.unwrappedUse(current) === base) {
|
|
549
|
+
const type = program.getTypeChecker().getTypeAtLocation(next.argumentExpression);
|
|
550
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
551
|
+
const literals = parts.flatMap((part) => part.isStringLiteral() || part.isNumberLiteral() ? [String(part.value)] : []);
|
|
552
|
+
if (literals.length !== parts.length) {
|
|
553
|
+
openIndex = LeftReasons.OPEN_KEY;
|
|
554
|
+
}
|
|
555
|
+
else if (!isImplicitAnyRefused && !literals.every((key) => keys.has(key))) {
|
|
556
|
+
openIndex = LeftReasons.ABSENT_KEY;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
current = next;
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
if ((ts.isPropertyAccessExpression(next) ||
|
|
563
|
+
ts.isParenthesizedExpression(next) ||
|
|
564
|
+
ts.isNonNullExpression(next) ||
|
|
565
|
+
ts.isAsExpression(next) ||
|
|
566
|
+
ts.isSatisfiesExpression(next)) &&
|
|
567
|
+
next.expression === current) {
|
|
568
|
+
current = next;
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
return { isWritten: this.isWriteTarget(current), openIndex };
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
unwrappedUse(expression) {
|
|
575
|
+
const { typescript: ts } = this.options;
|
|
576
|
+
return ts.isParenthesizedExpression(expression) || ts.isNonNullExpression(expression)
|
|
577
|
+
? this.unwrappedUse(expression.expression)
|
|
578
|
+
: expression;
|
|
579
|
+
}
|
|
580
|
+
isWriteTarget(node) {
|
|
581
|
+
const { typescript: ts } = this.options;
|
|
582
|
+
const { parent } = node;
|
|
583
|
+
if (ts.isBinaryExpression(parent)) {
|
|
584
|
+
const operator = parent.operatorToken.kind;
|
|
585
|
+
return (parent.left === node &&
|
|
586
|
+
operator >= ts.SyntaxKind.FirstAssignment &&
|
|
587
|
+
operator <= ts.SyntaxKind.LastAssignment);
|
|
588
|
+
}
|
|
589
|
+
if (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) {
|
|
590
|
+
return (parent.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
591
|
+
parent.operator === ts.SyntaxKind.MinusMinusToken);
|
|
592
|
+
}
|
|
593
|
+
if (ts.isDeleteExpression(parent)) {
|
|
594
|
+
return true;
|
|
595
|
+
}
|
|
596
|
+
if (ts.isCallExpression(parent)) {
|
|
597
|
+
return parent.arguments[0] === node && WRITING_CALLS.has(parent.expression.getText());
|
|
598
|
+
}
|
|
599
|
+
if (ts.isForInStatement(parent) || ts.isForOfStatement(parent)) {
|
|
600
|
+
return parent.initializer === node;
|
|
601
|
+
}
|
|
602
|
+
// A destructuring assignment: `[a.x] = list`, `({ y: a.x } = value)`.
|
|
603
|
+
if (ts.isArrayLiteralExpression(parent)) {
|
|
604
|
+
return this.isWriteTarget(parent);
|
|
605
|
+
}
|
|
606
|
+
return (ts.isPropertyAssignment(parent) &&
|
|
607
|
+
parent.initializer === node &&
|
|
608
|
+
this.isWriteTarget(parent.parent));
|
|
609
|
+
}
|
|
610
|
+
errorsOf(programs, edits) {
|
|
611
|
+
const { typescript: ts, sources } = this.options;
|
|
612
|
+
return programs.flatMap((program) => {
|
|
613
|
+
const { declaration } = program.getCompilerOptions();
|
|
614
|
+
return this.ownFiles(program).flatMap((file) => {
|
|
615
|
+
const diagnostics = [
|
|
616
|
+
...program.getSemanticDiagnostics(file),
|
|
617
|
+
...(declaration ? program.getDeclarationDiagnostics(file) : []),
|
|
618
|
+
];
|
|
619
|
+
return diagnostics
|
|
620
|
+
.filter((item) => item.category === ts.DiagnosticCategory.Error)
|
|
621
|
+
.map((item) => {
|
|
622
|
+
const position = item.start ?? 0;
|
|
623
|
+
const own = edits.get(file.fileName) ?? [];
|
|
624
|
+
const before = TextEdits.toSource({ edits: own, position });
|
|
625
|
+
const source = sources.get(file.fileName) ?? file.text;
|
|
626
|
+
const line = source.slice(0, before).split('\n').length;
|
|
627
|
+
return {
|
|
628
|
+
file: file.fileName,
|
|
629
|
+
position,
|
|
630
|
+
line,
|
|
631
|
+
code: item.code,
|
|
632
|
+
place: `${file.fileName}|${before}`,
|
|
633
|
+
key: `${file.fileName}|${before}|${item.code}`,
|
|
634
|
+
};
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* The candidates a new error leads to: those it points into, else those its statement reads, else those read by the
|
|
641
|
+
* variables and types that statement names, one step further at a time.
|
|
642
|
+
*/
|
|
643
|
+
blamed(opts) {
|
|
644
|
+
const { typescript: ts } = this.options;
|
|
645
|
+
const { programs, error, edits } = opts;
|
|
646
|
+
const own = edits.get(error.file) ?? [];
|
|
647
|
+
const position = TextEdits.toSource({ edits: own, position: error.position });
|
|
648
|
+
const around = [...this.accepted].filter((candidate) => candidate.file === error.file && candidate.start <= position && position <= candidate.end);
|
|
649
|
+
if (around.length > 0) {
|
|
650
|
+
return around;
|
|
651
|
+
}
|
|
652
|
+
const program = programs.find((item) => item.getSourceFile(error.file));
|
|
653
|
+
const file = program?.getSourceFile(error.file);
|
|
654
|
+
if (!program || !file) {
|
|
655
|
+
return [];
|
|
656
|
+
}
|
|
657
|
+
let statement = file;
|
|
658
|
+
const descend = (node) => {
|
|
659
|
+
if (node.getStart(file) > error.position || node.getEnd() <= error.position) {
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
if (ts.isStatement(node) || ts.isClassElement(node)) {
|
|
663
|
+
statement = node;
|
|
664
|
+
}
|
|
665
|
+
ts.forEachChild(node, descend);
|
|
666
|
+
};
|
|
667
|
+
ts.forEachChild(file, descend);
|
|
668
|
+
const bindings = this.bindings();
|
|
669
|
+
const seen = new Set();
|
|
670
|
+
let nodes = [statement];
|
|
671
|
+
while (nodes.length > 0) {
|
|
672
|
+
const read = [];
|
|
673
|
+
for (const node of nodes) {
|
|
674
|
+
this.forEachReference({
|
|
675
|
+
program,
|
|
676
|
+
node,
|
|
677
|
+
edits,
|
|
678
|
+
bindings,
|
|
679
|
+
isThroughMembers: true,
|
|
680
|
+
found: (_reference, candidates) => {
|
|
681
|
+
read.push(...candidates);
|
|
682
|
+
},
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
if (read.length > 0) {
|
|
686
|
+
return [...new Set(read)];
|
|
687
|
+
}
|
|
688
|
+
nodes = nodes
|
|
689
|
+
.flatMap((node) => this.namedBy({ program, node }))
|
|
690
|
+
.filter((node) => !seen.has(node) && seen.add(node));
|
|
691
|
+
}
|
|
692
|
+
return [];
|
|
693
|
+
}
|
|
694
|
+
/** The values and types of the variables, properties and type declarations that `node` names. */
|
|
695
|
+
namedBy(opts) {
|
|
696
|
+
const { typescript: ts } = this.options;
|
|
697
|
+
const { program, node } = opts;
|
|
698
|
+
const checker = program.getTypeChecker();
|
|
699
|
+
const own = new Set(this.ownFiles(program));
|
|
700
|
+
const found = [];
|
|
701
|
+
const visit = (current) => {
|
|
702
|
+
ts.forEachChild(current, visit);
|
|
703
|
+
if (!ts.isIdentifier(current)) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
for (const declaration of this.symbolOf({ checker, name: current })?.declarations ?? []) {
|
|
707
|
+
if (!own.has(declaration.getSourceFile())) {
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
if (ts.isVariableDeclaration(declaration) || ts.isPropertyDeclaration(declaration)) {
|
|
711
|
+
const { initializer, type } = declaration;
|
|
712
|
+
found.push(...[initializer, type].filter((part) => part !== undefined));
|
|
713
|
+
}
|
|
714
|
+
if (ts.isTypeAliasDeclaration(declaration) || ts.isInterfaceDeclaration(declaration)) {
|
|
715
|
+
found.push(declaration);
|
|
716
|
+
}
|
|
717
|
+
if (ts.isFunctionDeclaration(declaration)) {
|
|
718
|
+
const { type, body } = declaration;
|
|
719
|
+
found.push(...(type ? [type] : []), ...(body ? this.returnsOf(body) : []));
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
visit(node);
|
|
724
|
+
return found;
|
|
725
|
+
}
|
|
726
|
+
/** What a function body returns; the returns of a function inside it are not its own. */
|
|
727
|
+
returnsOf(node) {
|
|
728
|
+
const { typescript: ts } = this.options;
|
|
729
|
+
const found = [];
|
|
730
|
+
const visit = (current) => {
|
|
731
|
+
if (ts.isReturnStatement(current) && current.expression) {
|
|
732
|
+
found.push(current.expression);
|
|
733
|
+
}
|
|
734
|
+
if (!ts.isFunctionLike(current)) {
|
|
735
|
+
ts.forEachChild(current, visit);
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
visit(node);
|
|
739
|
+
return found;
|
|
740
|
+
}
|
|
741
|
+
/** The files of `errors` and those they import, through any number of `export ... from`. */
|
|
742
|
+
filesAround(opts) {
|
|
743
|
+
const { typescript: ts } = this.options;
|
|
744
|
+
const { programs, errors } = opts;
|
|
745
|
+
const found = new Set();
|
|
746
|
+
for (const { file } of errors) {
|
|
747
|
+
const program = programs.find((item) => item.getSourceFile(file));
|
|
748
|
+
const source = program?.getSourceFile(file);
|
|
749
|
+
if (!program || !source) {
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
const checker = program.getTypeChecker();
|
|
753
|
+
const queue = [source];
|
|
754
|
+
found.add(file);
|
|
755
|
+
for (const current of queue) {
|
|
756
|
+
for (const statement of current.statements) {
|
|
757
|
+
const isRead = ts.isExportDeclaration(statement) ||
|
|
758
|
+
(current === source && ts.isImportDeclaration(statement));
|
|
759
|
+
const specifier = isRead ? statement.moduleSpecifier : undefined;
|
|
760
|
+
const target = specifier && checker.getSymbolAtLocation(specifier)?.valueDeclaration;
|
|
761
|
+
if (target && ts.isSourceFile(target) && !found.has(target.fileName)) {
|
|
762
|
+
found.add(target.fileName);
|
|
763
|
+
queue.push(target);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return found;
|
|
769
|
+
}
|
|
770
|
+
/** Typechecks the projects with `candidates` written, and answers the errors that adds. */
|
|
771
|
+
check(opts) {
|
|
772
|
+
const { candidates, baseline, inDoubt } = opts;
|
|
773
|
+
this.rounds += 1;
|
|
774
|
+
this.options.onRound?.({ round: this.rounds, limit: this.roundLimit, inDoubt });
|
|
775
|
+
const { edits, contents } = TextEdits.writeAll({ sources: this.options.sources, candidates });
|
|
776
|
+
const programs = this.holding(this.options.programs(contents));
|
|
777
|
+
const added = this.errorsOf(programs, edits).filter((error) => !baseline.has(error.key));
|
|
778
|
+
return { edits, programs, added };
|
|
779
|
+
}
|
|
780
|
+
whereOf(error) {
|
|
781
|
+
return `${this.shown(error.file)}:${error.line} TS${error.code}`;
|
|
782
|
+
}
|
|
783
|
+
rejectBroken(opts) {
|
|
784
|
+
const reason = `the typecheck fails with it: ${this.whereOf(opts.error)}`;
|
|
785
|
+
this.reject({ candidate: opts.candidate, reason });
|
|
786
|
+
}
|
|
787
|
+
rejectUntried(candidates) {
|
|
788
|
+
for (const candidate of candidates) {
|
|
789
|
+
if (this.accepted.delete(candidate)) {
|
|
790
|
+
this.untried.push(candidate);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
get roundLimit() {
|
|
795
|
+
return this.options.roundLimit ?? ROUND_LIMIT;
|
|
796
|
+
}
|
|
797
|
+
/** Leaves candidates out until the rest add no error to what the projects already had. */
|
|
798
|
+
dropBreaking(baseline) {
|
|
799
|
+
/** The candidates the last round left on an error that led to one alone, with the place of that error. */
|
|
800
|
+
let alone = [];
|
|
801
|
+
while (this.accepted.size > 0) {
|
|
802
|
+
if (this.rounds >= this.roundLimit) {
|
|
803
|
+
this.rejectUntried([...this.accepted]);
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
const { edits, programs, added } = this.check({
|
|
807
|
+
candidates: [...this.accepted],
|
|
808
|
+
baseline,
|
|
809
|
+
inDoubt: this.accepted.size,
|
|
810
|
+
});
|
|
811
|
+
if (added.length === 0) {
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
const blames = added.map((error) => ({
|
|
815
|
+
error,
|
|
816
|
+
candidates: this.blamed({ programs, error, edits }),
|
|
817
|
+
}));
|
|
818
|
+
// An error still there without the candidate left for it was not that candidate's alone.
|
|
819
|
+
const unsettled = alone.filter(({ place }) => added.some((error) => error.place === place));
|
|
820
|
+
const certain = blames.filter((blame) => blame.candidates.length === 1);
|
|
821
|
+
if (unsettled.length === 0 && certain.length > 0) {
|
|
822
|
+
alone = [];
|
|
823
|
+
for (const { error, candidates } of certain) {
|
|
824
|
+
for (const candidate of candidates) {
|
|
825
|
+
this.rejectBroken({ candidate, error });
|
|
826
|
+
alone.push({ place: error.place, candidate });
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
continue;
|
|
830
|
+
}
|
|
831
|
+
for (const { candidate } of unsettled) {
|
|
832
|
+
this.restore(candidate);
|
|
833
|
+
}
|
|
834
|
+
// Several suspects, or none: which of them it takes is for the typecheck to say.
|
|
835
|
+
const suspects = new Set([
|
|
836
|
+
...unsettled.map(({ candidate }) => candidate),
|
|
837
|
+
...blames.flatMap((blame) => blame.candidates),
|
|
838
|
+
]);
|
|
839
|
+
const around = this.filesAround({ programs, errors: added });
|
|
840
|
+
const [first, rest] = this.split({
|
|
841
|
+
isFirst: (candidate) => suspects.size === 0 ? around.has(candidate.file) : !suspects.has(candidate),
|
|
842
|
+
});
|
|
843
|
+
// The configs no error points near are proved at once, so the rounds are not spent before they are tried;
|
|
844
|
+
// what is left is halved.
|
|
845
|
+
const groups = suspects.size === 0 ? [rest, ...this.halves(first)] : [first, ...this.halves(rest)];
|
|
846
|
+
this.bisect({ baseline, groups: groups.filter((group) => group.length > 0) });
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
/** The accepted candidates `isFirst` holds for, then the others. */
|
|
851
|
+
split(opts) {
|
|
852
|
+
const all = [...this.accepted];
|
|
853
|
+
return [all.filter(opts.isFirst), all.filter((candidate) => !opts.isFirst(candidate))];
|
|
854
|
+
}
|
|
855
|
+
halves(group) {
|
|
856
|
+
const middle = Math.ceil(group.length / 2);
|
|
857
|
+
return [group.slice(0, middle), group.slice(middle)].filter((half) => half.length > 0);
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* For errors no single candidate answers for, with all of them known to fail together: adds `groups` to those already
|
|
861
|
+
* proved, halving a group that fails, and leaves each candidate that fails alone on top of them. Which of two that
|
|
862
|
+
* only fail together stays follows the order of the groups.
|
|
863
|
+
*/
|
|
864
|
+
bisect(opts) {
|
|
865
|
+
const { baseline } = opts;
|
|
866
|
+
const all = [...this.accepted];
|
|
867
|
+
const proved = [];
|
|
868
|
+
const queue = [...opts.groups];
|
|
869
|
+
// The one group that is everything is known to fail: halve it without asking.
|
|
870
|
+
if (queue.length === 1 && queue[0]?.length === all.length && all.length > 1) {
|
|
871
|
+
queue.splice(0, 1, ...this.halves(all));
|
|
872
|
+
}
|
|
873
|
+
while (this.rounds < this.roundLimit) {
|
|
874
|
+
const group = queue.shift();
|
|
875
|
+
if (!group) {
|
|
876
|
+
break;
|
|
877
|
+
}
|
|
878
|
+
const [only, ...others] = group;
|
|
879
|
+
const { added } = this.check({
|
|
880
|
+
candidates: [...proved, ...group],
|
|
881
|
+
baseline,
|
|
882
|
+
inDoubt: this.accepted.size - proved.length,
|
|
883
|
+
});
|
|
884
|
+
const [error] = added;
|
|
885
|
+
if (!error) {
|
|
886
|
+
proved.push(...group);
|
|
887
|
+
}
|
|
888
|
+
else if (only && others.length === 0) {
|
|
889
|
+
this.rejectBroken({ candidate: only, error });
|
|
890
|
+
}
|
|
891
|
+
else {
|
|
892
|
+
queue.unshift(...this.halves(group));
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
this.rejectUntried(all.filter((candidate) => !proved.includes(candidate)));
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
/** Writes a config declared as `IAuthRouteConfig` (or a `Record` of it) with `satisfies`, which keeps its schemas. */
|
|
899
|
+
class SatisfiesRewrite {
|
|
900
|
+
static scan(opts) {
|
|
901
|
+
return opts.source.includes(CONFIG_TYPE)
|
|
902
|
+
? new ConfigDeclarations(opts).scan()
|
|
903
|
+
: { candidates: [], left: [] };
|
|
904
|
+
}
|
|
905
|
+
static prove(opts) {
|
|
906
|
+
return new SatisfiesProof(opts).run();
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
exports.SatisfiesRewrite = SatisfiesRewrite;
|
|
910
|
+
//# sourceMappingURL=typed-route-context-satisfies.js.map
|