diff --git a/src/linter.js b/src/linter.js index 02e3058..ad34a64 100644 --- a/src/linter.js +++ b/src/linter.js @@ -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, diff --git a/src/options.js b/src/options.js index d6ea242..a42e6ac 100644 --- a/src/options.js +++ b/src/options.js @@ -48,6 +48,7 @@ export function getOptions(pluginOptions) { const options = { extensions: 'js', ...pluginOptions, + ...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}), }; // @ts-ignore diff --git a/test/emit-error.test.js b/test/emit-error.test.js new file mode 100644 index 0000000..6048964 --- /dev/null +++ b/test/emit-error.test.js @@ -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(); + }); + }); +}); diff --git a/test/emit-warning.test.js b/test/emit-warning.test.js new file mode 100644 index 0000000..1eb0feb --- /dev/null +++ b/test/emit-warning.test.js @@ -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(); + }); + }); +}); diff --git a/test/fixtures/full-of-problems-entry.js b/test/fixtures/full-of-problems-entry.js new file mode 100644 index 0000000..03da8c9 --- /dev/null +++ b/test/fixtures/full-of-problems-entry.js @@ -0,0 +1 @@ +require('./full-of-problems'); \ No newline at end of file diff --git a/test/fixtures/full-of-problems.js b/test/fixtures/full-of-problems.js new file mode 100644 index 0000000..b6ab6ca --- /dev/null +++ b/test/fixtures/full-of-problems.js @@ -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 + } +}; \ No newline at end of file diff --git a/test/force-emit-error.test.js b/test/force-emit-error.test.js deleted file mode 100644 index e3c0242..0000000 --- a/test/force-emit-error.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import pack from './utils/pack'; - -describe('force emit error', () => { - it('should force to emit error', (done) => { - const compiler = pack('error-warn', { emitError: true }); - - compiler.run((err, stats) => { - expect(err).toBeNull(); - expect(stats.hasWarnings()).toBe(false); - expect(stats.hasErrors()).toBe(true); - done(); - }); - }); -}); diff --git a/test/force-emit-warning.test.js b/test/force-emit-warning.test.js deleted file mode 100644 index 8daa194..0000000 --- a/test/force-emit-warning.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import pack from './utils/pack'; - -describe('force emit warning', () => { - it('should force to emit warning', (done) => { - const compiler = pack('warn-error', { emitWarning: true }); - - compiler.run((err, stats) => { - expect(err).toBeNull(); - expect(stats.hasWarnings()).toBe(true); - expect(stats.hasErrors()).toBe(false); - done(); - }); - }); -}); diff --git a/test/quiet.test.js b/test/quiet.test.js index 6b35928..c29dd5c 100644 --- a/test/quiet.test.js +++ b/test/quiet.test.js @@ -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(); + }); + }); });