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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 30x 30x 180x 180x 180x 30x 30x 2x 2x 2x 2x 2x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 2x 2x 2x 2x 2x 2x 2x 2x 15x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 2x 2x 10x 10x 2x 2x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 39x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 37x 37x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 15x 15x 15x 15x 15x 2x | import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { ESLint } from 'eslint';
import js from '@eslint/js';
import ts from 'typescript-eslint';
import type { LinterAdapter, AdapterResult, LinterDiagnostic } from './adapter';
import { getEslintConfigPath } from '../config';
import { t } from '../i18n/messages';
import { eslintExtraRules, eslintExtraTsRules } from '../rules/builtin-rules';
const TS_FILES = ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'];
const JS_FILES = ['**/*.js', '**/*.jsx', '**/*.mjs', '**/*.cjs'];
const PROJECT_CONFIG_FILES = [
'eslint.config.js',
'eslint.config.mjs',
'eslint.config.cjs',
'eslint.config.ts',
'eslint.config.mts',
'eslint.config.cts',
];
const LEGACY_CONFIG_FILES = [
'.eslintrc.js',
'.eslintrc.cjs',
'.eslintrc.json',
'.eslintrc.yaml',
'.eslintrc.yml',
'.eslintrc',
];
function findConfigFile(dir: string, names: string[]): string | null {
for (const name of names) {
const p = path.join(dir, name);
if (fs.existsSync(p)) {
return p;
}
}
return null;
}
type EslintConfigResult =
| { kind: 'use'; config: { overrideConfigFile?: string | true; overrideConfig?: any[] } }
| { kind: 'legacy'; path: string };
function resolveEslintConfig(workingDir: string): EslintConfigResult {
const globalPath = getEslintConfigPath();
if (globalPath && globalPath.trim() !== '') {
const abs = path.isAbsolute(globalPath) ? globalPath : path.resolve(workingDir, globalPath);
if (fs.existsSync(abs)) {
return { kind: 'use', config: { overrideConfigFile: abs } };
}
}
const projectConfig = findConfigFile(workingDir, PROJECT_CONFIG_FILES);
if (projectConfig) {
return { kind: 'use', config: { overrideConfigFile: projectConfig } };
}
const legacyConfig = findConfigFile(workingDir, LEGACY_CONFIG_FILES);
if (legacyConfig) {
return { kind: 'legacy', path: legacyConfig };
}
return { kind: 'use', config: { overrideConfigFile: true, overrideConfig: ESLintAdapter.getDefaultConfig() } };
}
export class ESLintAdapter implements LinterAdapter {
id = 'eslint';
supportedLanguages = ['javascript', 'typescript'];
private static defaultConfig: any[] | null = null;
public static getDefaultConfig(): any[] {
if (!ESLintAdapter.defaultConfig) {
const tsConfigs = ts.configs.recommended.map(cfg => ({
...cfg,
files: (cfg as { files?: string[] }).files ?? TS_FILES,
}));
ESLintAdapter.defaultConfig = [
js.configs.recommended,
{ files: JS_FILES, languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
...tsConfigs,
{ rules: eslintExtraRules },
{ files: TS_FILES, rules: eslintExtraTsRules },
];
}
return ESLintAdapter.defaultConfig;
}
isAvailable(): boolean {
return true;
}
async check(document: vscode.TextDocument, workingDir: string): Promise<AdapterResult> {
try {
const resolved = resolveEslintConfig(workingDir);
if (resolved.kind === 'legacy') {
return {
diagnostics: [],
status: 'execution-failed',
errorMessage: t('adapter.eslintLegacyConfig', { 0: resolved.path }),
};
}
const engine = new ESLint({
cwd: workingDir,
...resolved.config,
});
const ext = document.languageId === 'typescript' ? 'ts' : 'js';
const isVirtual = document.uri.scheme === 'untitled';
const results = await engine.lintText(document.getText(), {
filePath: isVirtual ? `untitled.${ext}` : document.fileName,
});
const diagnostics: LinterDiagnostic[] = [];
for (const result of results) {
for (const msg of result.messages) {
if (msg.ruleId === null) {
if (!msg.fatal) { continue; }
diagnostics.push({
severity: 'error',
ruleId: 'eslint:parse-error',
message: msg.message,
range: new vscode.Range(
msg.line - 1,
msg.column - 1,
(msg.endLine ?? msg.line) - 1,
(msg.endColumn ?? msg.column) - 1
),
});
continue;
}
diagnostics.push({
severity: msg.severity === 2 ? 'error' : 'warning',
ruleId: `eslint:${msg.ruleId}`,
message: msg.message,
range: new vscode.Range(
msg.line - 1,
msg.column - 1,
(msg.endLine ?? msg.line) - 1,
(msg.endColumn ?? msg.column) - 1
),
suggestion: msg.fix?.text,
fix: msg.fix ? { range: [msg.fix.range[0], msg.fix.range[1]], text: msg.fix.text } : undefined,
});
}
}
return { diagnostics, status: 'ok' };
} catch (error) {
return {
diagnostics: [],
status: 'execution-failed',
errorMessage: error instanceof Error ? error.message : String(error),
};
}
}
}
|