@@ -8,25 +8,7 @@ const utils = require("./utils")
88const DELIMITER = / [ \s , ] + / gu
99const pool = new WeakMap ( )
1010
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 {
3012 /**
3113 * Constructor.
3214 */
@@ -45,7 +27,7 @@ module.exports = class DisabledArea {
4527 * @param {string[]|null } ruleIds - The ruleId names to disable.
4628 * @param {string } kind - The kind of disable-comments.
4729 * @returns {void }
48- * @private
30+ * @protected
4931 */
5032 _disable ( comment , location , ruleIds , kind ) {
5133 if ( ruleIds ) {
@@ -85,7 +67,7 @@ module.exports = class DisabledArea {
8567 * @param {string[]|null } ruleIds - The ruleId names to enable.
8668 * @param {string } kind - The kind of disable-comments.
8769 * @returns {void }
88- * @private
70+ * @protected
8971 */
9072 _enable ( comment , location , ruleIds , kind ) {
9173 const relatedDisableDirectives = new Set ( )
@@ -159,13 +141,62 @@ module.exports = class DisabledArea {
159141
160142 return null
161143 }
144+ }
162145
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 {
163195 /**
164196 * Scan the source code and setup disabled area list.
165197 *
166198 * @param {eslint.SourceCode } sourceCode - The source code to scan.
167199 * @returns {void }
168- * @private
169200 */
170201 _scan ( sourceCode ) {
171202 for ( const comment of sourceCode . getAllComments ( ) ) {
@@ -176,10 +207,12 @@ module.exports = class DisabledArea {
176207
177208 const kind = directiveComment . kind
178209 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 )
183216 ) {
184217 continue
185218 }
@@ -209,3 +242,27 @@ module.exports = class DisabledArea {
209242 }
210243 }
211244}
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