Skip to content

Commit a174001

Browse files
fix(mutation-gate): bind equivalence entries to the exact AST node fingerprint
Review follow-ups: findMatchedMutatorNode returns the matched node, and every registry entry now stores its normalized source text as a node fingerprint that validateEquivalenceEntries compares against the checked-in AST (a same-line edit leaving a different expression invalidates the entry). Added tests for missing/negative start columns and node fingerprint drift; the registry-shape test requires the fingerprint on every shipped entry. All 32 registry entries were regenerated from the checked-in sources. Issue #2060 now documents the gemini.ts exception tracked in #2201. node --test scripts/stryker-diff.test.mjs passes.
1 parent c0bdef1 commit a174001

3 files changed

Lines changed: 141 additions & 39 deletions

File tree

‎scripts/stryker-diff.mjs‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,10 @@ const MUTATOR_NODE_PREDICATES = new Map([
536536
// Mutators whose Stryker start location can point at a sub-expression of the mutated node.
537537
const MUTATOR_SUBELEMENT_LOCATIONS = new Set(["ConditionalExpression", "LogicalOperator"])
538538

539-
function lineHasMutatorNode(source, filePath, line, column, mutator) {
539+
/** The AST node matched for one registry entry's line/column/mutator, or undefined. */
540+
function findMatchedMutatorNode(source, filePath, line, column, mutator) {
540541
const predicate = MUTATOR_NODE_PREDICATES.get(mutator)
541-
if (!predicate) return false
542+
if (!predicate) return undefined
542543
const scriptKind = filePath.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS
543544
const sourceFile = ts.createSourceFile(filePath, source, ts.ScriptTarget.Latest, true, scriptKind)
544545
// Registry lines are Stryker report lines, which are 0-based: the editor shows the code at
@@ -550,7 +551,7 @@ function lineHasMutatorNode(source, filePath, line, column, mutator) {
550551
// candidate roots for the mutator and then match the column against the root or any of its
551552
// expression descendants on that line. Pinning the column stops a same-line edit from
552553
// silently re-binding an accepted equivalence to a different mutant.
553-
let found = false
554+
let matchedNode
554555
// Stryker reports a condition-shaped mutant's location at the mutated sub-expression (for an
555556
// if statement the test, for a logical expression the operand), so only those mutators may
556557
// match a descendant at the recorded column. Every other mutator must have its own node start
@@ -573,15 +574,20 @@ function lineHasMutatorNode(source, filePath, line, column, mutator) {
573574
return childMatch
574575
}
575576
function visit(node) {
576-
if (found) return
577+
if (matchedNode) return
577578
if (predicate(node) && matchesColumn(node)) {
578-
found = true
579+
matchedNode = node
579580
return
580581
}
581582
ts.forEachChild(node, visit)
582583
}
583584
visit(sourceFile)
584-
return found
585+
return matchedNode
586+
}
587+
588+
/** Normalize source text for a stable node fingerprint across whitespace-only edits. */
589+
function normalizeNodeText(text) {
590+
return text.replace(/\s+/g, " ").trim()
585591
}
586592

587593
/**
@@ -605,7 +611,8 @@ export function validateEquivalenceEntries(equivalences, readSource) {
605611
problems.push(entry.file + ":" + entry.line + ": missing start column")
606612
continue
607613
}
608-
if (!lineHasMutatorNode(source, entry.file, entry.line, entry.column, entry.mutator)) {
614+
const matchedNode = findMatchedMutatorNode(source, entry.file, entry.line, entry.column, entry.mutator)
615+
if (!matchedNode) {
609616
problems.push(
610617
entry.file +
611618
":" +
@@ -617,6 +624,23 @@ export function validateEquivalenceEntries(equivalences, readSource) {
617624
" code at line " +
618625
(entry.line + 1),
619626
)
627+
continue
628+
}
629+
// The node fingerprint binds the entry to this exact AST node: a same-line/column refactor
630+
// that leaves a different expression in place invalidates the entry instead of silently
631+
// re-binding the accepted equivalence.
632+
if (typeof entry.node === "string") {
633+
if (normalizeNodeText(matchedNode.getText()) !== normalizeNodeText(entry.node)) {
634+
problems.push(
635+
entry.file +
636+
":" +
637+
entry.line +
638+
":" +
639+
entry.column +
640+
": node fingerprint mismatch for " +
641+
entry.mutator,
642+
)
643+
}
620644
}
621645
}
622646
return problems

‎scripts/stryker-diff.test.mjs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,10 @@ describe("accepted equivalent mutants", () => {
12581258
assert.ok(entry.mutator)
12591259
assert.equal(typeof entry.replacement, "string")
12601260
assert.ok(entry.reason.length > 20)
1261+
// The node fingerprint binds the entry to the exact AST node, so a same-line edit that
1262+
// leaves a different expression cannot silently re-bind the accepted equivalence.
1263+
assert.equal(typeof entry.node, "string")
1264+
assert.ok(entry.node.length > 0)
12611265
}
12621266
})
12631267

@@ -1337,6 +1341,48 @@ describe("accepted equivalent mutants", () => {
13371341
)
13381342
})
13391343

1344+
it("rejects an entry with a missing or negative start column", () => {
1345+
const source = "const a = 1\nconst b = a === 2 ? 3 : 4\n"
1346+
const base = {
1347+
file: "src/example.ts",
1348+
line: 1,
1349+
column: 10,
1350+
mutator: "ConditionalExpression",
1351+
replacement: "true",
1352+
reason: "example",
1353+
}
1354+
1355+
const missing = { ...base }
1356+
delete missing.column
1357+
assert.match(
1358+
validateEquivalenceEntries([missing], () => source).join("\n"),
1359+
/src\/example\.ts:1: missing start column/,
1360+
)
1361+
1362+
const negative = { ...base, column: -1 }
1363+
assert.match(
1364+
validateEquivalenceEntries([negative], () => source).join("\n"),
1365+
/src\/example\.ts:1: missing start column/,
1366+
)
1367+
})
1368+
1369+
it("rejects an entry whose node fingerprint does not match the current AST node", () => {
1370+
const source = "const a = 1\nconst b = a === 2 ? 3 : 4\n"
1371+
const entry = {
1372+
file: "src/example.ts",
1373+
line: 1,
1374+
column: 10,
1375+
mutator: "ConditionalExpression",
1376+
replacement: "true",
1377+
reason: "example",
1378+
node: "a !== 2 ? 3 : 4",
1379+
}
1380+
assert.match(
1381+
validateEquivalenceEntries([entry], () => source).join("\n"),
1382+
/node fingerprint mismatch for ConditionalExpression/,
1383+
)
1384+
})
1385+
13401386
it("validates every shipped registry entry against the checked-in head", () => {
13411387
const equivalences = loadEquivalences(repositoryRoot)
13421388
assert.ok(equivalences.length > 0)

0 commit comments

Comments
 (0)