/
index.ts
executable file
路81 lines (64 loc) 路 2.16 KB
/
index.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
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
import * as path from 'path';
import { Plugin } from 'rollup';
import { createFilter } from '@rollup/pluginutils';
import { CLIEngine } from 'eslint';
import { RollupEslintOptions } from '../types';
function normalizePath(id: string) {
return path.relative(process.cwd(), id).split(path.sep).join('/');
}
export default function eslint(options = {} as RollupEslintOptions): Plugin {
if (typeof options === 'string') {
const configFile = path.resolve(process.cwd(), options);
// eslint-disable-next-line global-require, import/no-dynamic-require, no-param-reassign
options = require(configFile);
// Tell eslint not to look for configuration files.
// eslint-disable-next-line no-param-reassign
options.useEslintrc = false;
}
const cli = new CLIEngine(options);
let formatter: CLIEngine.Formatter;
switch (typeof options.formatter) {
case 'string':
formatter = cli.getFormatter(options.formatter);
break;
case 'function':
({ formatter } = options);
break;
default:
formatter = cli.getFormatter('stylish');
}
const filter = createFilter(options.include, options.exclude || /node_modules/);
return {
name: 'eslint',
// eslint-disable-next-line consistent-return
transform(code, id) {
const file = normalizePath(id);
if (!filter(id) || cli.isPathIgnored(file)) {
return null;
}
const report = cli.executeOnText(code, file);
const hasWarnings = options.throwOnWarning && report.warningCount !== 0;
const hasErrors = options.throwOnError && report.errorCount !== 0;
if (options.fix && report) {
CLIEngine.outputFixes(report);
}
if (report.warningCount === 0 && report.errorCount === 0) {
return null;
}
const result = formatter(report.results);
if (result) {
// eslint-disable-next-line no-console
console.log(result);
}
if (hasWarnings && hasErrors) {
throw Error('Warnings or errors were found');
}
if (hasWarnings) {
throw Error('Warnings were found');
}
if (hasErrors) {
throw Error('Errors were found');
}
}
};
}