-
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy patheslint-adapter.ts
More file actions
60 lines (46 loc) · 1.77 KB
/
eslint-adapter.ts
File metadata and controls
60 lines (46 loc) · 1.77 KB
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
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import {type Linter} from 'eslint';
import {Xo} from './xo.js';
const eslintConfigNames = [
'eslint.config.js',
'eslint.config.mjs',
'eslint.config.cjs',
'eslint.config.ts',
'eslint.config.mts',
'eslint.config.cts',
];
function findEslintConfigDirectory(cwd: string): string | undefined {
let currentDirectory = cwd;
for (;;) {
for (const configName of eslintConfigNames) {
if (fs.existsSync(path.join(currentDirectory, configName))) {
return currentDirectory;
}
}
const parentDirectory = path.dirname(currentDirectory);
if (parentDirectory === currentDirectory) {
return undefined;
}
currentDirectory = parentDirectory;
}
}
function resolveAdapterCwd(): string {
const inlineConfigPath = process.argv.find(argument => argument.startsWith('--config=') || argument.startsWith('-c='))?.split('=').slice(1).join('=');
if (inlineConfigPath) {
return path.dirname(path.resolve(process.cwd(), inlineConfigPath));
}
const configFlagIndex = process.argv.findIndex(argument => argument === '--config' || argument === '-c');
const configPath = configFlagIndex === -1 ? undefined : process.argv[configFlagIndex + 1];
if (configPath) {
return path.dirname(path.resolve(process.cwd(), configPath));
}
return findEslintConfigDirectory(process.cwd()) ?? process.cwd();
}
/*
Keep the adapter small: resolve XO relative to the ESLint config location, then reuse XO's existing project config pipeline.
This is a snapshot of the current project files, not a long-lived parser shim for files created after the adapter is imported.
*/
const eslintConfig: Linter.Config[] = await new Xo({cwd: resolveAdapterCwd(), ts: true}).getProjectEslintConfig();
export default eslintConfig;