@@ -34,19 +34,33 @@ function getSeverity(config, ruleId) {
34
34
}
35
35
}
36
36
37
+ /**
38
+ * Get the comment which is at a given message location.
39
+ * @param {Message } message The message to get.
40
+ * @param {SourceCode|undefined } sourceCode The source code object to get.
41
+ * @returns {Comment|undefined } The gotten comment.
42
+ */
43
+ function getCommentAt ( message , sourceCode ) {
44
+ if ( sourceCode == null ) {
45
+ return undefined
46
+ }
47
+
48
+ const index = sourceCode . getIndexFromLoc ( message ) - 1
49
+ return sourceCode . getTokenByRangeStart ( index , { includeComments : true } )
50
+ }
51
+
37
52
/**
38
53
* Check whether a given message is a `reportUnusedDisableDirectives` error.
39
54
* @param {Message } message The message.
40
- * @param {Comment } comment The comment which existed at the message location.
55
+ * @param {Comment|undefined } comment The comment which existed at the message location.
41
56
* @returns {boolean } `true` if the message is a `reportUnusedDisableDirectives` error.
42
57
*/
43
58
function isUnusedDisableDirectiveError ( message , comment ) {
44
59
return (
45
60
! message . fatal &&
46
61
! message . ruleId &&
47
62
message . message . includes ( "eslint-disable" ) &&
48
- comment != null &&
49
- ( comment . type === "Block" || comment . type === "Line" )
63
+ ( comment == null || comment . type === "Block" || comment . type === "Line" )
50
64
)
51
65
}
52
66
@@ -55,7 +69,7 @@ function isUnusedDisableDirectiveError(message, comment) {
55
69
* @param {string } ruleId The ruleId.
56
70
* @param {number } severity The severity of the rule.
57
71
* @param {Message } message The original message.
58
- * @param {Comment } comment The directive comment.
72
+ * @param {Comment|undefined } comment The directive comment.
59
73
* @returns {Message } The created error.
60
74
*/
61
75
function createNoUnusedDisableError ( ruleId , severity , message , comment ) {
@@ -69,15 +83,17 @@ function createNoUnusedDisableError(ruleId, severity, message, comment) {
69
83
? `'${ targetRuleId } ' rule is disabled but never reported.`
70
84
: "ESLint rules are disabled but never reported."
71
85
72
- if ( targetRuleId ) {
73
- const loc = toRuleIdLocation ( comment , targetRuleId )
74
- clone . line = loc . start . line
75
- clone . column = loc . start . column + 1
76
- clone . endLine = loc . end . line
77
- clone . endColumn = loc . end . column + 1
78
- } else {
79
- clone . endLine = comment . loc . end . line
80
- clone . endColumn = comment . loc . end . column + 1
86
+ if ( comment != null ) {
87
+ if ( targetRuleId ) {
88
+ const loc = toRuleIdLocation ( comment , targetRuleId )
89
+ clone . line = loc . start . line
90
+ clone . column = loc . start . column + 1
91
+ clone . endLine = loc . end . line
92
+ clone . endColumn = loc . end . column + 1
93
+ } else {
94
+ clone . endLine = comment . loc . end . line
95
+ clone . endColumn = comment . loc . end . column + 1
96
+ }
81
97
}
82
98
83
99
return clone
@@ -86,7 +102,7 @@ function createNoUnusedDisableError(ruleId, severity, message, comment) {
86
102
/**
87
103
* Convert `reportUnusedDisableDirectives` errors to `eslint-comments/no-unused-disable` errors.
88
104
* @param {Message[] } messages The original messages.
89
- * @param {SourceCode } sourceCode The source code object.
105
+ * @param {SourceCode|undefined } sourceCode The source code object.
90
106
* @param {string } ruleId The rule ID to convert.
91
107
* @param {number } severity The severity of the rule.
92
108
* @param {boolean } keepAsIs The flag to keep original errors as is.
@@ -95,10 +111,8 @@ function createNoUnusedDisableError(ruleId, severity, message, comment) {
95
111
function convert ( messages , sourceCode , ruleId , severity , keepAsIs ) {
96
112
for ( let i = messages . length - 1 ; i >= 0 ; -- i ) {
97
113
const message = messages [ i ]
98
- const rangeStart = sourceCode . getIndexFromLoc ( message ) - 1
99
- const comment = sourceCode . getTokenByRangeStart ( rangeStart , {
100
- includeComments : true ,
101
- } )
114
+ const comment = getCommentAt ( message , sourceCode )
115
+
102
116
if ( ! isUnusedDisableDirectiveError ( message , comment ) ) {
103
117
continue
104
118
}
0 commit comments