Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c791455

Browse files
committed
support &&
1 parent 38f5068 commit c791455

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

scripts/eslint-rules/__tests__/no-production-logging-test.internal.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ ruleTester.run('no-production-logging', rule, {
2424
},
2525
/* calls wrapped on an outer bound */
2626
{
27-
code: 'if (__DEV__) { if (potato) { while (true) { warning(test) }}}',
27+
code: 'if (__DEV__) { if (potato) { while (true) { warning(test) } } }',
28+
},
29+
/* calls wrapped on an if with && */
30+
{
31+
code: 'if (banana && __DEV__ && potato && kitten) { warning(test) }',
2832
},
2933
/* don't do anything to arbitrary fn's or invariants */
3034
{
@@ -44,7 +48,7 @@ ruleTester.run('no-production-logging', rule, {
4448
],
4549
},
4650
{
47-
code: 'if (potato) {warningWithoutStack(test)}',
51+
code: 'if (potato) { warningWithoutStack(test) }',
4852
errors: [
4953
{
5054
message: 'Wrap warningWithoutStack in a `if (__DEV__)` check',
@@ -59,5 +63,13 @@ ruleTester.run('no-production-logging', rule, {
5963
},
6064
],
6165
},
66+
{
67+
code: 'if (__DEV__ || potato && true) { warning(test) }',
68+
errors: [
69+
{
70+
message: 'Wrap warning in a `if (__DEV__)` check',
71+
},
72+
],
73+
},
6274
],
6375
});

scripts/eslint-rules/no-production-logging.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,31 @@
1010
'use strict';
1111

1212
module.exports = function(context) {
13+
function traverseIf(node) {
14+
switch (node.type) {
15+
case 'Identifier':
16+
return [node.name];
17+
case 'LogicalExpression':
18+
if (node.operator === '&&') {
19+
return [...traverseIf(node.left), ...traverseIf(node.right)];
20+
}
21+
return [];
22+
default:
23+
return [];
24+
}
25+
}
26+
1327
function hasIfInParents(node) {
1428
let done = false;
1529
while (!done) {
1630
if (!node.parent) {
1731
return false;
1832
}
1933
node = node.parent;
20-
if (node.type === 'IfStatement' && node.test.name === '__DEV__') {
34+
if (
35+
node.type === 'IfStatement' &&
36+
traverseIf(node.test).includes('__DEV__')
37+
) {
2138
return true;
2239
}
2340
}

0 commit comments

Comments
 (0)