Skip to content

Commit d38165b

Browse files
Fix emit warning error quiet (#46)
* test: add full-of-problems test from eslint example - https://eslint.org/docs/user-guide/formatters/#eslint-formatters - This provides a single file with a variety of errors and warnings * test: add test using full-of-problems and quiet flag - when linting a file that has both warnings and errors, if the quiet flag is set, then the errors should be emited, but the warnings should not * test: add emit-error and emit-warning tests - replace force-emit-error and force-emit-warning tests with more robust tests that match the functionality of eslint-loader - if emitError is undefined or true, it should emit errors, otherwise if emitError is false, it should suppress errors - if emitWarning is undefined or true, it should emit warnings, otherwise if emitWarning is false, it should suppress warnings * fix: suppress warnings when emitWarning is set to false - print errors only when emitError is undefined or true, otherwise suppress errors - print warnings only when emitWarning is undefined or true, otherwise suppress warnings - this allows the config to be set up to lint a file that has both warnings and errors and only show the relevant errors or warnings fix #19 BREAKING CHANGE: Updates to emitError and emitWarning Setting only emitError to true will no longer exclusively print files with errors and disregard the files with warnings. Similarly, setting only emitWarning to true will no longer exclusively print files with warnings disregard the files with errors. * fix: use quiet to override emitError and emitWarning - quiet is essentially syntactic sugar for setting emitError to true and emitWarning to false fix #19
1 parent f7f372e commit d38165b

9 files changed

+181
-48
lines changed

src/linter.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,29 +177,48 @@ function formatResults(formatter, results) {
177177
*/
178178
function parseResults(options, results) {
179179
/** @type {LintResult[]} */
180-
let errors = [];
180+
const errors = [];
181181

182182
/** @type {LintResult[]} */
183-
let warnings = [];
184-
185-
if (options.emitError) {
186-
errors = results.filter(
187-
(file) => fileHasErrors(file) || fileHasWarnings(file)
188-
);
189-
} else if (options.emitWarning) {
190-
warnings = results.filter(
191-
(file) => fileHasErrors(file) || fileHasWarnings(file)
192-
);
193-
} else {
194-
warnings = results.filter(
195-
(file) => !fileHasErrors(file) && fileHasWarnings(file)
196-
);
197-
errors = results.filter(fileHasErrors);
198-
}
183+
const warnings = [];
184+
185+
results.forEach((file) => {
186+
if (fileHasErrors(file)) {
187+
const messages = file.messages.filter((message) => {
188+
if (options.emitError === undefined) {
189+
return true;
190+
} else if (options.emitError) {
191+
return message.severity === 2;
192+
}
193+
return false;
194+
});
195+
196+
if (messages.length > 0) {
197+
errors.push({
198+
...file,
199+
messages,
200+
});
201+
}
202+
}
199203

200-
if (options.quiet && warnings.length > 0) {
201-
warnings = [];
202-
}
204+
if (fileHasWarnings(file)) {
205+
const messages = file.messages.filter((message) => {
206+
if (options.emitWarning === undefined) {
207+
return true;
208+
} else if (options.emitWarning) {
209+
return message.severity === 1;
210+
}
211+
return false;
212+
});
213+
214+
if (messages.length > 0) {
215+
warnings.push({
216+
...file,
217+
messages,
218+
});
219+
}
220+
}
221+
});
203222

204223
return {
205224
errors,

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export function getOptions(pluginOptions) {
4848
const options = {
4949
extensions: 'js',
5050
...pluginOptions,
51+
...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}),
5152
};
5253

5354
// @ts-ignore

test/emit-error.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pack from './utils/pack';
2+
3+
describe('emit error', () => {
4+
it('should not emit errors if emitError is false', (done) => {
5+
const compiler = pack('error', { emitError: false });
6+
7+
compiler.run((err, stats) => {
8+
expect(err).toBeNull();
9+
expect(stats.hasErrors()).toBe(false);
10+
done();
11+
});
12+
});
13+
14+
it('should emit errors if emitError is undefined', (done) => {
15+
const compiler = pack('error', {});
16+
17+
compiler.run((err, stats) => {
18+
expect(err).toBeNull();
19+
expect(stats.hasErrors()).toBe(true);
20+
done();
21+
});
22+
});
23+
24+
it('should emit errors if emitError is true', (done) => {
25+
const compiler = pack('error', { emitError: true });
26+
27+
compiler.run((err, stats) => {
28+
expect(err).toBeNull();
29+
expect(stats.hasErrors()).toBe(true);
30+
done();
31+
});
32+
});
33+
34+
it('should emit errors, but not warnings if emitError is true and emitWarning is false', (done) => {
35+
const compiler = pack('full-of-problems', {
36+
emitError: true,
37+
emitWarning: false,
38+
});
39+
40+
compiler.run((err, stats) => {
41+
expect(err).toBeNull();
42+
expect(stats.hasWarnings()).toBe(false);
43+
expect(stats.hasErrors()).toBe(true);
44+
done();
45+
});
46+
});
47+
48+
it('should emit errors and warnings if emitError is true and emitWarning is undefined', (done) => {
49+
const compiler = pack('full-of-problems', { emitError: true });
50+
51+
compiler.run((err, stats) => {
52+
expect(err).toBeNull();
53+
expect(stats.hasWarnings()).toBe(true);
54+
expect(stats.hasErrors()).toBe(true);
55+
done();
56+
});
57+
});
58+
});

test/emit-warning.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pack from './utils/pack';
2+
3+
describe('emit warning', () => {
4+
it('should not emit warnings if emitWarning is false', (done) => {
5+
const compiler = pack('warn', { emitWarning: false });
6+
7+
compiler.run((err, stats) => {
8+
expect(err).toBeNull();
9+
expect(stats.hasWarnings()).toBe(false);
10+
done();
11+
});
12+
});
13+
14+
it('should emit warnings if emitWarning is undefined', (done) => {
15+
const compiler = pack('warn', {});
16+
17+
compiler.run((err, stats) => {
18+
expect(err).toBeNull();
19+
expect(stats.hasWarnings()).toBe(true);
20+
done();
21+
});
22+
});
23+
24+
it('should emit warnings if emitWarning is true', (done) => {
25+
const compiler = pack('warn', { emitWarning: true });
26+
27+
compiler.run((err, stats) => {
28+
expect(err).toBeNull();
29+
expect(stats.hasWarnings()).toBe(true);
30+
done();
31+
});
32+
});
33+
34+
it('should emit warnings, but not warnings if emitWarning is true and emitError is false', (done) => {
35+
const compiler = pack('full-of-problems', {
36+
emitWarning: true,
37+
emitError: false,
38+
});
39+
40+
compiler.run((err, stats) => {
41+
expect(err).toBeNull();
42+
expect(stats.hasWarnings()).toBe(true);
43+
expect(stats.hasErrors()).toBe(false);
44+
done();
45+
});
46+
});
47+
48+
it('should emit warnings and errors if emitWarning is true and emitError is undefined', (done) => {
49+
const compiler = pack('full-of-problems', { emitWarning: true });
50+
51+
compiler.run((err, stats) => {
52+
expect(err).toBeNull();
53+
expect(stats.hasWarnings()).toBe(true);
54+
expect(stats.hasErrors()).toBe(true);
55+
done();
56+
});
57+
});
58+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./full-of-problems');

test/fixtures/full-of-problems.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint consistent-return: "error" */
2+
/* eslint indent: ["warn", 4] */
3+
/* eslint no-else-return: "warn" */
4+
/* eslint semi: ["warn", "always"] */
5+
/* eslint space-unary-ops: "error" */
6+
7+
function addOne(i) {
8+
if (i != NaN) {
9+
return i ++
10+
} else {
11+
return
12+
}
13+
};

test/force-emit-error.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/force-emit-warning.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/quiet.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,15 @@ describe('quiet', () => {
1111
done();
1212
});
1313
});
14+
15+
it('should emit errors, but not emit warnings if quiet is set', (done) => {
16+
const compiler = pack('full-of-problems', { quiet: true });
17+
18+
compiler.run((err, stats) => {
19+
expect(err).toBeNull();
20+
expect(stats.hasWarnings()).toBe(false);
21+
expect(stats.hasErrors()).toBe(true);
22+
done();
23+
});
24+
});
1425
});

0 commit comments

Comments
 (0)