1
- /**
2
- * from https://github.com/bahmutov/eslint-rules
3
- */
4
1
const { parse } = require ( 'espree' ) ;
5
- const quote = require ( 'quote' ) ;
6
-
7
- //function isJshint(text) {
8
- // return /^jshint\ /.test(text);
9
- //}
10
2
11
3
const isSingleWord = function ( text ) {
12
4
return / ^ [ \w - ] * $ / . test ( text ) ;
13
5
}
14
6
15
- const isValidCode = function ( text ) {
16
- if ( isSingleWord ( text ) ) { // || isJshint(text)
17
- return false ;
18
- }
19
- // string used to disable Flow typing for the next line
20
- if ( text . trim ( ) === "$FlowFixMe" ) { //TODO: add this string as a new option
7
+ const isValidCode = function ( text , MAX_LENGTH = 10 ) {
8
+ if ( text . length <= MAX_LENGTH ) {
21
9
return false
22
10
}
23
- // $FlowFixMe: suppressing this error until we can refactor
24
- if ( / ^ \ $F l o w F i x M e / . test ( text ) ) {
25
- return false
11
+
12
+ if ( text === " $FlowFixMe" || isSingleWord ( text ) ) {
13
+ return false ;
26
14
}
27
- if ( / ^ \$ F l o w T O D O / . test ( text ) ) {
15
+ if ( / ^ \$ F l o w F i x M e / . test ( text ) || / ^ \$ F l o w T O D O / . test ( text ) ) {
28
16
return false
29
17
}
30
- //todo: read .flowconfig file
31
- // see https://flow.org/en/docs/config/options/#suppress_comment-regex-
32
-
33
- //TODO: put max length value as option, in rule "no-commented-out-code"
34
- const MAX_LENGTH = 10
35
- //if (text.length <= MAX_LENGTH) {
36
- // return false
37
- //}
38
-
39
- //TODO: put this as an rule option
40
- const options = {
41
- ecmaVersion : 6 ,
42
- }
43
18
44
19
try {
45
- const ast = parse ( text , options ) ;
46
- // check EmptyStatement -> if EmptyStatement exist return false
20
+ const ast = parse ( text , {
21
+ ecmaVersion : 6 ,
22
+ } ) ;
47
23
return Boolean ( ast ) ;
48
- } catch ( err ) {
49
- //console.error(err)
24
+ } catch ( error ) {
50
25
return false ;
51
26
}
52
27
}
53
28
54
- const firstLine = function ( str ) {
55
- return str . split ( '\n' ) [ 0 ] ;
56
- }
57
-
58
- const cutStr = function ( str ) {
59
- const MAX_LENGTH = 20 ;
60
-
61
- var line = firstLine ( str ) ;
62
- if ( line . length > MAX_LENGTH ) {
63
- line = line . substr ( 0 , MAX_LENGTH ) + ' ...' ;
29
+ const create = function ( context ) {
30
+ const sourceCode = context . getSourceCode ( )
31
+ const comments = sourceCode . getAllComments ( )
32
+
33
+ return {
34
+ 'Program' : node => {
35
+ comments . forEach ( function ( comment ) {
36
+ if ( isValidCode ( comment . value ) ) {
37
+ context . report ( {
38
+ node : comments [ 0 ] ,
39
+ message : 'commented out code ' ,
40
+ // eslint-disable-next-line id-blacklist
41
+ data : {
42
+ nbComments : comments . length
43
+ }
44
+ } )
45
+ }
46
+ } )
47
+ } ,
64
48
}
65
- return line ;
66
- }
67
-
68
- /**
69
- * @return array
70
- */
71
- const getCommentsWithCode = function ( context ) {
72
- // deprecated: Use sourceCode.getAllComments() instead.
73
- var comments = context . getAllComments ( ) ;
74
-
75
- //TODO: array -> use map ?
76
- //TODO: can move to a function
77
- // eslint-disable-next-line no-useless-assign/no-useless-assign
78
- comments = comments . filter ( function ( comment ) {
79
- return isValidCode ( comment . value . trim ( ) ) ;
80
- } )
81
- return comments
82
49
}
83
50
84
- module . exports = function ( context ) {
85
- const comments = getCommentsWithCode ( context )
86
-
87
- comments . forEach ( function ( commentedCode ) {
88
- const code = cutStr ( commentedCode . value . trim ( ) ) ; //TODO: duplicate code : trim
89
- const lines = commentedCode . loc . end . line - commentedCode . loc . start . line + 1 ;
90
- const linesMsg = '(' + lines + ' line' + ( lines === 1 ? '' : 's' ) + ')' ;
91
-
92
- //TODO: support --fix in no-commented-out-code rule
93
- context . report ( {
94
- loc : commentedCode . loc
95
- } , 'commented out code ' //+ quote(code) + ' ' + linesMsg //TODO: update BIS
96
- ) ;
97
- } ) ;
98
-
99
- return { }
100
- } ;
101
-
102
- // TODO: add recommanded: false
51
+ module . exports = {
52
+ create,
53
+ meta : {
54
+ docs : {
55
+ description : "Detect commented code"
56
+ } ,
57
+ }
58
+ }
0 commit comments