targeted-mutation 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +21 -0
  3. package/README.md +111 -2
  4. package/bin/tmt-compile.mjs +46 -0
  5. package/bin/tmt.mjs +117 -0
  6. package/build/typescript-syntax/LICENSE.txt +55 -0
  7. package/build/typescript-syntax/ThirdPartyNoticeText.txt +193 -0
  8. package/build/typescript-syntax/typescript-syntax.cjs +43 -0
  9. package/docs/analysis.md +218 -0
  10. package/docs/compiler.md +462 -0
  11. package/docs/development.md +61 -0
  12. package/docs/how-it-works.svg +188 -0
  13. package/docs/l1.md +94 -0
  14. package/docs/performance.md +647 -0
  15. package/docs/releasing.md +48 -0
  16. package/package.json +49 -4
  17. package/src/analysis.mjs +689 -0
  18. package/src/assertion-session.mjs +93 -0
  19. package/src/assertion-strength.mjs +795 -0
  20. package/src/builtin-mutations.mjs +755 -0
  21. package/src/builtin-switches.mjs +642 -0
  22. package/src/change-undone.mjs +524 -0
  23. package/src/cli-output.mjs +166 -0
  24. package/src/compiler-compat.cjs +108 -0
  25. package/src/compiler-maps.cjs +60 -0
  26. package/src/compiler-routes.cjs +391 -0
  27. package/src/compiler.cjs +98 -0
  28. package/src/compiler.mjs +1 -0
  29. package/src/conventional.mjs +172 -0
  30. package/src/displacement.mjs +19 -0
  31. package/src/extract.mjs +135 -0
  32. package/src/fallback-process.mjs +14 -0
  33. package/src/in-parallel.mjs +20 -0
  34. package/src/lean-replay.mjs +39 -0
  35. package/src/operator-analysis.mjs +123 -0
  36. package/src/playwright.mjs +139 -0
  37. package/src/probe-runtime.mjs +50 -0
  38. package/src/probe.mjs +170 -0
  39. package/src/prototype.mjs +230 -0
  40. package/src/regex-mutations.mjs +102 -0
  41. package/src/runner-process.mjs +59 -0
  42. package/src/runtime-binaries.mjs +50 -0
  43. package/src/scope.mjs +202 -0
  44. package/src/source-text.mjs +49 -0
  45. package/src/standard-plan.mjs +308 -0
  46. package/src/standard-runner.mjs +185 -0
  47. package/src/standard-session.mjs +362 -0
  48. package/src/standard-setup.mjs +20 -0
  49. package/src/targeted-gaps.mjs +85 -0
  50. package/src/threshold.mjs +185 -0
  51. package/src/trace-buffer.mjs +126 -0
  52. package/src/typescript-bindings.mjs +243 -0
  53. package/src/typescript-compiler.mjs +59 -0
  54. package/src/typescript-extraction.mjs +354 -0
  55. package/src/typescript-grammar.mjs +923 -0
  56. package/src/vitest-reporter.mjs +64 -0
  57. package/src/vitest.mjs +262 -0
  58. package/src/witness-analysis.mjs +234 -0
  59. package/src/witness-contracts.mjs +110 -0
  60. package/src/witness-discovery.mjs +239 -0
  61. package/src/witness-fixtures.mjs +148 -0
  62. package/src/witness-order.mjs +83 -0
  63. package/src/witness-rules.mjs +254 -0
