Skip to content

refactor(consistent-data-testid): use createTestingLibraryRule #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 27, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';

import {
getAssertNodeInfo,
getDeepestIdentifierNode,
Expand All @@ -19,13 +20,13 @@ import {
isMemberExpression,
isObjectPattern,
isProperty,
} from './node-utils';
} from '../node-utils';
import {
ABSENCE_MATCHERS,
ALL_QUERIES_COMBINATIONS,
ASYNC_UTILS,
PRESENCE_MATCHERS,
} from './utils';
} from '../utils';

const SETTING_OPTION_OFF = 'off' as const;

Expand Down Expand Up @@ -123,14 +124,28 @@ const FIRE_EVENT_NAME = 'fireEvent';
const USER_EVENT_NAME = 'userEvent';
const RENDER_NAME = 'render';

export type DetectionOptions = {
/**
* If true, force `detectTestingLibraryUtils` to skip `canReportErrors`
* so it doesn't opt-out rule listener.
*
* Useful when some rule apply to files other than testing ones
* (e.g. `consistent-data-testid`)
*/
skipRuleReportingCheck: boolean;
};

/**
* Enhances a given rule `create` with helpers to detect Testing Library utils.
*/
export function detectTestingLibraryUtils<
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener
>(ruleCreate: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>) {
>(
ruleCreate: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>,
{ skipRuleReportingCheck = false }: Partial<DetectionOptions> = {}
) {
return (
context: TestingLibraryContext<TOptions, TMessageIds>,
optionsWithDefault: Readonly<TOptions>
Expand Down Expand Up @@ -742,7 +757,7 @@ export function detectTestingLibraryUtils<
* Determines if file inspected meets all conditions to be reported by rules or not.
*/
const canReportErrors: CanReportErrorsFn = () => {
return isTestingLibraryImported();
return skipRuleReportingCheck || isTestingLibraryImported();
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ESLintUtils, TSESLint } from '@typescript-eslint/experimental-utils';
import { getDocsUrl } from './utils';

import { getDocsUrl } from '../utils';

import {
DetectionOptions,
detectTestingLibraryUtils,
EnhancedRuleCreate,
} from './detect-testing-library-utils';
Expand All @@ -15,20 +18,22 @@ export function createTestingLibraryRule<
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener
>(
config: Readonly<{
name: string;
meta: CreateRuleMeta<TMessageIds>;
defaultOptions: Readonly<TOptions>;
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
}>
): TSESLint.RuleModule<TMessageIds, TOptions> {
const { create, ...remainingConfig } = config;

>({
create,
detectionOptions = {},
...remainingConfig
}: Readonly<{
name: string;
meta: CreateRuleMeta<TMessageIds>;
defaultOptions: Readonly<TOptions>;
detectionOptions?: Partial<DetectionOptions>;
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
}>): TSESLint.RuleModule<TMessageIds, TOptions> {
return ESLintUtils.RuleCreator(getDocsUrl)({
...remainingConfig,
create: detectTestingLibraryUtils<TOptions, TMessageIds, TRuleListener>(
create
create,
detectionOptions
),
});
}
17 changes: 7 additions & 10 deletions lib/rules/consistent-data-testid.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { getDocsUrl } from '../utils';
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import { isJSXAttribute, isLiteral } from '../node-utils';

export const RULE_NAME = 'consistent-data-testid';
export type MessageIds = 'consistentDataTestId';
type Options = [
export type Options = [
{
testIdAttribute?: string | string[];
testIdPattern: string;
Expand All @@ -13,12 +12,7 @@ type Options = [

const FILENAME_PLACEHOLDER = '{fileName}';

/**
* This rule is not created with `createTestingLibraryRule` since:
* - it doesn't need any detection helper
* - it doesn't apply to testing files but component files
*/
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -64,8 +58,11 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
testIdAttribute: 'data-testid',
},
],
detectionOptions: {
skipRuleReportingCheck: true,
},

create(context, [options]) {
create: (context, [options]) => {
const { getFilename } = context;
const { testIdPattern, testIdAttribute: attr } = options;

Expand Down
Loading