Skip to content

refactor: automatically generate all configs #358

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 11 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
21 changes: 21 additions & 0 deletions lib/configs/angular.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// THIS CODE WAS AUTOMATICALLY GENERATED
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING npm run generate:configs

export = {
plugins: ['testing-library'],
rules: {
'testing-library/await-async-query': 'error',
'testing-library/await-async-utils': 'error',
'testing-library/no-await-sync-query': 'error',
'testing-library/no-container': 'error',
'testing-library/no-debug': 'error',
'testing-library/no-dom-import': ['error', 'angular'],
'testing-library/no-node-access': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/prefer-find-by': 'error',
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
16 changes: 16 additions & 0 deletions lib/configs/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// THIS CODE WAS AUTOMATICALLY GENERATED
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING npm run generate:configs

export = {
plugins: ['testing-library'],
rules: {
'testing-library/await-async-query': 'error',
'testing-library/await-async-utils': 'error',
'testing-library/no-await-sync-query': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/prefer-find-by': 'error',
'testing-library/prefer-screen-queries': 'error',
},
};
24 changes: 24 additions & 0 deletions lib/configs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { join } from 'path';

import type { TSESLint } from '@typescript-eslint/experimental-utils';

import {
importDefault,
SUPPORTED_TESTING_FRAMEWORKS,
SupportedTestingFramework,
} from '../utils';

export type LinterConfigRules = Record<string, TSESLint.Linter.RuleEntry>;

const configsDir = __dirname;

const getConfigForFramework = (framework: SupportedTestingFramework) =>
importDefault<LinterConfigRules>(join(configsDir, framework));

export default SUPPORTED_TESTING_FRAMEWORKS.reduce(
(allConfigs, framework) => ({
...allConfigs,
[framework]: getConfigForFramework(framework),
}),
{}
) as Record<SupportedTestingFramework, LinterConfigRules>;
21 changes: 21 additions & 0 deletions lib/configs/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// THIS CODE WAS AUTOMATICALLY GENERATED
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING npm run generate:configs

export = {
plugins: ['testing-library'],
rules: {
'testing-library/await-async-query': 'error',
'testing-library/await-async-utils': 'error',
'testing-library/no-await-sync-query': 'error',
'testing-library/no-container': 'error',
'testing-library/no-debug': 'error',
'testing-library/no-dom-import': ['error', 'react'],
'testing-library/no-node-access': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/prefer-find-by': 'error',
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
22 changes: 22 additions & 0 deletions lib/configs/vue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// THIS CODE WAS AUTOMATICALLY GENERATED
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING npm run generate:configs

export = {
plugins: ['testing-library'],
rules: {
'testing-library/await-async-query': 'error',
'testing-library/await-async-utils': 'error',
'testing-library/await-fire-event': 'error',
'testing-library/no-await-sync-query': 'error',
'testing-library/no-container': 'error',
'testing-library/no-debug': 'error',
'testing-library/no-dom-import': ['error', 'vue'],
'testing-library/no-node-access': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/prefer-find-by': 'error',
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
20 changes: 12 additions & 8 deletions lib/create-testing-library-rule/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { ESLintUtils, TSESLint } from '@typescript-eslint/experimental-utils';

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

import {
DetectionOptions,
detectTestingLibraryUtils,
EnhancedRuleCreate,
} from './detect-testing-library-utils';

// These 2 types are copied from @typescript-eslint/experimental-utils
type CreateRuleMetaDocs = Omit<TSESLint.RuleMetaDataDocs, 'url'>;
type CreateRuleMeta<TMessageIds extends string> = {
docs: CreateRuleMetaDocs;
} & Omit<TSESLint.RuleMetaData<TMessageIds>, 'docs'>;

export function createTestingLibraryRule<
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener
>({
create,
detectionOptions = {},
meta,
...remainingConfig
}: Readonly<{
name: string;
meta: CreateRuleMeta<TMessageIds>;
meta: TestingLibraryRuleMeta<TMessageIds, TOptions>;
defaultOptions: Readonly<TOptions>;
detectionOptions?: Partial<DetectionOptions>;
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
Expand All @@ -35,5 +30,14 @@ export function createTestingLibraryRule<
create,
detectionOptions
),
meta: {
...meta,
docs: {
...meta.docs,
// We're using our own recommendedConfig meta to tell our build tools
// if the rule is recommended on a config basis
recommended: false,
},
},
});
}
58 changes: 2 additions & 56 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,7 @@
import configs from './configs';
import rules from './rules';

const domRules = {
'testing-library/await-async-query': 'error',
'testing-library/await-async-utils': 'error',
'testing-library/no-await-sync-query': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/prefer-find-by': 'error',
'testing-library/prefer-screen-queries': 'error',
};

const angularRules = {
...domRules,
'testing-library/no-container': 'error',
'testing-library/no-debug': 'error',
'testing-library/no-dom-import': ['error', 'angular'],
'testing-library/no-node-access': 'error',
'testing-library/render-result-naming-convention': 'error',
};

const reactRules = {
...domRules,
'testing-library/no-container': 'error',
'testing-library/no-debug': 'error',
'testing-library/no-dom-import': ['error', 'react'],
'testing-library/no-node-access': 'error',
'testing-library/render-result-naming-convention': 'error',
};

const vueRules = {
...domRules,
'testing-library/await-fire-event': 'error',
'testing-library/no-container': 'error',
'testing-library/no-debug': 'error',
'testing-library/no-dom-import': ['error', 'vue'],
'testing-library/no-node-access': 'error',
'testing-library/render-result-naming-convention': 'error',
};

export = {
configs,
rules,
configs: {
dom: {
plugins: ['testing-library'],
rules: domRules,
},
angular: {
plugins: ['testing-library'],
rules: angularRules,
},
react: {
plugins: ['testing-library'],
rules: reactRules,
},
vue: {
plugins: ['testing-library'],
rules: vueRules,
},
},
};
7 changes: 6 additions & 1 deletion lib/rules/await-async-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Enforce promises from async queries to be handled',
category: 'Best Practices',
recommended: 'warn',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
},
},
messages: {
awaitAsyncQuery: 'promise returned from {{ name }} query must be handled',
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/await-async-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Enforce promises from async utils to be handled',
category: 'Best Practices',
recommended: 'warn',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
},
},
messages: {
awaitAsyncUtil: 'Promise returned from `{{ name }}` must be handled',
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/await-fire-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Enforce promises from fire event methods to be handled',
category: 'Best Practices',
recommended: false,
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: 'error',
},
},
messages: {
awaitFireEvent:
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/consistent-data-testid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Ensures consistent usage of `data-testid`',
category: 'Best Practices',
recommended: false,
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
},
},
messages: {
consistentDataTestId: '`{{attr}}` "{{value}}" should match `{{regex}}`',
Expand Down
17 changes: 7 additions & 10 deletions lib/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import { join, parse } from 'path';

import { TSESLint } from '@typescript-eslint/experimental-utils';

type RuleModule = TSESLint.RuleModule<string, unknown[]>;
import { importDefault, TestingLibraryRuleMeta } from '../utils';

// Copied from https://github.com/babel/babel/blob/b35c78f08dd854b08575fc66ebca323fdbc59dab/packages/babel-helpers/src/helpers.js#L615-L619
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const interopRequireDefault = (obj: any): { default: unknown } =>
obj?.__esModule ? obj : { default: obj };

const importDefault = (moduleName: string) =>
// eslint-disable-next-line @typescript-eslint/no-var-requires
interopRequireDefault(require(moduleName)).default;
type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
meta: TestingLibraryRuleMeta<string, unknown[]> & {
recommended: false;
};
};

