@@ -31,13 +31,15 @@ module.exports = {
31
31
/**
32
32
* Calculate the location of the given rule in the given comment token.
33
33
*
34
+ * @param {Partial<import("@eslint/core").RuleContext> } context - The rule context code.
34
35
* @param {Token } comment - The comment token to calculate.
35
36
* @param {string|null } ruleId - The rule name to calculate.
36
37
* @returns {object } The location of the given information.
37
38
*/
38
- toRuleIdLocation ( comment , ruleId ) {
39
+ toRuleIdLocation ( context , comment , ruleId ) {
40
+ const commentLoc = getLoc ( context , comment )
39
41
if ( ruleId == null ) {
40
- return module . exports . toForceLocation ( comment . loc )
42
+ return module . exports . toForceLocation ( commentLoc )
41
43
}
42
44
43
45
const lines = comment . value . match ( LINE_PATTERN )
@@ -49,7 +51,7 @@ module.exports = {
49
51
{
50
52
const m = ruleIdPattern . exec ( lines [ 0 ] )
51
53
if ( m != null ) {
52
- const start = comment . loc . start
54
+ const start = commentLoc . start
53
55
return {
54
56
start : {
55
57
line : start . line ,
@@ -71,7 +73,7 @@ module.exports = {
71
73
for ( let i = 1 ; i < lines . length ; ++ i ) {
72
74
const m = ruleIdPattern . exec ( lines [ i ] )
73
75
if ( m != null ) {
74
- const start = comment . loc . start
76
+ const start = commentLoc . start
75
77
return {
76
78
start : {
77
79
line : start . line + i ,
@@ -86,7 +88,7 @@ module.exports = {
86
88
}
87
89
88
90
/*istanbul ignore next : foolproof */
89
- return comment . loc
91
+ return commentLoc
90
92
} ,
91
93
92
94
/**
@@ -129,6 +131,8 @@ module.exports = {
129
131
return parsed
130
132
} ,
131
133
parseDirectiveText,
134
+
135
+ getLoc,
132
136
}
133
137
134
138
/**
@@ -168,3 +172,18 @@ function divideDirectiveComment(value) {
168
172
description : divided . length > 1 ? divided [ 1 ] . trim ( ) : null ,
169
173
}
170
174
}
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
+ }
0 commit comments