This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
73 lines (57 loc) · 1.69 KB
/
index.js
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
const path = require("path");
const { createFilter } = require("rollup-pluginutils");
const { CLIEngine } = require("eslint");
function normalizePath(id) {
return path
.relative(process.cwd(), id)
.split(path.sep)
.join("/");
}
function eslint(options = {}) {
if (typeof options === "string") {
const configFile = path.resolve(process.cwd(), options);
options = require(configFile);
options.useEslintrc = false; // Tell eslint not to look for configuration files.
}
const cli = new CLIEngine(options);
let formatter = options.formatter;
if (typeof formatter !== "function") {
formatter = cli.getFormatter(formatter || "stylish");
}
const filter = createFilter(
options.include,
options.exclude || /node_modules/
);
return {
name: "eslint",
transform(code, id) {
const file = normalizePath(id);
if (cli.isPathIgnored(file) || !filter(id)) {
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) {
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");
}
}
};
}
exports.eslint = eslint;