Skip to content

Commit

Permalink
add max-warnings option (#75)
Browse files Browse the repository at this point in the history
The eslint CLI has a `--max-warnings` option which allows a user to
specify the maximun number of warnings after which the lint should
fail. This adds this same feature to esprint.
  • Loading branch information
rufman authored and bradencanderson committed Sep 20, 2018
1 parent b597e94 commit fcdc007
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Options:
|**`port`**|`{Number}`|(optional) Run the esprint background server on a specific port|
|**`formatter`**|`{string}`|(optional) Use a specific output format - default: stylish|
|**`quiet`**|`{boolean}`|(optional) Report errors only - default: false|
|**`maxWarnings`**|`{number}`|(optional) The max number of warnings that should trigger a failure. The default is to not fail on warnings|

## Usage

Expand Down
7 changes: 5 additions & 2 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ import { clearLine } from './cliUtils';

export default class Client {
constructor(options) {
const { port, formatter } = options;
const { port, formatter, maxWarnings } = options;
const eslint = new CLIEngine();
this.port = port;
this.formatter = eslint.getFormatter(formatter);
this.maxWarnings = maxWarnings;
}

connect() {
const d = dnode.connect(this.port);
const formatter = this.formatter;
const maxWarnings = this.maxWarnings;
d.on('remote', function(remote) {
setInterval(() => {
remote.status('', results => {
if (!results.message) {
d.end();
console.log(formatter(results.records));
process.exit(results && results.errorCount > 0 ? 1 : 0);
process.exit(results && (results.errorCount > 0 ? 1 : 0
|| results.warningCount > maxWarnings ? 1 : 0));
} else {
clearLine();
process.stdout.write(results.message);
Expand Down
4 changes: 3 additions & 1 deletion src/commands/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const check = (options) => {
paths,
formatter,
rcPath,
maxWarnings,
quiet,
} = options;

Expand All @@ -31,6 +32,7 @@ export const check = (options) => {

const lintFormatter = eslint.getFormatter(formatter);
console.log(lintFormatter(records));
process.exit(results && results.errorCount > 0 ? 1 : 0);
process.exit(results && (results.errorCount > 0 ? 1 : 0
|| results.warningCount > maxWarnings ? 1 : 0));
});
};
1 change: 1 addition & 0 deletions tests/fail-on-warning/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__tests__/*
9 changes: 9 additions & 0 deletions tests/fail-on-warning/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"rules": {
"no-var": 1
},
"env": {
"es6": true
},
"root": true
}
12 changes: 12 additions & 0 deletions tests/fail-on-warning/.esprintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

{
"port": 5004 ,
"paths": [
"fixture.js"
],

"ignored": [
"**/node_modules/**/*"
],
"maxWarnings": 0
}
28 changes: 28 additions & 0 deletions tests/fail-on-warning/__tests__/fail-on-warning.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');
const runEsprint = require('../../runEsprint.js');
const killProcess = require('../../killProcess.js');
const getPid = require('../../getPid.js');

beforeEach(() => {
killProcess();
});

afterEach(() => {
killProcess();
});

test('Properly lints and returns errors with server', () => {
const results = runEsprint(path.join(__dirname, '..'));
const expectedError = expect.stringContaining('warning Unexpected var, use let or const instead no-var');
expect(results.error).toBeDefined();
expect(results.error.stdout.toString()).toEqual(expectedError);
expect(getPid()).toBeDefined();
});

test('Properly lints and returns errors without server', () => {
const results = runEsprint(path.join(__dirname, '..'), 'check');
const expectedError = expect.stringContaining('warning Unexpected var, use let or const instead no-var');
expect(results.error).toBeDefined();
expect(results.error.stdout.toString()).toEqual(expectedError);
expect(getPid()).toBeUndefined();
});
2 changes: 2 additions & 0 deletions tests/fail-on-warning/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var x=0;
console.log(x);

0 comments on commit fcdc007

Please sign in to comment.