Skip to content

Commit

Permalink
Update all packages to use createConfig (#375)
Browse files Browse the repository at this point in the history
* Update all packages to use `createConfig`

* Update main config to use `createConfig`

* Update all READMEs
  • Loading branch information
Mrtenz authored Sep 26, 2024
1 parent 3a4c482 commit 33a5fbc
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 391 deletions.
8 changes: 3 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// @ts-check

import base from '@metamask/eslint-config';
import base, { createConfig } from '@metamask/eslint-config';
import nodejs from '@metamask/eslint-config-nodejs';
import typescript from '@metamask/eslint-config-typescript';
import vitest from '@metamask/eslint-config-vitest';
// eslint-disable-next-line import-x/no-unresolved
import tseslint from 'typescript-eslint';

const config = tseslint.config(
const config = createConfig([
{
ignores: ['.yarn/'],
},
Expand Down Expand Up @@ -58,6 +56,6 @@ const config = tseslint.config(
'n/no-unpublished-require': 'off',
},
},
);
]);

export default config;
20 changes: 10 additions & 10 deletions packages/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
order thereafter.

```js
import base from '@metamask/eslint-config';

const config = {
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
...base,
import base, { createConfig } from '@metamask/eslint-config';

const config = createConfig({
{
// Your overrides here.
extends: [
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
base,
],
}
};
});
```
60 changes: 59 additions & 1 deletion packages/base/src/index.d.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
declare module '@metamask/eslint-config' {
import type { Linter } from 'eslint';

const config: Linter.Config[];
/**
* An ESLint configuration object.
*/
type Config = Linter.Config;

/**
* An ESLint configuration object that may extend other configurations. This
* can only be used with the {@link createConfig} function.
*/
type ConfigWithExtends = Config & {
extends?: Config | Config[] | Config[][];
};

/**
* Create a config object that extends other configs.
*
* ESLint 9 removed support for extending arrays of configs, so this function
* provides a workaround. It takes an array of config objects, where each object
* may have an `extends` property that is an array of other config objects.
*
* This function is inspired by the `config` function in the `typescript-eslint`
* package, but to avoid a dependency on that package, this function is
* implemented here.
*
* @param configs - An array of config objects.
* @returns An array of config objects with all `extends` properties
* resolved.
* @example Basic usage.
* import { createConfig } from '@metamask/eslint-config';
* import typescript from '@metamask/eslint-config-typescript';
*
* const configs = createConfig([
* {
* files: ['**\/*.ts'],
* extends: typescript,
* },
* ]);
*
* export default configs;
*
* @example Multiple extends are supported as well.
* import { createConfig } from '@metamask/eslint-config';
* import typescript from '@metamask/eslint-config-typescript';
* import nodejs from '@metamask/eslint-config-nodejs';
*
* const configs = createConfig([
* {
* files: ['**\/*.ts'],
* extends: [typescript, nodejs],
* },
* ]);
*
* export default configs;
*/
export function createConfig(
configs: ConfigWithExtends | ConfigWithExtends[],
): Config[];

const config: Config[];
export default config;
}
21 changes: 11 additions & 10 deletions packages/browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
order thereafter.

```js
import base from '@metamask/eslint-config';
import base, { createConfig } from '@metamask/eslint-config';
import browser from '@metamask/eslint-config-browser';

const config = {
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
...base,
...browser,
const config = createConfig({
{
// Your overrides here.
extends: [
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
base,
browser,
],
}
};
});
```
34 changes: 10 additions & 24 deletions packages/browser/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createConfig } from '@metamask/eslint-config';
import globals from 'globals';
import { createRequire } from 'module';

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

files: [
'**/*.js',
'**/*.jsx',
'**/*.mjs',
'**/*.cjs',
'**/*.ts',
'**/*.tsx',
'**/*.mts',
'**/*.cts',
'**/*.mtsx',
'**/*.ctsx',
],

languageOptions: {
globals: {
...globals.browser,
},
languageOptions: {
globals: {
...globals.browser,
},
},

rules: {
...environmentRules,
},
rules: {
...environmentRules,
},
];
});

export default config;
22 changes: 11 additions & 11 deletions packages/commonjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
order thereafter.

```js
import base from '@metamask/eslint-config';
import base, { createConfig } from '@metamask/eslint-config';
import commonjs from '@metamask/eslint-config-commonjs';

const config = {
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
...base,
...commonjs,

const config = createConfig({
{
// Your overrides here.
extends: [
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
base,
commonjs,
],
}
};
});
```
34 changes: 10 additions & 24 deletions packages/commonjs/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createConfig } from '@metamask/eslint-config';
import globals from 'globals';
import { createRequire } from 'module';

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

files: [
'**/*.js',
'**/*.jsx',
'**/*.mjs',
'**/*.cjs',
'**/*.ts',
'**/*.tsx',
'**/*.mts',
'**/*.cts',
'**/*.mtsx',
'**/*.ctsx',
],

languageOptions: {
globals: {
...globals.commonjs,
},
languageOptions: {
globals: {
...globals.commonjs,
},
},

rules: {
...environmentRules,
},
rules: {
...environmentRules,
},
];
});

export default config;
24 changes: 12 additions & 12 deletions packages/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
order thereafter.

```js
import base from '@metamask/eslint-config';
import commonjs from '@metamask/eslint-config-commonjs';

const config = {
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
...base,
...commonjs,
import base, { createConfig } from '@metamask/eslint-config';
import jest from '@metamask/eslint-config-jest';

const config = createConfig({
{
// Your overrides here.
extends: [
// Any custom shared config should be added here.
// ...

// This should be added last unless you know what you're doing.
base,
jest,
],
}
};
});
```
Loading

0 comments on commit 33a5fbc

Please sign in to comment.