Skip to content

Commit 28cc291

Browse files
committed
add test and fix bugs
1 parent 95bd95d commit 28cc291

12 files changed

+164
-132
lines changed

lib/internal/utils.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ module.exports = {
3131
/**
3232
* Calculate the location of the given rule in the given comment token.
3333
*
34+
* @param {Partial<import("@eslint/core").RuleContext>} context - The rule context code.
3435
* @param {Token} comment - The comment token to calculate.
3536
* @param {string|null} ruleId - The rule name to calculate.
3637
* @returns {object} The location of the given information.
3738
*/
38-
toRuleIdLocation(comment, ruleId) {
39+
toRuleIdLocation(context, comment, ruleId) {
40+
const commentLoc = getLoc(context, comment)
3941
if (ruleId == null) {
40-
return module.exports.toForceLocation(comment.loc)
42+
return module.exports.toForceLocation(commentLoc)
4143
}
4244

4345
const lines = comment.value.match(LINE_PATTERN)
@@ -49,7 +51,7 @@ module.exports = {
4951
{
5052
const m = ruleIdPattern.exec(lines[0])
5153
if (m != null) {
52-
const start = comment.loc.start
54+
const start = commentLoc.start
5355
return {
5456
start: {
5557
line: start.line,
@@ -71,7 +73,7 @@ module.exports = {
7173
for (let i = 1; i < lines.length; ++i) {
7274
const m = ruleIdPattern.exec(lines[i])
7375
if (m != null) {
74-
const start = comment.loc.start
76+
const start = commentLoc.start
7577
return {
7678
start: {
7779
line: start.line + i,
@@ -86,7 +88,7 @@ module.exports = {
8688
}
8789

8890
/*istanbul ignore next : foolproof */
89-
return comment.loc
91+
return commentLoc
9092
},
9193

9294
/**
@@ -129,6 +131,8 @@ module.exports = {
129131
return parsed
130132
},
131133
parseDirectiveText,
134+
135+
getLoc,
132136
}
133137

134138
/**
@@ -168,3 +172,18 @@ function divideDirectiveComment(value) {
168172
description: divided.length > 1 ? divided[1].trim() : null,
169173
}
170174
}
175+
176+
/**
177+
* Get source code location from the given node.
178+
*
179+
* @param {Partial<import("@eslint/core").RuleContext>} context - The rule context code.
180+
* @param {unknown} nodeOrToken - The node or token to get.
181+
* @returns {object} The source code location.
182+
*/
183+
function getLoc(context, nodeOrToken) {
184+
const sourceCode = context.sourceCode || context.getSourceCode?.()
185+
if (typeof sourceCode?.getLoc === "function") {
186+
return sourceCode.getLoc(nodeOrToken)
187+
}
188+
return nodeOrToken.loc
189+
}

lib/rules/disable-enable-pair.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,32 @@ module.exports = {
4141
context.options[0] && context.options[0].allowWholeFile
4242
const disabledArea = getDisabledArea(context)
4343

44-
return {
45-
Program(node) {
46-
if (allowWholeFile && node.body.length === 0) {
47-
return
48-
}
44+
/** @type {import('@eslint/core').TextSourceCode} */
45+
const sourceCode = context.sourceCode || context.getSourceCode()
4946

50-
for (const area of disabledArea.areas) {
51-
if (area.end != null) {
52-
continue
53-
}
54-
if (
55-
allowWholeFile &&
56-
utils.lte(area.start, node.loc.start)
57-
) {
58-
continue
59-
}
47+
const firstToken = sourceCode.ast?.tokens?.[0]
6048

61-
context.report({
62-
loc: utils.toRuleIdLocation(area.comment, area.ruleId),
63-
messageId: area.ruleId
64-
? "missingRulePair"
65-
: "missingPair",
66-
data: area,
67-
})
68-
}
69-
},
49+
if (allowWholeFile && !firstToken) {
50+
return {}
51+
}
52+
53+
for (const area of disabledArea.areas) {
54+
if (area.end != null) {
55+
continue
56+
}
57+
if (
58+
allowWholeFile &&
59+
utils.lte(area.start, utils.getLoc(context, firstToken).start)
60+
) {
61+
continue
62+
}
63+
64+
context.report({
65+
loc: utils.toRuleIdLocation(context, area.comment, area.ruleId),
66+
messageId: area.ruleId ? "missingRulePair" : "missingPair",
67+
data: area,
68+
})
7069
}
70+
return {}
7171
},
7272
}

lib/rules/no-aggregating-enable.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,18 @@ module.exports = {
2828
create(context) {
2929
const disabledArea = getDisabledArea(context)
3030

31-
return {
32-
Program() {
33-
for (const entry of disabledArea.numberOfRelatedDisableDirectives) {
34-
const comment = entry[0]
35-
const count = entry[1]
31+
for (const entry of disabledArea.numberOfRelatedDisableDirectives) {
32+
const comment = entry[0]
33+
const count = entry[1]
3634

37-
if (count >= 2) {
38-
context.report({
39-
loc: utils.toForceLocation(comment.loc),
40-
messageId: "aggregatingEnable",
41-
data: { count },
42-
})
43-
}
44-
}
45-
},
35+
if (count >= 2) {
36+
context.report({
37+
loc: utils.toForceLocation(comment.loc),
38+
messageId: "aggregatingEnable",
39+
data: { count },
40+
})
41+
}
4642
}
43+
return {}
4744
},
4845
}

lib/rules/no-duplicate-disable.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ module.exports = {
2727
create(context) {
2828
const disabledArea = getDisabledArea(context)
2929

30-
return {
31-
Program() {
32-
for (const item of disabledArea.duplicateDisableDirectives) {
33-
context.report({
34-
loc: utils.toRuleIdLocation(item.comment, item.ruleId),
35-
messageId: item.ruleId ? "duplicateRule" : "duplicate",
36-
data: item,
37-
})
38-
}
39-
},
30+
for (const item of disabledArea.duplicateDisableDirectives) {
31+
context.report({
32+
loc: utils.toRuleIdLocation(context, item.comment, item.ruleId),
33+
messageId: item.ruleId ? "duplicateRule" : "duplicate",
34+
data: item,
35+
})
4036
}
37+
return {}
4138
},
4239
}

lib/rules/no-restricted-disable.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,21 @@ module.exports = {
4141
ig.add(pattern)
4242
}
4343

44-
return {
45-
Program() {
46-
for (const area of disabledArea.areas) {
47-
if (area.ruleId == null || ig.ignores(area.ruleId)) {
48-
context.report({
49-
loc: utils.toRuleIdLocation(
50-
area.comment,
51-
area.ruleId
52-
),
53-
messageId: "disallow",
54-
data: {
55-
ruleId: area.ruleId || String(context.options),
56-
},
57-
})
58-
}
59-
}
60-
},
44+
for (const area of disabledArea.areas) {
45+
if (area.ruleId == null || ig.ignores(area.ruleId)) {
46+
context.report({
47+
loc: utils.toRuleIdLocation(
48+
context,
49+
area.comment,
50+
area.ruleId
51+
),
52+
messageId: "disallow",
53+
data: {
54+
ruleId: area.ruleId || String(context.options),
55+
},
56+
})
57+
}
6158
}
59+
return {}
6260
},
6361
}

lib/rules/no-unlimited-disable.js

+17-22
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,23 @@ module.exports = {
2828
},
2929

3030
create(context) {
31-
return {
32-
Program() {
33-
for (const directiveComment of getAllDirectiveComments(
34-
context
35-
)) {
36-
const kind = directiveComment.kind
37-
if (
38-
kind !== "eslint-disable" &&
39-
kind !== "eslint-disable-line" &&
40-
kind !== "eslint-disable-next-line"
41-
) {
42-
continue
43-
}
44-
if (!directiveComment.value) {
45-
context.report({
46-
loc: utils.toForceLocation(directiveComment.loc),
47-
messageId: "unexpected",
48-
data: { kind: directiveComment.kind },
49-
})
50-
}
51-
}
52-
},
31+
for (const directiveComment of getAllDirectiveComments(context)) {
32+
const kind = directiveComment.kind
33+
if (
34+
kind !== "eslint-disable" &&
35+
kind !== "eslint-disable-line" &&
36+
kind !== "eslint-disable-next-line"
37+
) {
38+
continue
39+
}
40+
if (!directiveComment.value) {
41+
context.report({
42+
loc: utils.toForceLocation(directiveComment.loc),
43+
messageId: "unexpected",
44+
data: { kind: directiveComment.kind },
45+
})
46+
}
5347
}
48+
return {}
5449
},
5550
}

lib/rules/no-unused-enable.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ module.exports = {
2828
create(context) {
2929
const disabledArea = getDisabledArea(context)
3030

31-
return {
32-
Program() {
33-
for (const item of disabledArea.unusedEnableDirectives) {
34-
context.report({
35-
loc: utils.toRuleIdLocation(item.comment, item.ruleId),
36-
messageId: item.ruleId ? "unusedRule" : "unused",
37-
data: item,
38-
})
39-
}
40-
},
31+
for (const item of disabledArea.unusedEnableDirectives) {
32+
context.report({
33+
loc: utils.toRuleIdLocation(context, item.comment, item.ruleId),
34+
messageId: item.ruleId ? "unusedRule" : "unused",
35+
data: item,
36+
})
4137
}
38+
return {}
4239
},
4340
}

lib/rules/no-use.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,14 @@ module.exports = {
5555
(context.options[0] && context.options[0].allow) || []
5656
)
5757

58-
return {
59-
Program() {
60-
for (const directiveComment of getAllDirectiveComments(
61-
context
62-
)) {
63-
if (!allowed.has(directiveComment.kind)) {
64-
context.report({
65-
loc: utils.toForceLocation(directiveComment.loc),
66-
messageId: "disallow",
67-
})
68-
}
69-
}
70-
},
58+
for (const directiveComment of getAllDirectiveComments(context)) {
59+
if (!allowed.has(directiveComment.kind)) {
60+
context.report({
61+
loc: utils.toForceLocation(directiveComment.loc),
62+
messageId: "disallow",
63+
})
64+
}
7165
}
66+
return {}
7267
},
7368
}

lib/rules/require-description.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,17 @@ module.exports = {
5757
(context.options[0] && context.options[0].ignore) || []
5858
)
5959

60-
return {
61-
Program() {
62-
for (const directiveComment of getAllDirectiveComments(
63-
context
64-
)) {
65-
if (ignores.has(directiveComment.kind)) {
66-
continue
67-
}
68-
if (!directiveComment.description) {
69-
context.report({
70-
loc: utils.toForceLocation(directiveComment.loc),
71-
messageId: "missingDescription",
72-
})
73-
}
74-
}
75-
},
60+
for (const directiveComment of getAllDirectiveComments(context)) {
61+
if (ignores.has(directiveComment.kind)) {
62+
continue
63+
}
64+
if (!directiveComment.description) {
65+
context.report({
66+
loc: utils.toForceLocation(directiveComment.loc),
67+
messageId: "missingDescription",
68+
})
69+
}
7670
}
71+
return {}
7772
},
7873
}

lib/utils/patch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function createNoUnusedDisableError(ruleId, severity, message, comment) {
9090

9191
if (comment != null) {
9292
if (targetRuleId) {
93-
const loc = toRuleIdLocation(comment, targetRuleId)
93+
const loc = toRuleIdLocation({}, comment, targetRuleId)
9494
clone.line = loc.start.line
9595
clone.column = loc.start.column + 1
9696
clone.endLine = loc.end.line

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@babel/eslint-parser": "^7.22.9",
2828
"@eslint-community/eslint-plugin-mysticatea": "^15.5.1",
2929
"@eslint/core": "^0.13.0",
30+
"@eslint/css": "^0.6.0",
3031
"@types/node": "^14.18.54",
3132
"@vuepress/plugin-pwa": "^1.9.9",
3233
"cross-spawn": "^7.0.3",

0 commit comments

Comments
 (0)