Skip to content

Commit 371ebee

Browse files
authored
feat: integrate eslint-import-context to get rule context without additional params (#335)
1 parent 685477f commit 371ebee

24 files changed

+712
-1329
lines changed

.changeset/tired-lizards-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": minor
3+
---
4+
5+
feat: integrate `eslint-import-context` to get rule context without additional params

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
PARSER_NO_WATCH: true
6464

6565
- name: Codecov
66-
uses: codecov/codecov-action@ad3126e916f78f00edff4ed0317cf185271ccc2d # v5.4.2
66+
uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24 # v5.4.3
6767
with:
6868
token: ${{ secrets.CODECOV_TOKEN }}
6969

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.yarn
2+
.pnp.*
23
test/fixtures/just-json-files/invalid.json
34
test/fixtures/malformed.js
45
test/fixtures/with-syntax-error/package.json

index.d.cts

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "module",
55
"description": "Import with sanity.",
66
"repository": "https://github.com/un-ts/eslint-plugin-import-x",
7+
"homepage": "https://github.com/un-ts/eslint-plugin-import-x#readme",
78
"author": "JounQin <[email protected]> (https://www.1stG.me)",
89
"funding": "https://opencollective.com/eslint-plugin-import-x",
910
"license": "MIT",
@@ -12,7 +13,7 @@
1213
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
1314
},
1415
"main": "lib/index.cjs",
15-
"types": "index.d.cts",
16+
"types": "lib/index.d.cts",
1617
"module": "lib/index.js",
1718
"exports": {
1819
".": {
@@ -21,7 +22,7 @@
2122
"default": "./lib/index.js"
2223
},
2324
"require": {
24-
"types": "./index.d.cts",
25+
"types": "./lib/index.d.cts",
2526
"default": "./lib/index.cjs"
2627
}
2728
},
@@ -32,7 +33,6 @@
3233
"./*": "./lib/*.js"
3334
},
3435
"files": [
35-
"index.d.cts",
3636
"lib",
3737
"!lib/*.tsbuildinfo"
3838
],
@@ -48,9 +48,9 @@
4848
],
4949
"scripts": {
5050
"build": "run-p 'build:*'",
51-
"build:r": "r -f cjs -e named",
5251
"build:tsc": "tsc -p src",
53-
"clean": "rimraf lib",
52+
"build:tsdown": "tsdown --no-clean -d lib -f cjs src/index.ts",
53+
"clean": "premove coverage lib context/lib .eslintcache",
5454
"format": "prettier --write .",
5555
"lint": "run-p 'lint:*'",
5656
"lint:docs": "yarn update:eslint-docs --check",
@@ -70,8 +70,8 @@
7070
"@typescript-eslint/utils": "^8.31.0",
7171
"comment-parser": "^1.4.1",
7272
"debug": "^4.4.0",
73+
"eslint-import-context": "^0.1.3",
7374
"eslint-import-resolver-node": "^0.3.9",
74-
"get-tsconfig": "^4.10.0",
7575
"is-glob": "^4.0.3",
7676
"minimatch": "^9.0.3 || ^10.0.1",
7777
"semver": "^7.7.1",
@@ -101,7 +101,6 @@
101101
"@commitlint/cli": "^19.8.0",
102102
"@eslint/import-test-order-redirect-scoped": "link:./test/fixtures/order-redirect-scoped",
103103
"@eslint/js": "^9.25.1",
104-
"@pkgr/rollup": "^6.0.3",
105104
"@swc-node/jest": "^1.8.13",
106105
"@swc/core": "^1.11.22",
107106
"@swc/helpers": "^0.5.17",
@@ -145,12 +144,13 @@
145144
"nano-staged": "^0.8.0",
146145
"npm-run-all2": "^7.0.2",
147146
"path-serializer": "^0.4.0",
147+
"premove": "^4.0.0",
148148
"prettier": "^3.5.3",
149149
"redux": "^5.0.1",
150-
"rimraf": "^6.0.1",
151150
"simple-git-hooks": "^2.13.0",
152151
"tinyexec": "^1.0.1",
153152
"ts-node": "^10.9.2",
153+
"tsdown": "^0.12.2",
154154
"type-fest": "^4.40.0",
155155
"typescript": "^5.8.3",
156156
"typescript-eslint": "^8.31.0",

resolvers/README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Currently, version 1 is assumed if no `interfaceVersion` is available. (didn't t
77
- [v3](#v3)
88
- [Required `interfaceVersion: number`](#required-interfaceversion-number)
99
- [Required `resolve`](#required-resolve)
10+
- [`useRuleContext` and `getTsconfigWithContext`](#userulecontext-and-gettsconfigwithcontext)
1011
- [Arguments](#arguments)
1112
- [`source`](#source)
1213
- [`file`](#file)
@@ -15,6 +16,7 @@ Currently, version 1 is assumed if no `interfaceVersion` is available. (didn't t
1516
- [v2](#v2)
1617
- [`interfaceVersion: number`](#interfaceversion-number)
1718
- [`resolve`](#resolve)
19+
- [`useRuleContext` and `getTsconfigWithContext`](#userulecontext-and-gettsconfigwithcontext-1)
1820
- [Arguments](#arguments-1)
1921
- [`source`](#source-1)
2022
- [`file`](#file-1)
@@ -55,6 +57,7 @@ and
5557

5658
```js
5759
// eslint.config.js
60+
import { useRuleContext, getTsconfigWithContext } from 'eslint-import-context'
5861
import { createNodeResolver } from 'eslint-plugin-import-x'
5962

6063
export default [
@@ -65,6 +68,8 @@ export default [
6568
name: 'my-cool-resolver',
6669
interfaceVersion: 3,
6770
resolve(source, file) {
71+
const ruleContext = useRuleContext()
72+
const tsconfig = getTsconfigWithContext(ruleContext)
6873
// use a factory to get config outside of the resolver
6974
},
7075
},
@@ -77,6 +82,10 @@ export default [
7782
]
7883
```
7984

85+
#### `useRuleContext` and `getTsconfigWithContext`
86+
87+
They are powered by [eslint-import-context] in the above example, but they are not required to be used, and please be aware that `useRuleContext()` could be `undefined` when using [eslint-plugin-import] or old versions of [eslint-plugin-import-x].
88+
8089
#### Arguments
8190

8291
The arguments provided will be:
@@ -187,6 +196,10 @@ settings:
187196
node: { paths: [a, b, c] }
188197
```
189198
199+
#### `useRuleContext` and `getTsconfigWithContext`
200+
201+
They are also available via [eslint-import-context] in the `my-cool-resolver` example, same as [v3](#userulecontext-and-gettsconfigwithcontext).
202+
190203
#### Arguments
191204

192205
The arguments provided will be:
@@ -209,11 +222,13 @@ an object provided via the `import/resolver` setting. `my-cool-resolver` will ge
209222
Here is most of the [Node resolver] at the time of this writing. It is just a wrapper around substack/Browserify's synchronous [`resolve`][resolve]:
210223

211224
```js
212-
var resolve = require('resolve/sync')
213-
var isCoreModule = require('is-core-module')
225+
const resolve = require('resolve/sync')
226+
const isCoreModule = require('is-core-module')
214227
215228
exports.resolve = function (source, file, config) {
216-
if (isCoreModule(source)) return { found: true, path: null }
229+
if (isCoreModule(source)) {
230+
return { found: true, path: null }
231+
}
217232
try {
218233
return { found: true, path: resolve(source, opts(file, config)) }
219234
} catch (err) {
@@ -235,5 +250,8 @@ If the resolver cannot resolve `source` relative to `file`, it should just retur
235250

236251
[New Node resolver]: https://github.com/un-ts/eslint-plugin-import-x/blob/master/src/node-resolver.ts
237252
[Node resolver]: https://github.com/import-js/eslint-plugin-import/blob/main/resolvers/node/index.js
238-
[resolve]: https://www.npmjs.com/package/resolve
239-
[unrs-resolver]: https://www.npmjs.com/package/unrs-resolver
253+
[eslint-import-context]: https://github.com/un-ts/eslint-import-context
254+
[eslint-plugin-import]: https://github.com/import-js/eslint-plugin-import
255+
[eslint-plugin-import-x]: https://github.com/un-ts/eslint-plugin-import-x
256+
[resolve]: https://github.com/browserify/resolve
257+
[unrs-resolver]: https://github.com/unrs/unrs-resolver

src/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"compilerOptions": {
44
"rootDir": ".",
55
"outDir": "../lib"
6-
}
6+
},
7+
"include": ["."]
78
}

src/types.ts

Lines changed: 19 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
2+
import type { PluginName, PluginSettings } from 'eslint-import-context'
23
import type { MinimatchOptions } from 'minimatch'
3-
import type { KebabCase } from 'type-fest'
4-
import type { NapiResolveOptions as ResolveOptions } from 'unrs-resolver'
54

6-
import type {
7-
ImportType as ImportType_,
8-
LegacyImportResolver,
9-
LegacyResolver,
10-
PluginName,
11-
} from './utils/index.js'
5+
import type { ImportType as ImportType_ } from './utils/index.js'
126

137
export type {
148
LegacyResolver,
@@ -30,88 +24,28 @@ export type {
3024
// ResolverObject
3125
LegacyResolverObject,
3226
LegacyResolverObject as ResolverObject,
33-
} from './utils/index.js'
27+
NodeResolverOptions,
28+
WebpackResolverOptions,
29+
TsResolverOptions,
30+
NewResolverResolve,
31+
NewResolver,
32+
FileExtension,
33+
DocStyle,
34+
ResultNotFound,
35+
ResultFound,
36+
Resolver,
37+
ResolvedResult,
38+
ImportSettings,
39+
WithPluginName,
40+
PluginSettings,
41+
RuleContext,
42+
ChildContext,
43+
} from 'eslint-import-context'
3444

3545
export type ImportType = ImportType_ | 'object' | 'type'
3646

37-
export interface NodeResolverOptions {
38-
extensions?: readonly string[]
39-
moduleDirectory?: string[]
40-
paths?: string[]
41-
}
42-
43-
export interface WebpackResolverOptions {
44-
config?: string | { resolve: ResolveOptions }
45-
'config-index'?: number
46-
env?: Record<string, unknown>
47-
argv?: Record<string, unknown>
48-
}
49-
50-
export interface TsResolverOptions extends ResolveOptions {
51-
alwaysTryTypes?: boolean
52-
project?: string[] | string
53-
extensions?: string[]
54-
}
55-
56-
// TODO: remove prefix New in the next major version
57-
export type NewResolverResolve = (
58-
modulePath: string,
59-
sourceFile: string,
60-
) => ResolvedResult
61-
62-
// TODO: remove prefix New in the next major version
63-
export interface NewResolver {
64-
interfaceVersion: 3
65-
/** Optional name for the resolver, this is used in logs/debug output */
66-
name?: string
67-
resolve: NewResolverResolve
68-
}
69-
70-
export type FileExtension = `.${string}`
71-
72-
export type DocStyle = 'jsdoc' | 'tomdoc'
73-
7447
export type Arrayable<T> = T | readonly T[]
7548

76-
export interface ResultNotFound {
77-
found: false
78-
path?: undefined
79-
}
80-
81-
export interface ResultFound {
82-
found: true
83-
path: string | null
84-
}
85-
86-
export type Resolver = LegacyResolver | NewResolver
87-
88-
export type ResolvedResult = ResultNotFound | ResultFound
89-
90-
export interface ImportSettings {
91-
cache?: {
92-
lifetime?: number | '∞' | 'Infinity'
93-
}
94-
coreModules?: string[]
95-
docstyle?: DocStyle[]
96-
extensions?: readonly FileExtension[]
97-
externalModuleFolders?: string[]
98-
ignore?: string[]
99-
internalRegex?: string
100-
parsers?: Record<string, readonly FileExtension[]>
101-
resolve?: NodeResolverOptions
102-
resolver?: LegacyImportResolver
103-
'resolver-legacy'?: LegacyImportResolver
104-
'resolver-next'?: NewResolver[] | NewResolver
105-
}
106-
107-
export type WithPluginName<T extends string | object> = T extends string
108-
? `${PluginName}/${KebabCase<T>}`
109-
: {
110-
[K in keyof T as WithPluginName<`${KebabCase<K & string>}`>]: T[K]
111-
}
112-
113-
export type PluginSettings = WithPluginName<ImportSettings>
114-
11549
export interface PluginConfig extends TSESLint.ClassicConfig.Config {
11650
plugins?: [PluginName]
11751
settings?: PluginSettings
@@ -127,23 +61,6 @@ export interface PluginFlatConfig extends PluginFlatBaseConfig {
12761
name?: `${PluginName}/${string}`
12862
}
12963

130-
export interface RuleContext<
131-
TMessageIds extends string = string,
132-
TOptions extends readonly unknown[] = readonly unknown[],
133-
> extends Omit<TSESLint.RuleContext<TMessageIds, TOptions>, 'settings'> {
134-
settings: PluginSettings
135-
}
136-
137-
export interface ChildContext {
138-
cacheKey: string
139-
settings: PluginSettings
140-
parserPath?: string | null
141-
parserOptions?: TSESLint.ParserOptions
142-
languageOptions?: TSESLint.FlatConfig.LanguageOptions
143-
path: string
144-
filename?: string
145-
}
146-
14764
export interface ParseError extends Error {
14865
lineNumber: number
14966
column: number

src/utils/constants.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)