1
1
/* eslint-disable unicorn/prefer-module */
2
2
3
3
const allowedPatterns = [
4
- / t o d o / i, // Allow TODO (case-insensitive)
5
- / w a r n i n g / i, // Allow WARNING (case-insensitive)
6
- / e r r o r / i, // Allow ERROR (case-insensitive)
7
- / i n f o / i, // Allow INFO (case-insensitive)
8
- / ^ \s * e s l i n t - ( d i s a b l e | e n a b l e | e n v | g l o b a l s | i g n o r e | d i r e c t i v e ) / , // Allow ESLint directives
4
+ / t o d o / i, // Allow TODO (case-insensitive)
5
+ / w a r n i n g / i, // Allow WARNING (case-insensitive)
6
+ / e r r o r / i, // Allow ERROR (case-insensitive)
7
+ / i n f o / i, // Allow INFO (case-insensitive)
8
+ / ^ \s * e s l i n t - ( d i s a b l e | e n a b l e | e n v | g l o b a l s | i g n o r e | d i r e c t i v e ) / , // Allow ESLint directives
9
9
] ;
10
10
11
11
const meta = {
12
- type : 'problem' ,
13
- docs : {
14
- description : 'Disallow comments except for specified allowed patterns.' ,
15
- url : 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-comments.md'
16
- } ,
17
- fixable : 'code' ,
18
- schema : [
19
- {
20
- type : 'object' ,
21
- properties : {
22
- allow : {
23
- type : 'array' ,
24
- items : {
25
- type : 'string' ,
26
- } ,
27
- description : 'Additional patterns to allow in comments.' ,
28
- } ,
29
- disallow : {
30
- type : 'array' ,
31
- items : {
32
- type : 'string' ,
33
- } ,
34
- description : 'Additional patterns to disallow in comments.' ,
35
- } ,
36
- } ,
37
- additionalProperties : false ,
38
- } ,
39
- ] ,
12
+ type : 'problem' ,
13
+ docs : {
14
+ description : 'Disallow comments except for specified allowed patterns.' ,
15
+ url : 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-comments.md' ,
16
+ } ,
17
+ fixable : 'code' ,
18
+ schema : [
19
+ {
20
+ type : 'object' ,
21
+ properties : {
22
+ allow : {
23
+ type : 'array' ,
24
+ items : {
25
+ type : 'string' ,
26
+ } ,
27
+ description : 'Additional patterns to allow in comments.' ,
28
+ } ,
29
+ disallow : {
30
+ type : 'array' ,
31
+ items : {
32
+ type : 'string' ,
33
+ } ,
34
+ description : 'Additional patterns to disallow in comments.' ,
35
+ } ,
36
+ } ,
37
+ additionalProperties : false ,
38
+ } ,
39
+ ] ,
40
40
} ;
41
41
42
42
function create ( context ) {
43
- const options = context . options [ 0 ] || { } ;
44
- const userAllowedPatterns = ( options . allow || [ ] ) . map ( ( pattern ) => new RegExp ( pattern ) ) ;
45
- const userDisallowedPatterns = ( options . disallow || [ ] ) . map ( ( pattern ) => new RegExp ( pattern ) ) ;
43
+ const options = context . options [ 0 ] || { } ;
44
+ const userAllowedPatterns = ( options . allow || [ ] ) . map ( pattern => new RegExp ( pattern ) ) ;
45
+ const userDisallowedPatterns = ( options . disallow || [ ] ) . map ( pattern => new RegExp ( pattern ) ) ;
46
46
47
- function isCommentAllowed ( comment ) {
48
- const text = comment . value . trim ( ) ;
47
+ function isCommentAllowed ( comment ) {
48
+ const text = comment . value . trim ( ) ;
49
49
50
- // Check if the comment is a valid JSDoc comment
51
- if ( comment . type === 'Block' && comment . value . startsWith ( '*' ) ) {
52
- return true ; // Allow any JSDoc-style block comment (/** ... */)
53
- }
50
+ // Check if the comment is a valid JSDoc comment
51
+ if ( comment . type === 'Block' && comment . value . startsWith ( '*' ) ) {
52
+ return true ; // Allow any JSDoc-style block comment (/** ... */)
53
+ }
54
54
55
- // Check if the comment matches any allowed pattern
56
- for ( const pattern of [ ...allowedPatterns , ...userAllowedPatterns ] ) {
57
- if ( pattern . test ( text ) ) {
58
- return true ;
59
- }
60
- }
55
+ // Check if the comment matches any allowed pattern
56
+ for ( const pattern of [ ...allowedPatterns , ...userAllowedPatterns ] ) {
57
+ if ( pattern . test ( text ) ) {
58
+ return true ;
59
+ }
60
+ }
61
61
62
- // Check if the comment matches any disallowed pattern
63
- for ( const pattern of userDisallowedPatterns ) {
64
- if ( pattern . test ( text ) ) {
65
- return false ;
66
- }
67
- }
62
+ // Check if the comment matches any disallowed pattern
63
+ for ( const pattern of userDisallowedPatterns ) {
64
+ if ( pattern . test ( text ) ) {
65
+ return false ;
66
+ }
67
+ }
68
68
69
- return false ; // Disallow by default if no match
70
- }
69
+ return false ; // Disallow by default if no match
70
+ }
71
71
72
- return {
73
- Program ( ) {
74
- const sourceCode = context . getSourceCode ( ) ;
75
- const comments = sourceCode . getAllComments ( ) ;
72
+ return {
73
+ Program ( ) {
74
+ const sourceCode = context . getSourceCode ( ) ;
75
+ const comments = sourceCode . getAllComments ( ) ;
76
76
77
- comments . forEach ( ( comment ) => {
78
- if ( ! isCommentAllowed ( comment ) ) {
79
- context . report ( {
80
- node : comment ,
81
- message : 'Comment not allowed.' ,
82
- fix ( fixer ) {
83
- return fixer . remove ( comment ) ;
84
- } ,
85
- } ) ;
86
- }
87
- } ) ;
88
- } ,
89
- } ;
77
+ for ( const comment of comments ) {
78
+ if ( ! isCommentAllowed ( comment ) ) {
79
+ context . report ( {
80
+ node : comment ,
81
+ message : 'Comment not allowed.' ,
82
+ fix ( fixer ) {
83
+ return fixer . remove ( comment ) ;
84
+ } ,
85
+ } ) ;
86
+ }
87
+ }
88
+ } ,
89
+ } ;
90
90
}
91
91
92
92
const rule = {
93
- meta,
94
- create,
93
+ meta,
94
+ create,
95
95
} ;
96
96
97
- module . exports = rule ;
97
+ module . exports = rule ;
0 commit comments