Skip to content

Commit 33a5fbc

Browse files
authored
Update all packages to use createConfig (#375)
* Update all packages to use `createConfig` * Update main config to use `createConfig` * Update all READMEs
1 parent 3a4c482 commit 33a5fbc

File tree

17 files changed

+332
-391
lines changed

17 files changed

+332
-391
lines changed

eslint.config.mjs

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

3-
import base from '@metamask/eslint-config';
3+
import base, { createConfig } from '@metamask/eslint-config';
44
import nodejs from '@metamask/eslint-config-nodejs';
55
import typescript from '@metamask/eslint-config-typescript';
66
import vitest from '@metamask/eslint-config-vitest';
7-
// eslint-disable-next-line import-x/no-unresolved
8-
import tseslint from 'typescript-eslint';
97

10-
const config = tseslint.config(
8+
const config = createConfig([
119
{
1210
ignores: ['.yarn/'],
1311
},
@@ -58,6 +56,6 @@ const config = tseslint.config(
5856
'n/no-unpublished-require': 'off',
5957
},
6058
},
61-
);
59+
]);
6260

6361
export default config;

packages/base/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
2424
order thereafter.
2525

2626
```js
27-
import base from '@metamask/eslint-config';
28-
29-
const config = {
30-
// Any custom shared config should be added here.
31-
// ...
32-
33-
// This should be added last unless you know what you're doing.
34-
...base,
27+
import base, { createConfig } from '@metamask/eslint-config';
3528

29+
const config = createConfig({
3630
{
37-
// Your overrides here.
31+
extends: [
32+
// Any custom shared config should be added here.
33+
// ...
34+
35+
// This should be added last unless you know what you're doing.
36+
base,
37+
],
3838
}
39-
};
39+
});
4040
```

packages/base/src/index.d.mts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,64 @@
11
declare module '@metamask/eslint-config' {
22
import type { Linter } from 'eslint';
33

4-
const config: Linter.Config[];
4+
/**
5+
* An ESLint configuration object.
6+
*/
7+
type Config = Linter.Config;
8+
9+
/**
10+
* An ESLint configuration object that may extend other configurations. This
11+
* can only be used with the {@link createConfig} function.
12+
*/
13+
type ConfigWithExtends = Config & {
14+
extends?: Config | Config[] | Config[][];
15+
};
16+
17+
/**
18+
* Create a config object that extends other configs.
19+
*
20+
* ESLint 9 removed support for extending arrays of configs, so this function
21+
* provides a workaround. It takes an array of config objects, where each object
22+
* may have an `extends` property that is an array of other config objects.
23+
*
24+
* This function is inspired by the `config` function in the `typescript-eslint`
25+
* package, but to avoid a dependency on that package, this function is
26+
* implemented here.
27+
*
28+
* @param configs - An array of config objects.
29+
* @returns An array of config objects with all `extends` properties
30+
* resolved.
31+
* @example Basic usage.
32+
* import { createConfig } from '@metamask/eslint-config';
33+
* import typescript from '@metamask/eslint-config-typescript';
34+
*
35+
* const configs = createConfig([
36+
* {
37+
* files: ['**\/*.ts'],
38+
* extends: typescript,
39+
* },
40+
* ]);
41+
*
42+
* export default configs;
43+
*
44+
* @example Multiple extends are supported as well.
45+
* import { createConfig } from '@metamask/eslint-config';
46+
* import typescript from '@metamask/eslint-config-typescript';
47+
* import nodejs from '@metamask/eslint-config-nodejs';
48+
*
49+
* const configs = createConfig([
50+
* {
51+
* files: ['**\/*.ts'],
52+
* extends: [typescript, nodejs],
53+
* },
54+
* ]);
55+
*
56+
* export default configs;
57+
*/
58+
export function createConfig(
59+
configs: ConfigWithExtends | ConfigWithExtends[],
60+
): Config[];
61+
62+
const config: Config[];
563
export default config;
664
}

packages/browser/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
2525
order thereafter.
2626

2727
```js
28-
import base from '@metamask/eslint-config';
28+
import base, { createConfig } from '@metamask/eslint-config';
2929
import browser from '@metamask/eslint-config-browser';
3030

31-
const config = {
32-
// Any custom shared config should be added here.
33-
// ...
34-
35-
// This should be added last unless you know what you're doing.
36-
...base,
37-
...browser,
31+
const config = createConfig({
3832
{
39-
// Your overrides here.
33+
extends: [
34+
// Any custom shared config should be added here.
35+
// ...
36+
37+
// This should be added last unless you know what you're doing.
38+
base,
39+
browser,
40+
],
4041
}
41-
};
42+
});
4243
```

