Skip to content

Commit d94f697

Browse files
committed
add rule over
1 parent acfe4b2 commit d94f697

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
});

scripts/eslint-rules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
'no-primitive-constructors': require('./no-primitive-constructors'),
66
'no-to-warn-dev-within-to-throw': require('./no-to-warn-dev-within-to-throw'),
77
'warning-and-invariant-args': require('./warning-and-invariant-args'),
8+
'no-production-logging': require('./no-production-logging'),
89
},
910
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
};

0 commit comments

Comments
 (0)