Skip to content

Commit 86d95ba

Browse files
committed
feat(eslint): add support for flat configs
ESLint v8.57 has added a new loadESLint() function to load the correct ESLint class (either one reading legacy configs, either one reading flat configs). See: - https://eslint.org/docs/v8.x/integrate/nodejs-api#loadeslint - eslint/eslint@1120b9b Unfortunately @types/eslint is missing v8.57 and has been removed entirely from the DefinitelyType repository (since upstream now ships type definitions). As a result we need to manually declare the type of the loadESLint() function. Additionally, loadESLint() is async, so we can't call it at plugin initialization time. Instead, we call it the first time we need it.
1 parent ca47809 commit 86d95ba

File tree

18 files changed

+246
-68
lines changed

18 files changed

+246
-68
lines changed

β€Ž.eslintignoreβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
**/test/**/output
55
packages/commonjs/test/fixtures
66
packages/typescript/test/fixtures/syntax-error
7+
packages/eslint/test/fixtures/flat-config
78

89
# temporary workaround for eslint bug where package.json is a directory
910
packages/node-resolve/test/fixtures/package-json-in-path
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test/fixtures/ignored.js
1+
test/fixtures/legacy-config/ignored.js

β€Žpackages/eslint/package.jsonβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
},
6464
"dependencies": {
6565
"@rollup/pluginutils": "^5.0.1",
66-
"eslint": "^8.24.0"
66+
"eslint": "^8.57.1"
6767
},
6868
"devDependencies": {
6969
"@rollup/plugin-node-resolve": "^15.0.0",
7070
"@rollup/plugin-typescript": "^9.0.1",
71-
"@types/eslint": "^8.4.6",
71+
"@types/eslint": "^8.56.12",
7272
"rollup": "^4.0.0-24",
7373
"typescript": "^4.8.3"
7474
},

β€Žpackages/eslint/src/index.tsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { relative, resolve, sep } from 'path';
22

33
import type { Plugin } from 'rollup';
44
import { createFilter } from '@rollup/pluginutils';
5-
import { ESLint } from 'eslint';
5+
import { ESLint, loadESLint } from 'eslint';
66

77
import type { RollupEslintOptions } from '../types';
88

9+
// New API introduced in v8.57 missing from @types/eslint
10+
declare module 'eslint' {
11+
function loadESLint(): Promise<typeof ESLint>;
12+
}
13+
914
function normalizePath(id: string) {
1015
return relative(process.cwd(), id).split(sep).join('/');
1116
}
@@ -29,12 +34,17 @@ export default function eslint(options = {} as RollupEslintOptions): Plugin {
2934
...eslintOptions
3035
} = options;
3136

32-
const eslintInstance = new ESLint(eslintOptions);
37+
let eslintInstance: ESLint | null = null;
3338
const filter = createFilter(include, exclude);
3439

3540
return {
3641
name: 'eslint',
3742
async transform(_, id: string) {
43+
if (!eslintInstance) {
44+
const DefaultESLint = await loadESLint();
45+
eslintInstance = new DefaultESLint(eslintOptions);
46+
}
47+
3848
const file = normalizePath(id);
3949
if (!filter(id) || (await eslintInstance.isPathIgnored(file))) {
4050
return null;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export default [
2+
{
3+
files: ["**/*.js"],
4+
rules: {
5+
"no-alert": 2,
6+
"no-bitwise": 1,
7+
"camelcase": 1,
8+
"curly": 1,
9+
"eqeqeq": 0,
10+
"no-eq-null": 0,
11+
"guard-for-in": 1,
12+
"no-empty": 1,
13+
"no-use-before-define": 0,
14+
"object-curly-spacing": 0,
15+
"no-obj-calls": 2,
16+
"no-unused-vars": 0,
17+
"new-cap": 1,
18+
"no-shadow": 0,
19+
"strict": 2,
20+
"global-strict": 0,
21+
"no-invalid-regexp": 2,
22+
"comma-dangle": 2,
23+
"no-undef": 1,
24+
"no-new": 1,
25+
"no-extra-semi": 1,
26+
"no-debugger": 2,
27+
"no-caller": 1,
28+
"semi": 1,
29+
"quotes": 0,
30+
"no-unreachable": 2,
31+
"eol-last": 0,
32+
},
33+
},
34+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x = 0;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
Β (0)