Skip to content

Commit cf416a5

Browse files
committed
New: no-unused-disable rule
1 parent b9d514a commit cf416a5

File tree

3 files changed

+424
-0
lines changed

3 files changed

+424
-0
lines changed

docs/rules/no-unused-disable.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# disallows unused `eslint-disable` comments (eslint-comments/no-unused-disable)
2+
3+
Since refactoring or a bug fix of upstream, an `eslint-disable` directive-comment may become unnecessary.
4+
In that case, you should remove the garbage as soon as possible since the garbage may cause to overlook ESLint warnings in future.
5+
6+
This rule warns unnecessary `eslint-disable` directive-comments.
7+
8+
## Rule Details
9+
10+
Examples of :-1: **incorrect** code for this rule:
11+
12+
```js
13+
/*eslint eslint-comments/no-unused-disable: error */
14+
15+
var foo = bar() //eslint-disable-line no-undef,eqeqeq
16+
"※ 'eqeqeq' rule is disabled but never reported."
17+
```
18+
19+
```js
20+
/*eslint eslint-comments/no-unused-disable: error */
21+
/*globals doSomething */
22+
23+
doSomething() //eslint-disable-line
24+
"※ ESLint rules are disabled but never reported."
25+
```
26+
27+
Examples of :+1: **correct** code for this rule:
28+
29+
```js
30+
/*eslint eslint-comments/no-unused-disable: error */
31+
32+
var foo = bar() //eslint-disable-line no-undef
33+
```
34+
35+
```js
36+
/*eslint eslint-comments/no-unused-disable: error */
37+
/*globals doSomething */
38+
39+
doSomething()
40+
```

lib/rules/no-unused-disable.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const DisabledArea = require("../disabled-area")
13+
const utils = require("../utils")
14+
15+
//------------------------------------------------------------------------------
16+
// Rule Definition
17+
//------------------------------------------------------------------------------
18+
19+
module.exports = {
20+
meta: {
21+
docs: {
22+
description: "disallows unused `eslint-disable` comments",
23+
category: "Best Practices",
24+
recommended: false,
25+
},
26+
fixable: false,
27+
schema: [],
28+
},
29+
30+
create(context) {
31+
const originalReport = context.eslint.report
32+
const sourceCode = context.getSourceCode()
33+
const disabledArea = DisabledArea.get(sourceCode)
34+
35+
// Override `report` method to mark disabled-area as reported.
36+
context.eslint.report = function(ruleId, _severity, node, locationArg) {
37+
const location = (typeof locationArg === "string")
38+
? node.loc.start
39+
: locationArg.start || locationArg
40+
41+
disabledArea.report(ruleId, location)
42+
43+
originalReport.apply(this, arguments)
44+
}
45+
46+
/**
47+
* Reports the result.
48+
*
49+
* @returns {void}
50+
*/
51+
function report() {
52+
for (const area of disabledArea.areas) {
53+
if (area.reported) {
54+
continue
55+
}
56+
57+
context.report({
58+
loc: utils.toRuleIdLocation(area.comment, area.ruleId),
59+
message: (area.ruleId) ?
60+
"'{{ruleId}}' rule is disabled but never reported." :
61+
"ESLint rules are disabled but never reported.",
62+
data: area,
63+
})
64+
}
65+
66+
// Restore
67+
context.eslint.report = originalReport
68+
}
69+
70+
return {
71+
Program() {
72+
// Ensure that this listener is the last in `Program:exit` listeners
73+
// even if this rule was initialized before other rules.
74+
context.eslint.on("Program:exit", report)
75+
},
76+
}
77+
},
78+
}

0 commit comments

Comments
 (0)