All files / src/lib/dev-tools output-parsers.ts

93.66% Statements 207/221
85.41% Branches 41/48
75% Functions 3/4
93.66% Lines 207/221

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 2221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 4x 4x 4x 4x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 3x 3x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x                       1x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 3x 3x 3x 3x 3x 3x 3x 4x 5x 5x 5x 5x 5x 2x 2x 2x 5x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 1x 3x 7x 2x 2x 2x 2x 2x 2x 2x 2x 5x 3x 3x 3x 3x 4x 2x 2x 2x 3x 3x 3x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 6x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 1x 1x 1x 1x        
/**
 * Output Parsers for Dev Tools
 * Parse output from Jest, ESLint, and TypeScript
 */
 
import type {
  TestResult,
  LintResult,
  LintFileResult,
  LintMessage,
  TypeScriptResult } from './types';
 
/**
 * Parse Jest test output
 */
export function parseJestOutput(output: string, exitCode: number): TestResult {
  const result: TestResult = {
    testSuites: { passed: 0, failed: 0, total: 0 },
    tests: { passed: 0, failed: 0, total: 0 },
    failures: [],
    duration: 0,
    success: exitCode === 0};
 
  // Parse test suites summary
  // Format: "Test Suites: X passed, Y failed, Z total" or "Test Suites: X failed, Y passed, Z total"
  const suitesLine = output.match(/Test Suites:\s*(.+?)\s*total/i)?.[1] || '';
  const suitesPassed = suitesLine.match(/(\d+)\s*passed/i);
  const suitesFailed = suitesLine.match(/(\d+)\s*failed/i);
  const suitesTotal = output.match(/Test Suites:.*?(\d+)\s*total/i);
  if (suitesTotal) {
    result.testSuites.passed = suitesPassed ? parseInt(suitesPassed[1], 10) : 0;
    result.testSuites.failed = suitesFailed ? parseInt(suitesFailed[1], 10) : 0;
    result.testSuites.total = parseInt(suitesTotal[1], 10);
  }
 
  // Parse tests summary
  // Format: "Tests: X passed, Y failed, Z total" or "Tests: X failed, Y passed, Z total"
  const testsLine = output.match(/Tests:\s*(.+?)\s*total/i)?.[1] || '';
  const testsPassed = testsLine.match(/(\d+)\s*passed/i);
  const testsFailed = testsLine.match(/(\d+)\s*failed/i);
  const testsTotal = output.match(/Tests:.*?(\d+)\s*total/i);
  if (testsTotal) {
    result.tests.passed = testsPassed ? parseInt(testsPassed[1], 10) : 0;
    result.tests.failed = testsFailed ? parseInt(testsFailed[1], 10) : 0;
    result.tests.total = parseInt(testsTotal[1], 10);
  }
 
  // Parse snapshots if present
  const snapshotsMatch = output.match(/Snapshots:\s*(?:(\d+)\s*passed,?\s*)?(?:(\d+)\s*failed,?\s*)?(\d+)\s*total/i);
  if (snapshotsMatch) {
    result.snapshots = {
      passed: parseInt(snapshotsMatch[1] || '0', 10),
      failed: parseInt(snapshotsMatch[2] || '0', 10),
      total: parseInt(snapshotsMatch[3] || '0', 10)};
  }
 
  // Parse duration
  // Format: "Time: X.XXs" or "Time: Xm Xs"
  const timeMatch = output.match(/Time:\s*([\d.]+)\s*s/i);
  if (timeMatch) {
    result.duration = parseFloat(timeMatch[1]) * 1000;
  }
 
  // Parse failures
  // Look for FAIL blocks and extract test names and messages
  const failureBlocks = output.split(/FAIL\s+/);
  for (let i = 1; i < failureBlocks.length; i++) {
    const block = failureBlocks[i];
    const lines = block.split('\n');
    const suiteName = lines[0]?.trim() || 'Unknown Suite';
 
    // Find individual test failures
    const failureMatches = block.matchAll(/✕\s+(.+?)(?:\s+\((\d+)\s*ms\))?$/gm);
    for (const match of failureMatches) {
      const testName = match[1].trim();

      // Try to find the error message for this test
      const errorMatch = block.match(new RegExp(`${escapeRegex(testName)}[\\s\\S]*?Error:([^\\n]+)`));
      const message = errorMatch ? errorMatch[1].trim() : 'Test failed';

      result.failures.push({
        testName,
        suiteName,
        message});
    }
  }
 
  return result;
}
 
