Skip to content

Commit

Permalink
CLI: Support a maxErrors option to limit the number of reported errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed Oct 2, 2014
1 parent 37cf0e8 commit 2d57926
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions bin/jscs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ program
.option('-p, --preset <preset>', 'preset config')
.option('-r, --reporter <reporter>', 'error reporter, console - default, text, checkstyle, junit, inline')
.option('-v, --verbose', 'adds rule names to the error output')
.option('-m, --max-errors <number>', 'maximum number of errors to report')
.option('', 'Also accepts relative or absolute path to custom reporter')
.option('', 'For instance:')
.option('', '\t ../some-dir/my-reporter.js\t(relative path with extension)')
Expand Down
2 changes: 2 additions & 0 deletions lib/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Checker.prototype.configure = function(config) {
excludeFiles(config, this, cwd);
additionalRules(config, this, cwd);

this._maxErrors = config.maxErrors || 50;

StringChecker.prototype.configure.apply(this, arguments);
};

Expand Down
4 changes: 4 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ module.exports = function(program) {
config.preset = program.preset;
}

if (program.maxErrors) {
config.maxErrors = Number(program.maxErrors);
}

if (program.reporter) {
reporterPath = path.resolve(process.cwd(), program.reporter);
returnArgs.reporter = reporterPath;
Expand Down
5 changes: 5 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Errors.prototype = {
return;
}

// We're trying to add more than the max number of errors
if (typeof Errors.maxErrors !== 'undefined' && this.getErrorCount() === Errors.maxErrors) {
throw new Error();
}

this._errorList.push({
rule: this._currentRule,
message: this._verbose ? this._currentRule + ': ' + message : message,
Expand Down
23 changes: 18 additions & 5 deletions lib/string-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ StringChecker.prototype = {

preset.extend(config);

// Static property to limit the total number of errors reported
Errors.maxErrors = config.maxErrors || 50;
// To avoid being mistaken for an unsupported rule
delete config.maxErrors;

var configRules = Object.keys(config);
var activeRules = this._activeRules;

Expand Down Expand Up @@ -245,25 +250,33 @@ StringChecker.prototype = {
} catch (e) {
parseError = e;
}

var file = new JsFile(filename, str, tree);
var errors = new Errors(file, this._verbose);

if (parseError) {
errors.setCurrentRule('parseError');
errors.add(parseError.description, parseError.lineNumber, parseError.column);
try {
errors.add(parseError.description, parseError.lineNumber, parseError.column);
} catch (e) {}

return errors;
}

this._activeRules.forEach(function(rule) {
var rule;
for (var i = 0, l = this._activeRules.length; i < l; i++) {
rule = this._activeRules[i];

// Do not process the rule if it's equals to null (#203)
if (this._config[rule.getOptionName()] !== null) {
errors.setCurrentRule(rule.getOptionName());
rule.check(file, errors);
try {
rule.check(file, errors);
} catch (e) {
break;
}
}

}, this);
}

// sort errors list to show errors as they appear in source
errors.getErrorList().sort(function(a, b) {
Expand Down
23 changes: 23 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,27 @@ describe('modules/cli', function() {
});
});
});

describe('maxErrors option', function() {
beforeEach(function() {
sinon.spy(console, 'log');
});

afterEach(function() {
console.log.restore();
});

it('should limit the number of errors reported to the provided amount', function(done) {
return cli({
maxErrors: 1,
args: ['test/data/cli/error.js'],
config: 'test/data/cli/maxErrors.json'
})
.promise.always(function() {
assert(console.log.getCall(1).args[0].indexOf('1 code style error found.') !== -1);
rAfter();
done();
});
});
});
});
4 changes: 4 additions & 0 deletions test/data/cli/maxErrors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"disallowKeywords": ["with"],
"requireSpaceBeforePostfixUnaryOperators": ["++"]
}
2 changes: 1 addition & 1 deletion test/rules/validate-indentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('rules/validate-indentation', function() {
});

it('should validate 2 spaces indentation properly', function() {
checker.configure({ validateIndentation: 2 });
checker.configure({ validateIndentation: 2, maxErrors: 500 });
checkErrors(fixture, [
5,
10,
Expand Down

0 comments on commit 2d57926

Please sign in to comment.