const rulesDir = __dirname;
const excludedFiles = ['index'];
Expand All @@ -23,7 +20,7 @@ export default readdirSync(rulesDir)
.reduce<Record<string, RuleModule>>(
(allRules, ruleName) => ({
...allRules,
[ruleName]: importDefault(join(rulesDir, ruleName)) as RuleModule,
[ruleName]: importDefault<RuleModule>(join(rulesDir, ruleName)),
}),
{}
);
7 changes: 6 additions & 1 deletion lib/rules/no-await-sync-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Disallow unnecessary `await` for sync events',
category: 'Best Practices',
recommended: 'error',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
},
},
messages: {
noAwaitSyncEvents:
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/no-await-sync-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Disallow unnecessary `await` for sync queries',
category: 'Best Practices',
recommended: 'error',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
},
},
messages: {
noAwaitSyncQuery:
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/no-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Disallow the use of container methods',
category: 'Best Practices',
recommended: 'error',
recommendedConfig: {
dom: false,
angular: 'error',
react: 'error',
vue: 'error',
},
},
messages: {
noContainer:
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/no-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Disallow unnecessary debug usages in the tests',
category: 'Best Practices',
recommended: 'warn',
recommendedConfig: {
dom: false,
angular: 'error',
react: 'error',
vue: 'error',
},
},
messages: {
noDebug: 'Unexpected debug statement',
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/no-dom-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Disallow importing from DOM Testing Library',
category: 'Best Practices',
recommended: false,
recommendedConfig: {
dom: false,
angular: ['error', 'angular'],
react: ['error', 'react'],
vue: ['error', 'vue'],
},
},
messages: {
noDomImport:
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/no-manual-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
docs: {
description: 'Disallow the use of `cleanup`',
category: 'Best Practices',
recommended: false,
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
},
},
messages: {
noManualCleanup:
Expand Down
Loading