Skip to content

Commit 42d84c9

Browse files
refactor(consistent-data-testid): use createTestingLibraryRule (#362)
Uses our custom rule creator to be able to inherit config from it. New option added to be able to skip the `canReport` check since this rule doesn't apply to test files. * feat(createTestingLibraryRule): add detectionOptions config * refactor(consistent-data-testid): use createTestingLibraryRule * revert(createTestingLibraryRule): keep function style * test(consistent-data-testid): add tests with aggressive reporting disabled
1 parent 84d80a5 commit 42d84c9

File tree

4 files changed

+244
-201
lines changed

4 files changed

+244
-201
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
TSESLint,
44
TSESTree,
55
} from '@typescript-eslint/experimental-utils';
6+
67
import {
78
getAssertNodeInfo,
89
getDeepestIdentifierNode,
@@ -19,13 +20,13 @@ import {
1920
isMemberExpression,
2021
isObjectPattern,
2122
isProperty,
22-
} from './node-utils';
23+
} from '../node-utils';
2324
import {
2425
ABSENCE_MATCHERS,
2526
ALL_QUERIES_COMBINATIONS,
2627
ASYNC_UTILS,
2728
PRESENCE_MATCHERS,
28-
} from './utils';
29+
} from '../utils';
2930

3031
const SETTING_OPTION_OFF = 'off' as const;
3132

@@ -123,14 +124,28 @@ const FIRE_EVENT_NAME = 'fireEvent';
123124
const USER_EVENT_NAME = 'userEvent';
124125
const RENDER_NAME = 'render';
125126

127+
export type DetectionOptions = {
128+
/**
129+
* If true, force `detectTestingLibraryUtils` to skip `canReportErrors`
130+
* so it doesn't opt-out rule listener.
131+
*
132+
* Useful when some rule apply to files other than testing ones
133+
* (e.g. `consistent-data-testid`)
134+
*/
135+
skipRuleReportingCheck: boolean;
136+
};
137+
126138
/**
127139
* Enhances a given rule `create` with helpers to detect Testing Library utils.
128140
*/
129141
export function detectTestingLibraryUtils<
130142
TOptions extends readonly unknown[],
131143
TMessageIds extends string,
132144
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener
133-
>(ruleCreate: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>) {
145+
>(
146+
ruleCreate: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>,
147+
{ skipRuleReportingCheck = false }: Partial<DetectionOptions> = {}
148+
) {
134149
return (
135150
context: TestingLibraryContext<TOptions, TMessageIds>,
136151
optionsWithDefault: Readonly<TOptions>
@@ -742,7 +757,7 @@ export function detectTestingLibraryUtils<
742757
* Determines if file inspected meets all conditions to be reported by rules or not.
743758
*/
744759
const canReportErrors: CanReportErrorsFn = () => {
745-
return isTestingLibraryImported();
760+
return skipRuleReportingCheck || isTestingLibraryImported();
746761
};
747762

748763
/**
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ESLintUtils, TSESLint } from '@typescript-eslint/experimental-utils';
2-
import { getDocsUrl } from './utils';
2+
3+
import { getDocsUrl } from '../utils';
4+
35
import {
6+
DetectionOptions,
47
detectTestingLibraryUtils,
58
EnhancedRuleCreate,
69
} from './detect-testing-library-utils';
@@ -15,20 +18,22 @@ export function createTestingLibraryRule<
1518
TOptions extends readonly unknown[],
1619
TMessageIds extends string,
1720
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener
18-
>(
19-
config: Readonly<{
20-
name: string;
21-
meta: CreateRuleMeta<TMessageIds>;
22-
defaultOptions: Readonly<TOptions>;
23-
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
24-
}>
25-
): TSESLint.RuleModule<TMessageIds, TOptions> {
26-
const { create, ...remainingConfig } = config;
27-
21+
>({
22+
create,
23+
detectionOptions = {},
24+
...remainingConfig
25+
}: Readonly<{
26+
name: string;
27+
meta: CreateRuleMeta<TMessageIds>;
28+
defaultOptions: Readonly<TOptions>;
29+
detectionOptions?: Partial<DetectionOptions>;
30+
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
31+
}>): TSESLint.RuleModule<TMessageIds, TOptions> {
2832
return ESLintUtils.RuleCreator(getDocsUrl)({
2933
...remainingConfig,
3034
create: detectTestingLibraryUtils<TOptions, TMessageIds, TRuleListener>(
31-
create
35+
create,
36+
detectionOptions
3237
),
3338
});
3439
}

lib/rules/consistent-data-testid.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { getDocsUrl } from '../utils';
2-
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
1+
import { createTestingLibraryRule } from '../create-testing-library-rule';
32
import { isJSXAttribute, isLiteral } from '../node-utils';
43

54
export const RULE_NAME = 'consistent-data-testid';
65
export type MessageIds = 'consistentDataTestId';
7-
type Options = [
6+
export type Options = [
87
{
98
testIdAttribute?: string | string[];
109
testIdPattern: string;
@@ -13,12 +12,7 @@ type Options = [
1312

1413
const FILENAME_PLACEHOLDER = '{fileName}';
1514

16-
/**
17-
* This rule is not created with `createTestingLibraryRule` since:
18-
* - it doesn't need any detection helper
19-
* - it doesn't apply to testing files but component files
20-
*/
21-
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
15+
export default createTestingLibraryRule<Options, MessageIds>({
2216
name: RULE_NAME,
2317
meta: {
2418
type: 'suggestion',
@@ -64,8 +58,11 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
6458
testIdAttribute: 'data-testid',
6559
},
6660
],
61+
detectionOptions: {
62+
skipRuleReportingCheck: true,
63+
},
6764

68-
create(context, [options]) {
65+
create: (context, [options]) => {
6966
const { getFilename } = context;
7067
const { testIdPattern, testIdAttribute: attr } = options;
7168

0 commit comments

Comments
 (0)