Skip to content

Commit f72feeb

Browse files
committed
Fix: support ESLint 4 (fixes #1)
1 parent e1ab4b2 commit f72feeb

File tree

3 files changed

+134
-126
lines changed

3 files changed

+134
-126
lines changed

lib/rules/no-unlimited-disable.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ const utils = require("../utils")
1515
// Helpers
1616
//------------------------------------------------------------------------------
1717

18-
const BLOCK_COMMENT_DIRECTIVE = /^\s*(eslint-disable)\s*(\S)?/
19-
const LINE_COMMENT_DIRECTIVE = /^\s*(eslint-disable(?:-next)?-line)\s*(\S)?/
20-
const MESSAGE = "Unexpected unlimited '{{kind}}' comment. Specify some rule names to disable."
18+
const PATTERNS = {
19+
Block: /^\s*(eslint-disable)\s*(\S)?/,
20+
Line: /^\s*(eslint-disable(?:-next)?-line)\s*(\S)?/,
21+
}
2122

2223
//------------------------------------------------------------------------------
2324
// Rule Definition
@@ -34,26 +35,24 @@ module.exports = {
3435
},
3536

3637
create(context) {
37-
return {
38-
BlockComment(token) {
39-
const m = BLOCK_COMMENT_DIRECTIVE.exec(token.value)
40-
if (m && !m[2]) {
41-
context.report({
42-
loc: utils.toForceLocation(token.loc),
43-
message: MESSAGE,
44-
data: {kind: m[1]},
45-
})
46-
}
47-
},
38+
const sourceCode = context.getSourceCode()
4839

49-
LineComment(token) {
50-
const m = LINE_COMMENT_DIRECTIVE.exec(token.value)
51-
if (m && !m[2]) {
52-
context.report({
53-
loc: utils.toForceLocation(token.loc),
54-
message: MESSAGE,
55-
data: {kind: m[1]},
56-
})
40+
return {
41+
Program() {
42+
for (const comment of sourceCode.getAllComments()) {
43+
const pattern = PATTERNS[comment.type]
44+
if (pattern == null) {
45+
continue
46+
}
47+
48+
const m = pattern.exec(comment.value)
49+
if (m && !m[2]) {
50+
context.report({
51+
loc: utils.toForceLocation(comment.loc),
52+
message: "Unexpected unlimited '{{kind}}' comment. Specify some rule names to disable.",
53+
data: {kind: m[1]},
54+
})
55+
}
5756
}
5857
},
5958
}

lib/rules/no-use.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const utils = require("../utils")
1515
// Helpers
1616
//------------------------------------------------------------------------------
1717

18-
const BLOCK_COMMENT_DIRECTIVE = /^\s*(eslint(?:-disable|-enable|-env)?|exported|globals?)(?:\s|$)/
19-
const LINE_COMMENT_DIRECTIVE = /^\s*(eslint-disable(?:-next)?-line)(?:\s|$)/
20-
const MESSAGE = "Unexpected ESLint directive comment."
21-
const NOT_FOUND = -1
18+
const PATTERNS = {
19+
Block: /^\s*(eslint(?:-disable|-enable|-env)?|exported|globals?)(?:\s|$)/,
20+
Line: /^\s*(eslint-disable(?:-next)?-line)(?:\s|$)/,
21+
}
2222

2323
//------------------------------------------------------------------------------
2424
// Rule Definition
@@ -59,26 +59,26 @@ module.exports = {
5959
},
6060

6161
create(context) {
62-
const allowed = (context.options[0] && context.options[0].allow) || []
62+
const sourceCode = context.getSourceCode()
63+
const allowed = new Set(
64+
(context.options[0] && context.options[0].allow) || []
65+
)
6366

6467
return {
65-
BlockComment(token) {
66-
const m = BLOCK_COMMENT_DIRECTIVE.exec(token.value)
67-
if (m != null && allowed.indexOf(m[1]) === NOT_FOUND) {
68-
context.report({
69-
loc: utils.toForceLocation(token.loc),
70-
message: MESSAGE,
71-
})
72-
}
73-
},
68+
Program() {
69+
for (const comment of sourceCode.getAllComments()) {
70+
const pattern = PATTERNS[comment.type]
71+
if (pattern == null) {
72+
continue
73+
}
7474

75-
LineComment(token) {
76-
const m = LINE_COMMENT_DIRECTIVE.exec(token.value)
77-
if (m != null && allowed.indexOf(m[1]) === NOT_FOUND) {
78-
context.report({
79-
loc: utils.toForceLocation(token.loc),
80-
message: MESSAGE,
81-
})
75+
const m = pattern.exec(comment.value)
76+
if (m != null && !allowed.has(m[1])) {
77+
context.report({
78+
loc: utils.toForceLocation(comment.loc),
79+
message: "Unexpected ESLint directive comment.",
80+
})
81+
}
8282
}
8383
},
8484
}

0 commit comments

Comments
 (0)