@whaaaley/crush 0.0.0-stage → 0.2.6
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 +32 -0
- package/README.md +185 -2
- package/SKILL.md +187 -0
- package/THIRD_PARTY_NOTICES.md +277 -0
- package/dist/capture/capture.operations.d.ts +23 -0
- package/dist/capture/capture.operations.js +231 -0
- package/dist/capture/capture.schema.d.ts +73 -0
- package/dist/capture/capture.schema.js +28 -0
- package/dist/collapse/collapse.operations.d.ts +2 -0
- package/dist/collapse/collapse.operations.js +82 -0
- package/dist/collapse/collapse.schema.d.ts +40 -0
- package/dist/collapse/collapse.schema.js +35 -0
- package/dist/crush.tables.d.ts +3583 -0
- package/dist/crush.tables.js +456 -0
- package/dist/crushes/callback.crush.d.ts +2 -0
- package/dist/crushes/callback.crush.js +19 -0
- package/dist/crushes/crush.d.ts +36 -0
- package/dist/crushes/crush.js +12 -0
- package/dist/crushes/crush.names.d.ts +1 -0
- package/dist/crushes/crush.names.js +2 -0
- package/dist/crushes/crushes.d.ts +2 -0
- package/dist/crushes/crushes.js +9 -0
- package/dist/crushes/default-value.crush.d.ts +2 -0
- package/dist/crushes/default-value.crush.js +29 -0
- package/dist/crushes/drop-operand.crush.d.ts +2 -0
- package/dist/crushes/drop-operand.crush.js +37 -0
- package/dist/crushes/fold.d.ts +64 -0
- package/dist/crushes/fold.js +366 -0
- package/dist/crushes/force-fork.crush.d.ts +2 -0
- package/dist/crushes/force-fork.crush.js +79 -0
- package/dist/crushes/loop-once.crush.d.ts +2 -0
- package/dist/crushes/loop-once.crush.js +71 -0
- package/dist/crushes/optional-chain.crush.d.ts +2 -0
- package/dist/crushes/optional-chain.crush.js +30 -0
- package/dist/crushes/switch-case.crush.d.ts +2 -0
- package/dist/crushes/switch-case.crush.js +19 -0
- package/dist/decisions/decide.d.ts +61 -0
- package/dist/decisions/decide.js +161 -0
- package/dist/decisions/decisions.d.ts +24 -0
- package/dist/decisions/decisions.js +62 -0
- package/dist/decisions/forks.decision.d.ts +4 -0
- package/dist/decisions/forks.decision.js +32 -0
- package/dist/decisions/kinds.d.ts +101 -0
- package/dist/decisions/kinds.js +38 -0
- package/dist/decisions/loops.decision.d.ts +4 -0
- package/dist/decisions/loops.decision.js +39 -0
- package/dist/decisions/shorts.decision.d.ts +3 -0
- package/dist/decisions/shorts.decision.js +66 -0
- package/dist/decisions/switches.decision.d.ts +3 -0
- package/dist/decisions/switches.decision.js +20 -0
- package/dist/decisions/throws.decision.d.ts +3 -0
- package/dist/decisions/throws.decision.js +35 -0
- package/dist/decisions/values.decision.d.ts +3 -0
- package/dist/decisions/values.decision.js +67 -0
- package/dist/explore/edges/explore.d.ts +60 -0
- package/dist/explore/edges/explore.db.d.ts +22 -0
- package/dist/explore/edges/explore.db.js +55 -0
- package/dist/explore/edges/explore.js +305 -0
- package/dist/explore/edges/random.d.ts +2 -0
- package/dist/explore/edges/random.js +32 -0
- package/dist/explore/explore.operations.d.ts +30 -0
- package/dist/explore/explore.operations.js +117 -0
- package/dist/explore/limits.operations.d.ts +14 -0
- package/dist/explore/limits.operations.js +20 -0
- package/dist/explore/targets.operations.d.ts +36 -0
- package/dist/explore/targets.operations.js +32 -0
- package/dist/explore/utils/draws.d.ts +6 -0
- package/dist/explore/utils/draws.js +2 -0
- package/dist/explore/utils/edits.d.ts +15 -0
- package/dist/explore/utils/edits.js +151 -0
- package/dist/explore/utils/guide.d.ts +23 -0
- package/dist/explore/utils/guide.js +35 -0
- package/dist/explore/utils/keep.d.ts +10 -0
- package/dist/explore/utils/keep.js +61 -0
- package/dist/explore/utils/pools.d.ts +17 -0
- package/dist/explore/utils/pools.js +121 -0
- package/dist/explore/utils/reads.d.ts +19 -0
- package/dist/explore/utils/reads.js +118 -0
- package/dist/explore/utils/twin.d.ts +21 -0
- package/dist/explore/utils/twin.js +55 -0
- package/dist/forks/forks.operations.d.ts +32 -0
- package/dist/forks/forks.operations.js +134 -0
- package/dist/forks/forks.schema.d.ts +20 -0
- package/dist/forks/forks.schema.js +14 -0
- package/dist/graph/cfg/cfg.d.ts +28 -0
- package/dist/graph/cfg/cfg.js +31 -0
- package/dist/graph/cfg/code-paths/analyzer.d.ts +27 -0
- package/dist/graph/cfg/code-paths/analyzer.js +358 -0
- package/dist/graph/cfg/code-paths/segments.d.ts +44 -0
- package/dist/graph/cfg/code-paths/segments.js +105 -0
- package/dist/graph/cfg/code-paths/state.d.ts +121 -0
- package/dist/graph/cfg/code-paths/state.js +611 -0
- package/dist/graph/cfg/entries.d.ts +13 -0
- package/dist/graph/cfg/entries.js +120 -0
- package/dist/graph/cfg/functions.d.ts +6 -0
- package/dist/graph/cfg/functions.js +47 -0
- package/dist/graph/cfg/paths.d.ts +27 -0
- package/dist/graph/cfg/paths.js +91 -0
- package/dist/graph/cfg/scopes/scopes.d.ts +40 -0
- package/dist/graph/cfg/scopes/scopes.js +375 -0
- package/dist/graph/symbols/file.d.ts +90 -0
- package/dist/graph/symbols/file.js +427 -0
- package/dist/graph/symbols/keys.d.ts +13 -0
- package/dist/graph/symbols/keys.js +22 -0
- package/dist/graph/symbols/resolve.operations.d.ts +4 -0
- package/dist/graph/symbols/resolve.operations.js +33 -0
- package/dist/graph/symbols/symbols.d.ts +12 -0
- package/dist/graph/symbols/symbols.js +155 -0
- package/dist/graph/symbols/symbols.schema.d.ts +32 -0
- package/dist/graph/symbols/symbols.schema.js +19 -0
- package/dist/graph/unused/tests.d.ts +7 -0
- package/dist/graph/unused/tests.js +95 -0
- package/dist/graph/unused/unused.d.ts +53 -0
- package/dist/graph/unused/unused.js +319 -0
- package/dist/graph/walk/walk.d.ts +7 -0
- package/dist/graph/walk/walk.js +39 -0
- package/dist/instrument/instrument.operations.d.ts +72 -0
- package/dist/instrument/instrument.operations.js +786 -0
- package/dist/instrument/instrument.schema.d.ts +257 -0
- package/dist/instrument/instrument.schema.js +100 -0
- package/dist/judge/counters.d.ts +86 -0
- package/dist/judge/counters.js +402 -0
- package/dist/judge/edges/api.d.ts +14 -0
- package/dist/judge/edges/api.js +80 -0
- package/dist/judge/edges/completion.reporter.d.ts +9 -0
- package/dist/judge/edges/completion.reporter.js +51 -0
- package/dist/judge/edges/hints.reporter.d.ts +13 -0
- package/dist/judge/edges/hints.reporter.js +29 -0
- package/dist/judge/edges/judge.edges.d.ts +52 -0
- package/dist/judge/edges/judge.edges.js +59 -0
- package/dist/judge/edges/judge.fs.d.ts +8 -0
- package/dist/judge/edges/judge.fs.js +45 -0
- package/dist/judge/edges/merge.d.ts +11 -0
- package/dist/judge/edges/merge.js +60 -0
- package/dist/judge/edges/plugin.d.ts +132 -0
- package/dist/judge/edges/plugin.js +441 -0
- package/dist/judge/edges/reach.d.ts +68 -0
- package/dist/judge/edges/reach.js +132 -0
- package/dist/judge/edges/speed.d.ts +32 -0
- package/dist/judge/edges/speed.js +34 -0
- package/dist/judge/edges/strays.d.ts +1 -0
- package/dist/judge/edges/strays.js +14 -0
- package/dist/judge/edges/vitest.d.ts +20 -0
- package/dist/judge/edges/vitest.js +36 -0
- package/dist/judge/edges/vitest.schema.d.ts +14 -0
- package/dist/judge/edges/vitest.schema.js +16 -0
- package/dist/judge/explore.test.d.ts +1 -0
- package/dist/judge/explore.test.js +101 -0
- package/dist/judge/hints.d.ts +11 -0
- package/dist/judge/hints.js +44 -0
- package/dist/judge/judge.operations.d.ts +218 -0
- package/dist/judge/judge.operations.js +464 -0
- package/dist/judge/judge.schema.d.ts +135 -0
- package/dist/judge/judge.schema.js +129 -0
- package/dist/judge/judge.shard-1.test.d.ts +1 -0
- package/dist/judge/judge.shard-1.test.js +3 -0
- package/dist/judge/judge.shard-2.test.d.ts +1 -0
- package/dist/judge/judge.shard-2.test.js +3 -0
- package/dist/judge/judge.test.d.ts +1 -0
- package/dist/judge/judge.test.js +3 -0
- package/dist/judge/refusals.d.ts +14 -0
- package/dist/judge/refusals.js +10 -0
- package/dist/judge/report.test.d.ts +1 -0
- package/dist/judge/report.test.js +432 -0
- package/dist/judge/runner.d.ts +3 -0
- package/dist/judge/runner.js +1602 -0
- package/dist/judge/stresses.test.d.ts +1 -0
- package/dist/judge/stresses.test.js +12 -0
- package/dist/judge/task-meta.d.ts +51 -0
- package/dist/judge/task-meta.js +3 -0
- package/dist/justify/edges/load.d.ts +7 -0
- package/dist/justify/edges/load.js +40 -0
- package/dist/justify/edges/refresh.fs.d.ts +8 -0
- package/dist/justify/edges/refresh.fs.js +15 -0
- package/dist/justify/justify.operations.d.ts +46 -0
- package/dist/justify/justify.operations.js +80 -0
- package/dist/justify/justify.schema.d.ts +21 -0
- package/dist/justify/justify.schema.js +11 -0
- package/dist/mutators/arithmetic-operator.mutator.d.ts +2 -0
- package/dist/mutators/arithmetic-operator.mutator.js +22 -0
- package/dist/mutators/array-declaration.mutator.d.ts +2 -0
- package/dist/mutators/array-declaration.mutator.js +12 -0
- package/dist/mutators/arrow-function.mutator.d.ts +2 -0
- package/dist/mutators/arrow-function.mutator.js +10 -0
- package/dist/mutators/assignment-operator.mutator.d.ts +2 -0
- package/dist/mutators/assignment-operator.mutator.js +29 -0
- package/dist/mutators/await-expression.mutator.d.ts +2 -0
- package/dist/mutators/await-expression.mutator.js +7 -0
- package/dist/mutators/bitwise-operator.mutator.d.ts +2 -0
- package/dist/mutators/bitwise-operator.mutator.js +19 -0
- package/dist/mutators/block-statement.mutator.d.ts +2 -0
- package/dist/mutators/block-statement.mutator.js +30 -0
- package/dist/mutators/boolean-literal.mutator.d.ts +2 -0
- package/dist/mutators/boolean-literal.mutator.js +5 -0
- package/dist/mutators/coercion.mutator.d.ts +2 -0
- package/dist/mutators/coercion.mutator.js +28 -0
- package/dist/mutators/conditional-expression.mutator.d.ts +2 -0
- package/dist/mutators/conditional-expression.mutator.js +36 -0
- package/dist/mutators/disposal.mutator.d.ts +2 -0
- package/dist/mutators/disposal.mutator.js +13 -0
- package/dist/mutators/empty-destructuring.mutator.d.ts +2 -0
- package/dist/mutators/empty-destructuring.mutator.js +35 -0
- package/dist/mutators/empty-return.mutator.d.ts +2 -0
- package/dist/mutators/empty-return.mutator.js +35 -0
- package/dist/mutators/equality-operator.mutator.d.ts +2 -0
- package/dist/mutators/equality-operator.mutator.js +16 -0
- package/dist/mutators/loop-exit.mutator.d.ts +2 -0
- package/dist/mutators/loop-exit.mutator.js +5 -0
- package/dist/mutators/method-expression.mutator.d.ts +2 -0
- package/dist/mutators/method-expression.mutator.js +59 -0
- package/dist/mutators/missing-lookup.mutator.d.ts +2 -0
- package/dist/mutators/missing-lookup.mutator.js +25 -0
- package/dist/mutators/mutator.d.ts +40 -0
- package/dist/mutators/mutator.js +49 -0
- package/dist/mutators/mutator.names.d.ts +3 -0
- package/dist/mutators/mutator.names.js +43 -0
- package/dist/mutators/mutators.d.ts +8 -0
- package/dist/mutators/mutators.js +84 -0
- package/dist/mutators/nested-condition.mutator.d.ts +2 -0
- package/dist/mutators/nested-condition.mutator.js +16 -0
- package/dist/mutators/number-literal.mutator.d.ts +3 -0
- package/dist/mutators/number-literal.mutator.js +13 -0
- package/dist/mutators/object-literal.mutator.d.ts +2 -0
- package/dist/mutators/object-literal.mutator.js +6 -0
- package/dist/mutators/odd-number.mutator.d.ts +2 -0
- package/dist/mutators/odd-number.mutator.js +15 -0
- package/dist/mutators/operand-deletion.mutator.d.ts +2 -0
- package/dist/mutators/operand-deletion.mutator.js +9 -0
- package/dist/mutators/outcome.mutator.d.ts +3 -0
- package/dist/mutators/outcome.mutator.js +45 -0
- package/dist/mutators/plain-assignment.mutator.d.ts +2 -0
- package/dist/mutators/plain-assignment.mutator.js +7 -0
- package/dist/mutators/power-operator.mutator.d.ts +2 -0
- package/dist/mutators/power-operator.mutator.js +13 -0
- package/dist/mutators/private-brand.mutator.d.ts +2 -0
- package/dist/mutators/private-brand.mutator.js +7 -0
- package/dist/mutators/regex.mutator.d.ts +2 -0
- package/dist/mutators/regex.mutator.js +29 -0
- package/dist/mutators/short-iteration.mutator.d.ts +2 -0
- package/dist/mutators/short-iteration.mutator.js +8 -0
- package/dist/mutators/string-literal.mutator.d.ts +3 -0
- package/dist/mutators/string-literal.mutator.js +149 -0
- package/dist/mutators/switch-default.mutator.d.ts +2 -0
- package/dist/mutators/switch-default.mutator.js +13 -0
- package/dist/mutators/sync-iteration.mutator.d.ts +3 -0
- package/dist/mutators/sync-iteration.mutator.js +9 -0
- package/dist/mutators/thrown-call.mutator.d.ts +2 -0
- package/dist/mutators/thrown-call.mutator.js +7 -0
- package/dist/mutators/unary-operator.mutator.d.ts +2 -0
- package/dist/mutators/unary-operator.mutator.js +18 -0
- package/dist/mutators/unbound-call.mutator.d.ts +2 -0
- package/dist/mutators/unbound-call.mutator.js +14 -0
- package/dist/mutators/update-operator.mutator.d.ts +2 -0
- package/dist/mutators/update-operator.mutator.js +15 -0
- package/dist/mutators/yield-value.mutator.d.ts +2 -0
- package/dist/mutators/yield-value.mutator.js +7 -0
- package/dist/parity/compare.d.ts +27 -0
- package/dist/parity/compare.js +26 -0
- package/dist/parity/corpus.parity.d.ts +1 -0
- package/dist/parity/corpus.parity.js +97 -0
- package/dist/parity/edges/corpus.fs.d.ts +3 -0
- package/dist/parity/edges/corpus.fs.js +30 -0
- package/dist/parity/edges/ports.d.ts +2 -0
- package/dist/parity/edges/ports.js +21 -0
- package/dist/parity/edges/vitest.d.ts +2 -0
- package/dist/parity/edges/vitest.js +3 -0
- package/dist/parity/parity.operations.d.ts +33 -0
- package/dist/parity/parity.operations.js +60 -0
- package/dist/parity/parity.schema.d.ts +5 -0
- package/dist/parity/parity.schema.js +6 -0
- package/dist/parity/stryker/compare.d.ts +14 -0
- package/dist/parity/stryker/compare.js +52 -0
- package/dist/parity/stryker/edges/ports.d.ts +12 -0
- package/dist/parity/stryker/edges/ports.js +28 -0
- package/dist/parity/stryker/equivalence.d.ts +1 -0
- package/dist/parity/stryker/equivalence.js +42 -0
- package/dist/parity/stryker/intended.d.ts +29 -0
- package/dist/parity/stryker/intended.js +152 -0
- package/dist/parity/stryker/normalize.d.ts +27 -0
- package/dist/parity/stryker/normalize.js +181 -0
- package/dist/parity/stryker/rules.d.ts +6 -0
- package/dist/parity/stryker/rules.js +116 -0
- package/dist/parity/stryker/sides.d.ts +46 -0
- package/dist/parity/stryker/sides.js +199 -0
- package/dist/parity/stryker/stryker.parity.d.ts +1 -0
- package/dist/parity/stryker/stryker.parity.js +147 -0
- package/dist/parity/stryker/stryker.schema.d.ts +6 -0
- package/dist/parity/stryker/stryker.schema.js +7 -0
- package/dist/parity/views.d.ts +21 -0
- package/dist/parity/views.js +119 -0
- package/dist/questions/questions.operations.d.ts +110 -0
- package/dist/questions/questions.operations.js +446 -0
- package/dist/questions/questions.schema.d.ts +57 -0
- package/dist/questions/questions.schema.js +28 -0
- package/dist/record/edges/calls.db.d.ts +10 -0
- package/dist/record/edges/calls.db.js +91 -0
- package/dist/record/edges/hits.db.d.ts +3 -0
- package/dist/record/edges/hits.db.js +5 -0
- package/dist/record/edges/holds.db.d.ts +4 -0
- package/dist/record/edges/holds.db.js +31 -0
- package/dist/record/edges/judged-state.db.d.ts +5 -0
- package/dist/record/edges/judged-state.db.js +25 -0
- package/dist/record/edges/markers.db.d.ts +11 -0
- package/dist/record/edges/markers.db.js +55 -0
- package/dist/record/edges/merge.db.d.ts +7 -0
- package/dist/record/edges/merge.db.js +46 -0
- package/dist/record/edges/parts.db.d.ts +42 -0
- package/dist/record/edges/parts.db.js +389 -0
- package/dist/record/edges/parts.edges.d.ts +2 -0
- package/dist/record/edges/parts.edges.js +7 -0
- package/dist/record/edges/parts.fs.d.ts +1 -0
- package/dist/record/edges/parts.fs.js +11 -0
- package/dist/record/edges/parts.writer.d.ts +1 -0
- package/dist/record/edges/parts.writer.js +85 -0
- package/dist/record/edges/passes.db.d.ts +19 -0
- package/dist/record/edges/passes.db.js +31 -0
- package/dist/record/edges/plan.db.d.ts +21 -0
- package/dist/record/edges/plan.db.js +174 -0
- package/dist/record/edges/reach.db.d.ts +6 -0
- package/dist/record/edges/reach.db.js +62 -0
- package/dist/record/edges/record.db.d.ts +22 -0
- package/dist/record/edges/record.db.js +176 -0
- package/dist/record/edges/record.fs.d.ts +3 -0
- package/dist/record/edges/record.fs.js +7 -0
- package/dist/record/edges/site-verdicts.db.d.ts +11 -0
- package/dist/record/edges/site-verdicts.db.js +66 -0
- package/dist/record/edges/verified.db.d.ts +7 -0
- package/dist/record/edges/verified.db.js +71 -0
- package/dist/record/parts.operations.d.ts +26 -0
- package/dist/record/parts.operations.js +31 -0
- package/dist/record/path-checks.operations.d.ts +15 -0
- package/dist/record/path-checks.operations.js +84 -0
- package/dist/record/record.operations.d.ts +36 -0
- package/dist/record/record.operations.js +11 -0
- package/dist/stresses/edges/stresses.record.d.ts +4 -0
- package/dist/stresses/edges/stresses.record.js +37 -0
- package/dist/stresses/stresses.operations.d.ts +28 -0
- package/dist/stresses/stresses.operations.js +22 -0
- package/dist/utils/ast.d.ts +55 -0
- package/dist/utils/ast.js +181 -0
- package/dist/utils/errors.d.ts +8 -0
- package/dist/utils/errors.js +12 -0
- package/dist/utils/faults.d.ts +6 -0
- package/dist/utils/faults.js +10 -0
- package/dist/utils/lookup.d.ts +4 -0
- package/dist/utils/lookup.js +27 -0
- package/dist/utils/order.d.ts +2 -0
- package/dist/utils/order.js +17 -0
- package/dist/utils/retry.d.ts +14 -0
- package/dist/utils/retry.js +15 -0
- package/dist/utils/safe.d.ts +11 -0
- package/dist/utils/safe.js +31 -0
- package/migrations/main/20261129123324_initial/migration.sql +418 -0
- package/migrations/main/20261129123324_initial/snapshot.json +3641 -0
- package/package.json +89 -4
- package/references/consumer-setup.md +92 -0
- package/src/graph/cfg/code-paths/LICENSE +21 -0
- package/src/graph/cfg/scopes/LICENSE +24 -0
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
import { headCloseOf, shapeOf, spacePattern } from '../crushes/fold.js';
|
|
2
|
+
import { crushes } from '../crushes/crushes.js';
|
|
3
|
+
import { forksOf } from '../decisions/decisions.js';
|
|
4
|
+
import { cfgOf } from '../graph/cfg/cfg.js';
|
|
5
|
+
import { strykerMutatorNames } from '../mutators/mutators.js';
|
|
6
|
+
import { outcomeMutatorName, pluginMutatorName } from '../mutators/mutator.names.js';
|
|
7
|
+
import { pluginSitesOf } from '../forks/forks.operations.js';
|
|
8
|
+
import { namedFunctionsIn } from '../graph/cfg/functions.js';
|
|
9
|
+
import { pathsOf } from '../graph/cfg/paths.js';
|
|
10
|
+
import { hashOf, unitKeyOf } from '../record/record.operations.js';
|
|
11
|
+
import { groupedOf } from '../mutators/mutator.js';
|
|
12
|
+
import { entryAt, itemAt, locate, ofType, parse, present, walk } from '../utils/ast.js';
|
|
13
|
+
// A value that is or may hand back a function: a function, a generator among them, or a call such as a wrapper's.
|
|
14
|
+
const isCapturable = (node) => node?.type === 'ArrowFunctionExpression' || node?.type === 'FunctionExpression' || node?.type === 'CallExpression';
|
|
15
|
+
// Each local name an `export { ... }` list exports, with the first name it is exported as.
|
|
16
|
+
const listedOf = (parsed) => {
|
|
17
|
+
const listed = new Map();
|
|
18
|
+
for (const statement of parsed.program.body) {
|
|
19
|
+
if (statement.type !== 'ExportNamedDeclaration' || statement.declaration || statement.source)
|
|
20
|
+
continue;
|
|
21
|
+
for (const { local, exported } of statement.specifiers) {
|
|
22
|
+
const name = exported.type === 'Identifier' ? exported.name : String(exported.value);
|
|
23
|
+
if (local.type === 'Identifier' && !listed.has(local.name))
|
|
24
|
+
listed.set(local.name, name);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return listed;
|
|
28
|
+
};
|
|
29
|
+
// What one top-level declaration exports for capture, under the name `exportedAs` gives each local name it declares, if any.
|
|
30
|
+
const declaredOf = (path, declaration, exportedAs, form) => {
|
|
31
|
+
if (declaration.type === 'FunctionDeclaration') {
|
|
32
|
+
// An anonymous default declaration has no local name, which only the default export reads past.
|
|
33
|
+
const exported = exportedAs(declaration.id ? declaration.id.name : '');
|
|
34
|
+
const { body } = declaration;
|
|
35
|
+
// A generator's body cannot move into an arrow, which cannot yield, so a generator declaration stays uncaptured.
|
|
36
|
+
if (!exported || !body || declaration.generator || (form.isExact && declaration.async))
|
|
37
|
+
return [];
|
|
38
|
+
const key = unitKeyOf(path, exported);
|
|
39
|
+
const opening = `return ${form.body}(${JSON.stringify(key)}, arguments, ${declaration.async ? 'async ' : ''}() => {`;
|
|
40
|
+
const moved = { key, open: body.start + 1, close: body.end - 1, opening, closing: '}) ' };
|
|
41
|
+
if (!declaration.async)
|
|
42
|
+
return [moved];
|
|
43
|
+
// The function hands back the async arrow's own promise, settling when the unwrapped one would, so its own `async` is commented out.
|
|
44
|
+
return [{ key: JSON.stringify([key, 'async']), open: declaration.start, close: declaration.start + 'async'.length, opening: '/* ', closing: ' */' }, moved];
|
|
45
|
+
}
|
|
46
|
+
if (declaration.type !== 'VariableDeclaration')
|
|
47
|
+
return [];
|
|
48
|
+
return declaration.declarations.flatMap(({ id, init }) => {
|
|
49
|
+
if (id.type !== 'Identifier')
|
|
50
|
+
return [];
|
|
51
|
+
const exported = exportedAs(id.name);
|
|
52
|
+
return exported && isCapturable(init) ? [valueOf({ key: unitKeyOf(path, exported), local: id.name, value: init, form })] : [];
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
// A value captured where it is written, wrapped whole; an exact one is first held under its binding's name as a computed key, so a function keeps the name the binding would give it, `__proto__` included, which a plain key would make the object's prototype.
|
|
56
|
+
const valueOf = ({ key, local, value, form }) => {
|
|
57
|
+
const named = form.isExact ? { opening: `{ [${JSON.stringify(local)}]: `, closing: ` }[${JSON.stringify(local)}]` } : { opening: '', closing: '' };
|
|
58
|
+
return { key, open: value.start, close: value.end, opening: `${form.value}(${JSON.stringify(key)}, ${named.opening}`, closing: `${named.closing})` };
|
|
59
|
+
};
|
|
60
|
+
// Every exported function a baseline captures: exported declarations, a default export, and local functions an export list names.
|
|
61
|
+
const capturedOf = (path, parsed, form) => {
|
|
62
|
+
const listed = listedOf(parsed);
|
|
63
|
+
return parsed.program.body.flatMap((statement) => {
|
|
64
|
+
if (statement.type === 'ExportNamedDeclaration')
|
|
65
|
+
return statement.declaration ? declaredOf(path, statement.declaration, (local) => local, form) : [];
|
|
66
|
+
if (statement.type === 'ExportDefaultDeclaration') {
|
|
67
|
+
const { declaration } = statement;
|
|
68
|
+
if (declaration.type === 'FunctionDeclaration')
|
|
69
|
+
return declaredOf(path, declaration, () => 'default', form);
|
|
70
|
+
return isCapturable(declaration) ? [valueOf({ key: unitKeyOf(path, 'default'), local: 'default', value: declaration, form })] : [];
|
|
71
|
+
}
|
|
72
|
+
return declaredOf(path, statement, (local) => listed.get(local), form);
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
// The counter each hook renders through, passing its span's value.
|
|
76
|
+
const hookNames = { case: '__c', default: '__d', of: '__o', in: '__i', value: '__v', optional: '__q', method: '__q', callback: '__f' };
|
|
77
|
+
const isHook = (operator) => Object.hasOwn(hookNames, operator);
|
|
78
|
+
// Every instrumented file imports the counters from one module of the instrumented tree, so the runner and each file share one copy with no global.
|
|
79
|
+
export const preludeOf = (counters, repeat = '__r') => {
|
|
80
|
+
const repeatImport = repeat === '__r' ? '__r' : `__r as ${repeat}`;
|
|
81
|
+
return `import { __t, __n, __l, ${repeatImport}, __s, __w, __c, __d, __o, __i, __v, __q, __f, __m, __e, __x, __b } from ${JSON.stringify(counters)};`;
|
|
82
|
+
};
|
|
83
|
+
const chainParts = (node) => {
|
|
84
|
+
if (node.type === 'MemberExpression') {
|
|
85
|
+
const { base, links } = chainParts(node.object);
|
|
86
|
+
return { base, links: [...links, node] };
|
|
87
|
+
}
|
|
88
|
+
if (node.type === 'CallExpression') {
|
|
89
|
+
const { base, links } = chainParts(node.callee);
|
|
90
|
+
return { base, links: [...links, node] };
|
|
91
|
+
}
|
|
92
|
+
return { base: node, links: [] };
|
|
93
|
+
};
|
|
94
|
+
const spanKeyOf = (start, end) => JSON.stringify([start, end]);
|
|
95
|
+
const wrapsOf = (input) => {
|
|
96
|
+
const { forks, sites, pointBase, siteBase, parsed } = input;
|
|
97
|
+
// Each span gets one wrap, made the first time a point or switch covers it.
|
|
98
|
+
const wraps = new Map();
|
|
99
|
+
const wrapOf = (start, end) => {
|
|
100
|
+
const range = spanKeyOf(start, end);
|
|
101
|
+
const wrap = wraps.get(range) ?? { start, end, points: [], shorts: [], hooks: [], switches: [], chain: null, loop: null, iteration: null, discriminant: null };
|
|
102
|
+
wraps.set(range, wrap);
|
|
103
|
+
return wrap;
|
|
104
|
+
};
|
|
105
|
+
// A labeled loop's wrap starts at its first label, so a block around it leaves `continue label` naming the loop itself.
|
|
106
|
+
const labelStarts = new Map();
|
|
107
|
+
walk(parsed.program, ({ node }) => {
|
|
108
|
+
if (node.type !== 'LabeledStatement')
|
|
109
|
+
return;
|
|
110
|
+
let body = node.body;
|
|
111
|
+
while (body.type === 'LabeledStatement')
|
|
112
|
+
body = body.body;
|
|
113
|
+
if (!labelStarts.has(body))
|
|
114
|
+
labelStarts.set(body, node.start);
|
|
115
|
+
});
|
|
116
|
+
for (const [offset, { probe, node }] of forks.entries()) {
|
|
117
|
+
const index = pointBase + offset;
|
|
118
|
+
// A while's or for's statement is wrapped too, so each entry of the loop counts its own passes.
|
|
119
|
+
if (node.type === 'WhileStatement')
|
|
120
|
+
wrapOf(node.start, node.end).loop = { isFor: false, index, testStart: probe.start, testEnd: probe.end, bodyStart: node.body.start };
|
|
121
|
+
if (node.type === 'ForStatement') {
|
|
122
|
+
const start = labelStarts.get(node) ?? node.start;
|
|
123
|
+
wrapOf(start, node.end).loop = { isFor: true, index, testStart: probe.start, testEnd: probe.end, bodyStart: node.body.start };
|
|
124
|
+
}
|
|
125
|
+
if (node.type === 'ForInStatement' || (node.type === 'ForOfStatement' && !node.await)) {
|
|
126
|
+
const start = labelStarts.get(node) ?? node.start;
|
|
127
|
+
wrapOf(start, node.end).iteration = { index, bodyStart: node.body.start };
|
|
128
|
+
}
|
|
129
|
+
const wrap = wrapOf(probe.start, probe.end);
|
|
130
|
+
const { operator } = probe;
|
|
131
|
+
if (isHook(operator))
|
|
132
|
+
wrap.hooks.push({ index, operator, discriminant: probe.discriminant, leftEnd: probe.leftEnd });
|
|
133
|
+
else if (operator)
|
|
134
|
+
wrap.shorts.push({ index, probe });
|
|
135
|
+
else
|
|
136
|
+
wrap.points.push(index);
|
|
137
|
+
}
|
|
138
|
+
walk(parsed.program, ({ node }) => {
|
|
139
|
+
// A chain holding a second optional link renders link by link, so each link short-circuits the rest of its own chain alone.
|
|
140
|
+
if (node.type === 'ChainExpression') {
|
|
141
|
+
// Only an optional link's object or callee carries a hook, and only that link's own, so the last hook found is the chain's terminal.
|
|
142
|
+
let hasOptional = false;
|
|
143
|
+
let prefix = null;
|
|
144
|
+
for (const part of chainParts(node.expression).links) {
|
|
145
|
+
const operand = part.type === 'CallExpression' ? part.callee : part.object;
|
|
146
|
+
const range = spanKeyOf(operand.start, operand.end);
|
|
147
|
+
const [terminal] = wraps.get(range)?.hooks ?? [];
|
|
148
|
+
if (hasOptional && terminal)
|
|
149
|
+
prefix = { range, node: operand, terminal };
|
|
150
|
+
hasOptional ||= part.optional;
|
|
151
|
+
}
|
|
152
|
+
if (prefix)
|
|
153
|
+
entryAt(wraps, prefix.range, 'the wrap of a chain prefix').chain = { node: prefix.node, terminal: prefix.terminal };
|
|
154
|
+
}
|
|
155
|
+
if (node.type !== 'SwitchStatement')
|
|
156
|
+
return;
|
|
157
|
+
// A switch's discriminant is read once into a name every case compares, so a case never reads it again.
|
|
158
|
+
const hooks = node.cases.flatMap((item) => {
|
|
159
|
+
const span = item.test ?? { start: item.start, end: item.start + 'default'.length };
|
|
160
|
+
const wrap = wraps.get(spanKeyOf(span.start, span.end));
|
|
161
|
+
return wrap ? wrap.hooks : [];
|
|
162
|
+
});
|
|
163
|
+
if (!hooks.length)
|
|
164
|
+
return;
|
|
165
|
+
const name = `__w${node.start}`;
|
|
166
|
+
for (const hook of hooks)
|
|
167
|
+
hook.discriminant = name;
|
|
168
|
+
wrapOf(node.start, node.end).discriminant = { start: node.discriminant.start, end: node.discriminant.end, name };
|
|
169
|
+
});
|
|
170
|
+
for (const [offset, site] of sites.entries())
|
|
171
|
+
wrapOf(site.start, site.end).switches.push({ index: siteBase + offset, ...site });
|
|
172
|
+
return [...wraps.values()].sort((left, right) => left.start - right.start || right.end - left.end);
|
|
173
|
+
};
|
|
174
|
+
// A `default` written last becomes a case the counters match whenever it is reached, so its keyword is replaced rather than wrapped.
|
|
175
|
+
const hookOf = (hook, out) => {
|
|
176
|
+
const { index, operator, discriminant } = hook;
|
|
177
|
+
const name = hookNames[operator];
|
|
178
|
+
if (operator === 'default')
|
|
179
|
+
return `case ${name}(${index}, (${discriminant}))`;
|
|
180
|
+
if (operator === 'case')
|
|
181
|
+
return `${name}(${index}, (${discriminant}), (${out}))`;
|
|
182
|
+
if (operator === 'method')
|
|
183
|
+
return `((__r) => { const __m = __q(${index}, __r[1]); return __m === undefined ? undefined : (...__a) => globalThis.Reflect.apply(__m, __r[0], __a) })(${out})`;
|
|
184
|
+
return `${name}(${index}, (${out}))`;
|
|
185
|
+
};
|
|
186
|
+
// Matches a call, a template, an update, an assignment or a suspension, any of which a replacement run beside its original could run twice.
|
|
187
|
+
// A shift assignment such as `<<=` ends in what reads as a comparison, so a lone `<` or `>` before `=` is a comparison only when no second one precedes it.
|
|
188
|
+
const sideEffectPattern = /[(`]|\+\+|--|(?<![=!]|(?<![<>])[<>])=(?!=)|\b(?:await|yield|delete|new)\b/;
|
|
189
|
+
// Refuse to weigh dotted or bracketed code. A property read can execute a getter or proxy trap.
|
|
190
|
+
const propertyReadPattern = /\.|\[/;
|
|
191
|
+
const canWeigh = (code) => !sideEffectPattern.test(code) && !propertyReadPattern.test(code);
|
|
192
|
+
const apply = ({ wrap, body, written, opensStatement, path }) => {
|
|
193
|
+
let out = body;
|
|
194
|
+
// A forced test is never read, as the removal that forces it leaves no test to read.
|
|
195
|
+
for (const index of wrap.points)
|
|
196
|
+
out = `(__n(${index}) ?? __t(${index}, (${out})))`;
|
|
197
|
+
const expressions = wrap.switches.filter((item) => item.kind === 'expression');
|
|
198
|
+
// An empty wrap is a `for` loop's missing test, which reads as true when no switch is on.
|
|
199
|
+
const original = out || 'true';
|
|
200
|
+
const switched = expressions.map((item) => `__s(${item.index}) ? ${groupedOf(item.replacement)} : `).join('');
|
|
201
|
+
// Either form can do work the other skips, so both must pass the syntax check before the replacement can run beside the original.
|
|
202
|
+
const weighed = canWeigh(written) ? expressions.filter(({ replacement }) => canWeigh(replacement)) : [];
|
|
203
|
+
const indices = weighed.map((item) => item.index).join(', ');
|
|
204
|
+
const replacements = weighed.map(({ replacement }) => groupedOf(replacement)).join(', ');
|
|
205
|
+
const value = weighed.length ? `__w([${indices}], () => [${replacements}], (${original}))` : `(${original})`;
|
|
206
|
+
if (expressions.length)
|
|
207
|
+
out = `(${switched}${value})`;
|
|
208
|
+
// A chain renders its own terminal, the only hook its wrap holds.
|
|
209
|
+
for (const hook of wrap.chain ? [] : wrap.hooks)
|
|
210
|
+
out = hookOf(hook, out);
|
|
211
|
+
for (const item of wrap.switches) {
|
|
212
|
+
if (item.kind === 'disposal') {
|
|
213
|
+
const { data: reparsed, error: reparseError } = parse(out, path);
|
|
214
|
+
if (reparseError)
|
|
215
|
+
throw reparseError;
|
|
216
|
+
const declaration = itemAt(reparsed.program.body, 0);
|
|
217
|
+
if (declaration.type !== 'VariableDeclaration')
|
|
218
|
+
throw new Error('a disposal site must cover a declaration');
|
|
219
|
+
// Each binding stays in its own scope, while a hidden binding of the declaration's kind holds the resource that disposes it, left undefined while the switch is on.
|
|
220
|
+
out = declaration.declarations
|
|
221
|
+
.map((declarator, position) => {
|
|
222
|
+
const { id } = declarator;
|
|
223
|
+
const name = out.slice(id.start, id.typeAnnotation ? id.typeAnnotation.start : id.end);
|
|
224
|
+
return `const ${out.slice(declarator.start, declarator.end)}; ${declaration.kind} __dispose${item.index}_${position} = __s(${item.index}) ? undefined : ${name};`;
|
|
225
|
+
})
|
|
226
|
+
.join(' ');
|
|
227
|
+
}
|
|
228
|
+
if (item.kind === 'block')
|
|
229
|
+
out = `{ if (!__s(${item.index})) ${out} }`;
|
|
230
|
+
// A statement with a replacement runs it in place of the statement, as a `break` turned `return`.
|
|
231
|
+
if (item.kind === 'statements')
|
|
232
|
+
out = `if (!__s(${item.index})) { ${out} }${item.replacement ? ` else { ${item.replacement} }` : ''}`;
|
|
233
|
+
}
|
|
234
|
+
return opensStatement && out.startsWith('(') ? `0, ${out}` : out;
|
|
235
|
+
};
|
|
236
|
+
// A logical assignment reads its target again to write it, writing where its short-circuit would run its right side.
|
|
237
|
+
const assignBody = ({ index, probe }, left, right) => {
|
|
238
|
+
const decision = probe.operator === '??=' ? `(${left}) != null` : `(${left})`;
|
|
239
|
+
const write = `(${left} = (${right}))`;
|
|
240
|
+
const [onTrue, onFalse] = probe.operator === '&&=' ? [write, `(${left})`] : [`(${left})`, write];
|
|
241
|
+
return `(__t(${index}, ${decision}) ? ${onTrue} : ${onFalse})`;
|
|
242
|
+
};
|
|
243
|
+
// A computed target's object, key and value are read once before the right side runs, so recursion cannot replace the captured target.
|
|
244
|
+
const computedAssignBody = ({ index, probe }, object, property, right) => {
|
|
245
|
+
const kept = `__k[${index}]`;
|
|
246
|
+
const value = `${kept}[2]`;
|
|
247
|
+
const decision = probe.operator === '??=' ? `${value} != null` : value;
|
|
248
|
+
const target = object === 'super' ? `super[${kept}[1]]` : `${kept}[0][${kept}[1]]`;
|
|
249
|
+
const write = `(${target} = (${right}))`;
|
|
250
|
+
const [onTrue, onFalse] = probe.operator === '&&=' ? [write, value] : [value, write];
|
|
251
|
+
const key = `globalThis.Reflect.ownKeys({ [${property}]: 0 })[0]`;
|
|
252
|
+
const capture = object === 'super' ? `((__p) => [this, __p, super[__p]])(${key})` : `((__o, __p) => [__o, __p, __o[__p]])((${object}), ${key})`;
|
|
253
|
+
return `(${kept} = ${capture}, __t(${index}, ${decision}) ? ${onTrue} : ${onFalse})`;
|
|
254
|
+
};
|
|
255
|
+
// A short-circuit's true side is its left side holding, truthy under `&&` and `||` and present under `??`; `&&` runs its right side there, `||` and `??` return the left value.
|
|
256
|
+
const shortBody = (short, left, right) => {
|
|
257
|
+
const { index, probe } = short;
|
|
258
|
+
if (probe.operator.endsWith('='))
|
|
259
|
+
return assignBody(short, left, right);
|
|
260
|
+
const kept = `__k[${index}]`;
|
|
261
|
+
const decision = probe.operator === '??' ? `${kept} != null` : kept;
|
|
262
|
+
const [onTrue, onFalse] = probe.operator === '&&' ? [`(${right})`, kept] : [kept, `(${right})`];
|
|
263
|
+
return `(${kept} = (${left}), __t(${index}, ${decision}) ? ${onTrue} : ${onFalse})`;
|
|
264
|
+
};
|
|
265
|
+
// Matches the `?.` opening an optional call's arguments, with any space before it.
|
|
266
|
+
const optionalCallPattern = /^\s*\?\./;
|
|
267
|
+
// How much of a source stays ahead of the prelude.
|
|
268
|
+
// A pragma such as `/** @jsx h */` counts only among a file's leading comments, so those stay ahead of it.
|
|
269
|
+
// A decorator can open the file ahead of the statement it decorates, so the head ends at the first text no comment or space covers rather than at the first statement.
|
|
270
|
+
const headLengthOf = (source, parsed) => {
|
|
271
|
+
const commentEnds = new Map(parsed.comments.map(({ start, end }) => [start, end]));
|
|
272
|
+
let headLength = parsed.program.hashbang ? parsed.program.hashbang.end : 0;
|
|
273
|
+
for (let end = commentEnds.get(headLength); end !== undefined || spacePattern.test(source.charAt(headLength)); end = commentEnds.get(headLength))
|
|
274
|
+
headLength = end ?? headLength + 1;
|
|
275
|
+
return headLength;
|
|
276
|
+
};
|
|
277
|
+
// One file's source with a counter on every fork side and a switch on every mutation site, both answered by the counters module it imports.
|
|
278
|
+
export const instrumented = (input) => {
|
|
279
|
+
const { source, parsed, forks } = input;
|
|
280
|
+
const statements = new Set();
|
|
281
|
+
const names = new Set();
|
|
282
|
+
walk(parsed.program, ({ node }) => {
|
|
283
|
+
if (node.type === 'ExpressionStatement')
|
|
284
|
+
statements.add(node.start);
|
|
285
|
+
if (node.type === 'Identifier')
|
|
286
|
+
names.add(node.name);
|
|
287
|
+
});
|
|
288
|
+
const unusedNameOf = (start) => {
|
|
289
|
+
let name = start;
|
|
290
|
+
while (names.has(name))
|
|
291
|
+
name += '_';
|
|
292
|
+
return name;
|
|
293
|
+
};
|
|
294
|
+
const repeat = unusedNameOf('__r');
|
|
295
|
+
// Temporaries for a chain's links number on from the forks, so none shares a slot of `__k` a fork uses.
|
|
296
|
+
let nextTemporary = input.pointBase + forks.length;
|
|
297
|
+
// Each entry opens in the first stretch of source reaching its offset, and an expression body closes only in the render that opened it, so a wrap ending where the body ends closes inside it.
|
|
298
|
+
const openedIn = new Map();
|
|
299
|
+
const closed = new Set();
|
|
300
|
+
let renders = 0;
|
|
301
|
+
const captured = capturedOf(input.path, parsed, { value: '__x', body: '__b', isExact: false });
|
|
302
|
+
const capturedIn = new Map();
|
|
303
|
+
const capturedClosed = new Set();
|
|
304
|
+
const sliceOf = (from, to, frame) => {
|
|
305
|
+
const marks = [];
|
|
306
|
+
for (const { index, open, close } of input.entries) {
|
|
307
|
+
if (!openedIn.has(index) && from <= open && open <= to) {
|
|
308
|
+
openedIn.set(index, frame);
|
|
309
|
+
marks.push({ at: open, text: close === null ? `__e(${index});` : `(__e(${index}), ` });
|
|
310
|
+
}
|
|
311
|
+
const isClosing = close !== null && openedIn.get(index) === frame && !closed.has(index) && from <= close && close <= to;
|
|
312
|
+
if (isClosing) {
|
|
313
|
+
closed.add(index);
|
|
314
|
+
marks.push({ at: close, text: ')' });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// An exported function's capture wrap opens and closes in one render, as an expression body's mark does, so a switch replacing the whole function stays inside it.
|
|
318
|
+
for (const { key, open, close, opening, closing } of captured) {
|
|
319
|
+
if (!capturedIn.has(key) && from <= open && open <= to) {
|
|
320
|
+
capturedIn.set(key, frame);
|
|
321
|
+
marks.push({ at: open, text: opening });
|
|
322
|
+
}
|
|
323
|
+
if (capturedIn.get(key) === frame && !capturedClosed.has(key) && from <= close && close <= to) {
|
|
324
|
+
capturedClosed.add(key);
|
|
325
|
+
marks.push({ at: close, text: closing });
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const parts = [];
|
|
329
|
+
let cursor = from;
|
|
330
|
+
for (const { at, text } of marks.toSorted((left, right) => left.at - right.at)) {
|
|
331
|
+
parts.push(source.slice(cursor, at), text);
|
|
332
|
+
cursor = at;
|
|
333
|
+
}
|
|
334
|
+
parts.push(source.slice(cursor, to));
|
|
335
|
+
return parts.join('');
|
|
336
|
+
};
|
|
337
|
+
const render = (from, to, wraps) => {
|
|
338
|
+
const frame = renders++;
|
|
339
|
+
const parts = [];
|
|
340
|
+
let cursor = from;
|
|
341
|
+
let next = 1;
|
|
342
|
+
let wrap = wraps[0];
|
|
343
|
+
while (wrap) {
|
|
344
|
+
const inner = [];
|
|
345
|
+
let nested = wraps[next];
|
|
346
|
+
while (nested && nested.start < wrap.end) {
|
|
347
|
+
inner.push(nested);
|
|
348
|
+
nested = wraps[++next];
|
|
349
|
+
}
|
|
350
|
+
const opensStatement = statements.has(wrap.start) && wrap.start !== from;
|
|
351
|
+
parts.push(sliceOf(cursor, wrap.start, frame), apply({ wrap, body: bodyOf(wrap, inner), written: source.slice(wrap.start, wrap.end), opensStatement, path: parsed.path }));
|
|
352
|
+
cursor = wrap.end;
|
|
353
|
+
wrap = wraps[next++];
|
|
354
|
+
}
|
|
355
|
+
parts.push(sliceOf(cursor, to, frame));
|
|
356
|
+
return parts.join('');
|
|
357
|
+
};
|
|
358
|
+
// One logical expression fills a span, so a wrap holds at most one short-circuit, whose sides render apart.
|
|
359
|
+
const bodyOf = (wrap, inner) => {
|
|
360
|
+
if (wrap.discriminant) {
|
|
361
|
+
const { start, end, name } = wrap.discriminant;
|
|
362
|
+
const value = render(start, end, inner.filter((item) => item.end <= end));
|
|
363
|
+
const after = render(end, wrap.end, inner.filter((item) => item.end > end));
|
|
364
|
+
return `{ const ${name} = (${value}); ${source.slice(wrap.start, start)}${name}${after} }`;
|
|
365
|
+
}
|
|
366
|
+
if (wrap.chain)
|
|
367
|
+
return chainBody(wrap.chain, wrap, inner);
|
|
368
|
+
if (wrap.iteration) {
|
|
369
|
+
const { index, bodyStart } = wrap.iteration;
|
|
370
|
+
const pass = unusedNameOf(`__p${index}`);
|
|
371
|
+
const head = render(wrap.start, bodyStart, inner.filter((item) => item.start < bodyStart));
|
|
372
|
+
const body = render(bodyStart, wrap.end, inner.filter((item) => item.start >= bodyStart));
|
|
373
|
+
return `{ let ${pass} = 0; ${head}{ ${repeat}(${index}, ++${pass}); ${body} } }`;
|
|
374
|
+
}
|
|
375
|
+
if (wrap.loop) {
|
|
376
|
+
// A while becomes a for counting the passes of this entry, which a label, break or continue reads as the same loop, and `__l` ends before a second pass when the loop is forced to one.
|
|
377
|
+
// A for keeps its own head inside a block declaring its counter, fresh on each entry, its labels inside the block with it.
|
|
378
|
+
const { isFor, index, testStart, testEnd, bodyStart } = wrap.loop;
|
|
379
|
+
const pass = unusedNameOf(`__p${index}`);
|
|
380
|
+
const test = render(testStart, testEnd, inner.filter((item) => item.start >= testStart && item.end <= testEnd));
|
|
381
|
+
const counted = `__l(${index}, ${pass}++) && (${test}) && ${repeat}(${index}, ${pass})`;
|
|
382
|
+
if (isFor) {
|
|
383
|
+
const head = render(wrap.start, testStart, inner.filter((item) => item.start < testStart));
|
|
384
|
+
const rest = render(testEnd, wrap.end, inner.filter((item) => item.end > testEnd));
|
|
385
|
+
return `{ let ${pass} = 0; ${head}${counted}${rest} }`;
|
|
386
|
+
}
|
|
387
|
+
const close = headCloseOf({ source, comments: parsed.comments, testEnd, bodyStart });
|
|
388
|
+
const rest = render(close, wrap.end, inner.filter((item) => item.start >= close));
|
|
389
|
+
return `for (let ${pass} = 0; ${counted}; )${rest}`;
|
|
390
|
+
}
|
|
391
|
+
const method = wrap.hooks.find((hook) => hook.operator === 'method');
|
|
392
|
+
if (method) {
|
|
393
|
+
const end = method.leftEnd;
|
|
394
|
+
const object = render(wrap.start, end, inner.filter((item) => item.end <= end));
|
|
395
|
+
const member = render(end, wrap.end, inner.filter((item) => item.end > end));
|
|
396
|
+
if (object === 'super')
|
|
397
|
+
return `[this, super${member}]`;
|
|
398
|
+
return `[(__k[${method.index}] = (${object})), __k[${method.index}]${member}]`;
|
|
399
|
+
}
|
|
400
|
+
const [short] = wrap.shorts;
|
|
401
|
+
if (!short)
|
|
402
|
+
return render(wrap.start, wrap.end, inner);
|
|
403
|
+
const { leftStart, leftEnd, rightStart, rightEnd, reference } = short.probe;
|
|
404
|
+
const right = render(rightStart, rightEnd, inner.filter((item) => item.start >= rightStart));
|
|
405
|
+
if (reference) {
|
|
406
|
+
const object = render(reference.objectStart, reference.objectEnd, inner.filter((item) => item.end <= reference.objectEnd));
|
|
407
|
+
const property = render(reference.propertyStart, reference.propertyEnd, inner.filter((item) => item.start >= reference.propertyStart && item.end <= reference.propertyEnd));
|
|
408
|
+
return computedAssignBody(short, object, property, right);
|
|
409
|
+
}
|
|
410
|
+
// No wrap starts on the operator between the sides, so one test sorts each inner wrap to its side.
|
|
411
|
+
const left = render(leftStart, leftEnd, inner.filter((item) => item.start < rightStart));
|
|
412
|
+
return shortBody(short, left, right);
|
|
413
|
+
};
|
|
414
|
+
// A chain of several optional links, each link short-circuiting the rest, with the last native optional operation left in place so delete, tags and later calls keep their reference.
|
|
415
|
+
const chainBody = ({ node, terminal }, wrap, inner) => {
|
|
416
|
+
const { base, links } = chainParts(node);
|
|
417
|
+
const consumed = new Set();
|
|
418
|
+
const wrapsBySpan = new Map([wrap, ...inner].map((item) => [spanKeyOf(item.start, item.end), item]));
|
|
419
|
+
// Each link's object or callee holds at most its own optional or method hook, and an optional link always has one, since every optional member and call is a decision.
|
|
420
|
+
const hooks = links.map((part) => {
|
|
421
|
+
const at = part.type === 'MemberExpression' ? part.object : part.callee;
|
|
422
|
+
const [hook] = wrapsBySpan.get(spanKeyOf(at.start, at.end))?.hooks ?? [];
|
|
423
|
+
consumed.add(hook);
|
|
424
|
+
return hook;
|
|
425
|
+
});
|
|
426
|
+
const remaining = inner.map((item) => ({ ...item, hooks: item.hooks.filter((hook) => !consumed.has(hook)) }));
|
|
427
|
+
const fragment = (start, end) => {
|
|
428
|
+
return render(start, end, remaining.filter((item) => item.start >= start && item.end <= end));
|
|
429
|
+
};
|
|
430
|
+
const bind = (reference, hook) => {
|
|
431
|
+
const kept = `__k[${nextTemporary++}]`;
|
|
432
|
+
const pair = reference.object === 'super' ? `[this, super${reference.member}]` : `[(${kept} = (${reference.object})), ${kept}${reference.member}]`;
|
|
433
|
+
return hookOf(hook, pair);
|
|
434
|
+
};
|
|
435
|
+
const guard = (checked, follow) => {
|
|
436
|
+
const kept = `__k[${nextTemporary++}]`;
|
|
437
|
+
return `(${kept} = (${checked}), ${kept} == null ? undefined : ${follow(kept)})`;
|
|
438
|
+
};
|
|
439
|
+
const step = (position, value, reference) => {
|
|
440
|
+
const part = links[position];
|
|
441
|
+
if (!part)
|
|
442
|
+
return terminal.operator === 'method' && reference ? bind(reference, terminal) : hookOf(terminal, value);
|
|
443
|
+
const hook = hooks[position];
|
|
444
|
+
if (part.type === 'MemberExpression') {
|
|
445
|
+
const property = fragment(part.property.start, part.property.end);
|
|
446
|
+
const member = part.computed ? `[${property}]` : `.${property}`;
|
|
447
|
+
const follow = (object) => step(position + 1, `${object}${member}`, { object, member });
|
|
448
|
+
return part.optional ? guard(hookOf(present(hook), value), follow) : follow(value);
|
|
449
|
+
}
|
|
450
|
+
const args = fragment(part.callee.end, part.end).replace(optionalCallPattern, '');
|
|
451
|
+
if (!part.optional)
|
|
452
|
+
return step(position + 1, `${value}${args}`, null);
|
|
453
|
+
const callee = reference ? bind(reference, present(hook)) : hookOf(present(hook), value);
|
|
454
|
+
return guard(callee, (kept) => step(position + 1, `${kept}${args}`, null));
|
|
455
|
+
};
|
|
456
|
+
return step(0, fragment(base.start, base.end), null);
|
|
457
|
+
};
|
|
458
|
+
const headLength = headLengthOf(source, parsed);
|
|
459
|
+
// `__k` holds a value between reading it and handing it on, so forcing a side changes only the decision and never reads it twice.
|
|
460
|
+
return `${source.slice(0, headLength)}${preludeOf(input.counters, repeat)}__m(${JSON.stringify(input.path)});const __k = [];${render(headLength, source.length, wrapsOf(input))}`;
|
|
461
|
+
};
|
|
462
|
+
// Every function of a source with a body, each a unit of its own, since a function made in one call can run in another without its maker being entered.
|
|
463
|
+
// A function goes by the name the project gives it, or else by its maker's name and its place among the maker's unnamed functions, the top level's as `<top>`.
|
|
464
|
+
const unitsIn = (source, parsed, paths) => {
|
|
465
|
+
const named = new Map(namedFunctionsIn(source, parsed, paths).map(({ name, node }) => [node, name]));
|
|
466
|
+
const byNode = new Map();
|
|
467
|
+
const counts = new Map();
|
|
468
|
+
walk(parsed.program, ({ node, ancestors }) => {
|
|
469
|
+
if (node.type !== 'FunctionDeclaration' && node.type !== 'FunctionExpression' && node.type !== 'ArrowFunctionExpression')
|
|
470
|
+
return;
|
|
471
|
+
const { body } = node;
|
|
472
|
+
if (!body)
|
|
473
|
+
return;
|
|
474
|
+
const [maker] = ancestors.flatMap((ancestor) => byNode.get(ancestor) ?? []).toReversed();
|
|
475
|
+
const base = maker?.name ?? '<top>';
|
|
476
|
+
const count = counts.get(base) ?? 0;
|
|
477
|
+
counts.set(base, count + 1);
|
|
478
|
+
byNode.set(node, { name: named.get(node) ?? `${base}/${count}`, start: node.start, end: node.end, body, parent: maker ?? null });
|
|
479
|
+
});
|
|
480
|
+
return [...byNode.values()];
|
|
481
|
+
};
|
|
482
|
+
const outlined = (source, from, to, inner) => {
|
|
483
|
+
const parts = [];
|
|
484
|
+
let cursor = from;
|
|
485
|
+
for (const { text, start, end } of inner) {
|
|
486
|
+
parts.push(source.slice(cursor, start), text);
|
|
487
|
+
cursor = end;
|
|
488
|
+
}
|
|
489
|
+
parts.push(source.slice(cursor, to));
|
|
490
|
+
return parts.join('');
|
|
491
|
+
};
|
|
492
|
+
// A source's units with their hashes: first its top level, which runs as the source loads, then each function, each with the functions it makes left out.
|
|
493
|
+
export const unitsOf = (source, parsed, paths, unitBase, edits = []) => {
|
|
494
|
+
const found = unitsIn(source, parsed, paths);
|
|
495
|
+
const childrenOf = Map.groupBy(found, ({ parent }) => parent);
|
|
496
|
+
const textOf = (unit) => {
|
|
497
|
+
const from = unit?.start ?? 0;
|
|
498
|
+
const to = unit?.end ?? source.length;
|
|
499
|
+
const children = childrenOf.get(unit) ?? [];
|
|
500
|
+
const spans = children.map(({ start, end, name }) => ({ start, end, text: `<${name}>` }));
|
|
501
|
+
for (const edit of edits) {
|
|
502
|
+
if (edit.start < from || edit.end > to)
|
|
503
|
+
continue;
|
|
504
|
+
if (children.some(({ start, end }) => start <= edit.start && edit.end <= end))
|
|
505
|
+
continue;
|
|
506
|
+
spans.push(edit);
|
|
507
|
+
}
|
|
508
|
+
return outlined(source, from, to, spans.sort((left, right) => left.start - right.start));
|
|
509
|
+
};
|
|
510
|
+
const units = [{ index: unitBase, name: '', hash: hashOf(textOf(null)) }, ...found.map((unit, offset) => ({ index: unitBase + offset + 1, name: unit.name, hash: hashOf(textOf(unit)) }))];
|
|
511
|
+
const entries = found.map(({ body }, offset) => {
|
|
512
|
+
const index = unitBase + offset + 1;
|
|
513
|
+
return body.type === 'BlockStatement' ? { index, open: body.start + 1, close: null } : { index, open: body.start, close: body.end };
|
|
514
|
+
});
|
|
515
|
+
return { units, entries };
|
|
516
|
+
};
|
|
517
|
+
// A test file's code with every function marking itself entered and the file marking itself loaded, through counters imported under names of crush's own, since a test of the counters imports their usual names itself.
|
|
518
|
+
// A function nested in another closes its mark first where both end at once.
|
|
519
|
+
// An exported function's capture wrap holds its entry mark, so it opens before and closes after every mark inside it, which keeps the calls a test makes to a property it declares for explore.
|
|
520
|
+
const markedTestOf = (path, source, parsed, entries, counters, justificationMarks) => {
|
|
521
|
+
const capturedMarks = capturedOf(path, parsed, { value: '__crushCaptured', body: '__crushCapturedBody', isExact: true }).flatMap(({ open, close, opening, closing }) => [
|
|
522
|
+
{ at: open, order: -1, text: opening },
|
|
523
|
+
{ at: close, order: -open, text: closing },
|
|
524
|
+
]);
|
|
525
|
+
const marks = entries
|
|
526
|
+
.flatMap(({ index, open, close }) => [{ at: open, order: 0, text: close === null ? `__crushEntered(${index});` : `(__crushEntered(${index}), ` }, ...(close === null ? [] : [{ at: close, order: -open, text: ')' }])])
|
|
527
|
+
.concat(capturedMarks);
|
|
528
|
+
for (const { start, end } of justificationMarks) {
|
|
529
|
+
marks.push({ at: start, order: 0, text: '__crushJustifies(' }, { at: end, order: 0, text: ')' });
|
|
530
|
+
}
|
|
531
|
+
marks.sort((left, right) => left.at - right.at || left.order - right.order);
|
|
532
|
+
const headLength = headLengthOf(source, parsed);
|
|
533
|
+
const justificationImport = justificationMarks.length ? ', __j as __crushJustifies' : '';
|
|
534
|
+
const imported = `import { __e as __crushEntered, __m as __crushLoaded, __x as __crushCaptured, __b as __crushCapturedBody${justificationImport} } from ${JSON.stringify(counters)};`;
|
|
535
|
+
const parts = [source.slice(0, headLength), `${imported}__crushLoaded(${JSON.stringify(path)});`];
|
|
536
|
+
let cursor = headLength;
|
|
537
|
+
for (const { at, text } of marks) {
|
|
538
|
+
parts.push(source.slice(cursor, at), text);
|
|
539
|
+
cursor = at;
|
|
540
|
+
}
|
|
541
|
+
parts.push(source.slice(cursor));
|
|
542
|
+
return parts.join('');
|
|
543
|
+
};
|
|
544
|
+
const namedProperty = (node, name) => {
|
|
545
|
+
if (node?.type !== 'Property' || node.computed)
|
|
546
|
+
return false;
|
|
547
|
+
return (node.key.type === 'Identifier' && node.key.name === name) || (node.key.type === 'Literal' && node.key.value === name);
|
|
548
|
+
};
|
|
549
|
+
const literalEntries = (node) => {
|
|
550
|
+
return node.type === 'ArrayExpression' && node.elements.every((entry) => entry?.type === 'ObjectExpression' && entry.properties.every((field) => field.type === 'Property' && !field.computed && field.value.type === 'Literal'));
|
|
551
|
+
};
|
|
552
|
+
const describesOf = (parsed, { references, definitions }) => {
|
|
553
|
+
const imported = new Set();
|
|
554
|
+
for (const statement of parsed.program.body) {
|
|
555
|
+
if (statement.type !== 'ImportDeclaration' || (statement.source.value !== 'vite-plus/test' && statement.source.value !== 'vitest'))
|
|
556
|
+
continue;
|
|
557
|
+
for (const specifier of statement.specifiers) {
|
|
558
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && specifier.imported.name === 'describe')
|
|
559
|
+
imported.add(specifier);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
const bound = new Map(references.map(({ identifier, variable }) => [identifier, definitions.get(variable)]));
|
|
563
|
+
return (callee) => {
|
|
564
|
+
const definition = bound.get(callee);
|
|
565
|
+
return definition === undefined || imported.has(definition);
|
|
566
|
+
};
|
|
567
|
+
};
|
|
568
|
+
const suiteMetadataOf = (source, parsed, paths) => {
|
|
569
|
+
const edits = [];
|
|
570
|
+
const marks = [];
|
|
571
|
+
if (!source.includes('describe'))
|
|
572
|
+
return { edits, marks };
|
|
573
|
+
let isSuite;
|
|
574
|
+
walk(parsed.program, ({ node }) => {
|
|
575
|
+
if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier' || node.callee.name !== 'describe')
|
|
576
|
+
return;
|
|
577
|
+
const [title, options, body] = node.arguments;
|
|
578
|
+
if (!title || !options)
|
|
579
|
+
return;
|
|
580
|
+
if (node.arguments.length !== 3 || !body || options.type !== 'ObjectExpression')
|
|
581
|
+
return;
|
|
582
|
+
if (options.properties.some((property) => property.type !== 'Property' || property.computed))
|
|
583
|
+
return;
|
|
584
|
+
const meta = options.properties.find((property) => namedProperty(property, 'meta'));
|
|
585
|
+
if (meta?.type !== 'Property' || meta.value.type !== 'ObjectExpression')
|
|
586
|
+
return;
|
|
587
|
+
const metadata = meta.value;
|
|
588
|
+
if (metadata.properties.some((property) => property.type !== 'Property' || property.computed))
|
|
589
|
+
return;
|
|
590
|
+
const justification = metadata.properties.find((property) => namedProperty(property, 'justifies'));
|
|
591
|
+
if (justification?.type !== 'Property' || !literalEntries(justification.value))
|
|
592
|
+
return;
|
|
593
|
+
isSuite ??= describesOf(parsed, paths);
|
|
594
|
+
if (!isSuite(node.callee))
|
|
595
|
+
return;
|
|
596
|
+
marks.push(justification.value);
|
|
597
|
+
edits.push({ start: justification.value.start, end: justification.value.end, text: '[]' });
|
|
598
|
+
});
|
|
599
|
+
return { edits, marks };
|
|
600
|
+
};
|
|
601
|
+
// The meta fields a test states for crush's own questions, which no verdict reads.
|
|
602
|
+
const questionMetaNames = ['steps', 'stepsReason', 'answers'];
|
|
603
|
+
// Blanks each literal steps, stepsReason or answers value in a test's own meta, so editing one stales no verdict that rests on the test.
|
|
604
|
+
const testMetadataEditsOf = (source, parsed) => {
|
|
605
|
+
const edits = [];
|
|
606
|
+
if (!source.includes('meta'))
|
|
607
|
+
return edits;
|
|
608
|
+
walk(parsed.program, ({ node }) => {
|
|
609
|
+
if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier' || (node.callee.name !== 'it' && node.callee.name !== 'test'))
|
|
610
|
+
return;
|
|
611
|
+
const [, options] = node.arguments;
|
|
612
|
+
if (options?.type !== 'ObjectExpression')
|
|
613
|
+
return;
|
|
614
|
+
const meta = options.properties.find((property) => namedProperty(property, 'meta'));
|
|
615
|
+
if (meta?.type !== 'Property' || meta.value.type !== 'ObjectExpression')
|
|
616
|
+
return;
|
|
617
|
+
for (const property of meta.value.properties) {
|
|
618
|
+
if (property.type !== 'Property' || property.value.type !== 'Literal')
|
|
619
|
+
continue;
|
|
620
|
+
if (questionMetaNames.some((name) => namedProperty(property, name)))
|
|
621
|
+
edits.push({ start: property.value.start, end: property.value.end, text: 'null' });
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
return edits;
|
|
625
|
+
};
|
|
626
|
+
// One test file's code with every function marking itself entered, and its part of the plan, so a verdict rests on the bodies its witnesses ran rather than the whole file.
|
|
627
|
+
// The scan comes back too, so the whole-project graph reads the file's scopes without walking it again.
|
|
628
|
+
export const planTestOf = ({ path, hash, source, parsed, unitBase, counters, listeners = [] }) => {
|
|
629
|
+
const { data: paths, error } = pathsOf(parsed, listeners);
|
|
630
|
+
if (error)
|
|
631
|
+
return { data: null, error };
|
|
632
|
+
const { edits: suiteEdits, marks } = suiteMetadataOf(source, parsed, paths);
|
|
633
|
+
const edits = [...suiteEdits, ...testMetadataEditsOf(source, parsed)].toSorted((a, b) => a.start - b.start);
|
|
634
|
+
const { units, entries } = unitsOf(source, parsed, paths, unitBase, edits);
|
|
635
|
+
const metadataHash = marks.length ? hashOf(outlined(source, 0, source.length, edits)) : undefined;
|
|
636
|
+
const code = markedTestOf(path, source, parsed, entries, counters, marks);
|
|
637
|
+
return { data: { code, plan: { path, hash, metadataHash, units }, paths }, error: null };
|
|
638
|
+
};
|
|
639
|
+
// Matches a character a regular expression reads as more than itself.
|
|
640
|
+
export const regexSpecialPattern = /[.+?^${}()|[\]\\]/g;
|
|
641
|
+
// Whether a name is one a list names, each entry a name or a pattern whose `*` stands for any run of characters.
|
|
642
|
+
const namedBy = (names) => {
|
|
643
|
+
const patterns = names.map((entry) => new RegExp(`^${entry.replace(regexSpecialPattern, '\\$&').replaceAll('*', '.*')}$`));
|
|
644
|
+
return (name) => patterns.some((pattern) => pattern.test(name));
|
|
645
|
+
};
|
|
646
|
+
// The spans the settings leave without a site or crush: every call to an excluded function, or to a name bound to what one returns, as `const card = cva(...)` binds `card`, and the value of every excluded property.
|
|
647
|
+
const excludedSpansOf = (program, { excludedCalls, excludedProps }) => {
|
|
648
|
+
const isExcludedCall = namedBy(excludedCalls);
|
|
649
|
+
const isExcludedProp = namedBy(excludedProps);
|
|
650
|
+
const bound = new Set();
|
|
651
|
+
walk(program, ({ node }) => {
|
|
652
|
+
if (node.type !== 'VariableDeclarator' || node.id.type !== 'Identifier' || node.init?.type !== 'CallExpression')
|
|
653
|
+
return;
|
|
654
|
+
if (node.init.callee.type === 'Identifier' && isExcludedCall(node.init.callee.name))
|
|
655
|
+
bound.add(node.id.name);
|
|
656
|
+
});
|
|
657
|
+
const spans = [];
|
|
658
|
+
walk(program, ({ node }) => {
|
|
659
|
+
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && (isExcludedCall(node.callee.name) || bound.has(node.callee.name)))
|
|
660
|
+
spans.push(node);
|
|
661
|
+
if (node.type !== 'Property' || node.computed)
|
|
662
|
+
return;
|
|
663
|
+
const key = node.key.type === 'Identifier' ? node.key.name : node.key.type === 'Literal' ? String(node.key.value) : '';
|
|
664
|
+
if (isExcludedProp(key))
|
|
665
|
+
spans.push(node.value);
|
|
666
|
+
});
|
|
667
|
+
return spans;
|
|
668
|
+
};
|
|
669
|
+
// One source's instrumented code and its part of the plan: a counter per fork, a switch per mutator site, and each crush judged by forcing its fork's point.
|
|
670
|
+
// A ConditionalExpression mutant forcing a fork's own span to true or false is the force-fork crush already, so it is left out.
|
|
671
|
+
// Each site's key names its kind, file, function, code and replacement, and which occurrence of all that it is, never a line.
|
|
672
|
+
// The scan comes back too, so the whole-project graph reads the file's scopes without walking it again.
|
|
673
|
+
export const planFileOf = (input) => {
|
|
674
|
+
const { path, hash, source, parsed, project, forkPlugins, mutation, pointBase, siteBase, unitBase, counters, listeners } = input;
|
|
675
|
+
const { data: cfg, error: broken } = cfgOf({ source, parsed, project, listeners });
|
|
676
|
+
if (broken)
|
|
677
|
+
return { data: null, error: broken };
|
|
678
|
+
const { data: removals, error: refusal } = pluginSitesOf({ path, source, parsed, project, bindings: cfg.bindings, forks: forkPlugins });
|
|
679
|
+
if (refusal)
|
|
680
|
+
return { data: null, error: refusal };
|
|
681
|
+
// Each fork plugin's removal of a call is planned as one more site, under the plugin mutator's name and the functions holding the call.
|
|
682
|
+
const pluginSites = removals.map((site) => Object.assign(site, { mutator: pluginMutatorName, owner: cfg.ownerOf(site.start) }));
|
|
683
|
+
const at = locate(source);
|
|
684
|
+
const excludedSpans = excludedSpansOf(parsed.program, mutation);
|
|
685
|
+
const isInExcluded = ({ start, end }) => excludedSpans.some((span) => start >= span.start && end <= span.end);
|
|
686
|
+
// A fork inside a span the settings exclude decides nothing a test should pin, so it gets no counter and no side counts toward cov.
|
|
687
|
+
const forks = forksOf(cfg.decisions).filter(({ node }) => !isInExcluded(node));
|
|
688
|
+
// A fork's plain test or operand, as a per-item callback's answer, and the left side of an `&&` or `||` are what force-fork and drop-operand force to a boolean, so a ConditionalExpression mutant doing the same there repeats their crush.
|
|
689
|
+
// Forcing a `??` left side to a boolean gives that boolean, which no crush does, so that mutant stays.
|
|
690
|
+
const forcedSpanOf = ({ kind, node, probe }) => {
|
|
691
|
+
if (kind !== 'short')
|
|
692
|
+
return probe.operator === '' ? probe : null;
|
|
693
|
+
const short = ofType(node, 'LogicalExpression');
|
|
694
|
+
return short.operator === '??' ? null : short.left;
|
|
695
|
+
};
|
|
696
|
+
const forcedSpans = new Set(forks.flatMap((fork) => forcedSpanOf(fork) ?? []).map(({ start, end }) => spanKeyOf(start, end)));
|
|
697
|
+
const isOverlap = (site) => site.mutator === 'ConditionalExpression' && forcedSpans.has(spanKeyOf(site.start, site.end)) && (site.replacement === 'true' || site.replacement === 'false');
|
|
698
|
+
// A site whose replacement is the code it replaces changes nothing, so no test could ever catch it.
|
|
699
|
+
const isIdentical = (site) => source.slice(site.start, site.end) === site.replacement;
|
|
700
|
+
// The question names the plugin's declared removal while the switch runs the rendered expression.
|
|
701
|
+
const shownOf = new Map(pluginSites.map((site) => [site, site.shown]));
|
|
702
|
+
const excludedMutators = new Set([...mutation.excludedMutators, ...(mutation.outcomes ? [] : [outcomeMutatorName])]);
|
|
703
|
+
const left = { excluded: 0, overlap: 0 };
|
|
704
|
+
// An excluded mutator of crush's own counts nowhere, so the excluded count stays StrykerJS's and the spans'.
|
|
705
|
+
const kept = [...cfg.sites, ...pluginSites].filter((site) => {
|
|
706
|
+
if (isIdentical(site))
|
|
707
|
+
return false;
|
|
708
|
+
if (excludedMutators.has(site.mutator) && !strykerMutatorNames.has(site.mutator))
|
|
709
|
+
return false;
|
|
710
|
+
if (excludedMutators.has(site.mutator) || isInExcluded(site)) {
|
|
711
|
+
left.excluded += 1;
|
|
712
|
+
return false;
|
|
713
|
+
}
|
|
714
|
+
if (!isOverlap(site))
|
|
715
|
+
return true;
|
|
716
|
+
left.overlap += 1;
|
|
717
|
+
return false;
|
|
718
|
+
});
|
|
719
|
+
// An edit one of crush's own mutators makes that StrykerJS's or an earlier own one already makes is judged once, under the first.
|
|
720
|
+
// An outcome mutant always stands apart, since it asks what the function gives rather than whether a test tells one edit apart.
|
|
721
|
+
const editOf = ({ start, end, replacement }) => JSON.stringify([start, end, replacement]);
|
|
722
|
+
const made = new Set(kept.filter(({ mutator }) => strykerMutatorNames.has(mutator)).map(editOf));
|
|
723
|
+
const sites = kept.filter((site) => {
|
|
724
|
+
if (strykerMutatorNames.has(site.mutator) || site.mutator === outcomeMutatorName)
|
|
725
|
+
return true;
|
|
726
|
+
const edit = editOf(site);
|
|
727
|
+
if (!made.has(edit)) {
|
|
728
|
+
made.add(edit);
|
|
729
|
+
return true;
|
|
730
|
+
}
|
|
731
|
+
left.overlap += 1;
|
|
732
|
+
return false;
|
|
733
|
+
});
|
|
734
|
+
const seen = new Map();
|
|
735
|
+
const keyOf = (parts) => {
|
|
736
|
+
const base = JSON.stringify(parts);
|
|
737
|
+
const occurrence = seen.get(base) ?? 0;
|
|
738
|
+
seen.set(base, occurrence + 1);
|
|
739
|
+
return JSON.stringify([...parts, occurrence]);
|
|
740
|
+
};
|
|
741
|
+
const points = forks.map((fork, offset) => ({ index: pointBase + offset, decision: fork.id, kind: fork.kind }));
|
|
742
|
+
const pointOf = new Map(points.map((point) => [point.decision, point]));
|
|
743
|
+
const forkOf = new Map(forks.map((fork) => [fork.id, fork]));
|
|
744
|
+
const switches = sites.map((site, offset) => {
|
|
745
|
+
const code = source.slice(site.start, site.end);
|
|
746
|
+
const shown = shownOf.get(site) ?? site.replacement;
|
|
747
|
+
const replacement = site.kind === 'expression' ? groupedOf(shown) : shown;
|
|
748
|
+
const key = keyOf([site.mutator, path, site.owner, code, site.replacement]);
|
|
749
|
+
return { index: siteBase + offset, key, mutator: site.mutator, owner: site.owner, code, replacement, start: site.start, line: at(site.start).line };
|
|
750
|
+
});
|
|
751
|
+
const shape = shapeOf(source, parsed);
|
|
752
|
+
// A crush inside a span the settings exclude decides nothing a test should pin, as a class picked inside `cva(...)`, so it is left out with the sites there.
|
|
753
|
+
const crushSites = crushes.flatMap((crush) => crush.sitesIn({ source, parsed, forks, shape }).flatMap((site) => (isInExcluded(site) ? [] : [{ crush: crush.name, site }])));
|
|
754
|
+
// Each site carries what its removal forces, so every site is judged, a while run once included.
|
|
755
|
+
const judged = crushSites.map(({ crush, site }) => {
|
|
756
|
+
const point = entryAt(pointOf, site.decision, `the fork of crush ${crush}`);
|
|
757
|
+
const code = source.slice(site.start, site.end);
|
|
758
|
+
// A plain test or a braced callback forced to a value is never run, so the work its code does is skipped.
|
|
759
|
+
const { kind, owner, probe } = entryAt(forkOf, site.decision, `the fork of crush ${crush}`);
|
|
760
|
+
const isUnread = probe.operator === '' || probe.operator === 'callback';
|
|
761
|
+
const skips = isUnread && site.forced !== 'once' && sideEffectPattern.test(source.slice(probe.start, probe.end));
|
|
762
|
+
const forcedEffect = crush === 'force-fork' && (kind === 'short' || kind === 'logicalAssign')
|
|
763
|
+
? { kind, code: kind === 'short' ? source.slice(probe.start, probe.end) : code, always: kind === 'short' ? site.forced === (probe.operator === '&&') : site.edits.some(({ text }) => text === '=') }
|
|
764
|
+
: undefined;
|
|
765
|
+
return {
|
|
766
|
+
key: keyOf([crush, path, owner, code, site.side]),
|
|
767
|
+
crush,
|
|
768
|
+
decision: site.decision,
|
|
769
|
+
side: site.side,
|
|
770
|
+
point: point.index,
|
|
771
|
+
start: site.start,
|
|
772
|
+
end: site.end,
|
|
773
|
+
forced: site.forced,
|
|
774
|
+
skips,
|
|
775
|
+
forcedEffect,
|
|
776
|
+
owner,
|
|
777
|
+
code,
|
|
778
|
+
edits: site.edits,
|
|
779
|
+
dedents: site.dedents,
|
|
780
|
+
line: at(site.start).line,
|
|
781
|
+
};
|
|
782
|
+
});
|
|
783
|
+
const { units, entries } = unitsOf(source, parsed, cfg.paths, unitBase);
|
|
784
|
+
const code = instrumented({ path, source, parsed, forks, sites, entries, pointBase, siteBase, counters });
|
|
785
|
+
return { data: { code, plan: { path, hash, points, switches, crushes: judged, units, left }, cfg }, error: null };
|
|
786
|
+
};
|