targeted-mutation 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +21 -0
- package/README.md +111 -2
- package/bin/tmt-compile.mjs +46 -0
- package/bin/tmt.mjs +117 -0
- package/build/typescript-syntax/LICENSE.txt +55 -0
- package/build/typescript-syntax/ThirdPartyNoticeText.txt +193 -0
- package/build/typescript-syntax/typescript-syntax.cjs +43 -0
- package/docs/analysis.md +218 -0
- package/docs/compiler.md +462 -0
- package/docs/development.md +61 -0
- package/docs/how-it-works.svg +188 -0
- package/docs/l1.md +94 -0
- package/docs/performance.md +647 -0
- package/docs/releasing.md +48 -0
- package/package.json +49 -4
- package/src/analysis.mjs +689 -0
- package/src/assertion-session.mjs +93 -0
- package/src/assertion-strength.mjs +795 -0
- package/src/builtin-mutations.mjs +755 -0
- package/src/builtin-switches.mjs +642 -0
- package/src/change-undone.mjs +524 -0
- package/src/cli-output.mjs +166 -0
- package/src/compiler-compat.cjs +108 -0
- package/src/compiler-maps.cjs +60 -0
- package/src/compiler-routes.cjs +391 -0
- package/src/compiler.cjs +98 -0
- package/src/compiler.mjs +1 -0
- package/src/conventional.mjs +172 -0
- package/src/displacement.mjs +19 -0
- package/src/extract.mjs +135 -0
- package/src/fallback-process.mjs +14 -0
- package/src/in-parallel.mjs +20 -0
- package/src/lean-replay.mjs +39 -0
- package/src/operator-analysis.mjs +123 -0
- package/src/playwright.mjs +139 -0
- package/src/probe-runtime.mjs +50 -0
- package/src/probe.mjs +170 -0
- package/src/prototype.mjs +230 -0
- package/src/regex-mutations.mjs +102 -0
- package/src/runner-process.mjs +59 -0
- package/src/runtime-binaries.mjs +50 -0
- package/src/scope.mjs +202 -0
- package/src/source-text.mjs +49 -0
- package/src/standard-plan.mjs +308 -0
- package/src/standard-runner.mjs +185 -0
- package/src/standard-session.mjs +362 -0
- package/src/standard-setup.mjs +20 -0
- package/src/targeted-gaps.mjs +85 -0
- package/src/threshold.mjs +185 -0
- package/src/trace-buffer.mjs +126 -0
- package/src/typescript-bindings.mjs +243 -0
- package/src/typescript-compiler.mjs +59 -0
- package/src/typescript-extraction.mjs +354 -0
- package/src/typescript-grammar.mjs +923 -0
- package/src/vitest-reporter.mjs +64 -0
- package/src/vitest.mjs +262 -0
- package/src/witness-analysis.mjs +234 -0
- package/src/witness-contracts.mjs +110 -0
- package/src/witness-discovery.mjs +239 -0
- package/src/witness-fixtures.mjs +149 -0
- package/src/witness-order.mjs +83 -0
- package/src/witness-rules.mjs +254 -0
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { collectMutations, shiftPlaceholder } from './builtin-mutations.mjs';
|
|
3
|
+
import { byteOffsets } from './source-text.mjs';
|
|
4
|
+
|
|
5
|
+
// Runtime switches for the built-in mutation set. Expressions become `on(id) ? mutated : (cover(id), original)`;
|
|
6
|
+
// blocks, case clauses and removable statements become `if (on(id)) mutated else { cover(id); original }`.
|
|
7
|
+
// The helpers speak the test session's protocol: one active mutation and per-test hit counts.
|
|
8
|
+
const helpers = (on, cover, shift, scope) => `
|
|
9
|
+
function ${shift}() {
|
|
10
|
+
var scope = ${scope};
|
|
11
|
+
return (scope.__stryker__ && scope.__stryker__.shift) || 0;
|
|
12
|
+
}
|
|
13
|
+
function ${on}(id) {
|
|
14
|
+
var scope = ${scope}, ns = scope.__stryker__ || (scope.__stryker__ = {});
|
|
15
|
+
if (ns.activeMutant !== id) return false;
|
|
16
|
+
if (ns.hitCount !== void 0 && ++ns.hitCount > ns.hitLimit) throw new Error('Mutation hit limit reached (' + ns.hitCount + ')');
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
function ${cover}() {
|
|
20
|
+
var scope = ${scope}, ns = scope.__stryker__ || (scope.__stryker__ = {});
|
|
21
|
+
var coverage = ns.mutantCoverage || (ns.mutantCoverage = { static: {}, perTest: {} });
|
|
22
|
+
var counts = ns.currentTestId ? (coverage.perTest[ns.currentTestId] || (coverage.perTest[ns.currentTestId] = {})) : coverage.static;
|
|
23
|
+
for (var i = 0; i < arguments.length; i++) counts[arguments[i]] = (counts[arguments[i]] || 0) + 1;
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
// The probe of the targeted rules. It takes the operands of one expression, the right one of a logical operator as a
|
|
27
|
+
// function, and returns what the expression gives: with the active edit when one of the site's edits is on. While
|
|
28
|
+
// the baseline is recorded it also notes, for every edit of the site and per test, whether the edit would have
|
|
29
|
+
// given the same value after evaluating the same operands.
|
|
30
|
+
const probe = (see, table, cover, scope, rows) => `
|
|
31
|
+
function ${table}(i) {
|
|
32
|
+
return (${table}.rows || (${table}.rows = ${JSON.stringify(rows)}))[i];
|
|
33
|
+
}
|
|
34
|
+
function ${see}(i, l, r, r2) {
|
|
35
|
+
var site = ${table}(i), op = site[0], edits = site[2], kind = site[6], scope = ${scope}, ns = scope.__stryker__ || (scope.__stryker__ = {});
|
|
36
|
+
var utilTypes = ${table}.u !== void 0 ? ${table}.u : (${table}.u = typeof scope.process === 'object' && scope.process && typeof scope.process.getBuiltinModule === 'function' ? scope.process.getBuiltinModule('node:util').types : null);
|
|
37
|
+
var isProxy = utilTypes ? utilTypes.isProxy : null;
|
|
38
|
+
var isTypedArray = utilTypes ? utilTypes.isTypedArray : null;
|
|
39
|
+
function safeProtoMethod(proto, name) {
|
|
40
|
+
var d = scope.Object.getOwnPropertyDescriptor(proto, name);
|
|
41
|
+
return d && 'value' in d && typeof d.value === 'function' ? d.value : null;
|
|
42
|
+
}
|
|
43
|
+
var strMethods = ${table}.s || (${table}.s = { indexOf: safeProtoMethod(scope.String.prototype, 'indexOf'), lastIndexOf: safeProtoMethod(scope.String.prototype, 'lastIndexOf'), includes: safeProtoMethod(scope.String.prototype, 'includes'), replace: safeProtoMethod(scope.String.prototype, 'replace'), replaceAll: safeProtoMethod(scope.String.prototype, 'replaceAll') });
|
|
44
|
+
var arrMethods = ${table}.a || (${table}.a = { includes: safeProtoMethod(scope.Array.prototype, 'includes'), every: safeProtoMethod(scope.Array.prototype, 'every'), some: safeProtoMethod(scope.Array.prototype, 'some') });
|
|
45
|
+
function isNativeFn(fn) {
|
|
46
|
+
try { return typeof fn === 'function' && scope.Function.prototype.toString.call(fn).indexOf('[native code]') >= 0; } catch (e) { return false; }
|
|
47
|
+
}
|
|
48
|
+
function numMin(a, b) { return a !== a || b !== b ? NaN : (a === 0 && b === 0 ? (1 / a < 0 || 1 / b < 0 ? -0 : 0) : a < b ? a : b); }
|
|
49
|
+
function numMax(a, b) { return a !== a || b !== b ? NaN : (a === 0 && b === 0 ? (1 / a > 0 || 1 / b > 0 ? 0 : -0) : a > b ? a : b); }
|
|
50
|
+
function isPlainDataObj(v) {
|
|
51
|
+
if (v === null || v === void 0) return true;
|
|
52
|
+
if (typeof v !== 'object' || (isProxy && isProxy(v))) return false;
|
|
53
|
+
try {
|
|
54
|
+
var ks = scope.Reflect.ownKeys(v), ki;
|
|
55
|
+
for (ki = 0; ki < ks.length; ki++) {
|
|
56
|
+
var desc = scope.Object.getOwnPropertyDescriptor(v, ks[ki]);
|
|
57
|
+
if (desc && desc.enumerable && !('value' in desc)) return false;
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
} catch (e) { return false; }
|
|
61
|
+
}
|
|
62
|
+
function isolatedRegexTest(pat, flags, text) {
|
|
63
|
+
if (${table}.v === void 0) {
|
|
64
|
+
try {
|
|
65
|
+
var vmMod = typeof scope.process === 'object' && scope.process && typeof scope.process.getBuiltinModule === 'function' ? scope.process.getBuiltinModule('node:vm') : null;
|
|
66
|
+
${table}.v = vmMod ? vmMod.runInNewContext('(function(p,f,s){return new RegExp(p,f).test(s);})') : null;
|
|
67
|
+
} catch (e) { ${table}.v = null; }
|
|
68
|
+
}
|
|
69
|
+
return ${table}.v ? ${table}.v(pat, flags, text) : null;
|
|
70
|
+
}
|
|
71
|
+
var logical = kind === 'l', missing = l === null || l === void 0, k, form;
|
|
72
|
+
var taken = op === '&&' ? !!l : op === '||' ? !l : missing;
|
|
73
|
+
var constant = op === '&&' ? true : op === '||' ? false : void 0;
|
|
74
|
+
function equal(operator, a, b) { return operator === '===' ? a === b : operator === '!==' ? a !== b : operator === '==' ? a == b : a != b; }
|
|
75
|
+
// An operation on values: arithmetic, the functions of Math, and the ends of a sequence.
|
|
76
|
+
function apply(o, a, b, c) {
|
|
77
|
+
switch (o) {
|
|
78
|
+
case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; case '%': return a % b;
|
|
79
|
+
case 'max': return Math.max(a, b); case 'min': return Math.min(a, b);
|
|
80
|
+
case 'floor': return Math.floor(a); case 'ceil': return Math.ceil(a); case 'round': return Math.round(a); case 'trunc': return Math.trunc(a);
|
|
81
|
+
case 'idx0': return a[0]; case 'idxLast': return a[a.length - 1]; case 'at0': return a.at(0); case 'at-1': return a.at(-1);
|
|
82
|
+
case 'find': return a.find(b); case 'findLast': return a.findLast(b); case 'findIndex': return a.findIndex(b);
|
|
83
|
+
case 'findLastIndex': return a.findLastIndex(b); case 'indexOf': return a.indexOf(b); case 'lastIndexOf': return a.lastIndexOf(b);
|
|
84
|
+
case 'startsWith': return a.startsWith(b); case 'endsWith': return a.endsWith(b); case 'includes': return a.includes(b);
|
|
85
|
+
case 'replace': return a.replace(b, c); case 'replaceAll': return a.replaceAll(b, c);
|
|
86
|
+
case 'copyArray': return [...a]; case 'copyObject': return { ...a };
|
|
87
|
+
case 'sort': return a.sort(b); case 'toSorted': return a.toSorted(b); default: return op === 'sort' ? a.sort() : a.toSorted();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function callMethod(methodName) {
|
|
91
|
+
var fn = l[methodName];
|
|
92
|
+
var a1 = r();
|
|
93
|
+
if (site[7] === 3) {
|
|
94
|
+
var a2 = r2();
|
|
95
|
+
return scope.Reflect.apply(fn, l, [a1, a2]);
|
|
96
|
+
}
|
|
97
|
+
return scope.Reflect.apply(fn, l, [a1]);
|
|
98
|
+
}
|
|
99
|
+
function callClamp(mode) {
|
|
100
|
+
var cm = site[8], oMath, oFn, iMath, iFn, v0, v1, v2, iv;
|
|
101
|
+
if (mode === 'orig') {
|
|
102
|
+
oMath = scope.Math;
|
|
103
|
+
oFn = oMath[cm.outer];
|
|
104
|
+
if (cm.innerPos === 0) {
|
|
105
|
+
iMath = scope.Math;
|
|
106
|
+
iFn = iMath[cm.inner];
|
|
107
|
+
v0 = l[0](); v1 = l[1]();
|
|
108
|
+
iv = scope.Reflect.apply(iFn, iMath, [v0, v1]);
|
|
109
|
+
v2 = l[2]();
|
|
110
|
+
return scope.Reflect.apply(oFn, oMath, [iv, v2]);
|
|
111
|
+
}
|
|
112
|
+
v0 = l[0]();
|
|
113
|
+
iMath = scope.Math;
|
|
114
|
+
iFn = iMath[cm.inner];
|
|
115
|
+
v1 = l[1](); v2 = l[2]();
|
|
116
|
+
iv = scope.Reflect.apply(iFn, iMath, [v1, v2]);
|
|
117
|
+
return scope.Reflect.apply(oFn, oMath, [v0, iv]);
|
|
118
|
+
}
|
|
119
|
+
if (mode === 'dropOuter') {
|
|
120
|
+
iMath = scope.Math;
|
|
121
|
+
iFn = iMath[cm.inner];
|
|
122
|
+
return cm.innerPos === 0
|
|
123
|
+
? scope.Reflect.apply(iFn, iMath, [l[0](), l[1]()])
|
|
124
|
+
: scope.Reflect.apply(iFn, iMath, [l[1](), l[2]()]);
|
|
125
|
+
}
|
|
126
|
+
oMath = scope.Math;
|
|
127
|
+
oFn = oMath[cm.outer];
|
|
128
|
+
var keepA = mode === 'dropInnerB';
|
|
129
|
+
return cm.innerPos === 0
|
|
130
|
+
? scope.Reflect.apply(oFn, oMath, [keepA ? l[0]() : l[1](), l[2]()])
|
|
131
|
+
: scope.Reflect.apply(oFn, oMath, [l[0](), keepA ? l[1]() : l[2]()]);
|
|
132
|
+
}
|
|
133
|
+
function makeArrayWithout(src, skipIdx) {
|
|
134
|
+
if (skipIdx < 0) return src;
|
|
135
|
+
var out = [], idx;
|
|
136
|
+
for (idx = 0; idx < src.length; idx++) {
|
|
137
|
+
if (idx !== skipIdx) scope.Object.defineProperty(out, out.length, { value: src[idx], enumerable: true, writable: true, configurable: true });
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
function callLiteralIncludes(skipIdx) {
|
|
142
|
+
var arr = makeArrayWithout(l, skipIdx);
|
|
143
|
+
var fn = arr.includes;
|
|
144
|
+
var arg = r();
|
|
145
|
+
return scope.Reflect.apply(fn, arr, [arg]);
|
|
146
|
+
}
|
|
147
|
+
function callRegexTest(reFactory) {
|
|
148
|
+
var re = reFactory();
|
|
149
|
+
var fn = re.test;
|
|
150
|
+
var arg = r();
|
|
151
|
+
return scope.Reflect.apply(fn, re, [arg]);
|
|
152
|
+
}
|
|
153
|
+
function buildSpread(skipIndex) {
|
|
154
|
+
var keys = site[8], out = { ...l }, m;
|
|
155
|
+
for (m = 0; m < keys.length; m++) {
|
|
156
|
+
if (m === skipIndex) continue;
|
|
157
|
+
scope.Object.defineProperty(out, keys[m], { value: r[m](), enumerable: true, writable: true, configurable: true });
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
function edited(f) {
|
|
162
|
+
if (kind === 'c') return l;
|
|
163
|
+
if (kind === 'p') return buildSpread(Number(f.slice(5)));
|
|
164
|
+
if (kind === 'r') return callMethod(f);
|
|
165
|
+
if (kind === 'k') return callClamp(f);
|
|
166
|
+
if (kind === 'u') return callLiteralIncludes(Number(f.slice(6)));
|
|
167
|
+
if (kind === 'g') { var cb = { ...r() }; return { ...cb, ...l() }; }
|
|
168
|
+
if (kind === 'x') return callRegexTest(l[f]);
|
|
169
|
+
if (kind !== 'e') return apply(f, l, r, r2);
|
|
170
|
+
if (f === 'true') return true;
|
|
171
|
+
if (f === 'false') return false;
|
|
172
|
+
if (f === 'loose') return equal(op.slice(0, 2), l, r);
|
|
173
|
+
if (f === 'strict') return equal(op + '=', l, r);
|
|
174
|
+
var subject = site[3] ? l : r, other = (site[3] ? r : l) === null ? void 0 : null;
|
|
175
|
+
return (subject === other) === (op === '==');
|
|
176
|
+
}
|
|
177
|
+
function original() {
|
|
178
|
+
if (logical) return taken ? r() : l;
|
|
179
|
+
if (kind === 'e') return equal(op, l, r);
|
|
180
|
+
if (kind === 'p') return buildSpread(-1);
|
|
181
|
+
if (kind === 'r') return callMethod(op);
|
|
182
|
+
if (kind === 'k') return callClamp('orig');
|
|
183
|
+
if (kind === 'u') return callLiteralIncludes(-1);
|
|
184
|
+
if (kind === 'g') { var ca = { ...l() }; return { ...ca, ...r() }; }
|
|
185
|
+
if (kind === 'x') return callRegexTest(l.orig);
|
|
186
|
+
return apply(op, l, r, r2);
|
|
187
|
+
}
|
|
188
|
+
function plain(v) { return v === null || (typeof v !== 'object' && typeof v !== 'function'); }
|
|
189
|
+
// An array of the built-in kind with the same elements: slice would call the constructor of a subclass.
|
|
190
|
+
function copy(a) {
|
|
191
|
+
var plainArray = [], n;
|
|
192
|
+
plainArray.length = a.length;
|
|
193
|
+
for (n = 0; n < a.length; n++) if (n in a) plainArray[n] = a[n];
|
|
194
|
+
return plainArray;
|
|
195
|
+
}
|
|
196
|
+
function is(a, b) { return a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b; }
|
|
197
|
+
// Whether the edit gives the same, where the operation can be worked out again without an effect: on plain
|
|
198
|
+
// values and numbers, and on arrays and text for their ends. Otherwise it is not known (null).
|
|
199
|
+
function alike(f, value) {
|
|
200
|
+
var sequence = scope.Array.isArray(l);
|
|
201
|
+
if (op === 'sort' || op === 'toSorted') {
|
|
202
|
+
// The order without the comparison is the order of the elements as text. For numbers and text that can be
|
|
203
|
+
// worked out on a copy; anything else would be converted by its own code.
|
|
204
|
+
var from = op === 'sort' ? unsorted : l, n;
|
|
205
|
+
// Only the sort of the built-in arrays is known: a subclass may have one of its own.
|
|
206
|
+
if (!scope.Array.isArray(from) || !scope.Array.isArray(value) || from.length !== value.length || l[op] !== scope.Array.prototype[op]) return null;
|
|
207
|
+
for (n = 0; n < from.length; n++) if (!(n in from) || (typeof from[n] !== 'number' && typeof from[n] !== 'string')) return null;
|
|
208
|
+
var other = copy(from).sort();
|
|
209
|
+
for (n = 0; n < other.length; n++) if (!is(other[n], value[n])) return false;
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
if (op === 'startsWith' || op === 'endsWith') {
|
|
213
|
+
var incFn = safeProtoMethod(scope.String.prototype, f);
|
|
214
|
+
if (typeof l !== 'string' || !plain(arg1) || !incFn || incFn !== strMethods[f]) return null;
|
|
215
|
+
try { return is(value, scope.Reflect.apply(incFn, l, [arg1])); } catch (error) { return false; }
|
|
216
|
+
}
|
|
217
|
+
if (op === 'replace' || op === 'replaceAll') {
|
|
218
|
+
var repFn = safeProtoMethod(scope.String.prototype, f);
|
|
219
|
+
if (typeof l !== 'string' || !plain(arg1) || !plain(arg2) || !repFn || repFn !== strMethods[f]) return null;
|
|
220
|
+
try { return is(value, scope.Reflect.apply(repFn, l, [arg1, arg2])); } catch (error) { return false; }
|
|
221
|
+
}
|
|
222
|
+
if (op === 'every' || op === 'some') {
|
|
223
|
+
if (!qPreOk) return null;
|
|
224
|
+
if (qPreLen === 0) return false;
|
|
225
|
+
if (qPreLen === 1 && qPreDense0) return true;
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
if (kind === 'a' ? !plain(l) || !plain(r) : kind === 'm' ? typeof l !== 'number' || (site[7] === 2 && typeof r !== 'number')
|
|
229
|
+
: (!sequence && typeof l !== 'string') || ((op === 'indexOf' || op === 'lastIndexOf') && typeof l === 'string' && (!plain(r) || l[f] !== strMethods[f]))) return null;
|
|
230
|
+
// The callback of a search would run again: with at most one element both ends are the same search.
|
|
231
|
+
if (op.slice(0, 4) === 'find') return sequence && l.length <= 1 ? true : null;
|
|
232
|
+
try {
|
|
233
|
+
return is(value, apply(f, l, r, r2));
|
|
234
|
+
} catch (error) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function show(v) {
|
|
239
|
+
var t = typeof v;
|
|
240
|
+
return v === null ? 'null' : t === 'string' ? '"' + (v.length > 40 ? v.slice(0, 40) + '...' : v) + '"'
|
|
241
|
+
: t === 'number' ? (v === 0 && 1 / v < 0 ? '-0' : '' + v) : t === 'boolean' || t === 'undefined' ? '' + v : t === 'bigint' ? v + 'n'
|
|
242
|
+
: scope.Array.isArray(v) ? 'array of ' + v.length : t;
|
|
243
|
+
}
|
|
244
|
+
if (ns.activeMutant !== void 0) for (k = 0; k < edits.length; k++) if (edits[k][0] === ns.activeMutant) form = edits[k][1];
|
|
245
|
+
if (form !== void 0) {
|
|
246
|
+
if (ns.hitCount !== void 0 && ++ns.hitCount > ns.hitLimit) throw new Error('Mutation hit limit reached (' + ns.hitCount + ')');
|
|
247
|
+
if (!logical) return edited(form);
|
|
248
|
+
if (form === 'left') return r();
|
|
249
|
+
if (form === 'right') return taken ? (r(), constant) : l;
|
|
250
|
+
return (form === 'or' ? !l : missing) ? r() : l;
|
|
251
|
+
}
|
|
252
|
+
${cover}.apply(null, site[4]);
|
|
253
|
+
var state = scope[scope.Symbol.for('targeted-mutation-ts/session')];
|
|
254
|
+
if (!state || state.phase !== 'baseline') return original();
|
|
255
|
+
// An operation that throws is observed too: an edit that evaluates the same right side throws as well, and any other differs.
|
|
256
|
+
// A sort in place loses the order it started from.
|
|
257
|
+
var value, threw = true, unsorted = op === 'sort' && scope.Array.isArray(l) ? copy(l) : void 0, spreadBase, spreadVals, arg1, arg2, clampVals, clampBuiltins, mergeA, mergeB, mergePure = false, litFn, regexFn, regexProto, qPreOk = false, qPreLen = -1, qPreDense0 = false;
|
|
258
|
+
try {
|
|
259
|
+
if (kind === 'p') {
|
|
260
|
+
spreadBase = { ...l };
|
|
261
|
+
spreadVals = [];
|
|
262
|
+
value = { ...spreadBase };
|
|
263
|
+
var spreadKeys = site[8], sm;
|
|
264
|
+
for (sm = 0; sm < spreadKeys.length; sm++) {
|
|
265
|
+
var sv = r[sm]();
|
|
266
|
+
spreadVals[sm] = sv;
|
|
267
|
+
scope.Object.defineProperty(value, spreadKeys[sm], { value: sv, enumerable: true, writable: true, configurable: true });
|
|
268
|
+
}
|
|
269
|
+
} else if (kind === 'r') {
|
|
270
|
+
var origFn = l[op];
|
|
271
|
+
arg1 = r();
|
|
272
|
+
if ((op === 'every' || op === 'some') && l != null && (!isProxy || !isProxy(l)) && scope.Array.isArray(l)) {
|
|
273
|
+
try {
|
|
274
|
+
var qAlt = op === 'every' ? 'some' : 'every';
|
|
275
|
+
var qProto = scope.Object.getPrototypeOf(l);
|
|
276
|
+
var qOrigCur = safeProtoMethod(scope.Array.prototype, op);
|
|
277
|
+
var qAltCur = safeProtoMethod(scope.Array.prototype, qAlt);
|
|
278
|
+
if (qProto === scope.Array.prototype && qOrigCur && qAltCur && origFn === qOrigCur && qOrigCur === arrMethods[op] && qAltCur === arrMethods[qAlt] &&
|
|
279
|
+
!scope.Object.getOwnPropertyDescriptor(l, op) && !scope.Object.getOwnPropertyDescriptor(l, qAlt)) {
|
|
280
|
+
qPreOk = true;
|
|
281
|
+
qPreLen = l.length;
|
|
282
|
+
qPreDense0 = qPreLen === 1 && scope.Object.prototype.hasOwnProperty.call(l, 0);
|
|
283
|
+
}
|
|
284
|
+
} catch (error) { qPreOk = false; }
|
|
285
|
+
}
|
|
286
|
+
if (site[7] === 3) {
|
|
287
|
+
arg2 = r2();
|
|
288
|
+
value = scope.Reflect.apply(origFn, l, [arg1, arg2]);
|
|
289
|
+
} else {
|
|
290
|
+
value = scope.Reflect.apply(origFn, l, [arg1]);
|
|
291
|
+
}
|
|
292
|
+
} else if (kind === 'k') {
|
|
293
|
+
var cm = site[8], oMath = scope.Math, oFn = oMath[cm.outer], iMath, iFn;
|
|
294
|
+
if (cm.innerPos === 0) {
|
|
295
|
+
iMath = scope.Math;
|
|
296
|
+
iFn = iMath[cm.inner];
|
|
297
|
+
var cv0 = l[0](), cv1 = l[1](), civ = scope.Reflect.apply(iFn, iMath, [cv0, cv1]), cv2 = l[2]();
|
|
298
|
+
clampVals = [cv0, cv1, cv2];
|
|
299
|
+
value = scope.Reflect.apply(oFn, oMath, [civ, cv2]);
|
|
300
|
+
} else {
|
|
301
|
+
var cu0 = l[0]();
|
|
302
|
+
iMath = scope.Math;
|
|
303
|
+
iFn = iMath[cm.inner];
|
|
304
|
+
var cu1 = l[1](), cu2 = l[2](), ciu = scope.Reflect.apply(iFn, iMath, [cu1, cu2]);
|
|
305
|
+
clampVals = [cu0, cu1, cu2];
|
|
306
|
+
value = scope.Reflect.apply(oFn, oMath, [cu0, ciu]);
|
|
307
|
+
}
|
|
308
|
+
clampBuiltins = isNativeFn(oFn) && isNativeFn(iFn);
|
|
309
|
+
} else if (kind === 'u') {
|
|
310
|
+
var litArr = makeArrayWithout(l, -1);
|
|
311
|
+
litFn = litArr.includes;
|
|
312
|
+
arg1 = r();
|
|
313
|
+
value = scope.Reflect.apply(litFn, litArr, [arg1]);
|
|
314
|
+
} else if (kind === 'g') {
|
|
315
|
+
var rawA = l();
|
|
316
|
+
var pureA = isPlainDataObj(rawA);
|
|
317
|
+
mergeA = { ...rawA };
|
|
318
|
+
var rawB = r();
|
|
319
|
+
var pureB = isPlainDataObj(rawB);
|
|
320
|
+
mergeB = { ...rawB };
|
|
321
|
+
mergePure = Boolean(site[8]) && pureA && pureB;
|
|
322
|
+
value = { ...mergeA, ...mergeB };
|
|
323
|
+
} else if (kind === 'x') {
|
|
324
|
+
var origRe = l.orig();
|
|
325
|
+
regexProto = scope.Object.getPrototypeOf(origRe);
|
|
326
|
+
regexFn = origRe.test;
|
|
327
|
+
arg1 = r();
|
|
328
|
+
value = scope.Reflect.apply(regexFn, origRe, [arg1]);
|
|
329
|
+
} else {
|
|
330
|
+
value = original();
|
|
331
|
+
}
|
|
332
|
+
threw = false;
|
|
333
|
+
} finally {
|
|
334
|
+
if (threw) note();
|
|
335
|
+
}
|
|
336
|
+
note();
|
|
337
|
+
return value;
|
|
338
|
+
function note() {
|
|
339
|
+
var seen = ns.observed || (ns.observed = {}), test = ns.currentTestId || '';
|
|
340
|
+
for (k = 0; k < edits.length; k++) {
|
|
341
|
+
var f = edits[k][1], same, other, spreadIdx, spreadKey, spreadHad;
|
|
342
|
+
if (threw) same = logical ? (f !== 'nullish' || missing) : null;
|
|
343
|
+
else if (kind === 'c') same = null;
|
|
344
|
+
else if (kind === 'p') {
|
|
345
|
+
spreadIdx = Number(f.slice(5));
|
|
346
|
+
spreadKey = site[8][spreadIdx];
|
|
347
|
+
spreadHad = scope.Object.prototype.hasOwnProperty.call(spreadBase, spreadKey);
|
|
348
|
+
same = spreadHad && is(spreadBase[spreadKey], spreadVals[spreadIdx])
|
|
349
|
+
? (site[9][spreadIdx] ? true : null)
|
|
350
|
+
: false;
|
|
351
|
+
} else if (kind === 'k') {
|
|
352
|
+
var cmeta = site[8];
|
|
353
|
+
if (!clampBuiltins || typeof clampVals[0] !== 'number' || typeof clampVals[1] !== 'number' || typeof clampVals[2] !== 'number') same = null;
|
|
354
|
+
else {
|
|
355
|
+
var oF = cmeta.outer === 'min' ? numMin : numMax, iF = cmeta.inner === 'min' ? numMin : numMax, altV, droppedIdx;
|
|
356
|
+
if (f === 'dropOuter') {
|
|
357
|
+
altV = cmeta.innerPos === 0 ? iF(clampVals[0], clampVals[1]) : iF(clampVals[1], clampVals[2]);
|
|
358
|
+
droppedIdx = cmeta.innerPos === 0 ? 2 : 0;
|
|
359
|
+
} else {
|
|
360
|
+
var keepA = f === 'dropInnerB';
|
|
361
|
+
altV = cmeta.innerPos === 0 ? oF(keepA ? clampVals[0] : clampVals[1], clampVals[2]) : oF(clampVals[0], keepA ? clampVals[1] : clampVals[2]);
|
|
362
|
+
droppedIdx = cmeta.innerPos === 0 ? (keepA ? 1 : 0) : (keepA ? 2 : 1);
|
|
363
|
+
}
|
|
364
|
+
same = is(value, altV) ? (cmeta.pures[droppedIdx] ? true : null) : false;
|
|
365
|
+
}
|
|
366
|
+
} else if (kind === 'u') {
|
|
367
|
+
var curInc = safeProtoMethod(scope.Array.prototype, 'includes');
|
|
368
|
+
if (!curInc || litFn !== curInc || curInc !== arrMethods.includes) same = null;
|
|
369
|
+
else {
|
|
370
|
+
var skipU = Number(f.slice(6));
|
|
371
|
+
var prunedArr = makeArrayWithout(l, skipU);
|
|
372
|
+
same = is(value, scope.Reflect.apply(curInc, prunedArr, [arg1]));
|
|
373
|
+
}
|
|
374
|
+
} else if (kind === 'g') {
|
|
375
|
+
var swappedObj = { ...mergeB, ...mergeA };
|
|
376
|
+
var origKeys = scope.Reflect.ownKeys(value), swapKeys = scope.Reflect.ownKeys(swappedObj);
|
|
377
|
+
var ki, valDiff = false, orderDiff = origKeys.length !== swapKeys.length;
|
|
378
|
+
for (ki = 0; ki < origKeys.length; ki++) {
|
|
379
|
+
var keyName = origKeys[ki];
|
|
380
|
+
if (!is(value[keyName], swappedObj[keyName])) { valDiff = true; break; }
|
|
381
|
+
if (!orderDiff && swapKeys[ki] !== keyName) orderDiff = true;
|
|
382
|
+
}
|
|
383
|
+
same = valDiff ? false : (orderDiff || !mergePure) ? null : true;
|
|
384
|
+
} else if (kind === 'x') {
|
|
385
|
+
var curTest = regexProto ? safeProtoMethod(regexProto, 'test') : null;
|
|
386
|
+
var curExec = regexProto ? safeProtoMethod(regexProto, 'exec') : null;
|
|
387
|
+
if (typeof arg1 !== 'string' || !curTest || regexFn !== curTest || !isNativeFn(curTest) || !isNativeFn(curExec)) same = null;
|
|
388
|
+
else if (value === true && site[8].pattern.indexOf('|') >= 0) same = null;
|
|
389
|
+
else {
|
|
390
|
+
var iso = isolatedRegexTest(site[8].alts[f], site[8].flags, arg1);
|
|
391
|
+
same = iso === null ? null : is(value, iso);
|
|
392
|
+
}
|
|
393
|
+
} else if (kind === 'e') same = value === edited(f);
|
|
394
|
+
else if (!logical) same = alike(f, value);
|
|
395
|
+
else if (site[5] || f === 'right') {
|
|
396
|
+
// A written right side can be evaluated to compare; the constant of an ignored right side needs no evaluation.
|
|
397
|
+
if (f === 'right' && !taken) same = true;
|
|
398
|
+
else {
|
|
399
|
+
other = f === 'right' ? constant : f === 'left' ? r() : (f === 'or' ? !l : missing) ? r() : l;
|
|
400
|
+
same = site[1] ? !value === !other : is(value, other);
|
|
401
|
+
}
|
|
402
|
+
} else same = f === 'left' ? taken : missing || !!l;
|
|
403
|
+
var entry = seen[edits[k][0]] || (seen[edits[k][0]] = { tests: {} }), counts = entry.tests[test] || (entry.tests[test] = [0, 0, 0]);
|
|
404
|
+
if (kind === 'c' && !threw) {
|
|
405
|
+
if (op === 'copyArray') {
|
|
406
|
+
try { if (scope.Array.isArray(l)) entry.realCopy = true; else entry.nonArray = true; } catch (error) { /* leave unclassified */ }
|
|
407
|
+
} else if (l === null || (typeof l !== 'object' && typeof l !== 'function')) {
|
|
408
|
+
entry.nonPlain = true;
|
|
409
|
+
} else if (!isProxy || !isProxy(l)) {
|
|
410
|
+
try {
|
|
411
|
+
var proto = scope.Object.getPrototypeOf(l);
|
|
412
|
+
if (proto === scope.Object.prototype || proto === null) entry.realCopy = true; else entry.nonPlain = true;
|
|
413
|
+
} catch (error) { /* leave unclassified */ }
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (kind === 's' && op === 'idx0') {
|
|
417
|
+
if (threw) entry.threw = true;
|
|
418
|
+
else if (scope.Array.isArray(l) || typeof l === 'string' || (isTypedArray && isTypedArray(l))) entry.sequence = true;
|
|
419
|
+
else if (l != null) {
|
|
420
|
+
var isSeq = false;
|
|
421
|
+
try {
|
|
422
|
+
if (!isProxy || !isTypedArray || isProxy(l)) isSeq = true;
|
|
423
|
+
else {
|
|
424
|
+
for (var cur = l, depth = 0; cur; depth++) {
|
|
425
|
+
if (depth >= 16 || isProxy(cur)) { isSeq = true; break; }
|
|
426
|
+
var atDesc = scope.Object.getOwnPropertyDescriptor(cur, 'at');
|
|
427
|
+
if (atDesc) { isSeq = 'value' in atDesc ? typeof atDesc.value === 'function' : true; break; }
|
|
428
|
+
cur = scope.Object.getPrototypeOf(cur);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
} catch (error) { isSeq = true; }
|
|
432
|
+
if (isSeq) entry.sequence = true; else entry.nonSequence = true;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
var slot = same === null ? 'unsure' : same ? 'agree' : 'differ';
|
|
436
|
+
counts[same === null ? 2 : same ? 0 : 1]++;
|
|
437
|
+
if (kind === 'p' && !threw && !entry[slot]) {
|
|
438
|
+
entry[slot] = [spreadHad ? show(spreadBase[spreadKey]) : '(missing)', show(spreadVals[spreadIdx])];
|
|
439
|
+
} else if (kind === 'k' && !threw && !entry[slot]) {
|
|
440
|
+
entry[slot] = clampVals.map(show);
|
|
441
|
+
} else if ((kind === 'u' || kind === 'x') && !threw && !entry[slot]) {
|
|
442
|
+
entry[slot] = [show(arg1)];
|
|
443
|
+
} else if (kind === 'g' && !threw && !entry[slot]) {
|
|
444
|
+
entry[slot] = [show(mergeA), show(mergeB)];
|
|
445
|
+
} else if (kind === 'r' && !entry[slot]) {
|
|
446
|
+
entry[slot] = threw ? [show(l)] : [show(l), show(arg1)];
|
|
447
|
+
} else if (kind !== 'c' && kind !== 'p' && kind !== 'r' && kind !== 'k' && kind !== 'u' && kind !== 'g' && kind !== 'x' && !entry[slot]) entry[slot] = (logical ? !taken || threw : site[7] === 1) ? [show(l)] : [show(l), show(logical ? value : r)];
|
|
448
|
+
var type = typeof l;
|
|
449
|
+
if (type === 'number' || type === 'string' || type === 'bigint' || type === 'boolean') entry.typed = true;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
`;
|
|
454
|
+
const outerFirst = { statement: 0, block: 0, case: 0, expression: 1, observed: 2 };
|
|
455
|
+
|
|
456
|
+
function unusedName(text, base) {
|
|
457
|
+
let name = base;
|
|
458
|
+
while (text.includes(name)) name += '_';
|
|
459
|
+
return name;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// A plain edit at the start of a statement must not begin with a token that continues the previous line.
|
|
463
|
+
function asStatementStart(mutant, text) {
|
|
464
|
+
const edited = mutant.mutated.trimStart();
|
|
465
|
+
const risky = (mutant.statement || mutant.leading) && '([`+-/'.includes(edited[0]) && edited[0] !== text[mutant.placement[0]];
|
|
466
|
+
return risky ? ' void 0, ' + edited : mutant.mutated;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export function renderBuiltinSwitches(input, file, selected = () => true, options = {}) {
|
|
470
|
+
const text = input.toString();
|
|
471
|
+
const all = collectMutations(text, file, { ...options, selected });
|
|
472
|
+
const found = options.keep ? all.filter(mutation => options.keep(mutation, text)) : all;
|
|
473
|
+
if (!found.length) return { output: text, mutants: [] };
|
|
474
|
+
const live = found.filter(mutation => mutation.ignored === undefined);
|
|
475
|
+
const bytes = byteOffsets(text);
|
|
476
|
+
const lineStarts = [0];
|
|
477
|
+
for (const match of text.matchAll(/\r\n|[\n\r\u2028\u2029]/g)) lineStarts.push(match.index + match[0].length);
|
|
478
|
+
const lineOf = offset => {
|
|
479
|
+
let line = lineStarts.length - 1;
|
|
480
|
+
while (lineStarts[line] > offset) line--;
|
|
481
|
+
return line;
|
|
482
|
+
};
|
|
483
|
+
const on = unusedName(text, '__tmtOn');
|
|
484
|
+
const cover = unusedName(text, '__tmtCover');
|
|
485
|
+
const shift = unusedName(text, '__tmtShift');
|
|
486
|
+
const see = unusedName(text, '__tmtSee');
|
|
487
|
+
const table = unusedName(text, '__tmtSite');
|
|
488
|
+
const probed = [];
|
|
489
|
+
const identified = mutation => ({ ...mutation,
|
|
490
|
+
id: createHash('sha256').update(JSON.stringify([file, mutation.name, mutation.start, mutation.end, mutation.replacement])).digest('hex').slice(0, 16) });
|
|
491
|
+
const mutants = live.map(identified).map(mutant => mutant.shift === undefined ? mutant
|
|
492
|
+
: { ...mutant, mutated: mutant.mutated.replace(shiftPlaceholder, `${shift}()`) });
|
|
493
|
+
|
|
494
|
+
const groups = new Map();
|
|
495
|
+
for (const mutant of mutants) {
|
|
496
|
+
const key = `${mutant.placement[0]}:${mutant.placement[1]}:${mutant.kind}`;
|
|
497
|
+
if (!groups.has(key)) groups.set(key, { ...mutant, mutants: [] });
|
|
498
|
+
groups.get(key).mutants.push(mutant);
|
|
499
|
+
}
|
|
500
|
+
const ordered = [...groups.values()].sort((a, b) => a.placement[0] - b.placement[0] || b.placement[1] - a.placement[1] ||
|
|
501
|
+
outerFirst[a.kind] - outerFirst[b.kind]);
|
|
502
|
+
let index = 0;
|
|
503
|
+
|
|
504
|
+
function render(start, end) {
|
|
505
|
+
let out = '';
|
|
506
|
+
let from = start;
|
|
507
|
+
while (index < ordered.length && ordered[index].placement[0] < end) {
|
|
508
|
+
const group = ordered[index++];
|
|
509
|
+
const [first, last] = group.placement;
|
|
510
|
+
out += text.slice(from, first);
|
|
511
|
+
const ids = group.mutants.map(mutant => JSON.stringify(mutant.id));
|
|
512
|
+
if (group.kind === 'expression') {
|
|
513
|
+
const inner = render(first, last);
|
|
514
|
+
// An arrow function bound to a variable keeps the variable's name through an inner binding.
|
|
515
|
+
// A property initializer names the function without adding a binding the function body could see.
|
|
516
|
+
const keepName = body => group.named ? `({ ${group.named}: ${body} }).${group.named}` : body;
|
|
517
|
+
const choice = group.mutants.map(mutant => `${on}(${JSON.stringify(mutant.id)}) ? ${keepName(mutant.mutated)} : `).join('') +
|
|
518
|
+
`(${cover}(${ids.join(', ')}), ${keepName(inner)})`;
|
|
519
|
+
// A statement that starts with `(` could continue the previous line as a call.
|
|
520
|
+
// The leading space separates the switch from a keyword such as `do` or `else`.
|
|
521
|
+
out += group.statement ? ` ${choice}` : group.leading ? ` void 0, (${choice})` : `(${choice})`;
|
|
522
|
+
} else if (group.kind === 'observed') {
|
|
523
|
+
// The operands are rendered in source order; what stands between them is the operation the probe performs.
|
|
524
|
+
// Each in parentheses of its own: a probe inside may begin with `void 0,`, which must stay one argument.
|
|
525
|
+
const operands = group.operands.map(([start, end]) => `(${render(start, end)})`);
|
|
526
|
+
const { operator, boolean, thunk, spreadKeys, pureProps, clampMeta, setElems, regexMeta, pureMerge } = group.targeted;
|
|
527
|
+
// Only the edits beside a written null or undefined say on which side it stands.
|
|
528
|
+
const literalSide = group.mutants.find(mutant => mutant.targeted.literalSide !== undefined)?.targeted.literalSide ?? 0;
|
|
529
|
+
const kind = thunk ? 'l' : ['===', '!==', '==', '!='].includes(operator) ? 'e' : operator.length === 1 ? 'a'
|
|
530
|
+
: ['max', 'min', 'floor', 'ceil', 'round', 'trunc'].includes(operator) ? 'm'
|
|
531
|
+
: operator === 'copyArray' || operator === 'copyObject' ? 'c'
|
|
532
|
+
: operator === 'spreadUpdate' ? 'p'
|
|
533
|
+
: operator === 'clamp' ? 'k'
|
|
534
|
+
: operator === 'literalIncludes' ? 'u'
|
|
535
|
+
: operator === 'mergeSpread' ? 'g'
|
|
536
|
+
: operator === 'regexTest' ? 'x'
|
|
537
|
+
: ['startsWith', 'endsWith', 'replace', 'replaceAll', 'every', 'some'].includes(operator) ? 'r' : 's';
|
|
538
|
+
const row = [operator, boolean ? 1 : 0, group.mutants.map(mutant => [mutant.id, mutant.targeted.form]), literalSide,
|
|
539
|
+
group.mutants.map(mutant => mutant.id), group.targeted.pureRight ? 1 : 0, kind, operands.length];
|
|
540
|
+
if (spreadKeys) row.push(spreadKeys, pureProps);
|
|
541
|
+
else if (clampMeta) row.push(clampMeta);
|
|
542
|
+
else if (regexMeta) row.push(regexMeta);
|
|
543
|
+
else if (pureMerge !== undefined) row.push(pureMerge);
|
|
544
|
+
probed.push(row);
|
|
545
|
+
let callArgs;
|
|
546
|
+
if (thunk) {
|
|
547
|
+
operands[1] = `() => ${operands[1]}`;
|
|
548
|
+
callArgs = operands.join(', ');
|
|
549
|
+
} else if (kind === 'p') {
|
|
550
|
+
callArgs = `${operands[0]}, [${operands.slice(1).map(opExpr => `() => ${opExpr}`).join(', ')}]`;
|
|
551
|
+
} else if (kind === 'k') {
|
|
552
|
+
callArgs = `[${operands.map(opExpr => `() => ${opExpr}`).join(', ')}]`;
|
|
553
|
+
} else if (kind === 'u') {
|
|
554
|
+
callArgs = `[${setElems.join(', ')}], () => ${operands[0]}`;
|
|
555
|
+
} else if (kind === 'g') {
|
|
556
|
+
callArgs = `() => ${operands[0]}, () => ${operands[1]}`;
|
|
557
|
+
} else if (kind === 'x') {
|
|
558
|
+
const litEntries = [`orig: () => /${regexMeta.pattern || '(?:)'}/${regexMeta.flags}`];
|
|
559
|
+
for (const [fKey, altPat] of Object.entries(regexMeta.alts)) {
|
|
560
|
+
litEntries.push(`${fKey}: () => /${altPat || '(?:)'}/${regexMeta.flags}`);
|
|
561
|
+
}
|
|
562
|
+
callArgs = `{ ${litEntries.join(', ')} }, () => ${operands[0]}`;
|
|
563
|
+
} else if (kind === 'r') {
|
|
564
|
+
callArgs = `${operands[0]}, ${operands.slice(1).map(opExpr => `() => ${opExpr}`).join(', ')}`;
|
|
565
|
+
} else {
|
|
566
|
+
callArgs = operands.join(', ');
|
|
567
|
+
}
|
|
568
|
+
const call = `${see}(${probed.length - 1}, ${callArgs})`;
|
|
569
|
+
// Nothing inside the expression is left to render: an index the probe works out itself has no switch.
|
|
570
|
+
while (index < ordered.length && ordered[index].placement[0] < last) index++;
|
|
571
|
+
out += group.statement ? ` ${call}` : group.leading ? ` void 0, (${call})` : `(${call})`;
|
|
572
|
+
} else if (group.kind === 'block') {
|
|
573
|
+
// The semicolon ends a directive prologue that relies on a line break.
|
|
574
|
+
// A function body returns early instead of moving into a block, which would change how its declarations are scoped.
|
|
575
|
+
out += group.functionBody
|
|
576
|
+
? `${text.slice(first, group.bodyStart)}; if (${on}(${ids[0]})) return; ${cover}(${ids[0]});` +
|
|
577
|
+
`${render(group.bodyStart, group.bodyEnd)}${text.slice(group.bodyEnd, last)}`
|
|
578
|
+
: `${text.slice(first, group.bodyStart)}; if (${on}(${ids[0]})) {} else { ${cover}(${ids[0]});` +
|
|
579
|
+
`${render(group.bodyStart, group.bodyEnd)}\n} ${text.slice(group.bodyEnd, last)}`;
|
|
580
|
+
} else if (group.kind === 'case') {
|
|
581
|
+
// The case expression can hold switches of its own, so the head is rendered too.
|
|
582
|
+
out += `${render(first, group.bodyStart)} if (${on}(${ids[0]})) {} else { ${cover}(${ids[0]});${render(group.bodyStart, last)}\n}`;
|
|
583
|
+
} else {
|
|
584
|
+
out += ' { ' + group.mutants.map(mutant => `if (${on}(${JSON.stringify(mutant.id)})) { ${mutant.mutated} } else `).join('') +
|
|
585
|
+
`{ ${cover}(${ids.join(', ')}); ${render(first, last)}\n} }`;
|
|
586
|
+
}
|
|
587
|
+
from = last;
|
|
588
|
+
}
|
|
589
|
+
return out + text.slice(from, end);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// A module that binds its own `globalThis` reaches the real global object through an indirect eval.
|
|
593
|
+
const scope = /\bglobalThis\b/.test(text) ? "(0, eval)('this')" : 'globalThis';
|
|
594
|
+
// A file whose mutations are all ignored keeps its source.
|
|
595
|
+
const rendered = mutants.length ? render(0, text.length) : text;
|
|
596
|
+
const output = mutants.length ? rendered + helpers(on, cover, shift, scope) + (probed.length ? probe(see, table, cover, scope, probed) : '') : text;
|
|
597
|
+
// Ignored mutations are reported in source order with the others and get no switch.
|
|
598
|
+
const reported = [...mutants, ...found.filter(mutation => mutation.ignored !== undefined).map(identified)]
|
|
599
|
+
.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
600
|
+
return { output, mutants: reported.map(mutant => {
|
|
601
|
+
const line = lineOf(mutant.start);
|
|
602
|
+
const edit = [bytes[mutant.start], bytes[mutant.end]];
|
|
603
|
+
return {
|
|
604
|
+
id: mutant.id, mutatorName: mutant.name,
|
|
605
|
+
location: { file, line: line + 1, column: mutant.start - lineStarts[line] + 1, start: edit[0], end: edit[1] },
|
|
606
|
+
edit, original: text.slice(mutant.start, mutant.end), replacement: mutant.replacement,
|
|
607
|
+
// The same mutation as a plain source edit, for runners that cannot use switches.
|
|
608
|
+
source: { edit: [bytes[mutant.placement[0]], bytes[mutant.placement[1]]], replacement: asStatementStart(mutant, text) },
|
|
609
|
+
...(mutant.operator ? { operatorSwap: [bytes[mutant.operator[0]], mutant.operator[1]] } : {}),
|
|
610
|
+
...(mutant.targeted ? { targeted: mutant.targeted } : {}),
|
|
611
|
+
...(mutant.ignored === undefined ? {} : { status: 'ignored', statusReason: mutant.ignored }),
|
|
612
|
+
// A shift switch is driven by the focused analysis and is not a mutation of its own.
|
|
613
|
+
...(mutant.shift === undefined ? {} : { parametric: { file, at: bytes[mutant.shift] } }),
|
|
614
|
+
};
|
|
615
|
+
}) };
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// The swaps the focused analysis makes at a comparison boundary: `>=` with `>` and `<=` with `<`.
|
|
619
|
+
export const boundarySwap = (mutation, text) => mutation.shift !== undefined || (mutation.operator !== undefined &&
|
|
620
|
+
['>', '>=', '<', '<='].includes(mutation.operator[1]) && text[mutation.operator[0]] === mutation.operator[1][0]);
|
|
621
|
+
|
|
622
|
+
// The boundary swaps that a `// Stryker disable` comment covers, as `offset:replacement` keys in UTF-8 bytes.
|
|
623
|
+
export function ignoredSwaps(input, file, options = {}) {
|
|
624
|
+
if (!input.includes('Stryker ')) return new Set();
|
|
625
|
+
return new Set(renderBuiltinSwitches(input, file, () => true, { ...options, keep: boundarySwap }).mutants
|
|
626
|
+
.filter(mutant => mutant.status === 'ignored' && mutant.operatorSwap).map(mutant => mutant.operatorSwap.join(':')));
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// The targeted profile: its own rules, and the comparison boundaries the focused analysis asks about.
|
|
630
|
+
export const targetedOrBoundary = (mutation, text) => mutation.targeted !== undefined || boundarySwap(mutation, text);
|
|
631
|
+
|
|
632
|
+
export function prepareBuiltinSwitches(subjects, options = {}) {
|
|
633
|
+
const sources = [];
|
|
634
|
+
const mutants = [];
|
|
635
|
+
for (const { file, input, selected } of subjects) {
|
|
636
|
+
const rendered = renderBuiltinSwitches(input, file, selected, options);
|
|
637
|
+
if (!rendered.mutants.length) continue;
|
|
638
|
+
if (rendered.output !== input.toString()) sources.push({ file, output: rendered.output });
|
|
639
|
+
mutants.push(...rendered.mutants);
|
|
640
|
+
}
|
|
641
|
+
return { sources, mutants };
|
|
642
|
+
}
|