Skip to content

Commit

Permalink
feat: add flat config support
Browse files Browse the repository at this point in the history
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#2556
  • Loading branch information
michaelfaith committed Aug 4, 2024
1 parent ea7c13e commit c5379d8
Show file tree
Hide file tree
Showing 32 changed files with 2,708 additions and 27 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ test/fixtures/typescript-d-ts/
# we want to ignore "test/fixtures" here, but unfortunately doing so would
# interfere with unit test and fail it for some reason.
# test/fixtures
examples
25 changes: 25 additions & 0 deletions examples/flat/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import importPlugin from 'eslint-plugin-import-x';
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.mjs', '**/exports-unused.ts'],
rules: {
'no-unused-vars': 'off',
'import-x/no-dynamic-require': 'warn',
'import-x/no-nodejs-modules': 'warn',
'import-x/no-unused-modules': ['warn', { unusedExports: true }],
},
},
];
19 changes: 19 additions & 0 deletions examples/flat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "flat",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"clean": "rimraf node_modules",
"lint": "cross-env ESLINT_USE_FLAT_CONFIG=true eslint src --report-unused-disable-directives"
},
"devDependencies": {
"@eslint/js": "^9.5.0",
"@types/node": "^20.14.5",
"@typescript-eslint/parser": "^7.13.1",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint-plugin-import-x": "link:../..",
"rimraf": "^6.0.1",
"typescript": "^5.4.5"
}
}
12 changes: 12 additions & 0 deletions examples/flat/src/exports-unused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type ScalarType = string | number;
export type ObjType = {
a: ScalarType;
b: ScalarType;
};

export const a = 13;
export const b = 18;

const defaultExport: ObjType = { a, b };

export default defaultExport;
12 changes: 12 additions & 0 deletions examples/flat/src/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type ScalarType = string | number;
export type ObjType = {
a: ScalarType;
b: ScalarType;
};

export const a = 13;
export const b = 18;

const defaultExport: ObjType = { a, b };

export default defaultExport;
7 changes: 7 additions & 0 deletions examples/flat/src/imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//import c from './exports';
import { a, b } from './exports';

Check failure on line 2 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

All imports in import declaration are unused.

Check failure on line 2 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on macos-latest

All imports in import declaration are unused.

Check failure on line 2 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

All imports in import declaration are unused.

Check failure on line 2 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on macos-latest

All imports in import declaration are unused.
import type { ScalarType, ObjType } from './exports';

Check failure on line 3 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

All imports in import declaration are unused.

Check failure on line 3 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on macos-latest

All imports in import declaration are unused.

Check failure on line 3 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

All imports in import declaration are unused.

Check failure on line 3 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on macos-latest

All imports in import declaration are unused.

import path from 'path';

Check failure on line 5 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

'path' is declared but its value is never read.

Check failure on line 5 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on macos-latest

'path' is declared but its value is never read.

Check failure on line 5 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

'path' is declared but its value is never read.

Check failure on line 5 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on macos-latest

'path' is declared but its value is never read.
import fs from 'node:fs';

Check failure on line 6 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

'fs' is declared but its value is never read.

Check failure on line 6 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on macos-latest

'fs' is declared but its value is never read.

Check failure on line 6 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

'fs' is declared but its value is never read.

Check failure on line 6 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on macos-latest

'fs' is declared but its value is never read.
import console from 'console';

Check failure on line 7 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

'console' is declared but its value is never read.

Check failure on line 7 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on macos-latest

'console' is declared but its value is never read.

Check failure on line 7 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

'console' is declared but its value is never read.

Check failure on line 7 in examples/flat/src/imports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on macos-latest

'console' is declared but its value is never read.
3 changes: 3 additions & 0 deletions examples/flat/src/jsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Components = () => {

Check failure on line 1 in examples/flat/src/jsx.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

Cannot redeclare block-scoped variable 'Components'.

Check failure on line 1 in examples/flat/src/jsx.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on macos-latest

Cannot redeclare block-scoped variable 'Components'.

Check failure on line 1 in examples/flat/src/jsx.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Cannot redeclare block-scoped variable 'Components'.

Check failure on line 1 in examples/flat/src/jsx.tsx

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on macos-latest

Cannot redeclare block-scoped variable 'Components'.
return <></>;
};
14 changes: 14 additions & 0 deletions examples/flat/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"rootDir": "./",
"moduleResolution": "Bundler",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Loading

0 comments on commit c5379d8

Please sign in to comment.