@@ -0,0 +1,354 @@
1
+ import { byteOffsets, sourceDialect } from './source-text.mjs';
2
+ import { createTypeScriptBindings } from './typescript-bindings.mjs';
3
+ import { withTypeScriptSyntax } from './typescript-compiler.mjs';
4
+ import { createTypeScriptGrammar } from './typescript-grammar.mjs';
5
+
6
+ const extractors = new WeakMap();
7
+
8
+ export function createTypeScriptExtractor(ts) {
9
+ const {
10
+ LanguageVariant, ScriptKind, ScriptTarget, SyntaxKind, createScanner, createSourceFile,
11
+ flattenDiagnosticMessageText, forEachChild, isArrowFunction, isAsExpression, isBinaryExpression, isIdentifier,
12
+ isImportTypeNode, isJsxAttribute, isJsxClosingElement, isJsxElement, isJsxOpeningElement,
13
+ isJsxSelfClosingElement, isParenthesizedExpression, isSatisfiesExpression, isTypeAssertionExpression,
14
+ } = ts;
15
+ const checkGrammar = createTypeScriptGrammar(ts, createTypeScriptBindings(ts));
16
+
17
+ const kind = SyntaxKind;
18
+ const comparison = new Map([
19
+ [kind.LessThanToken, ['<', '<=']],
20
+ [kind.LessThanEqualsToken, ['<=', '<']],
21
+ [kind.GreaterThanToken, ['>', '>=']],
22
+ [kind.GreaterThanEqualsToken, ['>=', '>']],
23
+ ]);
24
+ const isName = token => token === kind.Identifier || token >= kind.FirstKeyword && token <= kind.LastKeyword;
25
+ const scannerFor = source => createScanner(ScriptTarget.Latest, true, LanguageVariant.Standard, source);
26
+
27
+ function jsxTagMarkers(source, diagnostics) {
28
+ const errors = new Set(diagnostics.filter(diagnostic => diagnostic.code === 1003).map(diagnostic => diagnostic.start));
29
+ if (!errors.size) return [];
30
+ const scanner = scannerFor(source);
31
+ const markers = [];
32
+ for (let start = source.indexOf('<'); start >= 0; start = source.indexOf('<', start + 1)) {
33
+ scanner.setTextPos(start);
34
+ if (scanner.scan() !== kind.LessThanToken) continue;
35
+ let token = scanner.scan();
36
+ const closing = token === kind.SlashToken;
37
+ if (closing) token = scanner.scan();
38
+ if (!isName(token)) continue;
39
+ let member = false;
40
+ let affected = false;
41
+ const edits = [];
42
+ while (isName(token) || member && token === kind.PrivateIdentifier) {
43
+ if (token === kind.PrivateIdentifier) edits.push(scanner.getTokenStart());
44
+ affected ||= errors.has(scanner.getTextPos());
45
+ let end = scanner.getTextPos();
46
+ token = scanner.scan();
47
+ while ((token === kind.MinusToken || token === kind.MinusMinusToken) && scanner.getTokenStart() === end) {
48
+ affected ||= errors.has(end);
49
+ for (; end < scanner.getTextPos(); end++) edits.push(end);
50
+ token = scanner.scan();
51
+ if ((isName(token) || token === kind.NumericLiteral) && scanner.getTokenStart() === end) {
52
+ end = scanner.getTextPos();
53
+ token = scanner.scan();
54
+ }
55
+ }
56
+ if (token !== kind.DotToken) break;
57
+ member = true;
58
+ token = scanner.scan();
59
+ }
60
+ if (member && (affected || closing) && edits.length && (isName(token) || token === kind.SlashToken ||
61
+ token === kind.GreaterThanToken || token === kind.OpenBraceToken || token === kind.LessThanToken)) {
62
+ for (const edit of edits) markers.push(edit);
63
+ }
64
+ }
65
+ return markers;
66
+ }
67
+
68
+ function repairJsxTags(source, tree, read) {
69
+ const markers = jsxTagMarkers(source, tree.parseDiagnostics);
70
+ if (!markers.length) return tree;
71
+ const available = [];
72
+ for (let code = 0x416; code <= 0x42f && available.length < 2; code++) {
73
+ const char = String.fromCharCode(code);
74
+ if (!source.includes(char)) available.push(char);
75
+ }
76
+ if (available.length < 2) return tree;
77
+ const text = source.split('');
78
+ for (const position of markers) text[position] = source[position] === '#' ? available[0] : available[1];
79
+ let repaired = read(text.join(''));
80
+ const confirmed = new Set();
81
+ const pending = [repaired];
82
+ while (pending.length) {
83
+ const node = pending.pop();
84
+ if (isJsxOpeningElement(node) || isJsxClosingElement(node) || isJsxSelfClosingElement(node)) {
85
+ const start = node.tagName.getStart(repaired);
86
+ for (let index = start; index < node.tagName.end; index++) {
87
+ if (text[index] !== source[index]) confirmed.add(index);
88
+ }
89
+ }
90
+ forEachChild(node, child => { pending.push(child); });
91
+ }
92
+ if (!confirmed.size) return tree;
93
+ if (markers.some(position => !confirmed.has(position))) {
94
+ for (const position of markers) {
95
+ if (!confirmed.has(position)) text[position] = source[position];
96
+ }
97
+ repaired = read(text.join(''));
98
+ }
99
+ return repaired;
100
+ }
101
+
102
+ function insertSyntax(source, edits) {
103
+ const pieces = [];
104
+ const inserted = [];
105
+ let previous = 0;
106
+ for (const [index, [offset, char]] of edits.entries()) {
107
+ pieces.push(source.slice(previous, offset), char);
108
+ inserted.push(offset + index);
109
+ previous = offset;
110
+ }
111
+ pieces.push(source.slice(previous));
112
+ const originalOffset = offset => {
113
+ let low = 0;
114
+ let high = inserted.length;
115
+ while (low < high) {
116
+ const mid = (low + high) >>> 1;
117
+ if (inserted[mid] < offset) low = mid + 1;
118
+ else high = mid;
119
+ }
120
+ return offset - low;
121
+ };
122
+ return { text: pieces.join(''), inserted, originalOffset };
123
+ }
124
+
125
+ function locateArrowRepairs(tree, source) {
126
+ // A comma may disambiguate a single bare type parameter in TSX. The reparsed
127
+ // AST must consume each inserted comma in an actual arrow type-parameter list.
128
+ const matchedTags = new Set();
129
+ const pending = [tree];
130
+ while (pending.length) {
131
+ const node = pending.pop();
132
+ if (isJsxElement(node) && node.openingElement.tagName.getText(tree) === node.closingElement.tagName.getText(tree)) {
133
+ matchedTags.add(node.openingElement.getStart(tree));
134
+ }
135
+ forEachChild(node, child => { pending.push(child); });
136
+ }
137
+ const scanner = scannerFor(source);
138
+ const offsets = [];
139
+ for (let start = source.indexOf('<'); start >= 0; start = source.indexOf('<', start + 1)) {
140
+ if (matchedTags.has(start)) continue;
141
+ scanner.setTextPos(start);
142
+ if (scanner.scan() !== kind.LessThanToken) continue;
143
+ let token = scanner.scan();
144
+ if (token === kind.ConstKeyword) token = scanner.scan();
145
+ if (!isName(token) || scanner.scan() !== kind.GreaterThanToken) continue;
146
+ const close = scanner.getTokenStart();
147
+ if (scanner.scan() === kind.OpenParenToken) offsets.push(close);
148
+ }
149
+ return offsets;
150
+ }
151
+
152
+ function arrowCommas(tree) {
153
+ const confirmed = new Set();
154
+ const pending = [tree];
155
+ while (pending.length) {
156
+ const node = pending.pop();
157
+ if (isArrowFunction(node) && node.typeParameters?.length === 1 && node.typeParameters.hasTrailingComma &&
158
+ node.equalsGreaterThanToken.getWidth(tree) > 0) {
159
+ confirmed.add(node.typeParameters.end - 1);
160
+ }
161
+ forEachChild(node, child => { pending.push(child); });
162
+ }
163
+ return confirmed;
164
+ }
165
+
166
+ function repairTsx(source, tree, read) {
167
+ const identity = offset => offset;
168
+ tree = repairJsxTags(source, tree, read);
169
+ if (!tree.parseDiagnostics.length) return { tree, originalOffset: identity };
170
+ const patched = tree.text;
171
+ const offsets = locateArrowRepairs(tree, patched);
172
+ if (!offsets.length) return { tree, originalOffset: identity };
173
+
174
+ const withCommas = positions => insertSyntax(patched, positions.map(position => [position, ',']));
175
+ const trial = withCommas(offsets);
176
+ const alternative = read(trial.text);
177
+ const confirmed = arrowCommas(alternative);
178
+ const selected = offsets.filter((offset, index) => confirmed.has(trial.inserted[index]));
179
+ if (!selected.length) return { tree, originalOffset: identity };
180
+ const repaired = selected.length === offsets.length ? trial : withCommas(selected);
181
+ const repairedTree = repaired === trial ? alternative : read(repaired.text);
182
+ const repairedCommas = repaired === trial ? confirmed : arrowCommas(repairedTree);
183
+ if (repaired.inserted.some(position => !repairedCommas.has(position))) {
184
+ return { tree, originalOffset: identity };
185
+ }
186
+ return { tree: repairedTree, originalOffset: repaired.originalOffset };
187
+ }
188
+
189
+ function repairAssertedAssignments(result, read) {
190
+ const { tree, originalOffset } = result;
191
+ const expected = new Set(tree.parseDiagnostics.filter(diagnostic => diagnostic.code === 1005).map(diagnostic => diagnostic.start));
192
+ if (!expected.size) return result;
193
+ const scanner = scannerFor(tree.text);
194
+ const isAssignment = token => token >= kind.FirstAssignment && token <= kind.LastAssignment;
195
+ const isAssertion = node => isAsExpression(node) || isSatisfiesExpression(node) || isTypeAssertionExpression(node);
196
+ const edits = [];
197
+ const pending = [tree];
198
+ while (pending.length) {
199
+ const node = pending.pop();
200
+ if (isAssertion(node)) {
201
+ scanner.setTextPos(node.end);
202
+ if (isAssignment(scanner.scan()) && expected.has(scanner.getTokenStart())) {
203
+ edits.push([node.getStart(tree), '('], [node.end, ')']);
204
+ }
205
+ }
206
+ forEachChild(node, child => { pending.push(child); });
207
+ }
208
+ if (!edits.length) return result;
209
+ edits.sort((a, b) => a[0] - b[0]);
210
+ const repair = insertSyntax(tree.text, edits);
211
+ const repaired = read(repair.text);
212
+ const confirmed = new Set();
213
+ pending.push(repaired);
214
+ while (pending.length) {
215
+ const node = pending.pop();
216
+ if (isBinaryExpression(node) && isAssignment(node.operatorToken.kind) &&
217
+ isParenthesizedExpression(node.left) && isAssertion(node.left.expression)) {
218
+ confirmed.add(node.left.getStart(repaired));
219
+ confirmed.add(node.left.end - 1);
220
+ }
221
+ forEachChild(node, child => { pending.push(child); });
222
+ }
223
+ if (repair.inserted.some(position => !confirmed.has(position))) return result;
224
+ return { tree: repaired, originalOffset: offset => originalOffset(repair.originalOffset(offset)) };
225
+ }
226
+
227
+ function repairImportTypeArguments(tree, read) {
228
+ const source = tree.text;
229
+ if (!/\bimport\b(?=\s*[(/])/.test(source)) return tree;
230
+ const scanner = scannerFor(source);
231
+ const pending = [tree];
232
+ const gaps = [];
233
+ while (pending.length) {
234
+ const node = pending.pop();
235
+ if (isImportTypeNode(node) && !node.typeArguments) {
236
+ scanner.setTextPos(node.end);
237
+ if (scanner.scan() === kind.LessThanToken && scanner.hasPrecedingLineBreak()) {
238
+ gaps.push([node.end, scanner.getTokenStart()]);
239
+ }
240
+ }
241
+ forEachChild(node, child => { pending.push(child); });
242
+ }
243
+ if (!gaps.length) return tree;
244
+ const parts = [];
245
+ let previous = 0;
246
+ for (const [start, end] of gaps.sort((a, b) => a[0] - b[0])) {
247
+ parts.push(source.slice(previous, start), ' '.repeat(end - start));
248
+ previous = end;
249
+ }
250
+ parts.push(source.slice(previous));
251
+ return read(parts.join(''));
252
+ }
253
+
254
+ function parse(source, file, dialect) {
255
+ const scriptKind = dialect === 'tsx' ? ScriptKind.TSX : ScriptKind.TS;
256
+ const read = text => createSourceFile(file, text, ScriptTarget.Latest, false, scriptKind);
257
+ const tree = repairImportTypeArguments(read(source), read);
258
+ let result = { tree, originalOffset: offset => offset };
259
+ if (!tree.parseDiagnostics.length) return result;
260
+ if (scriptKind === ScriptKind.TSX) result = repairTsx(tree.text, tree, read);
261
+ return repairAssertedAssignments(result, read);
262
+ }
263
+
264
+ function materializeCandidates(sites, tree, source, span, originalOffset) {
265
+ const candidates = sites.map(node => {
266
+ const boundary = comparison.get(node.operatorToken.kind);
267
+ if (boundary) {
268
+ const [operator, replacement] = boundary;
269
+ return {
270
+ rule: 'comparison-boundary/v1', operator, replacement,
271
+ edit: span(node.operatorToken), left: span(node.left), right: span(node.right),
272
+ };
273
+ }
274
+ return {
275
+ rule: 'jsx-disabled-drop-left/v1', operator: '||', operatorEdit: span(node.operatorToken),
276
+ replacement: '(' + source.slice(originalOffset(node.right.getStart(tree)), originalOffset(node.right.end)) + ')',
277
+ edit: span(node), left: span(node.left), right: span(node.right),
278
+ };
279
+ });
280
+ candidates.sort((a, b) => (a.operatorEdit ?? a.edit)[0] - (b.operatorEdit ?? b.edit)[0]);
281
+ return candidates;
282
+ }
283
+
284
+ return function extract(source, options) {
285
+ const path = options.file ?? null;
286
+ const { tree, originalOffset } = parse(source, path ?? 'input.ts', options.dialect ?? sourceDialect(path));
287
+ let offsets;
288
+ const bytes = unit => {
289
+ if (offsets === undefined) offsets = Buffer.byteLength(source) === source.length ? null : byteOffsets(source);
290
+ return offsets === null ? unit : offsets[unit];
291
+ };
292
+ const insideCodePoint = unit => {
293
+ const code = source.charCodeAt(unit);
294
+ return code >= 0xdc00 && code <= 0xdfff;
295
+ };
296
+ const span = node => [bytes(originalOffset(node.getStart(tree))), bytes(originalOffset(node.end))];
297
+ const diagnostics = new Map();
298
+ const syntaxErrors = [];
299
+ function record(start, end, message, labels = []) {
300
+ const diagnostic = { severity: 'error', message, path, start, end, labels, help: null };
301
+ diagnostics.set(JSON.stringify([start, end, message]), diagnostic);
302
+ }
303
+ for (const diagnostic of tree.parseDiagnostics) {
304
+ const at = diagnostic.start ?? 0;
305
+ const start = originalOffset(at);
306
+ const end = originalOffset(at + (diagnostic.length ?? 0));
307
+ // Triple-slash build directives do not change the syntax of the program.
308
+ const directive = diagnostic.code === 2458 || diagnostic.code === 1084;
309
+ if (directive && source.slice(start, end).trimStart().startsWith('///')) continue;
310
+ if (options.mode === 'script' && [1121, 1487, 1488, 1489].includes(diagnostic.code)) continue;
311
+ const first = insideCodePoint(start) ? start - 1 : start;
312
+ const last = insideCodePoint(end) ? end + 1 : end;
313
+ syntaxErrors.push({ start: at, end: at + (diagnostic.length ?? 0) });
314
+ record(bytes(first), bytes(last), flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
315
+ }
316
+ function overlapsSyntaxError(node) {
317
+ if (!syntaxErrors.length) return false;
318
+ const start = node.getStart(tree);
319
+ return start >= node.end || syntaxErrors.some(error => start <= error.end && error.start < node.end);
320
+ }
321
+ const sites = [];
322
+ function visitBinary(node, attribute) {
323
+ const token = node.operatorToken.kind;
324
+ if (comparison.has(token) || (options.logical && token === kind.BarBarToken && attribute && isJsxAttribute(attribute) &&
325
+ isIdentifier(attribute.name) && attribute.name.text === 'disabled')) sites.push(node);
326
+ }
327
+ const scanCandidates = source.includes('<') || source.includes('>') || (options.logical && source.includes('||'));
328
+ checkGrammar(tree, options.mode, (node, message, previous) => {
329
+ if (overlapsSyntaxError(node)) return;
330
+ const [start, end] = span(node);
331
+ const labels = [];
332
+ if (previous && !overlapsSyntaxError(previous)) {
333
+ const [priorStart, priorEnd] = span(previous);
334
+ labels.push({ start: priorStart, end: priorEnd, message: 'Previously declared here' });
335
+ }
336
+ record(start, end, message, labels);
337
+ }, scanCandidates ? visitBinary : undefined);
338
+ if (diagnostics.size) {
339
+ return { path: 'diagnostic', candidates: [], diagnostics: [...diagnostics.values()].sort((a, b) => a.start - b.start) };
340
+ }
341
+ return { path: 'fallback', fallback: 'typescript', candidates: materializeCandidates(sites, tree, source, span, originalOffset) };
342
+ };
343
+ }
344
+
345
+ export function extractTypeScript(source, options = {}) {
346
+ return withTypeScriptSyntax(ts => {
347
+ let extract = extractors.get(ts);
348
+ if (!extract) {
349
+ extract = createTypeScriptExtractor(ts);
350
+ extractors.set(ts, extract);
351
+ }
352
+ return extract(source, options);
353
+ });
354
+ }