Skip to content

Commit 2c473ab

Browse files
committed
feat(no-debug): support utilNames option
1 parent 610b3b9 commit 2c473ab

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

docs/rules/no-debug.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const { screen } = require('@testing-library/react');
2828
screen.debug();
2929
```
3030

31+
If you want to disallow the use of other debugging functions, you can configure what names this rule checks for with the `utilNames` option:
32+
33+
```
34+
"testing-library/no-debug": ["error", {"utilNames": ["debug", "logTestingPlaygroundURL"]}],
35+
```
36+
3137
## Further Reading
3238

3339
- [debug API in React Testing Library](https://testing-library.com/docs/react-testing-library/api#debug)

lib/create-testing-library-rule/detect-testing-library-utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ type IsRenderVariableDeclaratorFn = (
8080
node: TSESTree.VariableDeclarator
8181
) => boolean;
8282
type IsDebugUtilFn = (identifierNode: TSESTree.Identifier) => boolean;
83+
type IsOneOfDebugUtils = (
84+
identifierNode: TSESTree.Identifier,
85+
names: string[]
86+
) => boolean;
8387
type IsPresenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
8488
type IsAbsenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
8589
type CanReportErrorsFn = () => boolean;
@@ -112,6 +116,7 @@ export interface DetectionHelpers {
112116
isRenderUtil: IsRenderUtilFn;
113117
isRenderVariableDeclarator: IsRenderVariableDeclaratorFn;
114118
isDebugUtil: IsDebugUtilFn;
119+
isOneOfDebugUtils: IsOneOfDebugUtils;
115120
isPresenceAssert: IsPresenceAssertFn;
116121
isAbsenceAssert: IsAbsenceAssertFn;
117122
canReportErrors: CanReportErrorsFn;
@@ -595,6 +600,26 @@ export function detectTestingLibraryUtils<
595600
return isRenderUtil(initIdentifierNode);
596601
};
597602

603+
const isOneOfDebugUtils: IsOneOfDebugUtils = (
604+
identifierNode,
605+
names: string[]
606+
) => {
607+
const isBuiltInConsole =
608+
isMemberExpression(identifierNode.parent) &&
609+
ASTUtils.isIdentifier(identifierNode.parent.object) &&
610+
identifierNode.parent.object.name === 'console';
611+
612+
return (
613+
!isBuiltInConsole &&
614+
isTestingLibraryUtil(
615+
identifierNode,
616+
(identifierNodeName, originalNodeName) => {
617+
return names.includes(originalNodeName || identifierNodeName);
618+
}
619+
)
620+
);
621+
};
622+
598623
const isDebugUtil: IsDebugUtilFn = (identifierNode) => {
599624
const isBuiltInConsole =
600625
isMemberExpression(identifierNode.parent) &&
@@ -807,6 +832,7 @@ export function detectTestingLibraryUtils<
807832
isRenderUtil,
808833
isRenderVariableDeclarator,
809834
isDebugUtil,
835+
isOneOfDebugUtils,
810836
isPresenceAssert,
811837
isAbsenceAssert,
812838
canReportErrors,

lib/rules/no-debug.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils';
1313

1414
export const RULE_NAME = 'no-debug';
1515
export type MessageIds = 'noDebug';
16-
type Options = [];
16+
type Options = [{ utilNames: string[] }];
1717

1818
export default createTestingLibraryRule<Options, MessageIds>({
1919
name: RULE_NAME,
@@ -32,11 +32,22 @@ export default createTestingLibraryRule<Options, MessageIds>({
3232
messages: {
3333
noDebug: 'Unexpected debug statement',
3434
},
35-
schema: [],
35+
schema: [
36+
{
37+
type: 'object',
38+
properties: {
39+
utilNames: {
40+
type: 'array',
41+
items: { type: 'string' },
42+
},
43+
},
44+
additionalProperties: false,
45+
},
46+
],
3647
},
37-
defaultOptions: [],
48+
defaultOptions: [{ utilNames: ['debug'] }],
3849

39-
create(context, [], helpers) {
50+
create(context, [{ utilNames }], helpers) {
4051
const suspiciousDebugVariableNames: string[] = [];
4152
const suspiciousReferenceNodes: TSESTree.Identifier[] = [];
4253
const renderWrapperNames: string[] = [];
@@ -84,7 +95,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
8495
if (
8596
isProperty(property) &&
8697
ASTUtils.isIdentifier(property.key) &&
87-
property.key.name === 'debug'
98+
utilNames.includes(property.key.name)
8899
) {
89100
const identifierNode = getDeepestIdentifierNode(property.value);
90101

@@ -119,14 +130,17 @@ export default createTestingLibraryRule<Options, MessageIds>({
119130
return;
120131
}
121132

122-
const isDebugUtil = helpers.isDebugUtil(callExpressionIdentifier);
133+
const isDebugUtil = helpers.isOneOfDebugUtils(
134+
callExpressionIdentifier,
135+
utilNames
136+
);
123137
const isDeclaredDebugVariable = suspiciousDebugVariableNames.includes(
124138
callExpressionIdentifier.name
125139
);
126140
const isChainedReferenceDebug = suspiciousReferenceNodes.some(
127141
(suspiciousReferenceIdentifier) => {
128142
return (
129-
callExpressionIdentifier.name === 'debug' &&
143+
utilNames.includes(callExpressionIdentifier.name) &&
130144
suspiciousReferenceIdentifier.name === referenceIdentifier.name
131145
);
132146
}

tests/lib/rules/no-debug.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ ruleTester.run(RULE_NAME, rule, {
8787
screen.debug
8888
`,
8989
},
90+
{
91+
code: `
92+
import { screen } from '@testing-library/dom'
93+
screen.logTestingPlaygroundURL()
94+
`,
95+
},
96+
{
97+
code: `
98+
import { screen } from '@testing-library/dom'
99+
screen.debug()
100+
`,
101+
options: [{ utilNames: ['anotherUtil'] }],
102+
},
90103
{
91104
code: `const { queries } = require('@testing-library/dom')`,
92105
},
@@ -419,6 +432,20 @@ ruleTester.run(RULE_NAME, rule, {
419432
},
420433
],
421434
},
435+
{
436+
code: `
437+
import { screen } from '@testing-library/dom'
438+
screen.logTestingPlaygroundURL()
439+
`,
440+
options: [{ utilNames: ['logTestingPlaygroundURL'] }],
441+
errors: [
442+
{
443+
line: 3,
444+
column: 16,
445+
messageId: 'noDebug',
446+
},
447+
],
448+
},
422449
{
423450
settings: { 'testing-library/utils-module': 'test-utils' },
424451
code: `// aggressive reporting disabled

0 commit comments

Comments
 (0)