Skip to content

Commit 610b3b9

Browse files
refactor: automatically generate all shareable configs (#358)
* refactor: extract configs into their own files * chore: add generate-configs script * fix: make rules comply with TestingLibraryRuleMeta * chore: automatically generate all configs * refactor: use recommendedConfig instead of recommended in meta docs * fix: add prefix in front of rule names in configs * test: fix createTestingLibraryRule test * chore: resolve remarks * chore: reference running scripts via npm instead of yarn * chore: don't warn on unused variables beginning with _ * docs(CONTRIBUTING): add some explanation about recommendedConfig
1 parent 42d84c9 commit 610b3b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+407
-107
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"rules": {
2525
"no-var": "error",
26-
"@typescript-eslint/explicit-function-return-type": "off"
26+
"@typescript-eslint/explicit-function-return-type": "off",
27+
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }]
2728
}
2829
}

CONTRIBUTING.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ each rule has three files named with its identifier (e.g. `no-debug`):
6363
6464
Additionally, you need to do a couple of extra things:
6565
66-
- Import the new rule in `lib/index.ts` and include it
67-
in `rules` constant (there is a test which will make sure you did
68-
this). Remember to include your rule under corresponding `config` if necessary
69-
(a snapshot test will check this too, but you can update it just running
70-
`npm run test:update`).
7166
- Include your rule in the "Supported Rules" table within the [README.md](./README.md).
7267
Don't forget to include the proper badges if needed and to sort alphabetically the rules for readability.
7368
@@ -105,6 +100,8 @@ If you need some check related to Testing Library which is not available in any
105100
- pass it through `helpers`
106101
- write some generic test within `fake-rule.ts`, which is a dumb rule to be able to test all enhanced behavior from our custom Rule Creator.
107102
103+
Take also into account that we're using our own `recommendedConfig` meta instead of the default `recommended` one. This is done so that our tools can automatically generate (`npm run generate:configs`) our configs.
104+
108105
## Updating existing rules
109106
110107
A couple of things you need to remember when editing already existing rules:

lib/configs/angular.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// THIS CODE WAS AUTOMATICALLY GENERATED
2+
// DO NOT EDIT THIS CODE BY HAND
3+
// YOU CAN REGENERATE IT USING npm run generate:configs
4+
5+
export = {
6+
plugins: ['testing-library'],
7+
rules: {
8+
'testing-library/await-async-query': 'error',
9+
'testing-library/await-async-utils': 'error',
10+
'testing-library/no-await-sync-query': 'error',
11+
'testing-library/no-container': 'error',
12+
'testing-library/no-debug': 'error',
13+
'testing-library/no-dom-import': ['error', 'angular'],
14+
'testing-library/no-node-access': 'error',
15+
'testing-library/no-promise-in-fire-event': 'error',
16+
'testing-library/no-wait-for-empty-callback': 'error',
17+
'testing-library/prefer-find-by': 'error',
18+
'testing-library/prefer-screen-queries': 'error',
19+
'testing-library/render-result-naming-convention': 'error',
20+
},
21+
};

lib/configs/dom.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// THIS CODE WAS AUTOMATICALLY GENERATED
2+
// DO NOT EDIT THIS CODE BY HAND
3+
// YOU CAN REGENERATE IT USING npm run generate:configs
4+
5+
export = {
6+
plugins: ['testing-library'],
7+
rules: {
8+
'testing-library/await-async-query': 'error',
9+
'testing-library/await-async-utils': 'error',
10+
'testing-library/no-await-sync-query': 'error',
11+
'testing-library/no-promise-in-fire-event': 'error',
12+
'testing-library/no-wait-for-empty-callback': 'error',
13+
'testing-library/prefer-find-by': 'error',
14+
'testing-library/prefer-screen-queries': 'error',
15+
},
16+
};

lib/configs/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { join } from 'path';
2+
3+
import type { TSESLint } from '@typescript-eslint/experimental-utils';
4+
5+
import {
6+
importDefault,
7+
SUPPORTED_TESTING_FRAMEWORKS,
8+
SupportedTestingFramework,
9+
} from '../utils';
10+
11+
export type LinterConfigRules = Record<string, TSESLint.Linter.RuleEntry>;
12+
13+
const configsDir = __dirname;
14+
15+
const getConfigForFramework = (framework: SupportedTestingFramework) =>
16+
importDefault<LinterConfigRules>(join(configsDir, framework));
17+
18+
export default SUPPORTED_TESTING_FRAMEWORKS.reduce(
19+
(allConfigs, framework) => ({
20+
...allConfigs,
21+
[framework]: getConfigForFramework(framework),
22+
}),
23+
{}
24+
) as Record<SupportedTestingFramework, LinterConfigRules>;

lib/configs/react.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// THIS CODE WAS AUTOMATICALLY GENERATED
2+
// DO NOT EDIT THIS CODE BY HAND
3+
// YOU CAN REGENERATE IT USING npm run generate:configs
4+
5+
export = {
6+
plugins: ['testing-library'],
7+
rules: {
8+
'testing-library/await-async-query': 'error',
9+
'testing-library/await-async-utils': 'error',
10+
'testing-library/no-await-sync-query': 'error',
11+
'testing-library/no-container': 'error',
12+
'testing-library/no-debug': 'error',
13+
'testing-library/no-dom-import': ['error', 'react'],
14+
'testing-library/no-node-access': 'error',
15+
'testing-library/no-promise-in-fire-event': 'error',
16+
'testing-library/no-wait-for-empty-callback': 'error',
17+
'testing-library/prefer-find-by': 'error',
18+
'testing-library/prefer-screen-queries': 'error',
19+
'testing-library/render-result-naming-convention': 'error',
20+
},
21+
};

