This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathLinter.js
207 lines (167 loc) · 5.08 KB
/
Linter.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import process from 'process';
import { isAbsolute, join } from 'path';
import { writeFileSync, ensureFileSync } from 'fs-extra';
import { interpolateName } from 'loader-utils';
import ESLintError from './ESLintError';
import createEngine from './createEngine';
export default class Linter {
constructor(loaderContext, options) {
this.loaderContext = loaderContext;
this.options = options;
this.resourcePath = this.parseResourcePath();
const { CLIEngine, engine } = createEngine(options);
this.CLIEngine = CLIEngine;
this.engine = engine;
}
parseResourcePath() {
const cwd = process.cwd();
let { resourcePath } = this.loaderContext;
// remove cwd from resource path in case webpack has been started from project
// root, to allow having relative paths in .eslintignore
// istanbul ignore next
if (resourcePath.indexOf(cwd) === 0) {
resourcePath = resourcePath.substr(cwd.length + (cwd === '/' ? 0 : 1));
}
return resourcePath;
}
lint(content) {
try {
return this.engine.executeOnText(content, this.resourcePath, true);
} catch (_) {
this.getEmitter(false)(_);
return { src: content };
}
}
printOutput(data) {
const { options } = this;
// skip ignored file warning
if (this.constructor.skipIgnoredFileWarning(data)) {
return;
}
// quiet filter done now
// eslint allow rules to be specified in the input between comments
// so we can found warnings defined in the input itself
const res = this.filter(data);
// if enabled, use eslint auto-fixing where possible
if (options.fix) {
this.autoFix(res);
}
// skip if no errors or warnings
if (res.errorCount < 1 && res.warningCount < 1) {
return;
}
const results = this.parseResults(res);
// Do not analyze if there are no results or eslint config
if (!results) {
return;
}
const messages = options.formatter(results);
this.reportOutput(results, messages);
this.failOnErrorOrWarning(res, messages);
const emitter = this.getEmitter(res);
emitter(new ESLintError(messages));
}
static skipIgnoredFileWarning(res) {
return (
res &&
res.warningCount === 1 &&
res.results &&
res.results[0] &&
res.results[0].messages[0] &&
res.results[0].messages[0].message &&
res.results[0].messages[0].message.indexOf('ignore') > 1
);
}
filter(data) {
const res = data;
// quiet filter done now
// eslint allow rules to be specified in the input between comments
// so we can found warnings defined in the input itself
if (
this.options.quiet &&
res &&
res.warningCount &&
res.results &&
res.results[0]
) {
res.warningCount = 0;
res.results[0].warningCount = 0;
res.results[0].messages = res.results[0].messages.filter(
(message) => message.severity !== 1
);
}
return res;
}
autoFix(res) {
if (
res &&
res.results &&
res.results[0] &&
(res.results[0].output !== res.src ||
res.results[0].fixableErrorCount > 0 ||
res.results[0].fixableWarningCount > 0)
) {
this.CLIEngine.outputFixes(res);
}
}
parseResults({ results }) {
// add filename for each results so formatter can have relevant filename
if (results) {
results.forEach((r) => {
// eslint-disable-next-line no-param-reassign
r.filePath = this.loaderContext.resourcePath;
});
}
return results;
}
reportOutput(results, messages) {
const { outputReport } = this.options;
if (!outputReport || !outputReport.filePath) {
return;
}
let content = messages;
// if a different formatter is passed in as an option use that
if (outputReport.formatter) {
content = outputReport.formatter(results);
}
let filePath = interpolateName(this.loaderContext, outputReport.filePath, {
content,
});
if (!isAbsolute(filePath)) {
filePath = join(
// eslint-disable-next-line no-underscore-dangle
this.loaderContext._compiler.options.output.path,
filePath
);
}
ensureFileSync(filePath);
writeFileSync(filePath, content);
}
failOnErrorOrWarning({ errorCount, warningCount }, messages) {
const { failOnError, failOnWarning } = this.options;
if (failOnError && errorCount) {
throw new ESLintError(
`Module failed because of a eslint error.\n${messages}`
);
}
if (failOnWarning && warningCount) {
throw new ESLintError(
`Module failed because of a eslint warning.\n${messages}`
);
}
}
getEmitter({ errorCount }) {
const { options, loaderContext } = this;
// default behavior: emit error only if we have errors
let emitter = errorCount
? loaderContext.emitError
: loaderContext.emitWarning;
// force emitError or emitWarning if user want this
if (options.emitError) {
emitter = loaderContext.emitError;
} else if (options.emitWarning) {
emitter = loaderContext.emitWarning;
}
return emitter;
}
}