Skip to content

Commit 62f67a4

Browse files
committed
Fix: allowWholeFile for multiple (fixes #9)
1 parent 750fe11 commit 62f67a4

File tree

2 files changed

+12
-43
lines changed

2 files changed

+12
-43
lines changed

lib/rules/disable-enable-pair.js

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,6 @@
1212
const DisabledArea = require("../disabled-area")
1313
const utils = require("../utils")
1414

15-
//------------------------------------------------------------------------------
16-
// Helpers
17-
//------------------------------------------------------------------------------
18-
19-
/**
20-
* Checks whether `disabledArea` covers `node`.
21-
*
22-
* @param {DisabledArea} disabledArea - Disabled area in question.
23-
* @param {Node} node - The node that might be covered.
24-
* @returns {boolean} `true` if `disabledArea` covers `node`.
25-
* @private
26-
*/
27-
function disabledAreaCoversNode(disabledArea, node) {
28-
return utils.lte(disabledArea.start, node.loc.start) &&
29-
(!disabledArea.end || utils.lte(node.loc.end, disabledArea.end))
30-
}
31-
32-
/**
33-
* Checks whether `disabledArea` covers `node`'s body.
34-
*
35-
* @param {DisabledArea} disabledArea - Disabled area in question.
36-
* @param {Node} node - The node whose body may or may not be covered.
37-
* Must have a `body` property (a Program or BlockStatement will do).
38-
* @returns {boolean} `true` if `disabledArea` covers `node`'s body.
39-
* @private
40-
*/
41-
function disabledAreaCoversNodeBody(disabledArea, node) {
42-
const body = node.body
43-
const first = body[0]
44-
const last = body[body.length - 1]
45-
return !first ||
46-
(disabledAreaCoversNode(disabledArea, first) &&
47-
disabledAreaCoversNode(disabledArea, last))
48-
}
49-
5015
//------------------------------------------------------------------------------
5116
// Rule Definition
5217
//------------------------------------------------------------------------------
@@ -72,23 +37,20 @@ module.exports = {
7237

7338
create(context) {
7439
const allowWholeFile = context.options[0] && context.options[0].allowWholeFile
75-
7640
const sourceCode = context.getSourceCode()
7741
const disabledArea = DisabledArea.get(sourceCode)
7842

79-
const firstDisabledArea = disabledArea.areas[0]
80-
8143
return {
8244
Program(node) {
45+
if (allowWholeFile && node.body.length === 0) {
46+
return
47+
}
48+
8349
for (const area of disabledArea.areas) {
8450
if (area.end != null) {
8551
continue
8652
}
87-
88-
if (allowWholeFile &&
89-
area === firstDisabledArea &&
90-
disabledAreaCoversNodeBody(area, node)
91-
) {
53+
if (allowWholeFile && utils.lte(area.start, node.loc.start)) {
9254
continue
9355
}
9456

tests/lib/rules/disable-enable-pair.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ console.log('This code does not even have any special comments')
7676
*/
7777
7878
/*eslint-disable*/
79+
`,
80+
options: [{ allowWholeFile: true }],
81+
},
82+
{
83+
code: `
84+
/*eslint-disable no-unused-var, no-undef */
85+
var foo = 1
7986
`,
8087
options: [{ allowWholeFile: true }],
8188
},

0 commit comments

Comments
 (0)