@@ -8,25 +8,7 @@ const utils = require("./utils")
8
8
const DELIMITER = / [ \s , ] + / gu
9
9
const pool = new WeakMap ( )
10
10
11
- module . exports = class DisabledArea {
12
- /**
13
- * Get singleton instance for the given source code.
14
- *
15
- * @param {eslint.SourceCode } sourceCode - The source code to get.
16
- * @returns {DisabledArea } The singleton object for the source code.
17
- */
18
- static get ( sourceCode ) {
19
- let retv = pool . get ( sourceCode . ast )
20
-
21
- if ( retv == null ) {
22
- retv = new DisabledArea ( )
23
- retv . _scan ( sourceCode )
24
- pool . set ( sourceCode . ast , retv )
25
- }
26
-
27
- return retv
28
- }
29
-
11
+ class DisabledArea {
30
12
/**
31
13
* Constructor.
32
14
*/
@@ -45,7 +27,7 @@ module.exports = class DisabledArea {
45
27
* @param {string[]|null } ruleIds - The ruleId names to disable.
46
28
* @param {string } kind - The kind of disable-comments.
47
29
* @returns {void }
48
- * @private
30
+ * @protected
49
31
*/
50
32
_disable ( comment , location , ruleIds , kind ) {
51
33
if ( ruleIds ) {
@@ -85,7 +67,7 @@ module.exports = class DisabledArea {
85
67
* @param {string[]|null } ruleIds - The ruleId names to enable.
86
68
* @param {string } kind - The kind of disable-comments.
87
69
* @returns {void }
88
- * @private
70
+ * @protected
89
71
*/
90
72
_enable ( comment , location , ruleIds , kind ) {
91
73
const relatedDisableDirectives = new Set ( )
@@ -159,13 +141,62 @@ module.exports = class DisabledArea {
159
141
160
142
return null
161
143
}
144
+ }
162
145
146
+ class DisabledAreaForLanguagePlugin extends DisabledArea {
147
+ /**
148
+ * Scan the source code and setup disabled area list.
149
+ *
150
+ * @param {import('@eslint/core').TextSourceCode } sourceCode - The source code to scan.
151
+ * @returns {void }
152
+ */
153
+ _scan ( sourceCode ) {
154
+ const disableDirectives = sourceCode . getDisableDirectives ( )
155
+ for ( const directive of disableDirectives . directives ) {
156
+ if (
157
+ ! [
158
+ "disable" ,
159
+ "enable" ,
160
+ "disable-line" ,
161
+ "disable-next-line" ,
162
+ ] . includes ( directive . type )
163
+ ) {
164
+ continue
165
+ }
166
+ const ruleIds = directive . value
167
+ ? directive . value . split ( DELIMITER )
168
+ : null
169
+
170
+ const loc = sourceCode . getLoc ( directive . node )
171
+ if ( directive . type === "disable" ) {
172
+ this . _disable ( directive . node , loc . start , ruleIds , "block" )
173
+ } else if ( directive . type === "enable" ) {
174
+ this . _enable ( directive . node , loc . start , ruleIds , "block" )
175
+ } else if ( directive . type === "disable-line" ) {
176
+ const line = loc . start . line
177
+ const start = { line, column : 0 }
178
+ const end = { line : line + 1 , column : - 1 }
179
+
180
+ this . _disable ( directive . node , start , ruleIds , "line" )
181
+ this . _enable ( directive . node , end , ruleIds , "line" )
182
+ } else if ( directive . type === "disable-next-line" ) {
183
+ const line = loc . start . line
184
+ const start = { line : line + 1 , column : 0 }
185
+ const end = { line : line + 2 , column : - 1 }
186
+
187
+ this . _disable ( directive . node , start , ruleIds , "line" )
188
+ this . _enable ( directive . node , end , ruleIds , "line" )
189
+ }
190
+ }
191
+ }
192
+ }
193
+
194
+ class DisabledAreaForLegacy extends DisabledArea {
163
195
/**
164
196
* Scan the source code and setup disabled area list.
165
197
*
166
198
* @param {eslint.SourceCode } sourceCode - The source code to scan.
167
199
* @returns {void }
168
- * @private
169
200
*/
170
201
_scan ( sourceCode ) {
171
202
for ( const comment of sourceCode . getAllComments ( ) ) {
@@ -176,10 +207,12 @@ module.exports = class DisabledArea {
176
207
177
208
const kind = directiveComment . kind
178
209
if (
179
- kind !== "eslint-disable" &&
180
- kind !== "eslint-enable" &&
181
- kind !== "eslint-disable-line" &&
182
- kind !== "eslint-disable-next-line"
210
+ ! [
211
+ "eslint-disable" ,
212
+ "eslint-enable" ,
213
+ "eslint-disable-line" ,
214
+ "eslint-disable-next-line" ,
215
+ ] . includes ( kind )
183
216
) {
184
217
continue
185
218
}
@@ -209,3 +242,27 @@ module.exports = class DisabledArea {
209
242
}
210
243
}
211
244
}
245
+
246
+ module . exports = {
247
+ /**
248
+ * Get singleton instance for the given rule context.
249
+ *
250
+ * @param {import("@eslint/core").RuleContext } context - The rule context code to get.
251
+ * @returns {DisabledArea } The singleton object for the rule context.
252
+ */
253
+ getDisabledArea ( context ) {
254
+ const sourceCode = context . sourceCode || context . getSourceCode ( )
255
+ let retv = pool . get ( sourceCode . ast )
256
+
257
+ if ( retv == null ) {
258
+ retv =
259
+ typeof sourceCode . getDisableDirectives === "function"
260
+ ? new DisabledAreaForLanguagePlugin ( )
261
+ : new DisabledAreaForLegacy ( )
262
+ retv . _scan ( sourceCode )
263
+ pool . set ( sourceCode . ast , retv )
264
+ }
265
+
266
+ return retv
267
+ } ,
268
+ }
0 commit comments