packages/browser/src/index.mjs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createConfig } from '@metamask/eslint-config';
12
import globals from 'globals';
23
import { createRequire } from 'module';
34

@@ -8,33 +9,18 @@ const environmentRules = customRequire('./environment.json');
89
/**
910
* @type {import('eslint').Linter.Config[]}
1011
*/
11-
const config = [
12-
{
13-
name: '@metamask/eslint-config-browser',
12+
const config = createConfig({
13+
name: '@metamask/eslint-config-browser',
1414

15-
files: [
16-
'**/*.js',
17-
'**/*.jsx',
18-
'**/*.mjs',
19-
'**/*.cjs',
20-
'**/*.ts',
21-
'**/*.tsx',
22-
'**/*.mts',
23-
'**/*.cts',
24-
'**/*.mtsx',
25-
'**/*.ctsx',
26-
],
27-
28-
languageOptions: {
29-
globals: {
30-
...globals.browser,
31-
},
15+
languageOptions: {
16+
globals: {
17+
...globals.browser,
3218
},
19+
},
3320

34-
rules: {
35-
...environmentRules,
36-
},
21+
rules: {
22+
...environmentRules,
3723
},
38-
];
24+
});
3925

4026
export default config;

packages/commonjs/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
2323
order thereafter.
2424

2525
```js
26-
import base from '@metamask/eslint-config';
26+
import base, { createConfig } from '@metamask/eslint-config';
2727
import commonjs from '@metamask/eslint-config-commonjs';
2828

29-
const config = {
30-
// Any custom shared config should be added here.
31-
// ...
32-
33-
// This should be added last unless you know what you're doing.
34-
...base,
35-
...commonjs,
36-
29+
const config = createConfig({
3730
{
38-
// Your overrides here.
31+
extends: [
32+
// Any custom shared config should be added here.
33+
// ...
34+
35+
// This should be added last unless you know what you're doing.
36+
base,
37+
commonjs,
38+
],
3939
}
40-
};
40+
});
4141
```

packages/commonjs/src/index.mjs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createConfig } from '@metamask/eslint-config';
12
import globals from 'globals';
23
import { createRequire } from 'module';
34

@@ -8,33 +9,18 @@ const environmentRules = customRequire('./environment.json');
89
/**
910
* @type {import('eslint').Linter.Config[]}
1011
*/
11-
const config = [
12-
{
13-
name: '@metamask/eslint-config-commonjs',
12+
const config = createConfig({
13+
name: '@metamask/eslint-config-commonjs',
1414

15-
files: [
16-
'**/*.js',
17-
'**/*.jsx',
18-
'**/*.mjs',
19-
'**/*.cjs',
20-
'**/*.ts',
21-
'**/*.tsx',
22-
'**/*.mts',
23-
'**/*.cts',
24-
'**/*.mtsx',
25-
'**/*.ctsx',
26-
],
27-
28-
languageOptions: {
29-
globals: {
30-
...globals.commonjs,
31-
},
15+
languageOptions: {
16+
globals: {
17+
...globals.commonjs,
3218
},
19+
},
3320

34-
rules: {
35-
...environmentRules,
36-
},
21+
rules: {
22+
...environmentRules,
3723
},
38-
];
24+
});
3925

4026
export default config;

packages/jest/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
2424
order thereafter.
2525

2626
```js
27-
import base from '@metamask/eslint-config';
28-
import commonjs from '@metamask/eslint-config-commonjs';
29-
30-
const config = {
31-
// Any custom shared config should be added here.
32-
// ...
33-
34-
// This should be added last unless you know what you're doing.
35-
...base,
36-
...commonjs,
27+
import base, { createConfig } from '@metamask/eslint-config';
28+
import jest from '@metamask/eslint-config-jest';
3729

30+
const config = createConfig({
3831
{
39-
// Your overrides here.
32+
extends: [
33+
// Any custom shared config should be added here.
34+
// ...
35+
36+
// This should be added last unless you know what you're doing.
37+
base,
38+
jest,
39+
],
4040
}
41-
};
41+
});
4242
```

0 commit comments

Comments
 (0)