-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
EsLintReporter.ts
139 lines (119 loc) · 4.22 KB
/
EsLintReporter.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { CLIEngine, LintReport, LintResult } from '../types/eslint';
import { createIssuesFromEsLintResults } from '../issue/EsLintIssueFactory';
import { EsLintReporterConfiguration } from '../EsLintReporterConfiguration';
import { Reporter } from '../../reporter';
import minimatch from 'minimatch';
import glob from 'glob';
function createEsLintReporter(configuration: EsLintReporterConfiguration): Reporter {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { CLIEngine } = require('eslint');
const engine: CLIEngine = new CLIEngine(configuration.options);
let isInitialRun = true;
let isInitialGetFiles = true;
const lintResults = new Map<string, LintResult>();
const includedGlobPatterns = engine.resolveFileGlobPatterns(configuration.files);
const includedFiles = new Set<string>();
function isFileIncluded(path: string) {
return (
includedGlobPatterns.some((pattern) => minimatch(path, pattern)) &&
!engine.isPathIgnored(path)
);
}
async function getFiles() {
if (isInitialGetFiles) {
isInitialGetFiles = false;
const resolvedGlobs = await Promise.all(
includedGlobPatterns.map(
(globPattern) =>
new Promise<string[]>((resolve) => {
glob(globPattern, (error, resolvedFiles) => {
if (error) {
// fail silently
resolve([]);
} else {
resolve(resolvedFiles || []);
}
});
})
)
);
for (const resolvedGlob of resolvedGlobs) {
for (const resolvedFile of resolvedGlob) {
if (isFileIncluded(resolvedFile)) {
includedFiles.add(resolvedFile);
}
}
}
}
return Array.from(includedFiles);
}
function getDirs() {
return includedGlobPatterns || [];
}
function getExtensions() {
return configuration.options.extensions || [];
}
return {
getReport: async ({ changedFiles = [], deletedFiles = [] }) => {
return {
async getDependencies() {
for (const changedFile of changedFiles) {
if (isFileIncluded(changedFile)) {
includedFiles.add(changedFile);
}
}
for (const deletedFile of deletedFiles) {
includedFiles.delete(deletedFile);
}
return {
files: await getFiles(),
dirs: getDirs(),
extensions: getExtensions(),
};
},
async getIssues() {
// cleanup old results
for (const changedFile of changedFiles) {
lintResults.delete(changedFile);
}
for (const deletedFile of deletedFiles) {
lintResults.delete(deletedFile);
}
// get reports
const lintReports: LintReport[] = [];
if (isInitialRun) {
lintReports.push(engine.executeOnFiles(includedGlobPatterns));
isInitialRun = false;
} else {
// we need to take care to not lint files that are not included by the configuration.
// the eslint engine will not exclude them automatically
const changedAndIncludedFiles = changedFiles.filter((changedFile) =>
isFileIncluded(changedFile)
);
if (changedAndIncludedFiles.length) {
lintReports.push(engine.executeOnFiles(changedAndIncludedFiles));
}
}
// output fixes if `fix` option is provided
if (configuration.options.fix) {
await Promise.all(lintReports.map((lintReport) => CLIEngine.outputFixes(lintReport)));
}
// store results
for (const lintReport of lintReports) {
for (const lintResult of lintReport.results) {
lintResults.set(lintResult.filePath, lintResult);
}
}
// get actual list of previous and current reports
const results = Array.from(lintResults.values());
return createIssuesFromEsLintResults(results);
},
async close() {
// do nothing
},
};
},
};
}
export { createEsLintReporter };