Skip to content

Commit a66d8f6

Browse files
Bryan Smithamanda-mitchell
authored andcommitted
feat(rule-whitelist): Add cli option to specify a whitelist of ESLint rules.
1 parent 832bf8f commit a66d8f6

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,23 @@ Be sure to re-run any code formatting tools you use before committing!
3737

3838
## Options
3939

40-
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.
40+
**--message**: Sets the comment to add above eslint-disable-next-line comments.
41+
42+
**--rules**: Comma-separated list of ESLint rule IDs to disable. When specified, violations of rules not in this set will be left in place.
43+
44+
## Examples
45+
46+
Suppress all rule violations in the `index.js` file, using a custom comment:
47+
48+
```bash
49+
npx suppress-eslint-errors ./index.js --message="TODO: Issue #123"
50+
```
51+
52+
Suppress violations of the `eqeqeq` and `@typescript-eslint/no-explicit-any` rules in .ts and .tsx files in the `src` directory:
53+
54+
```bash
55+
npx suppress-eslint-errors ./src --extensions=ts,tsx --parser=tsx --rules=eqeqeq,@typescript-eslint/no-explicit-any
56+
```
4157

4258
## Is it perfect?
4359

transforms/__tests__/suppress-eslint-errors.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ test('supports alternative messages in jsx', () => {
133133
}`);
134134
});
135135

136+
test('supports rule whitelist in javascript', () => {
137+
const program = `export function foo(a, b) {
138+
return a == b;
139+
console.log('unreachable');
140+
}
141+
`;
142+
143+
expect(modifySource(program, { rules: 'no-unreachable' })).toBe(`export function foo(a, b) {
144+
return a == b;
145+
// TODO: Fix this the next time the file is edited.
146+
// eslint-disable-next-line no-unreachable
147+
console.log('unreachable');
148+
}
149+
`);
150+
});
151+
136152
const defaultPath = path.resolve(__dirname, 'examples', 'index.js');
137153
function modifySource(source, options) {
138154
return codeMod(

transforms/suppress-eslint-errors.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ module.exports = function codeMod(file, api, options) {
3131
? options.message
3232
: 'TODO: Fix this the next time the file is edited.';
3333

34+
const ruleIdWhitelist = (options.rules || '').split(',').filter(x => x);
35+
const ruleIdWhitelistSet = ruleIdWhitelist.length ? new Set(ruleIdWhitelist) : null;
36+
3437
for (const { targetLine, ruleId } of targets) {
38+
if (ruleIdWhitelistSet && !ruleIdWhitelistSet.has(ruleId)) {
39+
continue;
40+
}
41+
3542
const firstPathOnLine = result
3643
.find(
3744
'Node',

0 commit comments

Comments
 (0)