Skip to content

Commit

Permalink
feat(rule-whitelist): Add cli option to specify a whitelist of ESLint…
Browse files Browse the repository at this point in the history
… rules.
  • Loading branch information
Bryan Smith authored and amanda-mitchell committed Feb 22, 2019
1 parent 832bf8f commit a66d8f6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ Be sure to re-run any code formatting tools you use before committing!

## Options

If you'd like a message other than, `TODO: Fix this the next time the file is edited.`, you can specify this with the `--message` commandline flag.
**--message**: Sets the comment to add above eslint-disable-next-line comments.

**--rules**: Comma-separated list of ESLint rule IDs to disable. When specified, violations of rules not in this set will be left in place.

## Examples

Suppress all rule violations in the `index.js` file, using a custom comment:

```bash
npx suppress-eslint-errors ./index.js --message="TODO: Issue #123"
```

Suppress violations of the `eqeqeq` and `@typescript-eslint/no-explicit-any` rules in .ts and .tsx files in the `src` directory:

```bash
npx suppress-eslint-errors ./src --extensions=ts,tsx --parser=tsx --rules=eqeqeq,@typescript-eslint/no-explicit-any
```

## Is it perfect?

Expand Down
16 changes: 16 additions & 0 deletions transforms/__tests__/suppress-eslint-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ test('supports alternative messages in jsx', () => {
}`);
});

test('supports rule whitelist in javascript', () => {
const program = `export function foo(a, b) {
return a == b;
console.log('unreachable');
}
`;

expect(modifySource(program, { rules: 'no-unreachable' })).toBe(`export function foo(a, b) {
return a == b;
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line no-unreachable
console.log('unreachable');
}
`);
});

const defaultPath = path.resolve(__dirname, 'examples', 'index.js');
function modifySource(source, options) {
return codeMod(
Expand Down
7 changes: 7 additions & 0 deletions transforms/suppress-eslint-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ module.exports = function codeMod(file, api, options) {
? options.message
: 'TODO: Fix this the next time the file is edited.';

const ruleIdWhitelist = (options.rules || '').split(',').filter(x => x);
const ruleIdWhitelistSet = ruleIdWhitelist.length ? new Set(ruleIdWhitelist) : null;

for (const { targetLine, ruleId } of targets) {
if (ruleIdWhitelistSet && !ruleIdWhitelistSet.has(ruleId)) {
continue;
}

const firstPathOnLine = result
.find(
'Node',
Expand Down

0 comments on commit a66d8f6

Please sign in to comment.