Skip to content

Commit aa9db33

Browse files
bogdan.loborsindresorhus
authored andcommitted
Close #93 PR: Fixed issue when "autofix" doesn't fix all files..
1 parent 002c9b5 commit aa9db33

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ var loadConfigFile = require('jscs/lib/cli-config');
99
module.exports = function (opts) {
1010
opts = opts || {};
1111

12+
var config;
1213
var checker = new Checker();
1314

14-
checker.registerDefaultRules();
15-
1615
try {
17-
checker.configure(loadConfigFile.load(opts.configPath));
16+
config = loadConfigFile.load(opts.configPath);
1817
} catch (err) {
1918
err.message = 'Unable to load JSCS config file';
2019

@@ -27,6 +26,14 @@ module.exports = function (opts) {
2726
throw err;
2827
}
2928

29+
// run autofix over as many errors as possible
30+
if (opts.fix) {
31+
config.maxErrors = Infinity;
32+
}
33+
34+
checker.registerDefaultRules();
35+
checker.configure(config);
36+
3037
return through.obj(function (file, enc, cb) {
3138
if (file.isNull()) {
3239
cb(null, file);

test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,43 @@ it('should accept the fix option', function (cb) {
147147
stream.end();
148148
});
149149

150+
it('should run autofix over as many errors as possible', function (done) {
151+
var config = {
152+
maxErrors: 1,
153+
requireSpaceBeforeBinaryOperators: ['=']
154+
};
155+
var validJS = 'var foo =1;\nvar bar =2;';
156+
var invalidJS = 'var foo=1;\nvar bar=2;';
157+
158+
var stream = jscs({
159+
fix: true,
160+
configPath: tempWrite.sync(JSON.stringify(config))
161+
});
162+
163+
stream
164+
.pipe(streamAssert.first(function (file) {
165+
assert.equal(file.contents.toString(), validJS);
166+
}))
167+
.pipe(streamAssert.second(function (file) {
168+
assert.equal(file.contents.toString(), validJS);
169+
}))
170+
.pipe(streamAssert.end(done));
171+
172+
stream.write(new gutil.File({
173+
base: __dirname,
174+
path: path.join(__dirname, 'fixture.js'),
175+
contents: new Buffer(invalidJS)
176+
}));
177+
178+
stream.write(new gutil.File({
179+
base: __dirname,
180+
path: path.join(__dirname, 'fixture2.js'),
181+
contents: new Buffer(invalidJS)
182+
}));
183+
184+
stream.end();
185+
});
186+
150187
it('should not mutate the options object passed as argument', function () {
151188
var options = {foo: true};
152189
jscs(options);

0 commit comments

Comments
 (0)