Skip to content

Commit 9b81dde

Browse files
authored
fix: generate configs and rules statically (#1102)
It doesn't change anything for the end user, just simplifies the implementation.
1 parent 6e779f7 commit 9b81dde

22 files changed

+1447
-2013
lines changed

eslint.config.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @ts-check
22

33
import js from '@eslint/js';
4+
import vitest from '@vitest/eslint-plugin';
45
import { defineConfig, globalIgnores } from 'eslint/config';
56
import prettierConfig from 'eslint-config-prettier/flat';
67
import { importX } from 'eslint-plugin-import-x';
7-
import jest from 'eslint-plugin-jest';
88
import globals from 'globals';
99
import tseslint from 'typescript-eslint';
1010

@@ -88,12 +88,12 @@ const config = defineConfig(
8888
},
8989
},
9090
{
91-
name: 'Jest config',
91+
name: 'Vitest config',
9292
files: ['**/*.test.ts', '**/*.test.js'],
93-
...jest.configs['flat/recommended'],
93+
...vitest.configs.recommended,
9494
rules: {
95-
...jest.configs['flat/recommended'].rules,
96-
'jest/padding-around-all': 'error',
95+
...vitest.configs.recommended.rules,
96+
'vitest/padding-around-all': 'warn',
9797
},
9898
},
9999
{

jest.config.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/configs/angular.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// DO NOT EDIT THIS CODE BY HAND
33
// YOU CAN REGENERATE IT USING pnpm run generate:configs
44

5+
import type { Linter } from 'eslint';
6+
57
export = {
68
plugins: ['testing-library'],
79
rules: {
@@ -32,4 +34,4 @@ export = {
3234
'testing-library/prefer-screen-queries': 'error',
3335
'testing-library/render-result-naming-convention': 'error',
3436
},
35-
};
37+
} satisfies Linter.LegacyConfig;

lib/configs/dom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// DO NOT EDIT THIS CODE BY HAND
33
// YOU CAN REGENERATE IT USING pnpm run generate:configs
44

5+
import type { Linter } from 'eslint';
6+
57
export = {
68
plugins: ['testing-library'],
79
rules: {
@@ -27,4 +29,4 @@ export = {
2729
'testing-library/prefer-query-by-disappearance': 'error',
2830
'testing-library/prefer-screen-queries': 'error',
2931
},
30-
};
32+
} satisfies Linter.LegacyConfig;

lib/configs/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { join } from 'path';
2-
3-
import { importDefault, SUPPORTED_TESTING_FRAMEWORKS } from '../utils';
1+
import angular from './angular';
2+
import dom from './dom';
3+
import marko from './marko';
4+
import react from './react';
5+
import svelte from './svelte';
6+
import vue from './vue';
47

58
import type { SupportedTestingFramework } from '../utils';
6-
import type { TSESLint } from '@typescript-eslint/utils';
7-
8-
const configsDir = __dirname;
9+
import type { Linter } from 'eslint';
910

10-
const getConfigForFramework = (framework: SupportedTestingFramework) =>
11-
importDefault<TSESLint.SharedConfig.RulesRecord>(join(configsDir, framework));
11+
const legacyConfigs: Record<SupportedTestingFramework, Linter.LegacyConfig> = {
12+
dom,
13+
angular,
14+
react,
15+
vue,
16+
svelte,
17+
marko,
18+
};
1219

13-
export default SUPPORTED_TESTING_FRAMEWORKS.reduce(
14-
(allConfigs, framework) => ({
15-
...allConfigs,
16-
[framework]: getConfigForFramework(framework),
17-
}),
18-
{}
19-
) as Record<SupportedTestingFramework, TSESLint.SharedConfig.RulesRecord>;
20+
export { legacyConfigs };

lib/configs/marko.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// DO NOT EDIT THIS CODE BY HAND
33
// YOU CAN REGENERATE IT USING pnpm run generate:configs
44

5+
import type { Linter } from 'eslint';
6+
57
export = {
68
plugins: ['testing-library'],
79
rules: {
@@ -29,4 +31,4 @@ export = {
2931
'testing-library/prefer-screen-queries': 'error',
3032
'testing-library/render-result-naming-convention': 'error',
3133
},
32-
};
34+
} satisfies Linter.LegacyConfig;

lib/configs/react.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// DO NOT EDIT THIS CODE BY HAND
33
// YOU CAN REGENERATE IT USING pnpm run generate:configs
44

5+
import type { Linter } from 'eslint';
6+
57
export = {
68
plugins: ['testing-library'],
79
rules: {
@@ -34,4 +36,4 @@ export = {
3436
'testing-library/prefer-screen-queries': 'error',
3537
'testing-library/render-result-naming-convention': 'error',
3638
},
37-
};
39+
} satisfies Linter.LegacyConfig;

lib/configs/svelte.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// DO NOT EDIT THIS CODE BY HAND
33
// YOU CAN REGENERATE IT USING pnpm run generate:configs
44

5+
import type { Linter } from 'eslint';
6+
57
export = {
68
plugins: ['testing-library'],
79
rules: {
@@ -29,4 +31,4 @@ export = {
2931
'testing-library/prefer-screen-queries': 'error',
3032
'testing-library/render-result-naming-convention': 'error',
3133
},
32-
};
34+
} satisfies Linter.LegacyConfig;

lib/configs/vue.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// DO NOT EDIT THIS CODE BY HAND
33
// YOU CAN REGENERATE IT USING pnpm run generate:configs
44

5+
import type { Linter } from 'eslint';
6+
57
export = {
68
plugins: ['testing-library'],
79
rules: {
@@ -29,4 +31,4 @@ export = {
2931
'testing-library/prefer-screen-queries': 'error',
3032
'testing-library/render-result-naming-convention': 'error',
3133
},
32-
};
34+
} satisfies Linter.LegacyConfig;

lib/index.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
1-
import configs from './configs';
1+
import { legacyConfigs } from './configs';
22
import rules from './rules';
33

44
import type { SupportedTestingFramework } from './utils';
55
import type { TSESLint } from '@typescript-eslint/utils';
66

7-
// we can't natively import package.json as tsc will copy it into dist/
87
const {
98
name: packageName,
109
version: packageVersion,
10+
// we can't natively import package.json as tsc will copy it into dist/
1111
// eslint-disable-next-line @typescript-eslint/no-require-imports
1212
} = require('../package.json') as { name: string; version: string };
1313

14+
type FinalConfigs = Record<
15+
SupportedTestingFramework | `flat/${SupportedTestingFramework}`,
16+
TSESLint.ClassicConfig.Config | TSESLint.FlatConfig.Config
17+
>;
18+
1419
const plugin = {
1520
meta: {
1621
name: packageName,
1722
version: packageVersion,
1823
},
19-
// ugly cast for now to keep TypeScript happy since
20-
// we don't have types for flat config yet
21-
configs: {} as Record<
22-
SupportedTestingFramework | `flat/${SupportedTestingFramework}`,
23-
TSESLint.SharedConfig.RulesRecord
24-
>,
24+
configs: {} as FinalConfigs,
2525
rules,
2626
};
2727

2828
plugin.configs = {
29-
...configs,
30-
...(Object.fromEntries(
31-
Object.entries(configs).map(([framework, config]) => [
32-
`flat/${framework}`,
33-
{
34-
plugins: { 'testing-library': plugin },
35-
rules: config.rules,
36-
},
37-
])
38-
) as unknown as Record<
39-
`flat/${SupportedTestingFramework}`,
40-
TSESLint.SharedConfig.RulesRecord & { plugins: unknown }
41-
>),
42-
};
29+
...legacyConfigs,
30+
'flat/dom': {
31+
plugins: { 'testing-library': plugin },
32+
rules: legacyConfigs.dom.rules,
33+
},
34+
'flat/angular': {
35+
plugins: { 'testing-library': plugin },
36+
rules: legacyConfigs.angular.rules,
37+
},
38+
'flat/react': {
39+
plugins: { 'testing-library': plugin },
40+
rules: legacyConfigs.react.rules,
41+
},
42+
'flat/vue': {
43+
plugins: { 'testing-library': plugin },
44+
rules: legacyConfigs.vue.rules,
45+
},
46+
'flat/svelte': {
47+
plugins: { 'testing-library': plugin },
48+
rules: legacyConfigs.svelte.rules,
49+
},
50+
'flat/marko': {
51+
plugins: { 'testing-library': plugin },
52+
rules: legacyConfigs.marko.rules,
53+
},
54+
} satisfies FinalConfigs;
4355

4456
export = plugin;

0 commit comments

Comments
 (0)