Skip to content

Commit b9a23aa

Browse files
fix(mutation-gate): pin equivalence entries to the mutant start column
Review follow-ups: every registry entry now carries the Stryker start column and the gate match key includes it, so a same-line edit cannot silently re-bind an accepted equivalence to a different mutant; validateEquivalenceEntries rejects entries without a column and checks the column against the AST node (including condition operands). Adds a focused loadEquivalences(registry-present) test, cleans up the temp directory in the absent-registry test, and documents the enforced validation in the registry comment. Gate tests 58 passed.
1 parent 17bd94e commit b9a23aa

3 files changed

Lines changed: 157 additions & 53 deletions

File tree

‎scripts/stryker-diff.mjs‎

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,23 +533,40 @@ const MUTATOR_NODE_PREDICATES = new Map([
533533
],
534534
])
535535

536-
function lineHasMutatorNode(source, filePath, line, mutator) {
536+
function lineHasMutatorNode(source, filePath, line, column, mutator) {
537537
const predicate = MUTATOR_NODE_PREDICATES.get(mutator)
538538
if (!predicate) return false
539539
const scriptKind = filePath.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS
540540
const sourceFile = ts.createSourceFile(filePath, source, ts.ScriptTarget.Latest, true, scriptKind)
541541
// Registry lines are Stryker report lines, which are 0-based: the editor shows the code at
542542
// line + 1. Matching the editor line keeps the registry reviewable in a plain editor.
543543
const editorLine = line + 1
544+
// The registry stores Stryker's 0-based start line and column. Stryker's mutant location for
545+
// condition-shaped mutators points at the mutated sub-expression (for an if statement, the
546+
// test; for a logical expression, the operand), not always at the whole node, so collect the
547+
// candidate roots for the mutator and then match the column against the root or any of its
548+
// expression descendants on that line. Pinning the column stops a same-line edit from
549+
// silently re-binding an accepted equivalence to a different mutant.
544550
let found = false
551+
function matchesColumn(node) {
552+
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile))
553+
if (start.line + 1 !== editorLine) return false
554+
if (start.character === column) return true
555+
let childMatch = false
556+
ts.forEachChild(node, (child) => {
557+
if (childMatch) return
558+
// Walk only expression-shaped children: a condition's operands are expressions, while
559+
// the statement bodies a matched node contains are not, so this stays line-scoped.
560+
if (!ts.isExpression(child) && !ts.isIfStatement(child) && !ts.isConditionalExpression(child)) return
561+
if (matchesColumn(child)) childMatch = true
562+
})
563+
return childMatch
564+
}
545565
function visit(node) {
546566
if (found) return
547-
if (predicate(node)) {
548-
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1
549-
if (start === editorLine) {
550-
found = true
551-
return
552-
}
567+
if (predicate(node) && matchesColumn(node)) {
568+
found = true
569+
return
553570
}
554571
ts.forEachChild(node, visit)
555572
}
@@ -574,9 +591,21 @@ export function validateEquivalenceEntries(equivalences, readSource) {
574591
problems.push(entry.file + ":" + entry.line + ": unknown mutator " + entry.mutator)
575592
continue
576593
}
577-
if (!lineHasMutatorNode(source, entry.file, entry.line, entry.mutator)) {
594+
if (!Number.isInteger(entry.column) || entry.column < 0) {
595+
problems.push(entry.file + ":" + entry.line + ": missing start column")
596+
continue
597+
}
598+
if (!lineHasMutatorNode(source, entry.file, entry.line, entry.column, entry.mutator)) {
578599
problems.push(
579-
entry.file + ":" + entry.line + ": expected " + entry.mutator + " code at line " + (entry.line + 1),
600+
entry.file +
601+
":" +
602+
entry.line +
603+
":" +
604+
entry.column +
605+
": expected " +
606+
entry.mutator +
607+
" code at line " +
608+
(entry.line + 1),
580609
)
581610
}
582611
}
@@ -589,7 +618,7 @@ export function applyEquivalences(counts, equivalences, packageEntry, reportedFi
589618
// Matching is count-limited: a compound condition emits several mutants that share the key
590619
// (same line, mutator and replacement, different column), so one registry entry accepts
591620
// exactly one mutant and any further mutant with that key stays blocking.
592-
const entryKey = (entry) => `${entry.file}:${entry.line}:${entry.mutator}:${entry.replacement}`
621+
const entryKey = (entry) => `${entry.file}:${entry.line}:${entry.column}:${entry.mutator}:${entry.replacement}`
593622
const remaining = new Map()
594623
for (const entry of equivalences) {
595624
const key = entryKey(entry)
@@ -599,7 +628,7 @@ export function applyEquivalences(counts, equivalences, packageEntry, reportedFi
599628
const blocking = []
600629
let equivalent = 0
601630
for (const mutant of counts.blocking) {
602-
const key = `${packageEntry.root}/${mutant.filePath}:${mutant.location?.start?.line}:${mutant.mutatorName}:${mutant.replacement}`
631+
const key = `${packageEntry.root}/${mutant.filePath}:${mutant.location?.start?.line}:${mutant.location?.start?.column}:${mutant.mutatorName}:${mutant.replacement}`
603632
const left = remaining.get(key) ?? 0
604633
if (left > 0) {
605634
remaining.set(key, left - 1)

‎scripts/stryker-diff.test.mjs‎

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,13 +1001,13 @@ describe("accepted equivalent mutants", () => {
10011001
status: "Survived",
10021002
mutatorName: "EqualityOperator",
10031003
replacement: "<=",
1004-
location: { start: { line: 45 } },
1004+
location: { start: { line: 45, column: 0 } },
10051005
},
10061006
{
10071007
status: "Survived",
10081008
mutatorName: "ConditionalExpression",
10091009
replacement: "true",
1010-
location: { start: { line: 50 } },
1010+
location: { start: { line: 50, column: 2 } },
10111011
},
10121012
],
10131013
},
@@ -1017,20 +1017,23 @@ describe("accepted equivalent mutants", () => {
10171017
{
10181018
file: "src/utils/pathUtils.ts",
10191019
line: 45,
1020+
column: 0,
10201021
mutator: "EqualityOperator",
10211022
replacement: "<=",
10221023
reason: "defensive bound never hit",
10231024
},
10241025
{
10251026
file: "src/utils/pathUtils.ts",
10261027
line: 99,
1028+
column: 0,
10271029
mutator: "BlockStatement",
10281030
replacement: "{}",
10291031
reason: "entry whose mutant is gone",
10301032
},
10311033
{
10321034
file: "src/other.ts",
10331035
line: 7,
1036+
column: 0,
10341037
mutator: "Foo",
10351038
replacement: "bar",
10361039
reason: "file not part of this run",
@@ -1054,13 +1057,13 @@ describe("accepted equivalent mutants", () => {
10541057
status: "Survived",
10551058
mutatorName: "ConditionalExpression",
10561059
replacement: "true",
1057-
location: { start: { line: 50 } },
1060+
location: { start: { line: 50, column: 2 } },
10581061
},
10591062
{
10601063
status: "Survived",
10611064
mutatorName: "ConditionalExpression",
10621065
replacement: "false",
1063-
location: { start: { line: 50 } },
1066+
location: { start: { line: 50, column: 2 } },
10641067
},
10651068
],
10661069
},
@@ -1070,6 +1073,7 @@ describe("accepted equivalent mutants", () => {
10701073
{
10711074
file: "src/utils/pathUtils.ts",
10721075
line: 50,
1076+
column: 2,
10731077
mutator: "ConditionalExpression",
10741078
replacement: "true",
10751079
reason: "only the true replacement is equivalent here",
@@ -1100,6 +1104,7 @@ describe("accepted equivalent mutants", () => {
11001104
{
11011105
file: "src/utils/pathUtils.ts",
11021106
line: 50,
1107+
column: 1,
11031108
mutator: "ConditionalExpression",
11041109
replacement: "true",
11051110
reason: "the first sub-expression is equivalent here",
@@ -1114,7 +1119,10 @@ describe("accepted equivalent mutants", () => {
11141119
)
11151120
assert.deepEqual(limited.advisories, [])
11161121

1117-
const twoEntries = [...oneEntry, { ...oneEntry[0], reason: "the second sub-expression is also equivalent" }]
1122+
const twoEntries = [
1123+
...oneEntry,
1124+
{ ...oneEntry[0], column: 2, reason: "the second sub-expression is also equivalent" },
1125+
]
11181126
const both = evaluateReport(threeSameKey, packageEntry, twoEntries)
11191127
assert.equal(both.equivalent, 2)
11201128
assert.equal(both.blocking.length, 1)
@@ -1139,6 +1147,7 @@ describe("accepted equivalent mutants", () => {
11391147
const oneEntry = {
11401148
file: "src/utils/pathUtils.ts",
11411149
line: 50,
1150+
column: 1,
11421151
mutator: "ConditionalExpression",
11431152
replacement: "true",
11441153
reason: "only one mutant carries this key",
@@ -1210,8 +1219,31 @@ describe("accepted equivalent mutants", () => {
12101219
}
12111220
})
12121221

1213-
it("loads no equivalences when the registry file is absent", () => {
1214-
assert.deepEqual(loadEquivalences(fs.mkdtempSync(path.join(os.tmpdir(), "stryker-eq-"))), [])
1222+
it("loads no equivalences when the registry file is absent", (t) => {
1223+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "stryker-eq-"))
1224+
t.after(() => fs.rmSync(directory, { recursive: true, force: true }))
1225+
assert.deepEqual(loadEquivalences(directory), [])
1226+
})
1227+
1228+
it("loads entries from a repository registry file", (t) => {
1229+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "stryker-eq-present-"))
1230+
t.after(() => fs.rmSync(directory, { recursive: true, force: true }))
1231+
fs.mkdirSync(path.join(directory, "scripts"), { recursive: true })
1232+
const entry = {
1233+
file: "src/example.ts",
1234+
line: 3,
1235+
column: 2,
1236+
mutator: "ConditionalExpression",
1237+
replacement: "true",
1238+
reason: "example reason that is long enough to be reviewable",
1239+
}
1240+
fs.writeFileSync(
1241+
path.join(directory, "scripts/stryker-equivalences.json"),
1242+
JSON.stringify({ equivalences: [entry] }),
1243+
"utf8",
1244+
)
1245+
1246+
assert.deepEqual(loadEquivalences(directory), [entry])
12151247
})
12161248

12171249
it("ships registry entries with file, line, mutator, and reason", () => {
@@ -1222,6 +1254,7 @@ describe("accepted equivalent mutants", () => {
12221254
for (const entry of registry) {
12231255
assert.match(entry.file, /^(src|packages)\//)
12241256
assert.ok(Number.isInteger(entry.line))
1257+
assert.ok(Number.isInteger(entry.column) && entry.column >= 0)
12251258
assert.ok(entry.mutator)
12261259
assert.equal(typeof entry.replacement, "string")
12271260
assert.ok(entry.reason.length > 20)
@@ -1234,6 +1267,7 @@ describe("accepted equivalent mutants", () => {
12341267
const conditional = {
12351268
file: "src/example.ts",
12361269
line: 1,
1270+
column: 10,
12371271
mutator: "ConditionalExpression",
12381272
replacement: "true",
12391273
reason: "example",
@@ -1246,7 +1280,7 @@ describe("accepted equivalent mutants", () => {
12461280
const driftedObject = { ...conditional, mutator: "ObjectLiteral", line: 1 }
12471281
assert.match(
12481282
validateEquivalenceEntries([driftedObject], readSource).join("\n"),
1249-
/src\/example\.ts:1: expected ObjectLiteral code at line 2/,
1283+
/src\/example\.ts:1:10: expected ObjectLiteral code at line 2/,
12501284
)
12511285

12521286
// ConditionalExpression is a different node kind than the object literal.
@@ -1255,6 +1289,14 @@ describe("accepted equivalent mutants", () => {
12551289
validateEquivalenceEntries([driftedConditional], readSource).join("\n"),
12561290
/expected ConditionalExpression code at line 3/,
12571291
)
1292+
1293+
// A same-line edit that moves the mutated node must invalidate the entry rather than
1294+
// silently accepting whatever mutant now sits on the recorded line.
1295+
const driftedColumn = { ...conditional, column: 11 }
1296+
assert.match(
1297+
validateEquivalenceEntries([driftedColumn], readSource).join("\n"),
1298+
/src\/example\.ts:1:11: expected ConditionalExpression code at line 2/,
1299+
)
12581300
})
12591301

12601302
it("rejects an entry with an unknown mutator or missing file", () => {

0 commit comments

Comments
 (0)