Skip to content

Commit 97456f0

Browse files
committed
New: no-unused-enable rule
1 parent cf416a5 commit 97456f0

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

docs/rules/no-unused-enable.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# disallows unused `eslint-enable` comments (eslint-comments/no-unused-enable)
2+
3+
This rule warns `eslint-enable` directive-comments which have no effect.
4+
5+
## Rule Details
6+
7+
Examples of :-1: **incorrect** code for this rule:
8+
9+
```js
10+
/*eslint eslint-comments/no-unused-enable: error */
11+
12+
/*eslint-disable no-undef */
13+
doSomething()
14+
/*eslint-enable no-undef-init */ "※ 'no-undef-init' rule is re-enabled but it has not been disabled."
15+
```
16+
17+
```js
18+
/*eslint eslint-comments/no-unused-enable: error */
19+
20+
doSomething()
21+
/*eslint-enable */ "※ ESLint rules are re-enabled but those have not been disabled."
22+
```
23+
24+
Examples of :+1: **correct** code for this rule:
25+
26+
```js
27+
/*eslint eslint-comments/no-unused-enable: error */
28+
29+
/*eslint-disable no-undef */
30+
doSomething()
31+
/*eslint-enable no-undef */
32+
```
33+
34+
```js
35+
/*eslint eslint-comments/no-unused-enable: error */
36+
37+
doSomething()
38+
```

lib/rules/no-unused-enable.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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-enable` comments",
23+
category: "Best Practices",
24+
recommended: false,
25+
},
26+
fixable: false,
27+
schema: [],
28+
},
29+
30+
create(context) {
31+
const sourceCode = context.getSourceCode()
32+
const disabledArea = DisabledArea.get(sourceCode)
33+
34+
return {
35+
Program() {
36+
for (const item of disabledArea.unusedEnableDirectives) {
37+
context.report({
38+
loc: utils.toRuleIdLocation(item.comment, item.ruleId),
39+
message: (item.ruleId) ?
40+
"'{{ruleId}}' rule is re-enabled but it has not been disabled." :
41+
"ESLint rules are re-enabled but those have not been disabled.",
42+
data: item,
43+
})
44+
}
45+
},
46+
}
47+
},
48+
}

tests/lib/rules/no-unused-enable.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 RuleTester = require("eslint").RuleTester
13+
const rule = require("../../../lib/rules/no-unused-enable")
14+
15+
//------------------------------------------------------------------------------
16+
// Tests
17+
//------------------------------------------------------------------------------
18+
19+
const tester = new RuleTester()
20+
21+
tester.run("no-unused-enable", rule, {
22+
valid: [
23+
`
24+
/*eslint no-undef:error*/
25+
/*eslint-disable*/
26+
var a = b
27+
/*eslint-enable*/
28+
`,
29+
`
30+
/*eslint no-undef:error*/
31+
/*eslint-disable no-undef*/
32+
var a = b
33+
/*eslint-enable no-undef*/
34+
`,
35+
`
36+
/*eslint no-undef:error*/
37+
/*eslint-disable no-undef*/
38+
var a = b
39+
/*eslint-enable*/
40+
`,
41+
`
42+
/*eslint no-undef:error, no-unused-vars:error*/
43+
/*eslint-disable no-undef,no-unused-vars*/
44+
var a = b
45+
/*eslint-enable no-undef*/
46+
`,
47+
`
48+
/*eslint no-undef:error, no-unused-vars:error*/
49+
/*eslint-disable no-undef,no-unused-vars*/
50+
var a = b
51+
/*eslint-enable*/
52+
`,
53+
],
54+
invalid: [
55+
{
56+
code: "/*eslint-enable*/",
57+
errors: [
58+
{
59+
message: "ESLint rules are re-enabled but those have not been disabled.",
60+
line: 1,
61+
column: 0,
62+
endLine: 1,
63+
endColumn: 18,
64+
},
65+
],
66+
},
67+
{
68+
code: "/*eslint-enable no-undef*/",
69+
errors: [
70+
{
71+
message: "'no-undef' rule is re-enabled but it has not been disabled.",
72+
line: 1,
73+
column: 17,
74+
endLine: 1,
75+
endColumn: 25,
76+
},
77+
],
78+
},
79+
{
80+
code: `
81+
/*eslint-disable no-unused-vars*/
82+
/*eslint-enable no-undef*/
83+
`,
84+
errors: [
85+
{
86+
message: "'no-undef' rule is re-enabled but it has not been disabled.",
87+
line: 3,
88+
column: 17,
89+
endLine: 3,
90+
endColumn: 25,
91+
},
92+
],
93+
},
94+
],
95+
})

0 commit comments

Comments
 (0)