From 704024864f737a487ec0b9f9c9b00b11881f2558 Mon Sep 17 00:00:00 2001 From: eschablowski <31389869+eschablowski@users.noreply.github.com> Date: Wed, 19 Sep 2018 13:20:17 -0700 Subject: [PATCH] Stop fixing loop (#244) * Stop Fixing if there are no fixable errors or warnings * Update README.md * Added Tests for infinte loop * Fix Typo --- README.md | 3 +- index.js | 2 +- test/autofix-stop.js | 56 +++++++++++++++++++++++++++++++++++++ test/fixtures/nonfixable.js | 5 ++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/autofix-stop.js create mode 100644 test/fixtures/nonfixable.js diff --git a/README.md b/README.md index 36b00d8..071a636 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,7 @@ See the [eslint docs](http://eslint.org/docs/developer-guide/nodejs-api#cliengin This option will enable [ESLint autofix feature](http://eslint.org/docs/user-guide/command-line-interface#fix). -**Be careful: this option might cause webpack to enter an infinite build loop if -some issues cannot be fixed properly.** +**Be careful: this option will change source files.** #### `cache` (default: false) diff --git a/index.js b/index.js index 645f2b0..bee3171 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,7 @@ function printLinterOutput(res, config, webpack) { } // if enabled, use eslint auto-fixing where possible - if (config.fix && res.results[0].output) { + if (config.fix && (res.results[0].fixableErrorCount > 0 || res.results[0].fixableWarningCount)) { var eslint = require(config.eslintPath); eslint.CLIEngine.outputFixes(res); } diff --git a/test/autofix-stop.js b/test/autofix-stop.js new file mode 100644 index 0000000..86db406 --- /dev/null +++ b/test/autofix-stop.js @@ -0,0 +1,56 @@ +var fs = require("fs"); + +var test = require("ava"); +var webpack = require("webpack"); + +var conf = require("./utils/conf"); + +var changed = false; + +// clone the "fixable" file, so that we do not lose the original contents +// when the fixes are applied to disk +test.before(function() { + fs.createReadStream("./test/fixtures/nonfixable.js") + .pipe(fs.createWriteStream("./test/fixtures/nonfixable-clone.js")) + .on("close", function() { + fs.watch("./test/fixtures/nonfixable-clone.js", function() { + changed = true; + }); + }); +}); + +test.cb("loader shouldn't change file if there are no fixable errors/warnings", function( + t +) { + t.plan(1); + webpack( + conf({ + entry: "./test/fixtures/nonfixable-clone.js", + module: { + rules: [ + { + test: /\.js$/, + use: "./index?fix=true", + exclude: /node_modules/ + } + ] + } + }), + function(err) { + if (err) { + throw err; + } + // console.log(stats.compilation.errors) + t.false( + changed, + "should not output to file again (triggering a recompile)" + ); + t.end(); + } + ); +}); + +// remove the clone +test.after.always(function() { + fs.unlinkSync("./test/fixtures/nonfixable-clone.js"); +}); diff --git a/test/fixtures/nonfixable.js b/test/fixtures/nonfixable.js new file mode 100644 index 0000000..ff1302a --- /dev/null +++ b/test/fixtures/nonfixable.js @@ -0,0 +1,5 @@ +function foo() { + return stuff; +} + +foo();