Skip to content

Commit 7643d5a

Browse files
thomaslombartBelco90
authored andcommitted
feat: add no-debug (#6)
* feat: add no-debug * docs: add documentation url to no-debug
1 parent ca1c96e commit 7643d5a

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ To enable this configuration use the `extends` property in your `.eslintrc` conf
6666
| -------------------------------------------------------- | --------------------------------------------- | ---------------- |
6767
| [await-async-query](docs/rules/await-async-query.md) | Enforce async queries to have proper `await` | ![recommended][] |
6868
| [no-await-sync-query](docs/rules/no-await-sync-query.md) | Disallow unnecessary `await` for sync queries | ![recommended][] |
69+
| [no-debug](docs/rules/no-debug.md) | Disallow the use of `debug` | |
6970

7071
[recommended]: https://img.shields.io/badge/recommended-lightgrey?style=flat-square

docs/rules/no-debug.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Disallow the use of `debug` (no-debug)
2+
3+
Just like `console.log` statements pollutes the browser's output, debug statements also pollutes the tests if one of your team mates forgot to remove it. `debug` statements should be used when you actually want to debug your tests but should not be pushed to the codebase.
4+
5+
## Rule Details
6+
7+
This rule aims to disallow the use of `debug` in your tests.
8+
9+
Examples of **incorrect** code for this rule:
10+
11+
```js
12+
const { debug } = render(<Hello />);
13+
debug();
14+
// OR
15+
const utils = render(<Hello />);
16+
utils.debug();
17+
```
18+
19+
## Further Reading
20+
21+
- [debug API in React Testing Library](https://testing-library.com/docs/react-testing-library/api#debug)

lib/rules/no-debug.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
const { getDocsUrl } = require('../utils');
4+
5+
module.exports = {
6+
meta: {
7+
type: 'problem',
8+
docs: {
9+
description: 'Disallow unnecessary debug usages in the tests',
10+
category: 'Best Practices',
11+
recommended: true,
12+
url: getDocsUrl('no-debug'),
13+
},
14+
messages: {
15+
noDebug: 'Unexpected debug statement',
16+
},
17+
fixable: null,
18+
schema: [],
19+
},
20+
21+
create: function(context) {
22+
let hasDestructuredDebugStatement = false;
23+
const renderVariableDeclarators = [];
24+
return {
25+
VariableDeclarator(node) {
26+
if (node.init.callee.name === 'render') {
27+
if (
28+
node.id.type === 'ObjectPattern' &&
29+
node.id.properties.some(property => property.key.name === 'debug')
30+
) {
31+
hasDestructuredDebugStatement = true;
32+
}
33+
34+
if (node.id.type === 'Identifier') {
35+
renderVariableDeclarators.push(node);
36+
}
37+
}
38+
},
39+
[`CallExpression > Identifier[name="debug"]`](node) {
40+
if (hasDestructuredDebugStatement) {
41+
context.report({
42+
node,
43+
messageId: 'noDebug',
44+
});
45+
}
46+
},
47+
'Program:exit'() {
48+
renderVariableDeclarators.forEach(renderVar => {
49+
const renderVarReferences = context
50+
.getDeclaredVariables(renderVar)[0]
51+
.references.slice(1);
52+
renderVarReferences.forEach(ref => {
53+
const parent = ref.identifier.parent;
54+
if (
55+
parent &&
56+
parent.type === 'MemberExpression' &&
57+
parent.property.name === 'debug' &&
58+
parent.parent.type === 'CallExpression'
59+
) {
60+
context.report({
61+
node: parent.property,
62+
messageId: 'noDebug',
63+
});
64+
}
65+
});
66+
});
67+
},
68+
};
69+
},
70+
};

tests/lib/rules/no-debug.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
'use strict';
2+
3+
// ------------------------------------------------------------------------------
4+
// Requirements
5+
// ------------------------------------------------------------------------------
6+
7+
const rule = require('../../../lib/rules/no-debug');
8+
const RuleTester = require('eslint').RuleTester;
9+
10+
// ------------------------------------------------------------------------------
11+
// Tests
12+
// ------------------------------------------------------------------------------
13+
14+
const ruleTester = new RuleTester({
15+
parserOptions: {
16+
ecmaVersion: 2018,
17+
ecmaFeatures: {
18+
jsx: true,
19+
},
20+
},
21+
});
22+
ruleTester.run('no-debug', rule, {
23+
valid: [
24+
{
25+
code: `debug()`,
26+
},
27+
{
28+
code: `() => {
29+
const { debug } = foo()
30+
debug()
31+
}`,
32+
},
33+
{
34+
code: `
35+
const debug = require('debug')
36+
debug()
37+
`,
38+
},
39+
{
40+
code: `
41+
const { test } = render(<Component/>)
42+
test()
43+
`,
44+
},
45+
{
46+
code: `
47+
const utils = render(<Component/>)
48+
utils.debug
49+
`,
50+
},
51+
{
52+
code: `
53+
const utils = render(<Component/>)
54+
utils.foo()
55+
`,
56+
},
57+
],
58+
59+
invalid: [
60+
{
61+
code: `
62+
const { debug } = render(<Component/>)
63+
debug()
64+
`,
65+
errors: [
66+
{
67+
messageId: 'noDebug',
68+
},
69+
],
70+
},
71+
{
72+
code: `
73+
const utils = render(<Component/>)
74+
utils.debug()
75+
`,
76+
errors: [
77+
{
78+
messageId: 'noDebug',
79+
},
80+
],
81+
},
82+
{
83+
code: `
84+
const utils = render(<Component/>)
85+
utils.debug()
86+
utils.foo()
87+
utils.debug()
88+
`,
89+
errors: [
90+
{
91+
messageId: 'noDebug',
92+
},
93+
{
94+
messageId: 'noDebug',
95+
},
96+
],
97+
},
98+
],
99+
});

0 commit comments

Comments
 (0)