Skip to content

Commit dcf5e15

Browse files
committed
refactor: merge isDebugUtil & isOneOfDebugUtils utils
1 parent 03564b5 commit dcf5e15

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ABSENCE_MATCHERS,
2626
ALL_QUERIES_COMBINATIONS,
2727
ASYNC_UTILS,
28+
DEBUG_UTILS,
2829
PRESENCE_MATCHERS,
2930
} from '../utils';
3031

@@ -79,10 +80,9 @@ type IsRenderUtilFn = (node: TSESTree.Identifier) => boolean;
7980
type IsRenderVariableDeclaratorFn = (
8081
node: TSESTree.VariableDeclarator
8182
) => boolean;
82-
type IsDebugUtilFn = (identifierNode: TSESTree.Identifier) => boolean;
83-
type IsOneOfDebugUtils = (
83+
type IsDebugUtilFn = (
8484
identifierNode: TSESTree.Identifier,
85-
names: string[]
85+
validNames?: ReadonlyArray<typeof DEBUG_UTILS[number]>
8686
) => boolean;
8787
type IsPresenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
8888
type IsAbsenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
@@ -116,7 +116,6 @@ export interface DetectionHelpers {
116116
isRenderUtil: IsRenderUtilFn;
117117
isRenderVariableDeclarator: IsRenderVariableDeclaratorFn;
118118
isDebugUtil: IsDebugUtilFn;
119-
isOneOfDebugUtils: IsOneOfDebugUtils;
120119
isPresenceAssert: IsPresenceAssertFn;
121120
isAbsenceAssert: IsAbsenceAssertFn;
122121
canReportErrors: CanReportErrorsFn;
@@ -600,9 +599,9 @@ export function detectTestingLibraryUtils<
600599
return isRenderUtil(initIdentifierNode);
601600
};
602601

603-
const isOneOfDebugUtils: IsOneOfDebugUtils = (
602+
const isDebugUtil: IsDebugUtilFn = (
604603
identifierNode,
605-
names: string[]
604+
validNames = DEBUG_UTILS
606605
) => {
607606
const isBuiltInConsole =
608607
isMemberExpression(identifierNode.parent) &&
@@ -614,26 +613,11 @@ export function detectTestingLibraryUtils<
614613
isTestingLibraryUtil(
615614
identifierNode,
616615
(identifierNodeName, originalNodeName) => {
617-
return names.includes(originalNodeName || identifierNodeName);
618-
}
619-
)
620-
);
621-
};
622-
623-
const isDebugUtil: IsDebugUtilFn = (identifierNode) => {
624-
const isBuiltInConsole =
625-
isMemberExpression(identifierNode.parent) &&
626-
ASTUtils.isIdentifier(identifierNode.parent.object) &&
627-
identifierNode.parent.object.name === 'console';
628-
629-
return (
630-
!isBuiltInConsole &&
631-
isTestingLibraryUtil(
632-
identifierNode,
633-
(identifierNodeName, originalNodeName) => {
634-
return [identifierNodeName, originalNodeName]
635-
.filter(Boolean)
636-
.includes('debug');
616+
return (
617+
(validNames as string[]).includes(identifierNodeName) ||
618+
(!!originalNodeName &&
619+
(validNames as string[]).includes(originalNodeName))
620+
);
637621
}
638622
)
639623
);
@@ -832,7 +816,6 @@ export function detectTestingLibraryUtils<
832816
isRenderUtil,
833817
isRenderVariableDeclarator,
834818
isDebugUtil,
835-
isOneOfDebugUtils,
836819
isPresenceAssert,
837820
isAbsenceAssert,
838821
canReportErrors,

lib/rules/no-debug.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import {
88
isObjectPattern,
99
isProperty,
1010
} from '../node-utils';
11+
import { DEBUG_UTILS } from '../utils';
1112
import { createTestingLibraryRule } from '../create-testing-library-rule';
1213
import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils';
1314

1415
export const RULE_NAME = 'no-debug';
1516
export type MessageIds = 'noDebug';
16-
type Options = [{ utilNames: Array<'debug' | 'logTestingPlaygroundURL'> }];
17+
type Options = [{ utilNames: Array<typeof DEBUG_UTILS[number]> }];
1718

1819
export default createTestingLibraryRule<Options, MessageIds>({
1920
name: RULE_NAME,
@@ -40,7 +41,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
4041
type: 'array',
4142
items: {
4243
type: 'string',
43-
enum: ['debug', 'logTestingPlaygroundURL'],
44+
enum: DEBUG_UTILS,
4445
},
4546
},
4647
},
@@ -133,7 +134,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
133134
return;
134135
}
135136

136-
const isDebugUtil = helpers.isOneOfDebugUtils(
137+
const isDebugUtil = helpers.isDebugUtil(
137138
callExpressionIdentifier,
138139
utilNames
139140
);

lib/utils/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ const ASYNC_UTILS = [
6666
'waitForDomChange',
6767
] as const;
6868

69+
const DEBUG_UTILS = [
70+
'debug', //
71+
'logTestingPlaygroundURL',
72+
] as const;
73+
6974
const EVENTS_SIMULATORS = ['fireEvent', 'userEvent'] as const;
7075

7176
const TESTING_FRAMEWORK_SETUP_HOOKS = ['beforeEach', 'beforeAll'];
@@ -119,6 +124,7 @@ export {
119124
ASYNC_QUERIES_COMBINATIONS,
120125
ALL_QUERIES_COMBINATIONS,
121126
ASYNC_UTILS,
127+
DEBUG_UTILS,
122128
EVENTS_SIMULATORS,
123129
TESTING_FRAMEWORK_SETUP_HOOKS,
124130
LIBRARY_MODULES,

0 commit comments

Comments
 (0)