Skip to content

Commit

Permalink
Close #93 PR: Fixed issue when "autofix" doesn't fix all files..
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan.lobor authored and sindresorhus committed Oct 2, 2015
1 parent 002c9b5 commit aa9db33
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
13 changes: 10 additions & 3 deletions index.js
Expand Up @@ -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';

Expand All @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions test.js
Expand Up @@ -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);
Expand Down

0 comments on commit aa9db33

Please sign in to comment.