Skip to content

Commit 39f537e

Browse files
fix(gate): match suppression directives in comment tokens only
CodeRabbit: the line-based regex also matched string literals such as "// Stryker disable all" used in documentation strings, failing CI for files with no actual directive. Scan comment trivia with the TypeScript scanner instead, flag trailing comments too, and document the legacy-directive touch requirement in CONTRIBUTING.md. Tests: node --test scripts/stryker-diff.test.mjs (45/45).
1 parent c717e70 commit 39f537e

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pnpm install
119119
- Follow the ESLint and TypeScript best practices.
120120
- Write clear, descriptive commits that reference issues (for example, `Fixes #123`). Follow the [Writing Style](#writing-style).
121121
- Test your change fully (`npm test`).
122-
- Do not add Stryker suppression comments (`// Stryker disable ...`). The mutation-testing CI check rejects them. Kill surviving mutants with focused tests instead.
122+
- Do not add Stryker suppression comments (`// Stryker disable ...` or `// Stryker enable ...`). The mutation-testing CI check rejects them in any touched mutation-tested file, including directives left by earlier revisions. Remove existing directives when you touch the file. Kill surviving mutants with focused tests instead.
123123
- Rebase onto the latest `main` branch before you submit.
124124

125125
### Submitting a Pull Request

‎scripts/stryker-diff.mjs‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,26 @@ export function executableChangedLines(source, changedLines, filePath = "source.
159159
}
160160

161161
export function assertNoDisableDirectives(source, lineNumbers, filePath) {
162-
const lines = source.split(/\r?\n/)
163-
for (const lineNumber of lineNumbers) {
164-
const line = lines[lineNumber - 1] ?? ""
165-
if (!SUPPRESSION_DIRECTIVE.test(line)) continue
166-
167-
throw new Error(
168-
`${filePath}:${lineNumber}: Stryker suppression comments are not allowed in this repository. ` +
169-
"Kill the mutant with a focused test instead; if the mutant is genuinely equivalent, explain the " +
170-
"equivalence in the pull request and a maintainer will decide on a narrow gate-config exclusion.",
171-
)
162+
// Scan comment trivia only: a string literal such as "// Stryker disable all" documents the
163+
// directive syntax without suppressing anything, so it must not fail the gate.
164+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, false, ts.LanguageVariant.Standard, source)
165+
let token = scanner.scan()
166+
while (token !== ts.SyntaxKind.EndOfFileToken) {
167+
if (token === ts.SyntaxKind.SingleLineCommentTrivia || token === ts.SyntaxKind.MultiLineCommentTrivia) {
168+
const position = scanner.getTokenPos()
169+
const comment = source.slice(position, scanner.getTextPos())
170+
if (SUPPRESSION_DIRECTIVE.test(comment)) {
171+
const lineNumber = (source.slice(0, position).match(/\n/g)?.length ?? 0) + 1
172+
if (lineNumbers.has(lineNumber)) {
173+
throw new Error(
174+
`${filePath}:${lineNumber}: Stryker suppression comments are not allowed in this repository. ` +
175+
"Kill the mutant with a focused test instead; if the mutant is genuinely equivalent, explain the " +
176+
"equivalence in the pull request and a maintainer will decide on a narrow gate-config exclusion.",
177+
)
178+
}
179+
}
180+
}
181+
token = scanner.scan()
172182
}
173183
}
174184

‎scripts/stryker-diff.test.mjs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,32 @@ describe("Stryker suppression directive prohibition", () => {
603603
}
604604
})
605605

606+
it("matches comment tokens only, never string literals", () => {
607+
assert.doesNotThrow(() =>
608+
assertNoDisableDirectives(
609+
'export const guidance = "// Stryker disable all"\nreturn value\n',
610+
new Set([1, 2]),
611+
"source.ts",
612+
),
613+
)
614+
assert.doesNotThrow(() =>
615+
assertNoDisableDirectives(
616+
"const doc = `// Stryker disable next-line all`\nreturn value\n",
617+
new Set([1, 2]),
618+
"source.ts",
619+
),
620+
)
621+
assert.throws(
622+
() =>
623+
assertNoDisableDirectives(
624+
"return value // Stryker disable next-line all: reason\n",
625+
new Set([1]),
626+
"source.ts",
627+
),
628+
/Stryker suppression comments are not allowed/,
629+
)
630+
})
631+
606632
it("accepts sources without suppression comments", () => {
607633
assert.doesNotThrow(() => assertNoDisableDirectives("return value <= limit\n", new Set([1]), "source.ts"))
608634
assert.doesNotThrow(() =>

0 commit comments

Comments
 (0)