File tree 2 files changed +26
-4
lines changed
2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -12,12 +12,18 @@ const message = 'Please add a skipIfInspectorDisabled() call to allow this ' +
12
12
new RuleTester ( ) . run ( 'inspector-check' , rule , {
13
13
valid : [
14
14
'foo;' ,
15
- 'common.skipIfInspectorDisabled(); require("inspector");'
15
+ 'require("common")\n' +
16
+ 'common.skipIfInspectorDisabled();\n' +
17
+ 'require("inspector")'
16
18
] ,
17
19
invalid : [
18
20
{
19
- code : 'require("inspector")' ,
20
- errors : [ { message } ]
21
+ code : 'require("common")\n' +
22
+ 'require("inspector")' ,
23
+ errors : [ { message } ] ,
24
+ output : 'require("common")\n' +
25
+ 'common.skipIfInspectorDisabled();\n' +
26
+ 'require("inspector")'
21
27
}
22
28
]
23
29
} ) ;
Original file line number Diff line number Diff line change @@ -15,12 +15,17 @@ const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' +
15
15
16
16
module . exports = function ( context ) {
17
17
const missingCheckNodes = [ ] ;
18
+ var commonModuleNode = null ;
18
19
var hasInspectorCheck = false ;
19
20
20
21
function testInspectorUsage ( context , node ) {
21
22
if ( utils . isRequired ( node , [ 'inspector' ] ) ) {
22
23
missingCheckNodes . push ( node ) ;
23
24
}
25
+
26
+ if ( utils . isCommonModule ( node ) ) {
27
+ commonModuleNode = node ;
28
+ }
24
29
}
25
30
26
31
function checkMemberExpression ( context , node ) {
@@ -32,7 +37,18 @@ module.exports = function(context) {
32
37
function reportIfMissing ( context ) {
33
38
if ( ! hasInspectorCheck ) {
34
39
missingCheckNodes . forEach ( ( node ) => {
35
- context . report ( node , msg ) ;
40
+ context . report ( {
41
+ node,
42
+ message : msg ,
43
+ fix : ( fixer ) => {
44
+ if ( commonModuleNode ) {
45
+ return fixer . insertTextAfter (
46
+ commonModuleNode ,
47
+ '\ncommon.skipIfInspectorDisabled();'
48
+ ) ;
49
+ }
50
+ }
51
+ } ) ;
36
52
} ) ;
37
53
}
38
54
}
You can’t perform that action at this time.
0 commit comments