File tree Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @emails react-core
8
+ */
9
+
10
+ 'use strict' ;
11
+
12
+ const rule = require ( '../no-production-logging' ) ;
13
+ const RuleTester = require ( 'eslint' ) . RuleTester ;
14
+ const ruleTester = new RuleTester ( ) ;
15
+
16
+ ruleTester . run ( "no-production-logging" , rule , {
17
+ valid : [
18
+ {
19
+ code : "if (__DEV__) {consoleLog(test)}" ,
20
+ } ,
21
+ {
22
+ code : "if (__DEV__) {consoleError(test)}" ,
23
+ } ,
24
+ {
25
+ code : "if (__DEV__) { if (potato) { while (true) { consoleError(test) }}}" ,
26
+ } ,
27
+ {
28
+ code : "normalFunctionCall(test)" ,
29
+ } ,
30
+ {
31
+ code : "if (__DEV__) {normalFunctionCall(test)}" ,
32
+ } ,
33
+ ] ,
34
+ invalid : [
35
+ {
36
+ code : "consoleLog(test)" ,
37
+ errors : [
38
+ {
39
+ message : "Wrap consoleLog in a `if (__DEV__)` check" ,
40
+ } ,
41
+ ] ,
42
+ } ,
43
+ {
44
+ code : "if (potato) {consoleLog(test)}" ,
45
+ errors : [
46
+ {
47
+ message : "Wrap consoleLog in a `if (__DEV__)` check" ,
48
+ } ,
49
+ ] ,
50
+ } ,
51
+ {
52
+ code : "consoleError(test)" ,
53
+ errors : [
54
+ {
55
+ message : "Wrap consoleError in a `if (__DEV__)` check" ,
56
+ } ,
57
+ ] ,
58
+ } ,
59
+ ] ,
60
+ } ) ;
Original file line number Diff line number Diff line change @@ -5,5 +5,6 @@ module.exports = {
5
5
'no-primitive-constructors' : require ( './no-primitive-constructors' ) ,
6
6
'no-to-warn-dev-within-to-throw' : require ( './no-to-warn-dev-within-to-throw' ) ,
7
7
'warning-and-invariant-args' : require ( './warning-and-invariant-args' ) ,
8
+ 'no-production-logging' : require ( './no-production-logging' ) ,
8
9
} ,
9
10
} ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @emails react-core
8
+ */
9
+
10
+ 'use strict' ;
11
+
12
+ module . exports = function ( context ) {
13
+ function hasIfInParents ( node ) {
14
+ let done = false ;
15
+ while ( ! done ) {
16
+ if ( ! node . parent ) {
17
+ return false ;
18
+ }
19
+ node = node . parent ;
20
+ if ( node . type === 'IfStatement' && node . test . name === '__DEV__' ) {
21
+ return true ;
22
+ }
23
+ }
24
+ }
25
+
26
+ const isLoggerNode = name => [ 'consoleLog' , 'consoleError' ] . includes ( name ) ;
27
+
28
+ return {
29
+ CallExpression : node => {
30
+ if ( ! isLoggerNode ( node . callee . name ) ) {
31
+ return ;
32
+ }
33
+ if ( ! hasIfInParents ( node ) ) {
34
+ context . report ( {
35
+ node : node ,
36
+ message : 'Wrap {{identifier}} in a `if (__DEV__)` check' ,
37
+ data : {
38
+ identifier : node . callee . name ,
39
+ } ,
40
+ } ) ;
41
+ }
42
+ } ,
43
+ } ;
44
+ } ;
You can’t perform that action at this time.
0 commit comments