All files / src/fix fixEngine.ts

89.36% Statements 126/141
68.75% Branches 22/32
100% Functions 4/4
89.36% Lines 126/141

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1422x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 10x 3x 10x 2x 2x 2x 10x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 6x 1x 6x 6x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 3x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x       2x 2x 2x 2x 3x 2x 3x     2x 3x     2x 3x     2x 2x 2x 2x 2x 2x 2x 2x 3x     2x 2x 2x  
import * as vscode from 'vscode';
import type { LinterAdapter, LinterDiagnostic } from '../types';
import { mockDocument } from '../utils/mockDocument';
 
export interface AppliedFix {
  originalText: string;
  newText: string;
  line: number;
}
 
export interface FixResult {
  success: boolean;
  attempts: number;
  message?: string;
  appliedFixes: AppliedFix[];
  newText?: string;
}
 
function applyFixToText(text: string, fix: { range: [number, number]; text: string }): string {
  const [start, end] = fix.range;
  if (start < 0 || end < start || end > text.length) { return text; }
  return text.slice(0, start) + fix.text + text.slice(end);
}
 
function findClosestFixable(
  diagnostics: LinterDiagnostic[],
  ruleId: string,
  line: number
): LinterDiagnostic | null {
  let best: LinterDiagnostic | null = null;
  let bestDist = Number.MAX_SAFE_INTEGER;
  for (const d of diagnostics) {
    if (d.ruleId !== ruleId || !d.fix) { continue; }
    const dist = Math.abs(d.range.start.line - line);
    if (dist < bestDist) {
      bestDist = dist;
      best = d;
    }
  }
  return best;
}
 
function issueStillExists(
  diagnostics: LinterDiagnostic[],
  ruleId: string,
  fixedStart: number,
  fixedEnd: number
): boolean {
  for (const d of diagnostics) {
    if (d.ruleId !== ruleId || !d.fix) { continue; }
    const [s, e] = d.fix.range;
    if (s < fixedEnd && e > fixedStart) { return true; }
  }
  return false;
}
 
export async function fixDiagnostic(
  document: vscode.TextDocument,
  workingDir: string,
  adapter: LinterAdapter,
  diag: LinterDiagnostic,
  maxIterations: number,
  dryRun?: boolean
): Promise<FixResult> {
  const originalText = document.getText();
  let currentText = originalText;
  let prevLine = diag.range.start.line;
  let converged = false;
  const appliedFixes: AppliedFix[] = [];
 
  for (let round = 1; round <= maxIterations; round++) {
    let result;
    try {
      const mock = mockDocument(currentText, document.languageId, document.fileName);
      result = await adapter.check(mock, workingDir);
    } catch {
      return { success: false, attempts: round, message: 'lint-execution-failed', appliedFixes };
    }
 
    const target = findClosestFixable(result.diagnostics, diag.ruleId, prevLine);
    if (!target) {
      return { success: false, attempts: round, message: 'not-autofixable', appliedFixes };
    }
 
    const fix = target.fix!;
    const [start, end] = fix.range;
    const originalFragment = currentText.slice(start, end);
    const nextText = applyFixToText(currentText, fix);
    if (nextText === currentText) {
      return { success: false, attempts: round, message: 'no-change', appliedFixes };
    }
 
    appliedFixes.push({
      originalText: originalFragment,
      newText: fix.text,
      line: target.range.start.line,
    });
    currentText = nextText;
    prevLine = target.range.start.line;
 
    const fixedStart = start;
    const fixedEnd = start + fix.text.length;
 
    let verify;
    try {
      verify = await adapter.check(mockDocument(currentText, document.languageId, document.fileName), workingDir);
    } catch {
      converged = false;
      break;
    }
    if (!issueStillExists(verify.diagnostics, diag.ruleId, fixedStart, fixedEnd)) {
      converged = true;
      break;
    }
  }
 
  if (!converged) {
    return { success: false, attempts: maxIterations, message: 'max-iterations', appliedFixes };
  }
 
  if (currentText === originalText) {
    return { success: true, attempts: 0, appliedFixes };
  }
 
  if (dryRun) {
    return { success: true, attempts: maxIterations, appliedFixes, newText: currentText };
  }
 
  const edit = new vscode.WorkspaceEdit();
  const fullRange = new vscode.Range(
    document.positionAt(0),
    document.positionAt(originalText.length)
  );
  edit.replace(document.uri, fullRange, currentText);
  const applied = await vscode.workspace.applyEdit(edit);
  if (!applied) {
    return { success: false, attempts: maxIterations, message: 'apply-failed', appliedFixes };
  }
 
  return { success: true, attempts: maxIterations, appliedFixes };
}