@@ -20,107 +20,108 @@ const cryptoModules = ['crypto', 'http2'];
20
20
const requireModules = cryptoModules . concat ( [ 'tls' , 'https' ] ) ;
21
21
const bindingModules = cryptoModules . concat ( [ 'tls_wrap' ] ) ;
22
22
23
- module . exports = function ( context ) {
24
- const missingCheckNodes = [ ] ;
25
- const requireNodes = [ ] ;
26
- let commonModuleNode = null ;
27
- let hasSkipCall = false ;
23
+ module . exports = {
24
+ meta : {
25
+ fixable : 'code' ,
26
+ } ,
27
+ create ( context ) {
28
+ const missingCheckNodes = [ ] ;
29
+ const requireNodes = [ ] ;
30
+ let commonModuleNode = null ;
31
+ let hasSkipCall = false ;
32
+
33
+ function testCryptoUsage ( node ) {
34
+ if ( utils . isRequired ( node , requireModules ) ||
35
+ utils . isBinding ( node , bindingModules ) ) {
36
+ requireNodes . push ( node ) ;
37
+ }
28
38
29
- function testCryptoUsage ( node ) {
30
- if ( utils . isRequired ( node , requireModules ) ||
31
- utils . isBinding ( node , bindingModules ) ) {
32
- requireNodes . push ( node ) ;
39
+ if ( utils . isCommonModule ( node ) ) {
40
+ commonModuleNode = node ;
41
+ }
33
42
}
34
43
35
- if ( utils . isCommonModule ( node ) ) {
36
- commonModuleNode = node ;
44
+ function testIfStatement ( node ) {
45
+ if ( node . test . argument === undefined ) {
46
+ return ;
47
+ }
48
+ if ( isCryptoCheck ( node . test . argument ) ) {
49
+ checkCryptoCall ( node ) ;
50
+ }
37
51
}
38
- }
39
52
40
- function testIfStatement ( node ) {
41
- if ( node . test . argument === undefined ) {
42
- return ;
43
- }
44
- if ( isCryptoCheck ( node . test . argument ) ) {
45
- checkCryptoCall ( node ) ;
53
+ function isCryptoCheck ( node ) {
54
+ return utils . usesCommonProperty ( node , [ 'hasCrypto' , 'hasFipsCrypto' ] ) ;
46
55
}
47
- }
48
-
49
- function isCryptoCheck ( node ) {
50
- return utils . usesCommonProperty ( node , [ 'hasCrypto' , 'hasFipsCrypto' ] ) ;
51
- }
52
56
53
- function checkCryptoCall ( node ) {
54
- if ( utils . inSkipBlock ( node ) ) {
55
- hasSkipCall = true ;
56
- } else {
57
- missingCheckNodes . push ( node ) ;
57
+ function checkCryptoCall ( node ) {
58
+ if ( utils . inSkipBlock ( node ) ) {
59
+ hasSkipCall = true ;
60
+ } else {
61
+ missingCheckNodes . push ( node ) ;
62
+ }
58
63
}
59
- }
60
64
61
- function testMemberExpression ( node ) {
62
- if ( isCryptoCheck ( node ) ) {
63
- checkCryptoCall ( node ) ;
65
+ function testMemberExpression ( node ) {
66
+ if ( isCryptoCheck ( node ) ) {
67
+ checkCryptoCall ( node ) ;
68
+ }
64
69
}
65
- }
66
-
67
- function reportIfMissingCheck ( ) {
68
- if ( hasSkipCall ) {
69
- // There is a skip, which is good, but verify that the require() calls
70
- // in question come after at least one check.
71
- if ( missingCheckNodes . length > 0 ) {
72
- requireNodes . forEach ( ( requireNode ) => {
73
- const beforeAllChecks = missingCheckNodes . every ( ( checkNode ) => {
74
- return requireNode . range [ 0 ] < checkNode . range [ 0 ] ;
75
- } ) ;
76
70
77
- if ( beforeAllChecks ) {
78
- context . report ( {
79
- node : requireNode ,
80
- message : msg
71
+ function reportIfMissingCheck ( ) {
72
+ if ( hasSkipCall ) {
73
+ // There is a skip, which is good, but verify that the require() calls
74
+ // in question come after at least one check.
75
+ if ( missingCheckNodes . length > 0 ) {
76
+ requireNodes . forEach ( ( requireNode ) => {
77
+ const beforeAllChecks = missingCheckNodes . every ( ( checkNode ) => {
78
+ return requireNode . range [ 0 ] < checkNode . range [ 0 ] ;
81
79
} ) ;
82
- }
83
- } ) ;
80
+
81
+ if ( beforeAllChecks ) {
82
+ context . report ( {
83
+ node : requireNode ,
84
+ message : msg
85
+ } ) ;
86
+ }
87
+ } ) ;
88
+ }
89
+ return ;
84
90
}
85
- return ;
86
- }
87
91
88
- if ( requireNodes . length > 0 ) {
89
- if ( missingCheckNodes . length > 0 ) {
90
- report ( missingCheckNodes ) ;
91
- } else {
92
- report ( requireNodes ) ;
92
+ if ( requireNodes . length > 0 ) {
93
+ if ( missingCheckNodes . length > 0 ) {
94
+ report ( missingCheckNodes ) ;
95
+ } else {
96
+ report ( requireNodes ) ;
97
+ }
93
98
}
94
99
}
95
- }
96
100
97
- function report ( nodes ) {
98
- nodes . forEach ( ( node ) => {
99
- context . report ( {
100
- node,
101
- message : msg ,
102
- fix : ( fixer ) => {
103
- if ( commonModuleNode ) {
104
- return fixer . insertTextAfter (
105
- commonModuleNode ,
106
- '\nif (!common.hasCrypto) {' +
107
- ' common.skip("missing crypto");' +
108
- '}'
109
- ) ;
101
+ function report ( nodes ) {
102
+ nodes . forEach ( ( node ) => {
103
+ context . report ( {
104
+ node,
105
+ message : msg ,
106
+ fix : ( fixer ) => {
107
+ if ( commonModuleNode ) {
108
+ return fixer . insertTextAfter (
109
+ commonModuleNode ,
110
+ '\nif (!common.hasCrypto) {' +
111
+ ' common.skip("missing crypto");' +
112
+ '}'
113
+ ) ;
114
+ }
110
115
}
111
- }
116
+ } ) ;
112
117
} ) ;
113
- } ) ;
114
- }
115
-
116
- return {
117
- 'CallExpression' : ( node ) => testCryptoUsage ( node ) ,
118
- 'IfStatement:exit' : ( node ) => testIfStatement ( node ) ,
119
- 'MemberExpression:exit' : ( node ) => testMemberExpression ( node ) ,
120
- 'Program:exit' : ( ) => reportIfMissingCheck ( )
121
- } ;
122
- } ;
118
+ }
123
119
124
- module . exports . meta = {
125
- fixable : 'code'
120
+ return {
121
+ 'CallExpression' : ( node ) => testCryptoUsage ( node ) ,
122
+ 'IfStatement:exit' : ( node ) => testIfStatement ( node ) ,
123
+ 'MemberExpression:exit' : ( node ) => testMemberExpression ( node ) ,
124
+ 'Program:exit' : ( ) => reportIfMissingCheck ( )
125
+ } ;
126
+ }
126
127
} ;
0 commit comments