Skip to content

Commit 5742346

Browse files
committed
[Fix] flatConfigs types: remove nonexistent stage-0; add type tests
The root index.d.ts declared `flatConfigs['stage-0']`, but the runtime `flatConfigs` object in src/index.js has no `stage-0` key, so the types promised a defined flat config that is `undefined` at runtime. Also adds a type-test suite (wired into `test-types`) locking in that `flatConfigs`/`configs` and their entries are non-optional, fully-typed `Linter.FlatConfig`/`Linter.LegacyConfig` values, matching the README flat-config examples. Fixes #3169.
1 parent bf2d2aa commit 5742346

5 files changed

Lines changed: 63 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2222
- [`no-unused-modules`]: normalize path separators so `listFilesWithNodeFs` glob matching works on Windows ([#3230], thanks [@ljharb])
2323
- [`no-unused-modules`]: honor the flat config's global `ignores` in the `listFilesWithNodeFs` fallback, restoring parity with the file set ESLint lints ([#3230], thanks [@ljharb])
2424
- [`no-deprecated`], [`namespace`]: route `declaredScope` through the `getScope` compat shim instead of the removed `context.getScope()` (ESLint 9+) ([#3230], thanks [@ljharb])
25+
- [types] remove `stage-0` from the `flatConfigs` types, since it does not exist at runtime, and add type tests for the plugin's exports ([#3169], thanks [@KAMRONBEK])
2526

2627
### Changed
2728
- [Refactor] [`order`]: extract the ESLint 10 token/comment compatibility shims into a reusable `getTokenOrComment` util ([#3230], thanks [@captaindonald])
@@ -1220,6 +1221,7 @@ for info on changes for earlier releases.
12201221
[#3191]: https://github.com/import-js/eslint-plugin-import/pull/3191
12211222
[#3173]: https://github.com/import-js/eslint-plugin-import/pull/3173
12221223
[#3172]: https://github.com/import-js/eslint-plugin-import/pull/3172
1224+
[#3169]: https://github.com/import-js/eslint-plugin-import/issues/3169
12231225
[#3167]: https://github.com/import-js/eslint-plugin-import/pull/3167
12241226
[#3166]: https://github.com/import-js/eslint-plugin-import/pull/3166
12251227
[#3152]: https://github.com/import-js/eslint-plugin-import/pull/3152
@@ -1980,6 +1982,7 @@ for info on changes for earlier releases.
19801982
[@justinanastos]: https://github.com/justinanastos
19811983
[@jwbth]: https://github.com/jwbth
19821984
[@k15a]: https://github.com/k15a
1985+
[@KAMRONBEK]: https://github.com/KAMRONBEK
19831986
[@kentcdodds]: https://github.com/kentcdodds
19841987
[@kevin940726]: https://github.com/kevin940726
19851988
[@kgregory]: https://github.com/kgregory

index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ declare const plugin: ESLint.Plugin & {
1919
'recommended': Linter.FlatConfig;
2020
'errors': Linter.FlatConfig;
2121
'warnings': Linter.FlatConfig;
22-
'stage-0': Linter.FlatConfig;
2322
'react': Linter.FlatConfig;
2423
'react-native': Linter.FlatConfig;
2524
'electron': Linter.FlatConfig;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"test-example:legacy": "cd examples/legacy && npm install && npm run lint",
3838
"test-example:flat": "cd examples/flat && npm install && npm run lint",
3939
"test-example:v9": "cd examples/v9 && npm install && npm run lint",
40-
"test-types": "npx --package typescript@latest tsc --noEmit index.d.ts",
40+
"test-types": "npx --package typescript@latest tsc --noEmit index.d.ts && npx --package typescript@latest tsc -p tests/types",
4141
"prepublishOnly": "safe-publish-latest && npm run build",
4242
"prepublish": "not-in-publish || npm run prepublishOnly",
4343
"preupdate:eslint-docs": "npm run build",

tests/types/index.test-d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Type tests for the root `index.d.ts` (see #3169).
2+
// Compiled by `npm run test-types` via `tests/types/tsconfig.json`;
3+
// never executed at runtime.
4+
5+
import importPlugin from 'eslint-plugin-import';
6+
import { ESLint, Linter, Rule } from 'eslint';
7+
8+
type IsAny<T> = 0 extends 1 & T ? true : false;
9+
10+
// the plugin object itself is a usable, fully-typed ESLint plugin
11+
const plugin: ESLint.Plugin = importPlugin;
12+
13+
// `flatConfigs` and its entries must not be optional or `any` (#3169)
14+
const flatConfigsNotAny: IsAny<typeof importPlugin.flatConfigs> = false;
15+
const recommendedNotAny: IsAny<typeof importPlugin.flatConfigs.recommended> = false;
16+
17+
// the README flat-config examples: every flat config is a defined `Linter.FlatConfig`
18+
const flatConfigs: Linter.FlatConfig[] = [
19+
importPlugin.flatConfigs.recommended,
20+
importPlugin.flatConfigs.errors,
21+
importPlugin.flatConfigs.warnings,
22+
importPlugin.flatConfigs.react,
23+
importPlugin.flatConfigs['react-native'],
24+
importPlugin.flatConfigs.electron,
25+
importPlugin.flatConfigs.typescript,
26+
];
27+
28+
// @ts-expect-error `stage-0` only exists in the legacy `configs`, not in `flatConfigs`
29+
importPlugin.flatConfigs['stage-0'];
30+
31+
// the legacy configs are eslintrc-style configs, including `stage-0`
32+
const legacyConfigs: Linter.LegacyConfig[] = [
33+
importPlugin.configs.recommended,
34+
importPlugin.configs.errors,
35+
importPlugin.configs.warnings,
36+
importPlugin.configs['stage-0'],
37+
importPlugin.configs.react,
38+
importPlugin.configs['react-native'],
39+
importPlugin.configs.electron,
40+
importPlugin.configs.typescript,
41+
];
42+
43+
// every rule is a fully-typed rule module
44+
const orderRule: Rule.RuleModule = importPlugin.rules.order;

tests/types/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2022",
4+
"module": "nodenext",
5+
"moduleResolution": "nodenext",
6+
"strict": true,
7+
"esModuleInterop": true,
8+
"noEmit": true,
9+
"types": [],
10+
"paths": {
11+
"eslint-plugin-import": ["../../index.d.ts"]
12+
}
13+
},
14+
"files": ["index.test-d.ts"]
15+
}

0 commit comments

Comments
 (0)