-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Added ESLint for linting service as a Toolcall for linting the output #1799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { ESLint } from 'eslint'; | ||
| import type { Linter } from 'eslint'; | ||
| import path from 'path'; | ||
| import fs from 'fs/promises'; | ||
|
|
||
| type ESLintMessage = { | ||
| ruleId: string | null; | ||
| severity: number; | ||
| message: string; | ||
| line: number; | ||
| column: number; | ||
| nodeType?: string; | ||
| messageId?: string; | ||
| endLine?: number; | ||
| endColumn?: number; | ||
| fix?: { | ||
| range: [number, number]; | ||
| text: string; | ||
| }; | ||
| }; | ||
|
|
||
| type ESLintResult = { | ||
| filePath: string; | ||
| messages: ESLintMessage[]; | ||
| fixed: boolean; | ||
| output?: string; | ||
| }; | ||
|
|
||
| export interface LintResult { | ||
| filePath: string; | ||
| messages: ESLintMessage[]; | ||
| fixed: boolean; | ||
| output?: string; | ||
| } | ||
|
|
||
| export interface LintSummary { | ||
| totalFiles: number; | ||
| totalErrors: number; | ||
| totalWarnings: number; | ||
| fixedFiles: number; | ||
| results: LintResult[]; | ||
| } | ||
|
|
||
| export class LintingService { | ||
| private static instance: LintingService; | ||
| private eslint: ESLint; | ||
|
|
||
| private constructor() { | ||
| this.eslint = new ESLint({ | ||
| fix: true, | ||
| baseConfig: { | ||
| extends: [ | ||
| 'eslint:recommended', | ||
| 'plugin:@typescript-eslint/recommended', | ||
| 'plugin:react/recommended', | ||
| 'plugin:react-hooks/recommended', | ||
| ], | ||
| parser: '@typescript-eslint/parser', | ||
| parserOptions: { | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| ecmaVersion: 12, | ||
| sourceType: 'module', | ||
| }, | ||
| plugins: ['@typescript-eslint', 'react', 'react-hooks'], | ||
| rules: { | ||
| 'react/react-in-jsx-scope': 'off', | ||
| 'react/prop-types': 'off', | ||
| '@typescript-eslint/no-unused-vars': ['warn'], | ||
| '@typescript-eslint/no-explicit-any': 'off', | ||
| 'no-console': 'warn', | ||
| }, | ||
| settings: { | ||
| react: { | ||
| version: 'detect', | ||
| }, | ||
| }, | ||
| } as unknown as Linter.Config, | ||
| }); | ||
| } | ||
|
|
||
| public static getInstance(): LintingService { | ||
| if (!LintingService.instance) { | ||
| LintingService.instance = new LintingService(); | ||
| } | ||
| return LintingService.instance; | ||
| } | ||
|
|
||
| public async lintAndFix(filePath: string, content: string): Promise<LintResult> { | ||
| const tempPath = path.join( | ||
| path.dirname(filePath), | ||
| `.temp.${Date.now()}.${path.basename(filePath)}`, | ||
| ); | ||
| try { | ||
| await fs.writeFile(tempPath, content); | ||
|
|
||
| const results = await this.eslint.lintFiles([tempPath]); | ||
| if (!results.length) { | ||
| throw new Error('No lint result returned'); | ||
| } | ||
|
|
||
| const result = results[0] as unknown as ESLintResult; | ||
|
|
||
| let fixedContent = content; | ||
| if (result.output) { | ||
| fixedContent = result.output; | ||
| await fs.writeFile(tempPath, fixedContent); | ||
| } | ||
|
|
||
| return { | ||
| filePath, | ||
| messages: result.messages, | ||
| fixed: result.fixed, | ||
| output: fixedContent, | ||
| }; | ||
| } catch (error) { | ||
| console.error('Linting error:', error); | ||
| return { | ||
| filePath, | ||
| messages: [], | ||
| fixed: false, | ||
| output: content, | ||
| }; | ||
| } finally { | ||
| await fs.unlink(tempPath).catch(() => {}); | ||
| } | ||
| } | ||
|
|
||
| public async lintProject(projectPath: string): Promise<LintSummary> { | ||
| const results: LintResult[] = []; | ||
| let totalErrors = 0; | ||
| let totalWarnings = 0; | ||
| let fixedFiles = 0; | ||
|
|
||
| const files = await this.eslint.lintFiles([ | ||
| `${projectPath}/**/*.{ts,tsx,js,jsx}`, | ||
| `!${projectPath}/node_modules/**`, | ||
| ]); | ||
| const typedFiles = files as unknown as ESLintResult[]; | ||
|
|
||
| for (const result of typedFiles) { | ||
| const messages = result.messages; | ||
| const hasErrors = messages.some((m) => m.severity === 2); | ||
| const hasWarnings = messages.some((m) => m.severity === 1); | ||
|
|
||
| if (hasErrors) totalErrors += messages.filter((m) => m.severity === 2).length; | ||
| if (hasWarnings) totalWarnings += messages.filter((m) => m.severity === 1).length; | ||
| if (result.fixed) fixedFiles++; | ||
|
|
||
| results.push({ | ||
| filePath: result.filePath, | ||
| messages, | ||
| fixed: result.fixed, | ||
| output: result.output, | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| totalFiles: files.length, | ||
| totalErrors, | ||
| totalWarnings, | ||
| fixedFiles, | ||
| results, | ||
| }; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.