From cbd90f0eb7b17509ffd0f10f326757d39beb8b7b Mon Sep 17 00:00:00 2001 From: Manuel Schiller <6340397+schiller-manuel@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:55:05 +0100 Subject: [PATCH] fix(oracle-gaps): scope mutant anchors to declarations outside statement range anchorOf() previously checked only FirstStatement..LastStatement, which excludes Parameter, PropertyDeclaration, EnumMember, Decorator, HeritageClause, ComputedPropertyName, and ExportAssignment. Mutants inside default parameter initializers or class field initializers walked all the way to SourceFile, dumping the entire file into mutant.anchor and mutant.key. Stop at statements (via ts.isStatement) and small enclosing declarations (parameters, property declarations, enum members, decorators, heritage clauses, class static blocks, accessors, and computed member names), and add regression tests covering tight anchors and edit stability. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- packages/oracles/oracle-gaps/report.mjs | 6 +- packages/oracles/oracle-gaps/schemata.mjs | 37 ++-- .../oracles/tests/oracle-gaps-anchor.test.ts | 187 ++++++++++++++++++ 3 files changed, 215 insertions(+), 15 deletions(-) create mode 100644 packages/oracles/tests/oracle-gaps-anchor.test.ts diff --git a/packages/oracles/oracle-gaps/report.mjs b/packages/oracles/oracle-gaps/report.mjs index 17e746c..71ddee8 100644 --- a/packages/oracles/oracle-gaps/report.mjs +++ b/packages/oracles/oracle-gaps/report.mjs @@ -16,9 +16,9 @@ // paths. Unowned warnings are counted per function. A gap is identified by an // anchor that survives unrelated edits and reformatting: the source file, the // function (`name`, or `Class.method`), the operator, and the tokens of the -// innermost statement that holds the site, joined by single spaces and kept in -// full, with `~n` for the nth repeat of one anchor in a function. An authored -// mutant's key is its source and id (`source#authored:id`). +// innermost statement or declaration that holds the site, joined by single +// spaces and kept in full, with `~n` for the nth repeat of one anchor in a +// function. An authored mutant's key is its source and id (`source#authored:id`). import { baselineLines, blockedLines, diff --git a/packages/oracles/oracle-gaps/schemata.mjs b/packages/oracles/oracle-gaps/schemata.mjs index ec4889f..cae485e 100644 --- a/packages/oracles/oracle-gaps/schemata.mjs +++ b/packages/oracles/oracle-gaps/schemata.mjs @@ -75,23 +75,36 @@ const ordering = new Set([`<`, `<=`, `>`, `>=`]) const DEAD_CODE = `dead code` const BOUNDARY_HINT = `boundary: -0` -// A mutant's anchor is the innermost statement that holds its site, as its -// tokens joined by single spaces, so formatting does not move it. -function anchorOf(ts, file, node) { - let statement = node - while ( - statement.parent && - !( - statement.kind >= ts.SyntaxKind.FirstStatement && - statement.kind <= ts.SyntaxKind.LastStatement - ) +// A mutant's anchor is the innermost statement or declaration that holds its +// site (parameters, class fields, enum members, decorators, heritage clauses, +// and computed member names included, since TypeScript's statement range +// leaves them out), as its tokens joined by single spaces, so formatting does +// not move it. +function isAnchor(ts, node) { + return ( + ts.isStatement(node) || + ts.isParameter(node) || + ts.isPropertyDeclaration(node) || + ts.isEnumMember(node) || + ts.isDecorator(node) || + ts.isHeritageClause(node) || + ts.isClassStaticBlockDeclaration(node) || + ts.isAccessor(node) || + (ts.isComputedPropertyName(node) && + (ts.isClassElement(node.parent) || + ts.isMethodDeclaration(node.parent) || + ts.isAccessor(node.parent))) ) - statement = statement.parent +} + +function anchorOf(ts, file, node) { + let anchor = node + while (anchor.parent && !isAnchor(ts, anchor)) anchor = anchor.parent const scanner = ts.createScanner( ts.ScriptTarget.Latest, true, ts.LanguageVariant.Standard, - statement.getText(file), + anchor.getText(file), ) const tokens = [] for ( diff --git a/packages/oracles/tests/oracle-gaps-anchor.test.ts b/packages/oracles/tests/oracle-gaps-anchor.test.ts new file mode 100644 index 0000000..44539c8 --- /dev/null +++ b/packages/oracles/tests/oracle-gaps-anchor.test.ts @@ -0,0 +1,187 @@ +import { describe, expect, test } from 'vitest' +import ts from 'typescript-for-tanstack-oracles' +import { assignKeys } from '../oracle-gaps/report.mjs' +import { instrument } from '../oracle-gaps/schemata.mjs' + +describe(`gap report mutant anchors`, () => { + test(`default parameters and class fields get declaration-scoped anchors, not the whole file`, () => { + const source = [ + `export function loadManifest(root = process.cwd(), nominal = false) {`, + ` return root`, + `}`, + ``, + `export class Config extends getBase(true) {`, + ` flag = false`, + ` count = compute() + 1`, + ` accessor active = true`, + ` handle = (x = false) => x || true`, + ` constructor(a = false, public b = true) {`, + ` super()`, + ` }`, + ` method(x = 1 + 2, { nested = false } = {}) {`, + ` return x`, + ` }`, + ` set value(v = false) {}`, + ` [getKey(true)]() {`, + ` return 1`, + ` }`, + `}`, + ``, + `export enum Mode {`, + ` Fast = 1 + 2,`, + `}`, + ``, + `export default () => true`, + ``, + `export function unrelatedTail() {`, + ` return 'should-never-appear-in-earlier-anchors'`, + `}`, + ``, + ].join(`\n`) + + const { mutants } = instrument(ts, `src/sample.ts`, source, `src/sample.ts`) + assignKeys(mutants) + + expect(mutants.length).toBeGreaterThan(0) + // No mutant's anchor or key may dump the whole file. + for (const mutant of mutants) { + if (mutant.line < 28) { + expect(mutant.anchor).not.toContain(`unrelatedTail`) + expect(mutant.key).not.toContain(`unrelatedTail`) + } + } + + const anchorsAt = (line: number) => + mutants.filter((m) => m.line === line).map((m) => [m.operator, m.anchor]) + + // Default parameters on a function declaration. + expect(anchorsAt(1)).toEqual([ + [`value-undefined`, `root = process . cwd ( )`], + [`bool-flip`, `nominal = false`], + ]) + // Class heritage clause. + expect(anchorsAt(5)).toEqual([ + [`value-undefined`, `extends getBase ( true )`], + [`bool-flip`, `extends getBase ( true )`], + ]) + // Class field initializers (plain, arithmetic + call, auto-accessor, concise arrow). + expect(anchorsAt(6)).toEqual([[`bool-flip`, `flag = false`]]) + expect(anchorsAt(7)).toEqual([ + [`arithmetic-swap`, `count = compute ( ) + 1`], + [`value-undefined`, `count = compute ( ) + 1`], + ]) + expect(anchorsAt(8)).toEqual([[`bool-flip`, `accessor active = true`]]) + expect(anchorsAt(9)).toEqual([ + [`bool-flip`, `x = false`], + [`binary-swap`, `handle = ( x = false ) => x || true`], + [`bool-flip`, `handle = ( x = false ) => x || true`], + ]) + // Method default parameters (plain and destructured) and setter parameter. + expect(anchorsAt(13)).toEqual([ + [`arithmetic-swap`, `x = 1 + 2`], + [`bool-flip`, `{ nested = false } = { }`], + ]) + expect(anchorsAt(16)).toEqual([[`bool-flip`, `v = false`]]) + // Computed method name stays tight instead of dumping the method body. + expect(anchorsAt(17)).toEqual([ + [`value-undefined`, `[ getKey ( true ) ]`], + [`bool-flip`, `[ getKey ( true ) ]`], + ]) + // Enum member initializer. + expect(anchorsAt(23)).toEqual([[`arithmetic-swap`, `Fast = 1 + 2`]]) + // Export assignment. + expect(anchorsAt(26)).toEqual([[`bool-flip`, `export default ( ) => true`]]) + }) + + test(`decorators and non-derived constructor default parameters get tight anchors`, () => { + const source = [ + `@dec(false)`, + `export class Box {`, + ` @fieldDec(true)`, + ` flag = false`, + ` constructor(a = false, public b = true) {}`, + ` @methodDec(true)`, + ` run(@paramDec(false) x = true) {`, + ` return x`, + ` }`, + `}`, + ``, + `export const tail = 'unrelated-tail'`, + ``, + ].join(`\n`) + + const { mutants } = instrument(ts, `src/box.ts`, source, `src/box.ts`) + + for (const mutant of mutants) + expect(mutant.anchor).not.toContain(`unrelated-tail`) + + const anchorsAt = (line: number) => + mutants.filter((m) => m.line === line).map((m) => [m.operator, m.anchor]) + + expect(anchorsAt(1)).toEqual([ + [`value-undefined`, `@ dec ( false )`], + [`bool-flip`, `@ dec ( false )`], + ]) + expect(anchorsAt(3)).toEqual([ + [`value-undefined`, `@ fieldDec ( true )`], + [`bool-flip`, `@ fieldDec ( true )`], + ]) + expect(anchorsAt(4)).toEqual([ + [`bool-flip`, `@ fieldDec ( true ) flag = false`], + ]) + expect(anchorsAt(5)).toEqual([ + [`bool-flip`, `a = false`], + [`bool-flip`, `public b = true`], + ]) + expect(anchorsAt(6)).toEqual([ + [`value-undefined`, `@ methodDec ( true )`], + [`bool-flip`, `@ methodDec ( true )`], + ]) + expect(anchorsAt(7)).toEqual([ + [`value-undefined`, `@ paramDec ( false )`], + [`bool-flip`, `@ paramDec ( false )`], + [`bool-flip`, `@ paramDec ( false ) x = true`], + ]) + }) + + test(`default-parameter and class-field anchors stay stable under unrelated edits and reformatting`, () => { + const before = [ + `export function load(root = process.cwd(), nominal = false) {`, + ` return root`, + `}`, + `export class State {`, + ` flag = false`, + `}`, + ``, + ].join(`\n`) + const after = [ + `// An unrelated comment and helper added above.`, + `export function helper() {`, + ` return 42`, + `}`, + `export function load(`, + ` root = process.cwd(),`, + ` nominal =`, + ` false,`, + `) {`, + ` return root`, + `}`, + `export class State {`, + ` extra = 'added'`, + ` flag =`, + ` false`, + `}`, + ``, + ].join(`\n`) + + const anchorsOf = (text: string) => + instrument(ts, `src/load.ts`, text, `src/load.ts`).mutants.map((m) => [ + m.operator, + m.anchor, + ]) + + const beforeAnchors = anchorsOf(before) + const afterAnchors = anchorsOf(after) + for (const pair of beforeAnchors) expect(afterAnchors).toContainEqual(pair) + }) +})