Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix emit warning error quiet #46

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});