/
eslint-utils.ts
48 lines (44 loc) · 1.69 KB
/
eslint-utils.ts
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
import { ESLint } from 'eslint';
import type { Schema } from '../schema';
export async function loadESLint() {
let eslint;
try {
eslint = await import('eslint');
return eslint;
} catch {
throw new Error('Unable to find ESLint. Ensure ESLint is installed.');
}
}
export async function lint(
eslintConfigPath: string | undefined,
options: Schema
): Promise<ESLint.LintResult[]> {
const projectESLint: { ESLint: typeof ESLint } = await loadESLint();
const eslint = new projectESLint.ESLint({
/**
* If "noEslintrc" is set to `true` (and therefore here "useEslintrc" will be `false`), then ESLint will not
* merge the provided config with others it finds automatically.
*/
useEslintrc: !options.noEslintrc,
overrideConfigFile: eslintConfigPath,
ignorePath: options.ignorePath || undefined,
fix: !!options.fix,
cache: !!options.cache,
cacheLocation: options.cacheLocation || undefined,
cacheStrategy: options.cacheStrategy || undefined,
resolvePluginsRelativeTo: options.resolvePluginsRelativeTo || undefined,
rulePaths: options.rulesdir || [],
/**
* Default is `true` and if not overridden the eslint.lintFiles() method will throw an error
* when no target files are found.
*
* We don't want ESLint to throw an error if a user has only just created
* a project and therefore doesn't necessarily have matching files, for example.
*
* An angular generator creates lint pattern for `html` files, but there may
* not be any html files in the project, so keeping it true would break linting everytime
*/
errorOnUnmatchedPattern: false,
});
return await eslint.lintFiles(options.lintFilePatterns);
}