From d5f55c0545a199ea759bba67fbac1fc36af6a53a Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Wed, 10 Jul 2019 09:22:29 -0300 Subject: [PATCH 1/4] fix: emit warning/error if no config was found/given --- index.js | 23 +++++++++++++++++++---- test/no-eslint-configuration.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test/no-eslint-configuration.js diff --git a/index.js b/index.js index 73cc587..b59b15f 100644 --- a/index.js +++ b/index.js @@ -235,6 +235,7 @@ module.exports = function(input, map) { webpack.cacheable(); + var emitter = config.emitError ? webpack.emitError : webpack.emitWarning; var engine = engines[configHash]; var resourcePath = webpack.resourcePath; var cwd = process.cwd(); @@ -255,7 +256,7 @@ module.exports = function(input, map) { options: config, source: input, transform: function() { - return lint(engine, input, resourcePath); + return lint(engine, input, resourcePath, emitter); } }, function(err, res) { @@ -276,10 +277,24 @@ module.exports = function(input, map) { } ); } - printLinterOutput(lint(engine, input, resourcePath), config, webpack); + printLinterOutput( + lint(engine, input, resourcePath, emitter), + config, + webpack + ); webpack.callback(null, input, map); }; -function lint(engine, input, resourcePath) { - return engine.executeOnText(input, resourcePath, true); +function lint(engine, input, resourcePath, emitter) { + if (!emitter) { + return engine.executeOnText(input, resourcePath, true); + } + + try { + return engine.executeOnText(input, resourcePath, true); + } catch (_) { + emitter(_); + + return { src: input }; + } } diff --git a/test/no-eslint-configuration.js b/test/no-eslint-configuration.js new file mode 100644 index 0000000..ce4d437 --- /dev/null +++ b/test/no-eslint-configuration.js @@ -0,0 +1,33 @@ +var test = require("ava"); +var webpack = require("webpack"); + +var conf = require("./utils/conf"); + +test.cb( + "eslint-loader emit warning when there is no eslint configuration", + function(t) { + t.plan(2); + webpack( + conf( + { + entry: "./test/fixtures/good.js" + }, + { + cwd: "/" + } + ), + function(err, stats) { + if (err) { + throw err; + } + + t.true(stats.hasWarnings()); + t.regex( + "No ESLint configuration found in /home/ricardo/code/test/fixtures.", + /^no eslint configuration/i + ); + t.end(); + } + ); + } +); From 3f4cbb061b85b90c9dc2ab775c436e90cb2957f5 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Wed, 10 Jul 2019 09:30:14 -0300 Subject: [PATCH 2/4] test: use compilation message --- test/no-eslint-configuration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/no-eslint-configuration.js b/test/no-eslint-configuration.js index ce4d437..87509b5 100644 --- a/test/no-eslint-configuration.js +++ b/test/no-eslint-configuration.js @@ -23,7 +23,7 @@ test.cb( t.true(stats.hasWarnings()); t.regex( - "No ESLint configuration found in /home/ricardo/code/test/fixtures.", + stats.compilation.warnings[0].message, /^no eslint configuration/i ); t.end(); From 9054a701a83228f697da08186aee42ac137c67e2 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Wed, 10 Jul 2019 09:31:25 -0300 Subject: [PATCH 3/4] refactor: unnecessary condition --- index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/index.js b/index.js index b59b15f..38fa855 100644 --- a/index.js +++ b/index.js @@ -286,14 +286,10 @@ module.exports = function(input, map) { }; function lint(engine, input, resourcePath, emitter) { - if (!emitter) { - return engine.executeOnText(input, resourcePath, true); - } - try { return engine.executeOnText(input, resourcePath, true); } catch (_) { - emitter(_); + if (emitter) emitter(_); return { src: input }; } From d1b0c08e7f1c66ebe03f216d169d07fa3e689805 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Wed, 10 Jul 2019 09:40:24 -0300 Subject: [PATCH 4/4] test: resolve regex --- test/no-eslint-configuration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/no-eslint-configuration.js b/test/no-eslint-configuration.js index 87509b5..1969738 100644 --- a/test/no-eslint-configuration.js +++ b/test/no-eslint-configuration.js @@ -24,7 +24,7 @@ test.cb( t.true(stats.hasWarnings()); t.regex( stats.compilation.warnings[0].message, - /^no eslint configuration/i + /no eslint configuration/i ); t.end(); }