Skip to content

Commit 2ae6ac4

Browse files
committed
Fix: no-unused-disable crashes
1 parent aa86e72 commit 2ae6ac4

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

lib/patch.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,31 @@ function getSeverity(config, ruleId) {
4141
* @returns {Comment|undefined} The gotten comment.
4242
*/
4343
function getCommentAt(message, sourceCode) {
44-
if (sourceCode == null) {
45-
return undefined
44+
if (sourceCode != null) {
45+
const loc = { line: message.line, column: message.column - 1 }
46+
const index = sourceCode.getIndexFromLoc(loc)
47+
const options = { includeComments: true }
48+
const comment = sourceCode.getTokenByRangeStart(index, options)
49+
if (
50+
comment != null &&
51+
(comment.type === "Line" || comment.type === "Block")
52+
) {
53+
return comment
54+
}
4655
}
47-
48-
const index = sourceCode.getIndexFromLoc(message) - 1
49-
return sourceCode.getTokenByRangeStart(index, { includeComments: true })
56+
return undefined
5057
}
5158

5259
/**
5360
* Check whether a given message is a `reportUnusedDisableDirectives` error.
5461
* @param {Message} message The message.
55-
* @param {Comment|undefined} comment The comment which existed at the message location.
5662
* @returns {boolean} `true` if the message is a `reportUnusedDisableDirectives` error.
5763
*/
58-
function isUnusedDisableDirectiveError(message, comment) {
64+
function isUnusedDisableDirectiveError(message) {
5965
return (
6066
!message.fatal &&
6167
!message.ruleId &&
62-
message.message.includes("eslint-disable") &&
63-
(comment == null || comment.type === "Block" || comment.type === "Line")
68+
message.message.includes("eslint-disable")
6469
)
6570
}
6671

@@ -111,17 +116,15 @@ function createNoUnusedDisableError(ruleId, severity, message, comment) {
111116
function convert(messages, sourceCode, ruleId, severity, keepAsIs) {
112117
for (let i = messages.length - 1; i >= 0; --i) {
113118
const message = messages[i]
114-
const comment = getCommentAt(message, sourceCode)
115-
116-
if (!isUnusedDisableDirectiveError(message, comment)) {
119+
if (!isUnusedDisableDirectiveError(message)) {
117120
continue
118121
}
119122

120123
const newMessage = createNoUnusedDisableError(
121124
ruleId,
122125
severity,
123126
message,
124-
comment
127+
getCommentAt(message, sourceCode)
125128
)
126129

127130
if (keepAsIs) {

0 commit comments

Comments
 (0)