Skip to content

Commit 14d3298

Browse files
committed
refactor: replace custom config functions with getConfigAdapters utility
1 parent d6af21c commit 14d3298

File tree

10 files changed

+112
-96
lines changed

10 files changed

+112
-96
lines changed

packages/plugins/eslint-plugin-react-debug/src/index.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import type { CompatibleConfig } from "@eslint-react/kit";
1+
import { getConfigAdapters } from "@eslint-react/shared";
22

33
import * as allConfig from "./configs/all";
44
import { plugin } from "./plugin";
55

6-
function toFlatConfig(config: CompatibleConfig) {
7-
return {
8-
...config,
9-
plugins: {
10-
"react-debug": plugin,
11-
},
12-
};
13-
}
14-
15-
function toLegacyConfig({ rules }: CompatibleConfig) {
16-
return {
17-
plugins: ["react-debug"],
18-
rules,
19-
};
20-
}
6+
const { toFlatConfig, toLegacyConfig } = getConfigAdapters("react-debug", plugin);
217

228
export default {
239
...plugin,

packages/plugins/eslint-plugin-react-dom/src/index.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import type { CompatibleConfig } from "@eslint-react/kit";
1+
import { getConfigAdapters } from "@eslint-react/shared";
22

33
import * as recommendedConfig from "./configs/recommended";
44
import { plugin } from "./plugin";
55

6-
function toFlatConfig(config: CompatibleConfig) {
7-
return {
8-
...config,
9-
plugins: {
10-
"react-dom": plugin,
11-
},
12-
};
13-
}
14-
15-
function toLegacyConfig({ rules }: CompatibleConfig) {
16-
return {
17-
plugins: ["react-dom"],
18-
rules,
19-
};
20-
}
6+
const { toFlatConfig, toLegacyConfig } = getConfigAdapters("react-dom", plugin);
217

228
export default {
239
...plugin,

packages/plugins/eslint-plugin-react-hooks-extra/src/index.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import type { CompatibleConfig } from "@eslint-react/kit";
1+
import { getConfigAdapters } from "@eslint-react/shared";
22

33
import * as recommendedConfig from "./configs/recommended";
44
import { plugin } from "./plugin";
55

6-
function toFlatConfig(config: CompatibleConfig) {
7-
return {
8-
...config,
9-
plugins: {
10-
"react-hooks-extra": plugin,
11-
},
12-
};
13-
}
14-
15-
function toLegacyConfig({ rules }: CompatibleConfig) {
16-
return {
17-
plugins: ["react-hooks-extra"],
18-
rules,
19-
};
20-
}
6+
const { toFlatConfig, toLegacyConfig } = getConfigAdapters("react-hooks-extra", plugin);
217

228
export default {
239
...plugin,

packages/plugins/eslint-plugin-react-naming-convention/src/index.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import type { CompatibleConfig } from "@eslint-react/kit";
1+
import { getConfigAdapters } from "@eslint-react/shared";
22

33
import * as recommendedConfig from "./configs/recommended";
44
import { plugin } from "./plugin";
55

6-
function toFlatConfig(config: CompatibleConfig) {
7-
return {
8-
...config,
9-
plugins: {
10-
"react-naming-convention": plugin,
11-
},
12-
};
13-
}
14-
15-
function toLegacyConfig({ rules }: CompatibleConfig) {
16-
return {
17-
plugins: ["react-naming-convention"],
18-
rules,
19-
};
20-
}
6+
const { toFlatConfig, toLegacyConfig } = getConfigAdapters("react-naming-convention", plugin);
217

228
export default {
239
...plugin,

packages/plugins/eslint-plugin-react-web-api/src/index.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import type { CompatibleConfig } from "@eslint-react/kit";
1+
import { getConfigAdapters } from "@eslint-react/shared";
22

33
import * as recommendedConfig from "./configs/recommended";
44
import { plugin } from "./plugin";
55

6-
function toFlatConfig(config: CompatibleConfig) {
7-
return {
8-
...config,
9-
plugins: {
10-
"react-web-api": plugin,
11-
},
12-
};
13-
}
14-
15-
function toLegacyConfig({ rules }: CompatibleConfig) {
16-
return {
17-
plugins: ["react-web-api"],
18-
rules,
19-
};
20-
}
6+
const { toFlatConfig, toLegacyConfig } = getConfigAdapters("react-web-api", plugin);
217

228
export default {
239
...plugin,

packages/plugins/eslint-plugin-react-x/src/index.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
import type { CompatibleConfig } from "@eslint-react/kit";
1+
import { getConfigAdapters } from "@eslint-react/shared";
22

33
import * as recommendedConfig from "./configs/recommended";
44
import * as recommendedTypeCheckedConfig from "./configs/recommended-type-checked";
55
import * as recommendedTypeScriptConfig from "./configs/recommended-typescript";
66
import { plugin } from "./plugin";
77

8-
function toFlatConfig(config: CompatibleConfig) {
9-
return {
10-
...config,
11-
plugins: {
12-
"react-x": plugin,
13-
},
14-
};
15-
}
16-
17-
function toLegacyConfig({ rules }: CompatibleConfig) {
18-
return {
19-
plugins: ["react-x"],
20-
rules,
21-
};
22-
}
8+
const { toFlatConfig, toLegacyConfig } = getConfigAdapters("react-x", plugin);
239

2410
export default {
2511
...plugin,

packages/shared/docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [coerceSettings](functions/coerceSettings.md)
3737
- [decodeESLintSettings](functions/decodeESLintSettings.md)
3838
- [decodeSettings](functions/decodeSettings.md)
39+
- [getConfigAdapters](functions/getConfigAdapters.md)
3940
- [getReactVersion](functions/getReactVersion.md)
4041
- [getSettingsFromContext](functions/getSettingsFromContext.md)
4142
- [isESLintReactSettings](functions/isESLintReactSettings.md)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[**@eslint-react/shared**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/shared](../README.md) / getConfigAdapters
6+
7+
# Function: getConfigAdapters()
8+
9+
> **getConfigAdapters**(`pluginName`, `plugin`): `object`
10+
11+
## Parameters
12+
13+
### pluginName
14+
15+
`string`
16+
17+
### plugin
18+
19+
`CompatiblePlugin`
20+
21+
## Returns
22+
23+
`object`
24+
25+
### toFlatConfig()
26+
27+
> **toFlatConfig**: (`config`) => `object`
28+
29+
#### Parameters
30+
31+
##### config
32+
33+
`CompatibleConfig`
34+
35+
#### Returns
36+
37+
`object`
38+
39+
##### name?
40+
41+
> `optional` **name**: `string`
42+
43+
##### plugins
44+
45+
> **plugins**: `object`
46+
47+
###### Index Signature
48+
49+
\[`key`: `string`\]: `CompatiblePlugin`
50+
51+
##### rules?
52+
53+
> `optional` **rules**: [`Record`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type)\<`string`, `RuleConfig`\<`unknown`[]\>\>
54+
55+
##### settings?
56+
57+
> `optional` **settings**: `SettingsConfig`
58+
59+
### toLegacyConfig()
60+
61+
> **toLegacyConfig**: (`__namedParameters`) => `object`
62+
63+
#### Parameters
64+
65+
##### \_\_namedParameters
66+
67+
`CompatibleConfig`
68+
69+
#### Returns
70+
71+
`object`
72+
73+
##### plugins
74+
75+
> **plugins**: `string`[]
76+
77+
##### rules
78+
79+
> **rules**: `undefined` \| [`Record`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type)\<`string`, `RuleConfig`\<`unknown`[]\>\>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { CompatibleConfig, CompatiblePlugin } from "@eslint-react/kit";
2+
3+
export function getConfigAdapters(pluginName: string, plugin: CompatiblePlugin) {
4+
function toFlatConfig(config: CompatibleConfig) {
5+
return {
6+
...config,
7+
plugins: {
8+
[pluginName]: plugin,
9+
},
10+
};
11+
}
12+
function toLegacyConfig({ rules }: CompatibleConfig) {
13+
return {
14+
plugins: [pluginName],
15+
rules,
16+
};
17+
}
18+
return { toFlatConfig, toLegacyConfig } as const;
19+
}

packages/shared/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from "./_id";
22
export * from "./_require";
33
export * from "./constants";
4+
export * from "./get-config-adapters";
45
export * from "./get-doc-url";
56
export * from "./get-react-version";
67
export * from "./settings";

0 commit comments

Comments
 (0)