diff --git a/index.js b/index.js index 0733055..f194054 100644 --- a/index.js +++ b/index.js @@ -9,12 +9,11 @@ var loadConfigFile = require('jscs/lib/cli-config'); module.exports = function (opts) { opts = opts || {}; + var config; var checker = new Checker(); - checker.registerDefaultRules(); - try { - checker.configure(loadConfigFile.load(opts.configPath)); + config = loadConfigFile.load(opts.configPath); } catch (err) { err.message = 'Unable to load JSCS config file'; @@ -27,6 +26,14 @@ module.exports = function (opts) { throw err; } + // run autofix over as many errors as possible + if (opts.fix) { + config.maxErrors = Infinity; + } + + checker.registerDefaultRules(); + checker.configure(config); + return through.obj(function (file, enc, cb) { if (file.isNull()) { cb(null, file); diff --git a/test.js b/test.js index 07f4655..1c2ea8f 100644 --- a/test.js +++ b/test.js @@ -147,6 +147,43 @@ it('should accept the fix option', function (cb) { stream.end(); }); +it('should run autofix over as many errors as possible', function (done) { + var config = { + maxErrors: 1, + requireSpaceBeforeBinaryOperators: ['='] + }; + var validJS = 'var foo =1;\nvar bar =2;'; + var invalidJS = 'var foo=1;\nvar bar=2;'; + + var stream = jscs({ + fix: true, + configPath: tempWrite.sync(JSON.stringify(config)) + }); + + stream + .pipe(streamAssert.first(function (file) { + assert.equal(file.contents.toString(), validJS); + })) + .pipe(streamAssert.second(function (file) { + assert.equal(file.contents.toString(), validJS); + })) + .pipe(streamAssert.end(done)); + + stream.write(new gutil.File({ + base: __dirname, + path: path.join(__dirname, 'fixture.js'), + contents: new Buffer(invalidJS) + })); + + stream.write(new gutil.File({ + base: __dirname, + path: path.join(__dirname, 'fixture2.js'), + contents: new Buffer(invalidJS) + })); + + stream.end(); +}); + it('should not mutate the options object passed as argument', function () { var options = {foo: true}; jscs(options);