/**
 * Parse ESLint JSON output
 */
export function parseEslintOutput(output: string, exitCode: number): LintResult {
  const result: LintResult = {
    errorCount: 0,
    warningCount: 0,
    fixableErrorCount: 0,
    fixableWarningCount: 0,
    files: [],
    success: exitCode === 0};
 
  // Try to parse as JSON first (if run with --format json)
  try {
    const jsonOutput = JSON.parse(output);
    if (Array.isArray(jsonOutput)) {
      for (const file of jsonOutput) {
        const fileResult: LintFileResult = {
          filePath: file.filePath,
          messages: file.messages.map((msg: LintMessage) => ({
            line: msg.line,
            column: msg.column,
            severity: msg.severity,
            message: msg.message,
            ruleId: msg.ruleId})),
          errorCount: file.errorCount,
          warningCount: file.warningCount};
        result.files.push(fileResult);
        result.errorCount += file.errorCount;
        result.warningCount += file.warningCount;
        result.fixableErrorCount += file.fixableErrorCount || 0;
        result.fixableWarningCount += file.fixableWarningCount || 0;
      }
      return result;
    }
  } catch {
    // Not JSON, parse text output
  }
 
  // Parse text output format
  // Format: "/path/to/file.ts"
  //         "  line:col  error/warning  message  rule-id"
  const fileBlocks = output.split(/\n(?=\/|[A-Z]:)/);
 
  for (const block of fileBlocks) {
    const lines = block.trim().split('\n');
    if (lines.length === 0) continue;
 
    const filePath = lines[0].trim();
    if (!filePath || filePath.startsWith('✖')) continue;
 
    const messages: LintMessage[] = [];
 
    for (let i = 1; i < lines.length; i++) {
      const line = lines[i].trim();
      // Match: "  10:5  error  Some message  rule-id" or "  10:5  warning  Some message  rule-id"
      const match = line.match(/^\s*(\d+):(\d+)\s+(error|warning)\s+(.+?)\s+(\S+)\s*$/);
      if (match) {
        const severity = match[3] === 'error' ? 2 : 1;
        messages.push({
          line: parseInt(match[1], 10),
          column: parseInt(match[2], 10),
          severity,
          message: match[4],
          ruleId: match[5]});
        if (severity === 2) result.errorCount++;
        else result.warningCount++;
      }
    }
 
    if (messages.length > 0) {
      result.files.push({
        filePath,
        messages,
        errorCount: messages.filter(m => m.severity === 2).length,
        warningCount: messages.filter(m => m.severity === 1).length});
    }
  }
 
  // Parse summary line if present
  // Format: "✖ X problems (Y errors, Z warnings)"
  const summaryMatch = output.match(/✖\s*(\d+)\s*problems?\s*\((\d+)\s*errors?,\s*(\d+)\s*warnings?\)/);
  if (summaryMatch) {
    result.errorCount = parseInt(summaryMatch[2], 10);
    result.warningCount = parseInt(summaryMatch[3], 10);
  }
 
  return result;
}
 
/**
 * Parse TypeScript compiler output
 */
export function parseTypescriptOutput(output: string, exitCode: number): TypeScriptResult {
  const result: TypeScriptResult = {
    errorCount: 0,
    errors: [],
    success: exitCode === 0};
 
  // Parse TypeScript errors
  // Format: "src/file.ts(10,5): error TS2345: Message"
  // Or: "src/file.ts:10:5 - error TS2345: Message"
  const errorRegex = /^(.+?)[:(](\d+)[,:](\d+)\)?[:\s-]+error\s+(TS\d+):\s*(.+)$/gm;
 
  let match;
  while ((match = errorRegex.exec(output)) !== null) {
    result.errors.push({
      file: match[1].trim(),
      line: parseInt(match[2], 10),
      column: parseInt(match[3], 10),
      code: match[4],
      message: match[5].trim()});
  }
 
  result.errorCount = result.errors.length;
 
  // Check for "Found X errors" summary
  const summaryMatch = output.match(/Found\s+(\d+)\s+errors?/i);
  if (summaryMatch) {
    result.errorCount = parseInt(summaryMatch[1], 10);
  }
 
  return result;
}
 
/**
 * Helper to escape regex special characters
 */
function escapeRegex(str: string): string {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}