-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
createEslinter.ts
32 lines (26 loc) · 900 Bytes
/
createEslinter.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
import * as path from 'path';
import { LintReport } from './types/eslint';
import { throwIfIsInvalidSourceFileError } from './FsHelper';
export function createEslinter(eslintOptions: object) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { CLIEngine } = require('eslint');
// See https://eslint.org/docs/1.0.0/developer-guide/nodejs-api#cliengine
const eslinter = new CLIEngine(eslintOptions);
function getReport(filepath: string): LintReport | undefined {
try {
if (
eslinter.isPathIgnored(filepath) ||
path.extname(filepath).localeCompare('.json', undefined, {
sensitivity: 'accent'
}) === 0
) {
return undefined;
}
return eslinter.executeOnFiles([filepath]);
} catch (e) {
throwIfIsInvalidSourceFileError(filepath, e);
}
return undefined;
}
return { getReport };
}