Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/detect-testing-library-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type IsRenderUtilFn = (node: TSESTree.Identifier) => boolean;
type IsRenderVariableDeclaratorFn = (
node: TSESTree.VariableDeclarator
) => boolean;
type IsDebugUtilFn = (node: TSESTree.Identifier) => boolean;
type IsDebugUtilFn = (identifierNode: TSESTree.Identifier) => boolean;
type IsPresenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
type IsAbsenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
type CanReportErrorsFn = () => boolean;
Expand Down Expand Up @@ -580,14 +580,22 @@ export function detectTestingLibraryUtils<
return isRenderUtil(initIdentifierNode);
};

const isDebugUtil: IsDebugUtilFn = (node) => {
return isTestingLibraryUtil(
node,
(identifierNodeName, originalNodeName) => {
return [identifierNodeName, originalNodeName]
.filter(Boolean)
.includes('debug');
}
const isDebugUtil: IsDebugUtilFn = (identifierNode) => {
const isBuiltInConsole =
isMemberExpression(identifierNode.parent) &&
ASTUtils.isIdentifier(identifierNode.parent.object) &&
identifierNode.parent.object.name === 'console';

return (
!isBuiltInConsole &&
isTestingLibraryUtil(
identifierNode,
(identifierNodeName, originalNodeName) => {
return [identifierNodeName, originalNodeName]
.filter(Boolean)
.includes('debug');
}
)
);
};

Expand Down
23 changes: 22 additions & 1 deletion lib/rules/no-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getInnermostReturningFunction,
getPropertyIdentifierNode,
getReferenceNode,
isCallExpression,
isObjectPattern,
isProperty,
} from '../node-utils';
Expand Down Expand Up @@ -34,6 +35,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
const suspiciousDebugVariableNames: string[] = [];
const suspiciousReferenceNodes: TSESTree.Identifier[] = [];
const renderWrapperNames: string[] = [];
const builtInConsoleNodes: TSESTree.VariableDeclarator[] = [];

function detectRenderWrapper(node: TSESTree.Identifier): void {
const innerFunction = getInnermostReturningFunction(context, node);
Expand All @@ -54,6 +56,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

if (initIdentifierNode.name === 'console') {
builtInConsoleNodes.push(node);
return;
}

const isRenderWrapperVariableDeclarator = initIdentifierNode
? renderWrapperNames.includes(initIdentifierNode.name)
: false;
Expand Down Expand Up @@ -120,7 +127,21 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
);

if (isDebugUtil || isDeclaredDebugVariable || isChainedReferenceDebug) {
const isVariableFromBuiltInConsole = builtInConsoleNodes.some(
(variableDeclarator) => {
const variables = context.getDeclaredVariables(variableDeclarator);
return variables.some(
({ name }) =>
name === callExpressionIdentifier.name &&
isCallExpression(callExpressionIdentifier.parent)
);
}
);

if (
!isVariableFromBuiltInConsole &&
(isDebugUtil || isDeclaredDebugVariable || isChainedReferenceDebug)
) {
context.report({
node: callExpressionIdentifier,
messageId: 'noDebug',
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/rules/no-debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ ruleTester.run(RULE_NAME, rule, {
settings: { 'testing-library/utils-module': 'test-utils' },
code: `screen.debug()`,
},
{
code: `console.debug()`,
},
{
code: `
const consoleDebug = console.debug
consoleDebug()
`,
},
{
code: `
const { debug } = console
debug()
`,
},
{
code: `
const { debug: consoleDebug } = console
consoleDebug()
`,
},
{
code: `
const { screen } = require('@testing-library/dom')
Expand Down Expand Up @@ -516,5 +537,23 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { render } from '@testing-library/react'

const utils = render(element)
const { debug: renamedDestructuredDebug } = console
const { debug } = console
const assignedDebug = console.debug
console.debug('debugging')
debug('destructured')
assignedDebug('foo')
// the following line is the one that fails
utils.debug()
renamedDestructuredDebug('foo')
`,
errors: [{ line: 12, column: 13, messageId: 'noDebug' }],
},
],
});