Skip to content

Commit

Permalink
Fix emit warning error quiet (#46)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
michaelangeliu authored Jan 25, 2021
1 parent f7f372e commit d38165b
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 48 deletions.
59 changes: 39 additions & 20 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,29 +177,48 @@ function formatResults(formatter, results) {
*/
function parseResults(options, results) {
/** @type {LintResult[]} */
let errors = [];
const errors = [];

/** @type {LintResult[]} */
let warnings = [];

if (options.emitError) {
errors = results.filter(
(file) => fileHasErrors(file) || fileHasWarnings(file)
);
} else if (options.emitWarning) {
warnings = results.filter(
(file) => fileHasErrors(file) || fileHasWarnings(file)
);
} else {
warnings = results.filter(
(file) => !fileHasErrors(file) && fileHasWarnings(file)
);
errors = results.filter(fileHasErrors);
}
const warnings = [];

results.forEach((file) => {
if (fileHasErrors(file)) {
const messages = file.messages.filter((message) => {
if (options.emitError === undefined) {
return true;
} else if (options.emitError) {
return message.severity === 2;
}
return false;
});

if (messages.length > 0) {
errors.push({
...file,
messages,
});
}
}

if (options.quiet && warnings.length > 0) {
warnings = [];
}
if (fileHasWarnings(file)) {
const messages = file.messages.filter((message) => {
if (options.emitWarning === undefined) {
return true;
} else if (options.emitWarning) {
return message.severity === 1;
}
return false;
});

if (messages.length > 0) {
warnings.push({
...file,
messages,
});
}
}
});

return {
errors,
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function getOptions(pluginOptions) {
const options = {
extensions: 'js',
...pluginOptions,
...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}),
};

// @ts-ignore
Expand Down
58 changes: 58 additions & 0 deletions test/emit-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pack from './utils/pack';

describe('emit error', () => {
it('should not emit errors if emitError is false', (done) => {
const compiler = pack('error', { emitError: false });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(false);
done();
});
});

it('should emit errors if emitError is undefined', (done) => {
const compiler = pack('error', {});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
});

it('should emit errors if emitError is true', (done) => {
const compiler = pack('error', { emitError: true });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
});

it('should emit errors, but not warnings if emitError is true and emitWarning is false', (done) => {
const compiler = pack('full-of-problems', {
emitError: true,
emitWarning: false,
});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
done();
});
});

it('should emit errors and warnings if emitError is true and emitWarning is undefined', (done) => {
const compiler = pack('full-of-problems', { emitError: true });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
done();
});
});
});
58 changes: 58 additions & 0 deletions test/emit-warning.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pack from './utils/pack';

describe('emit warning', () => {
it('should not emit warnings if emitWarning is false', (done) => {
const compiler = pack('warn', { emitWarning: false });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
done();
});
});

it('should emit warnings if emitWarning is undefined', (done) => {
const compiler = pack('warn', {});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
done();
});
});

it('should emit warnings if emitWarning is true', (done) => {
const compiler = pack('warn', { emitWarning: true });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
done();
});
});

it('should emit warnings, but not warnings if emitWarning is true and emitError is false', (done) => {
const compiler = pack('full-of-problems', {
emitWarning: true,
emitError: false,
});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(false);
done();
});
});

it('should emit warnings and errors if emitWarning is true and emitError is undefined', (done) => {
const compiler = pack('full-of-problems', { emitWarning: true });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
done();
});
});
});
1 change: 1 addition & 0 deletions test/fixtures/full-of-problems-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./full-of-problems');
13 changes: 13 additions & 0 deletions test/fixtures/full-of-problems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint consistent-return: "error" */
/* eslint indent: ["warn", 4] */
/* eslint no-else-return: "warn" */
/* eslint semi: ["warn", "always"] */
/* eslint space-unary-ops: "error" */

function addOne(i) {
if (i != NaN) {
return i ++
} else {
return
}
};
14 changes: 0 additions & 14 deletions test/force-emit-error.test.js

This file was deleted.

14 changes: 0 additions & 14 deletions test/force-emit-warning.test.js

This file was deleted.

11 changes: 11 additions & 0 deletions test/quiet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ describe('quiet', () => {
done();
});
});

it('should emit errors, but not emit warnings if quiet is set', (done) => {
const compiler = pack('full-of-problems', { quiet: true });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
done();
});
});
});

0 comments on commit d38165b

Please sign in to comment.