Skip to content

Commit 43082b7

Browse files
committed
feat: add flat config support
This change adds support for ESLint's new Flat config system. It maintains backwards compatibility with eslintrc style configs as well. To achieve this, we're now dynamically creating flat configs on a new `flatConfigs` export. I was a bit on the fence about using this convention, or the other convention that's become prevalent in the community: adding the flat configs directly to the `configs` object, but with a 'flat/' prefix. I like this better, since it's slightly more ergonomic when using it in practice. e.g. `...importX.flatConfigs.recommended` vs `...importX.configs['flat/recommended']`, but i'm open to changing that. Example Usage ```js import importPlugin from 'eslint-plugin-import'; import js from '@eslint/js'; import tsParser from '@typescript-eslint/parser'; export default [ js.configs.recommended, importPlugin.flatConfigs.recommended, importPlugin.flatConfigs.react, importPlugin.flatConfigs.typescript, { files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'], languageOptions: { parser: tsParser, ecmaVersion: 'latest', sourceType: 'module', }, ignores: ['eslint.config.js'], rules: { 'no-unused-vars': 'off', 'import/no-dynamic-require': 'warn', 'import/no-nodejs-modules': 'warn', }, }, ]; ``` Note: in order to fill a gap in a future API gap for the `no-unused-module`, this takes advantage of a *proposed* new API on the ESLint context, that currently only exists in a POC state (unreleased). Closes import-js#29
1 parent ea7c13e commit 43082b7

33 files changed

+2780
-28
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ test/fixtures/typescript-d-ts/
88
# we want to ignore "test/fixtures" here, but unfortunately doing so would
99
# interfere with unit test and fail it for some reason.
1010
# test/fixtures
11+
examples

examples/flat/eslint.config.mjs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import importPlugin from 'eslint-plugin-import-x';
2+
import js from '@eslint/js';
3+
import tsParser from '@typescript-eslint/parser';
4+
5+
export default [
6+
js.configs.recommended,
7+
importPlugin.flatConfigs.recommended,
8+
importPlugin.flatConfigs.react,
9+
importPlugin.flatConfigs.typescript,
10+
{
11+
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
12+
languageOptions: {
13+
parser: tsParser,
14+
ecmaVersion: 'latest',
15+
sourceType: 'module',
16+
},
17+
ignores: ['eslint.config.mjs', '**/exports-unused.ts'],
18+
rules: {
19+
'no-unused-vars': 'off',
20+
'import-x/no-dynamic-require': 'warn',
21+
'import-x/no-nodejs-modules': 'warn',
22+
'import-x/no-unused-modules': ['warn', { unusedExports: true }],
23+
},
24+
},
25+
];

examples/flat/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "flat",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"clean": "rimraf node_modules",
7+
"lint": "cross-env ESLINT_USE_FLAT_CONFIG=true eslint src --report-unused-disable-directives"
8+
},
9+
"devDependencies": {
10+
"@eslint/js": "^9.5.0",
11+
"@types/node": "^20.14.5",
12+
"@typescript-eslint/parser": "^7.13.1",
13+
"cross-env": "^7.0.3",
14+
"eslint": "^8.57.0",
15+
"eslint-plugin-import-x": "link:../..",
16+
"rimraf": "^6.0.1",
17+
"typescript": "^5.4.5"
18+
}
19+
}

examples/flat/src/exports-unused.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type ScalarType = string | number;
2+
export type ObjType = {
3+
a: ScalarType;
4+
b: ScalarType;
5+
};
6+
7+
export const a = 13;
8+
export const b = 18;
9+
10+
const defaultExport: ObjType = { a, b };
11+
12+
export default defaultExport;

examples/flat/src/exports.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type ScalarType = string | number;
2+
export type ObjType = {
3+
a: ScalarType;
4+
b: ScalarType;
5+
};
6+
7+
export const a = 13;
8+
export const b = 18;
9+
10+
const defaultExport: ObjType = { a, b };
11+
12+
export default defaultExport;

examples/flat/src/imports.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//import c from './exports';
2+
import { a, b } from './exports';
3+
import type { ScalarType, ObjType } from './exports';
4+
5+
import path from 'path';
6+
import fs from 'node:fs';
7+
import console from 'console';

examples/flat/src/jsx.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const Components = () => {
2+
return <></>;
3+
};

examples/flat/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react-jsx",
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"rootDir": "./",
8+
"moduleResolution": "Bundler",
9+
"esModuleInterop": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"strict": true,
12+
"skipLibCheck": true
13+
}
14+
}

0 commit comments

Comments
 (0)