Skip to content

Commit f60b8d7

Browse files
committed
feat: add should-be-fine support for flat configs
1 parent 62e2c79 commit f60b8d7

File tree

5 files changed

+114
-10
lines changed

5 files changed

+114
-10
lines changed

.eslint-doc-generatorrc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ const { prettier: prettierRC } = require('./package.json');
33

44
/** @type {import('eslint-doc-generator').GenerateOptions} */
55
const config = {
6-
ignoreConfig: ['all'],
6+
ignoreConfig: [
7+
'all',
8+
'flat/all',
9+
'flat/recommended',
10+
'flat/style',
11+
'flat/snapshots',
12+
],
713
ruleDocTitleFormat: 'desc-parens-name',
814
ruleDocSectionInclude: ['Rule details'],
915
ruleListColumns: [

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ $ yarn add --dev eslint eslint-plugin-jest-extended
2222

2323
## Usage
2424

25+
> [!NOTE]
26+
>
27+
> `eslint.config.js` is supported, though most of the plugin documentation still
28+
> currently uses `.eslintrc` syntax.
29+
>
30+
> Refer to the
31+
> [ESLint documentation on the new configuration file format](https://eslint.org/docs/latest/use/configure/configuration-files-new)
32+
> for more.
33+
2534
Add `jest-extended` to the plugins section of your `.eslintrc` configuration
2635
file. You can omit the `eslint-plugin-` prefix:
2736

@@ -61,6 +70,22 @@ If you want to enable all rules instead of only some you can do so by adding the
6170
}
6271
```
6372

73+
To enable this configuration with `eslint.config.js`, use
74+
`jestExtended.configs['flat/all']`:
75+
76+
```js
77+
const jestExtended = require('eslint-plugin-jest-extended');
78+
79+
module.exports = [
80+
{
81+
files: [
82+
/* glob matching your test files */
83+
],
84+
...jestExtended.configs['flat/all'],
85+
},
86+
];
87+
```
88+
6489
Note that the `all` configuration may change in any release and is thus unsuited
6590
for installations requiring long-term consistency.
6691

src/__tests__/__snapshots__/rules.test.ts.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,34 @@ exports[`rules should export configs that refer to actual rules 1`] = `
1414
"jest-extended/prefer-to-have-been-called-once": "error",
1515
},
1616
},
17+
"flat/all": {
18+
"plugins": {
19+
"jest-extended": ObjectContaining {
20+
"meta": {
21+
"name": "eslint-plugin-jest-extended",
22+
"version": Any<String>,
23+
},
24+
},
25+
},
26+
"rules": {
27+
"jest-extended/prefer-to-be-array": "error",
28+
"jest-extended/prefer-to-be-false": "error",
29+
"jest-extended/prefer-to-be-object": "error",
30+
"jest-extended/prefer-to-be-true": "error",
31+
"jest-extended/prefer-to-have-been-called-once": "error",
32+
},
33+
},
34+
"flat/recommended": {
35+
"plugins": {
36+
"jest-extended": ObjectContaining {
37+
"meta": {
38+
"name": "eslint-plugin-jest-extended",
39+
"version": Any<String>,
40+
},
41+
},
42+
},
43+
"rules": {},
44+
},
1745
"recommended": {
1846
"plugins": [
1947
"jest-extended",

src/__tests__/rules.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,37 @@ describe('rules', () => {
2929
});
3030

3131
it('should export configs that refer to actual rules', () => {
32+
const expectJestExtendedPlugin = expect.objectContaining({
33+
meta: {
34+
name: 'eslint-plugin-jest-extended',
35+
version: expect.any(String),
36+
},
37+
});
38+
3239
const recommendedConfigs = plugin.configs;
3340

34-
expect(recommendedConfigs).toMatchSnapshot();
35-
expect(Object.keys(recommendedConfigs)).toEqual(['all', 'recommended']);
41+
expect(recommendedConfigs).toMatchSnapshot({
42+
'flat/recommended': {
43+
plugins: { 'jest-extended': expectJestExtendedPlugin },
44+
},
45+
'flat/all': {
46+
plugins: { 'jest-extended': expectJestExtendedPlugin },
47+
},
48+
});
49+
expect(Object.keys(recommendedConfigs)).toEqual([
50+
'all',
51+
'recommended',
52+
'flat/all',
53+
'flat/recommended',
54+
]);
3655
expect(Object.keys(recommendedConfigs.all.rules)).toHaveLength(
3756
ruleNames.length,
3857
);
58+
expect(Object.keys(recommendedConfigs['flat/all'].rules)).toHaveLength(
59+
ruleNames.length,
60+
);
3961
const allConfigRules = Object.values(recommendedConfigs)
40-
.map(config => Object.keys(config.rules))
62+
.map(config => Object.keys(config.rules ?? {}))
4163
.reduce((previousValue, currentValue) => [
4264
...previousValue,
4365
...currentValue,

src/index.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { readdirSync } from 'fs';
22
import { join, parse } from 'path';
33
import { TSESLint, TSESTree } from '@typescript-eslint/utils';
4+
import {
5+
name as packageName,
6+
version as packageVersion,
7+
} from '../package.json';
48

59
type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
610
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
@@ -68,15 +72,34 @@ const allRules = Object.entries(rules)
6872
{},
6973
);
7074

71-
const createConfig = (rules: Record<string, TSESLint.Linter.RuleLevel>) => ({
75+
const plugin = {
76+
meta: { name: packageName, version: packageVersion },
77+
// ugly cast for now to keep TypeScript happy since
78+
// we don't have types for flat config yet
79+
configs: {} as Record<
80+
'all' | 'recommended' | 'flat/all' | 'flat/recommended',
81+
Pick<Required<TSESLint.Linter.Config>, 'rules'>
82+
>,
83+
rules,
84+
};
85+
86+
const createRCConfig = (rules: Record<string, TSESLint.Linter.RuleLevel>) => ({
7287
plugins: ['jest-extended'],
7388
rules,
7489
});
7590

76-
export = {
77-
configs: {
78-
all: createConfig(allRules),
79-
recommended: createConfig(recommendedRules),
80-
},
91+
const createFlatConfig = (
92+
rules: Record<string, TSESLint.Linter.RuleLevel>,
93+
) => ({
94+
plugins: { 'jest-extended': plugin },
8195
rules,
96+
});
97+
98+
plugin.configs = {
99+
all: createRCConfig(allRules),
100+
recommended: createRCConfig(recommendedRules),
101+
'flat/all': createFlatConfig(allRules),
102+
'flat/recommended': createFlatConfig(recommendedRules),
82103
};
104+
105+
export = plugin;

0 commit comments

Comments
 (0)