|
| 1 | +import { ESLint } from 'eslint'; |
| 2 | +import type { Linter } from 'eslint'; |
| 3 | +import path from 'path'; |
| 4 | +import fs from 'fs/promises'; |
| 5 | + |
| 6 | +type ESLintMessage = { |
| 7 | + ruleId: string | null; |
| 8 | + severity: number; |
| 9 | + message: string; |
| 10 | + line: number; |
| 11 | + column: number; |
| 12 | + nodeType?: string; |
| 13 | + messageId?: string; |
| 14 | + endLine?: number; |
| 15 | + endColumn?: number; |
| 16 | + fix?: { |
| 17 | + range: [number, number]; |
| 18 | + text: string; |
| 19 | + }; |
| 20 | +}; |
| 21 | + |
| 22 | +type ESLintResult = { |
| 23 | + filePath: string; |
| 24 | + messages: ESLintMessage[]; |
| 25 | + fixed: boolean; |
| 26 | + output?: string; |
| 27 | +}; |
| 28 | + |
| 29 | +export interface LintResult { |
| 30 | + filePath: string; |
| 31 | + messages: ESLintMessage[]; |
| 32 | + fixed: boolean; |
| 33 | + output?: string; |
| 34 | +} |
| 35 | + |
| 36 | +export interface LintSummary { |
| 37 | + totalFiles: number; |
| 38 | + totalErrors: number; |
| 39 | + totalWarnings: number; |
| 40 | + fixedFiles: number; |
| 41 | + results: LintResult[]; |
| 42 | +} |
| 43 | + |
| 44 | +export class LintingService { |
| 45 | + private static instance: LintingService; |
| 46 | + private eslint: ESLint; |
| 47 | + |
| 48 | + private constructor() { |
| 49 | + this.eslint = new ESLint({ |
| 50 | + fix: true, |
| 51 | + baseConfig: { |
| 52 | + extends: [ |
| 53 | + 'eslint:recommended', |
| 54 | + 'plugin:@typescript-eslint/recommended', |
| 55 | + 'plugin:react/recommended', |
| 56 | + 'plugin:react-hooks/recommended', |
| 57 | + ], |
| 58 | + parser: '@typescript-eslint/parser', |
| 59 | + parserOptions: { |
| 60 | + ecmaFeatures: { |
| 61 | + jsx: true, |
| 62 | + }, |
| 63 | + ecmaVersion: 12, |
| 64 | + sourceType: 'module', |
| 65 | + }, |
| 66 | + plugins: ['@typescript-eslint', 'react', 'react-hooks'], |
| 67 | + rules: { |
| 68 | + 'react/react-in-jsx-scope': 'off', |
| 69 | + 'react/prop-types': 'off', |
| 70 | + '@typescript-eslint/no-unused-vars': ['warn'], |
| 71 | + '@typescript-eslint/no-explicit-any': 'off', |
| 72 | + 'no-console': 'warn', |
| 73 | + }, |
| 74 | + settings: { |
| 75 | + react: { |
| 76 | + version: 'detect', |
| 77 | + }, |
| 78 | + }, |
| 79 | + } as unknown as Linter.Config, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + public static getInstance(): LintingService { |
| 84 | + if (!LintingService.instance) { |
| 85 | + LintingService.instance = new LintingService(); |
| 86 | + } |
| 87 | + return LintingService.instance; |
| 88 | + } |
| 89 | + |
| 90 | + public async lintAndFix(filePath: string, content: string): Promise<LintResult> { |
| 91 | + const tempPath = path.join( |
| 92 | + path.dirname(filePath), |
| 93 | + `.temp.${Date.now()}.${path.basename(filePath)}`, |
| 94 | + ); |
| 95 | + try { |
| 96 | + await fs.writeFile(tempPath, content); |
| 97 | + |
| 98 | + const results = await this.eslint.lintFiles([tempPath]); |
| 99 | + if (!results.length) { |
| 100 | + throw new Error('No lint result returned'); |
| 101 | + } |
| 102 | + |
| 103 | + const result = results[0] as unknown as ESLintResult; |
| 104 | + |
| 105 | + let fixedContent = content; |
| 106 | + if (result.output) { |
| 107 | + fixedContent = result.output; |
| 108 | + await fs.writeFile(tempPath, fixedContent); |
| 109 | + } |
| 110 | + |
| 111 | + return { |
| 112 | + filePath, |
| 113 | + messages: result.messages, |
| 114 | + fixed: result.fixed, |
| 115 | + output: fixedContent, |
| 116 | + }; |
| 117 | + } catch (error) { |
| 118 | + console.error('Linting error:', error); |
| 119 | + return { |
| 120 | + filePath, |
| 121 | + messages: [], |
| 122 | + fixed: false, |
| 123 | + output: content, |
| 124 | + }; |
| 125 | + } finally { |
| 126 | + await fs.unlink(tempPath).catch(() => {}); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public async lintProject(projectPath: string): Promise<LintSummary> { |
| 131 | + const results: LintResult[] = []; |
| 132 | + let totalErrors = 0; |
| 133 | + let totalWarnings = 0; |
| 134 | + let fixedFiles = 0; |
| 135 | + |
| 136 | + const files = await this.eslint.lintFiles([ |
| 137 | + `${projectPath}/**/*.{ts,tsx,js,jsx}`, |
| 138 | + `!${projectPath}/node_modules/**`, |
| 139 | + ]); |
| 140 | + const typedFiles = files as unknown as ESLintResult[]; |
| 141 | + |
| 142 | + for (const result of typedFiles) { |
| 143 | + const messages = result.messages; |
| 144 | + const hasErrors = messages.some((m) => m.severity === 2); |
| 145 | + const hasWarnings = messages.some((m) => m.severity === 1); |
| 146 | + |
| 147 | + if (hasErrors) totalErrors += messages.filter((m) => m.severity === 2).length; |
| 148 | + if (hasWarnings) totalWarnings += messages.filter((m) => m.severity === 1).length; |
| 149 | + if (result.fixed) fixedFiles++; |
| 150 | + |
| 151 | + results.push({ |
| 152 | + filePath: result.filePath, |
| 153 | + messages, |
| 154 | + fixed: result.fixed, |
| 155 | + output: result.output, |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + return { |
| 160 | + totalFiles: files.length, |
| 161 | + totalErrors, |
| 162 | + totalWarnings, |
| 163 | + fixedFiles, |
| 164 | + results, |
| 165 | + }; |
| 166 | + } |
| 167 | +} |
0 commit comments