lib/configs/vue.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// THIS CODE WAS AUTOMATICALLY GENERATED
2+
// DO NOT EDIT THIS CODE BY HAND
3+
// YOU CAN REGENERATE IT USING npm run generate:configs
4+
5+
export = {
6+
plugins: ['testing-library'],
7+
rules: {
8+
'testing-library/await-async-query': 'error',
9+
'testing-library/await-async-utils': 'error',
10+
'testing-library/await-fire-event': 'error',
11+
'testing-library/no-await-sync-query': 'error',
12+
'testing-library/no-container': 'error',
13+
'testing-library/no-debug': 'error',
14+
'testing-library/no-dom-import': ['error', 'vue'],
15+
'testing-library/no-node-access': 'error',
16+
'testing-library/no-promise-in-fire-event': 'error',
17+
'testing-library/no-wait-for-empty-callback': 'error',
18+
'testing-library/prefer-find-by': 'error',
19+
'testing-library/prefer-screen-queries': 'error',
20+
'testing-library/render-result-naming-convention': 'error',
21+
},
22+
};
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
import { ESLintUtils, TSESLint } from '@typescript-eslint/experimental-utils';
22

3-
import { getDocsUrl } from '../utils';
3+
import { getDocsUrl, TestingLibraryRuleMeta } from '../utils';
44

55
import {
66
DetectionOptions,
77
detectTestingLibraryUtils,
88
EnhancedRuleCreate,
99
} from './detect-testing-library-utils';
1010

11-
// These 2 types are copied from @typescript-eslint/experimental-utils
12-
type CreateRuleMetaDocs = Omit<TSESLint.RuleMetaDataDocs, 'url'>;
13-
type CreateRuleMeta<TMessageIds extends string> = {
14-
docs: CreateRuleMetaDocs;
15-
} & Omit<TSESLint.RuleMetaData<TMessageIds>, 'docs'>;
16-
1711
export function createTestingLibraryRule<
1812
TOptions extends readonly unknown[],
1913
TMessageIds extends string,
2014
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener
2115
>({
2216
create,
2317
detectionOptions = {},
18+
meta,
2419
...remainingConfig
2520
}: Readonly<{
2621
name: string;
27-
meta: CreateRuleMeta<TMessageIds>;
22+
meta: TestingLibraryRuleMeta<TMessageIds, TOptions>;
2823
defaultOptions: Readonly<TOptions>;
2924
detectionOptions?: Partial<DetectionOptions>;
3025
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
@@ -35,5 +30,14 @@ export function createTestingLibraryRule<
3530
create,
3631
detectionOptions
3732
),
33+
meta: {
34+
...meta,
35+
docs: {
36+
...meta.docs,
37+
// We're using our own recommendedConfig meta to tell our build tools
38+
// if the rule is recommended on a config basis
39+
recommended: false,
40+
},
41+
},
3842
});
3943
}

lib/index.ts

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,7 @@
1+
import configs from './configs';
12
import rules from './rules';
23

3-
const domRules = {
4-
'testing-library/await-async-query': 'error',
5-
'testing-library/await-async-utils': 'error',
6-
'testing-library/no-await-sync-query': 'error',
7-
'testing-library/no-promise-in-fire-event': 'error',
8-
'testing-library/no-wait-for-empty-callback': 'error',
9-
'testing-library/prefer-find-by': 'error',
10-
'testing-library/prefer-screen-queries': 'error',
11-
};
12-
13-
const angularRules = {
14-
...domRules,
15-
'testing-library/no-container': 'error',
16-
'testing-library/no-debug': 'error',
17-
'testing-library/no-dom-import': ['error', 'angular'],
18-
'testing-library/no-node-access': 'error',
19-
'testing-library/render-result-naming-convention': 'error',
20-
};
21-
22-
const reactRules = {
23-
...domRules,
24-
'testing-library/no-container': 'error',
25-
'testing-library/no-debug': 'error',
26-
'testing-library/no-dom-import': ['error', 'react'],
27-
'testing-library/no-node-access': 'error',
28-
'testing-library/render-result-naming-convention': 'error',
29-
};
30-
31-
const vueRules = {
32-
...domRules,
33-
'testing-library/await-fire-event': 'error',
34-
'testing-library/no-container': 'error',
35-
'testing-library/no-debug': 'error',
36-
'testing-library/no-dom-import': ['error', 'vue'],
37-
'testing-library/no-node-access': 'error',
38-
'testing-library/render-result-naming-convention': 'error',
39-
};
40-
414
export = {
5+
configs,
426
rules,
43-
configs: {
44-
dom: {
45-
plugins: ['testing-library'],
46-
rules: domRules,
47-
},
48-
angular: {
49-
plugins: ['testing-library'],
50-
rules: angularRules,
51-
},
52-
react: {
53-
plugins: ['testing-library'],
54-
rules: reactRules,
55-
},
56-
vue: {
57-
plugins: ['testing-library'],
58-
rules: vueRules,
59-
},
60-
},
617
};

lib/rules/await-async-query.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
1919
docs: {
2020
description: 'Enforce promises from async queries to be handled',
2121
category: 'Best Practices',
22-
recommended: 'warn',
22+
recommendedConfig: {
23+
dom: 'error',
24+
angular: 'error',
25+
react: 'error',
26+
vue: 'error',
27+
},
2328
},
2429
messages: {
2530
awaitAsyncQuery: 'promise returned from {{ name }} query must be handled',

0 commit comments

Comments
 (0)