@@ -15,10 +15,10 @@ const utils = require("../utils")
15
15
// Helpers
16
16
//------------------------------------------------------------------------------
17
17
18
- const BLOCK_COMMENT_DIRECTIVE = / ^ \s * ( e s l i n t (?: - d i s a b l e | - e n a b l e | - e n v ) ? | e x p o r t e d | g l o b a l s ? ) (?: \s | $ ) /
19
- const LINE_COMMENT_DIRECTIVE = / ^ \s * ( e s l i n t - d i s a b l e (?: - n e x t ) ? - l i n e ) (?: \s | $ ) /
20
- const MESSAGE = "Unexpected ESLint directive comment."
21
- const NOT_FOUND = - 1
18
+ const PATTERNS = {
19
+ Block : / ^ \s * ( e s l i n t (?: - d i s a b l e | - e n a b l e | - e n v ) ? | e x p o r t e d | g l o b a l s ? ) (?: \s | $ ) / ,
20
+ Line : / ^ \s * ( e s l i n t - d i s a b l e (?: - n e x t ) ? - l i n e ) (?: \s | $ ) / ,
21
+ }
22
22
23
23
//------------------------------------------------------------------------------
24
24
// Rule Definition
@@ -59,26 +59,26 @@ module.exports = {
59
59
} ,
60
60
61
61
create ( context ) {
62
- const allowed = ( context . options [ 0 ] && context . options [ 0 ] . allow ) || [ ]
62
+ const sourceCode = context . getSourceCode ( )
63
+ const allowed = new Set (
64
+ ( context . options [ 0 ] && context . options [ 0 ] . allow ) || [ ]
65
+ )
63
66
64
67
return {
65
- BlockComment ( token ) {
66
- const m = BLOCK_COMMENT_DIRECTIVE . exec ( token . value )
67
- if ( m != null && allowed . indexOf ( m [ 1 ] ) === NOT_FOUND ) {
68
- context . report ( {
69
- loc : utils . toForceLocation ( token . loc ) ,
70
- message : MESSAGE ,
71
- } )
72
- }
73
- } ,
68
+ Program ( ) {
69
+ for ( const comment of sourceCode . getAllComments ( ) ) {
70
+ const pattern = PATTERNS [ comment . type ]
71
+ if ( pattern == null ) {
72
+ continue
73
+ }
74
74
75
- LineComment ( token ) {
76
- const m = LINE_COMMENT_DIRECTIVE . exec ( token . value )
77
- if ( m != null && allowed . indexOf ( m [ 1 ] ) === NOT_FOUND ) {
78
- context . report ( {
79
- loc : utils . toForceLocation ( token . loc ) ,
80
- message : MESSAGE ,
81
- } )
75
+ const m = pattern . exec ( comment . value )
76
+ if ( m != null && ! allowed . has ( m [ 1 ] ) ) {
77
+ context . report ( {
78
+ loc : utils . toForceLocation ( comment . loc ) ,
79
+ message : "Unexpected ESLint directive comment." ,
80
+ } )
81
+ }
82
82
}
83
83
} ,
84
84
}
0 commit comments