-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a rule proposal, but as I wanted to know whether it was actually possible, I *accidentally* went all the way :p I saw [this message](https://gitter.im/eslint/eslint?at=5751c0393bdac7ae37b46224) by @alanhussey on the ESLint Gitter, and thought it was a pretty good idea. The proposal is to enforce specifying the rules to disable when you use `eslint-disable`/`eslint-disable-line`, as you can otherwise hide some useful error reports. Let me know what you think :) Closes #33
- Loading branch information
1 parent
8f97d7b
commit 5494fbb
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Enforce specifying rules to disable in `eslint-disable` comments | ||
|
||
This rule makes you specify the rules you want to disable when using `eslint-disable` or `eslint-disable-line` comments. | ||
|
||
If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. | ||
|
||
On a single line: | ||
|
||
```js | ||
const message = 'foo'; | ||
console.log(message); // eslint-disable-line no-console | ||
``` | ||
|
||
On the whole (rest of the) file: | ||
|
||
```js | ||
/* eslint-disable no-console */ | ||
const message = 'foo'; | ||
console.log(message); | ||
``` | ||
|
||
You don't have to specify any rules (like `no-console` in the examples above), but you should, as you might otherwise hide useful errors. | ||
|
||
```js | ||
/* eslint-disable */ | ||
console.log(message); // `message` is not defined, but it won't be reported | ||
``` | ||
|
||
This rule enforces specifying the rules to disable. If you want to disable ESLint on a file altogether, you should ignore it through [`.eslintignore`](http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories) for ESLint or through the [`ignores` property](https://github.com/sindresorhus/xo#ignores) in `package.json` for `XO`. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
/* eslint-disable */ | ||
console.log(message); | ||
|
||
console.log(message); // eslint-disable-line | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
/* eslint-disable no-console */ | ||
console.log(message); | ||
|
||
console.log(message); // eslint-disable-line no-console | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
var disableRegex = /^eslint-disable(-line)?(\s+([\w-]+))?/; | ||
|
||
module.exports = function (context) { | ||
return { | ||
Program: function (node) { | ||
node.comments.forEach(function (comment) { | ||
var value = comment.value.trim(); | ||
var res = disableRegex.exec(value); | ||
if (res && // It is a eslint-disable comment | ||
!res[2] // but it did not specify any rules | ||
) { | ||
context.report({ | ||
// Can't set it at the given location as the warning | ||
// will be ignored due to the disable comment | ||
loc: {line: 0, column: 0}, | ||
// So specify it in the message | ||
message: 'Specify the rules you want to disable at line {{line}}:{{column}}', | ||
data: comment.loc.start | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import test from 'ava'; | ||
import {RuleTester} from 'eslint'; | ||
import rule from '../rules/no-abusive-eslint-disable'; | ||
|
||
const ruleTester = new RuleTester({ | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const error = (message) => ({ | ||
ruleId: 'no-abusive-eslint-disable', | ||
message | ||
}); | ||
|
||
test(() => { | ||
ruleTester.run('no-abusive-eslint-disable', rule, { | ||
valid: [ | ||
`eval();`, | ||
`eval(); // eslint-disable-line no-eval`, | ||
`eval(); // eslint-disable-line no-eval, no-console`, | ||
`eval(); //eslint-disable-line no-eval`, | ||
`eval(); // eslint-disable-line no-eval`, | ||
`eval(); //\teslint-disable-line no-eval`, | ||
`eval(); /* eslint-disable-line no-eval */`, | ||
`eval(); /* eslint-disable-line no-eval */`, | ||
`eval(); // eslint-line-disable`, | ||
`eval(); // some comment`, | ||
`foo(); | ||
// eslint-disable-line no-eval | ||
eval();`, | ||
'/* eslint-disable no-eval */', | ||
`foo(); | ||
/* eslint-disable no-eval */ | ||
eval();` | ||
], | ||
invalid: [ | ||
{ | ||
code: `eval(); // eslint-disable-line`, | ||
errors: [error('Specify the rules you want to disable at line 1:8')] | ||
}, | ||
{ | ||
code: 'foo();\neval(); // eslint-disable-line', | ||
errors: [error('Specify the rules you want to disable at line 2:8')] | ||
}, | ||
{ | ||
code: '/* eslint-disable */', | ||
errors: [error('Specify the rules you want to disable at line 1:0')] | ||
}, | ||
{ | ||
code: 'foo();\n/* eslint-disable */\neval();', | ||
errors: [error('Specify the rules you want to disable at line 2:0')] | ||
} | ||
] | ||
}); | ||
}); |