Skip to content

Commit 10fa0d8

Browse files
SukkaW9romise
andauthored
feat: implement interface version 3 (#41)
* feat: implement interface version 3 * test: fix tests w/ eslint-plugin-import-x@4.5.0 * refactor: rename v3 interface factory function * chore: update --------- Co-authored-by: Vida Xie <38204901+9romise@users.noreply.github.com>
1 parent 00fd415 commit 10fa0d8

File tree

7 files changed

+65
-51
lines changed

7 files changed

+65
-51
lines changed

pnpm-lock.yaml

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ import { ResolverFactory } from 'oxc-resolver'
55
import { normalizeOptions } from './normalizeOptions'
66
import { hashObject } from './utils'
77

8-
let cacheOptionsHash: string | undefined
9-
let resolver: ResolverFactory | undefined
10-
export function resolve(source: string, file: string, options?: NapiResolveOptions | null): { found: boolean, path: string | null | undefined } {
8+
let cachedOptionsHash: string | undefined
9+
let cachedResolver: ResolverFactory | undefined
10+
11+
export function resolve(source: string, file: string, options?: NapiResolveOptions | null, resolver: ResolverFactory | null = null): { found: boolean, path: string | null | undefined } {
1112
if (isBuiltin(source))
1213
return { found: true, path: null }
1314

14-
options ??= {}
15-
const optionsHash = hashObject(options)
15+
if (resolver == null) {
16+
options ??= {}
17+
const optionsHash = hashObject(options)
18+
19+
if (!cachedResolver || cachedOptionsHash !== optionsHash) {
20+
options = normalizeOptions(options)
21+
cachedResolver = new ResolverFactory(options)
22+
cachedOptionsHash = optionsHash
23+
}
1624

17-
if (!resolver || cacheOptionsHash !== optionsHash) {
18-
options = normalizeOptions(options)
19-
resolver = new ResolverFactory(options)
20-
cacheOptionsHash = optionsHash
25+
resolver = cachedResolver
2126
}
2227

2328
// https://github.com/oxc-project/oxc-resolver/blob/main/npm/README.md#api
@@ -30,3 +35,15 @@ export function resolve(source: string, file: string, options?: NapiResolveOptio
3035
}
3136

3237
export const interfaceVersion = 2
38+
39+
export function createOxcImportResolver(options?: NapiResolveOptions | null) {
40+
const resolver = new ResolverFactory(normalizeOptions(options))
41+
42+
return {
43+
interfaceVersion: 3,
44+
name: 'eslint-import-resolver-oxc',
45+
resolve(source: string, file: string) {
46+
return resolve(source, file, null, resolver)
47+
},
48+
}
49+
}

tests/eslint-plugin/rules/no-cycle.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { run } from '../utils'
22

33
function createCycleSourceError(p: string) {
44
return {
5-
message: `Dependency cycle via ${p}`,
5+
messageId: 'cycleSource',
6+
data: { source: p },
67
}
78
}
89

tests/eslint-plugin/rules/no-extraneous-dependencies.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { NapiResolveOptions } from 'oxc-resolver'
2-
import { oxcResolver, run, testFilePath } from '../utils'
1+
import { createOxcImportResolver, run, testFilePath } from '../utils'
32

43
run({
54
rule: 'no-extraneous-dependencies',
@@ -13,27 +12,27 @@ run({
1312
{
1413
code: 'import "@custom-internal-alias/api/service";',
1514
settings: {
16-
'import-x/resolver': {
17-
[oxcResolver]: {
15+
'import-x/resolver-next': [
16+
createOxcImportResolver({
1817
alias: {
1918
'@custom-internal-alias': [testFilePath('internal-modules')],
2019
},
21-
} satisfies NapiResolveOptions,
22-
},
20+
}),
21+
],
2322
},
2423
},
2524
],
2625
invalid: [
2726
{
2827
code: 'import jest from "alias/jest";',
2928
settings: {
30-
'import-x/resolver': {
31-
[oxcResolver]: {
29+
'import-x/resolver-next': [
30+
createOxcImportResolver({
3231
alias: {
3332
'alias/jest': ['jest'],
3433
},
35-
} satisfies NapiResolveOptions,
36-
},
34+
}),
35+
],
3736
},
3837
errors: [
3938
// missing dependency is jest not alias

tests/eslint-plugin/rules/no-internal-modules.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { NapiResolveOptions } from 'oxc-resolver'
2-
import { oxcResolver, run, testFilePath } from '../utils'
1+
import { createOxcImportResolver, run, testFilePath } from '../utils'
32

43
run({
54
rule: 'no-internal-modules',
@@ -19,13 +18,13 @@ run({
1918
},
2019
],
2120
settings: {
22-
'import-x/resolver': {
23-
[oxcResolver]: {
21+
'import-x/resolver-next': [
22+
createOxcImportResolver({
2423
alias: {
2524
'@': [testFilePath('internal-modules')],
2625
},
27-
} satisfies NapiResolveOptions,
28-
},
26+
}),
27+
],
2928
},
3029
},
3130
],

tests/eslint-plugin/rules/order.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { NapiResolveOptions } from 'oxc-resolver'
2-
import { oxcResolver, run, testFilePath } from '../utils'
1+
import { createOxcImportResolver, run, testFilePath } from '../utils'
32

43
run({
54
rule: 'order',
@@ -54,13 +53,13 @@ run({
5453
},
5554
],
5655
settings: {
57-
'import-x/resolver': {
58-
[oxcResolver]: {
56+
'import-x/resolver-next': [
57+
createOxcImportResolver({
5958
alias: {
6059
'@': [testFilePath('internal-modules')],
6160
},
62-
} satisfies NapiResolveOptions,
63-
},
61+
}),
62+
],
6463
},
6564
},
6665
],

tests/eslint-plugin/utils.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { RuleTesterInitOptions, TestCasesOptions } from 'eslint-vitest-rule-tester'
2-
import type { NapiResolveOptions } from 'oxc-resolver'
32
import path from 'node:path'
4-
import { cwd } from 'node:process'
53
import tsParser from '@typescript-eslint/parser'
64
import { rules } from 'eslint-plugin-import-x'
75
import { run as _run } from 'eslint-vitest-rule-tester'
6+
import { createOxcImportResolver } from '../../src'
87

9-
export * from 'eslint-vitest-rule-tester'
8+
export { createOxcImportResolver } from '../../src'
109
export { unindent as $ } from 'eslint-vitest-rule-tester'
10+
export * from 'eslint-vitest-rule-tester'
1111

1212
export interface ExtendedRuleTesterOptions extends RuleTesterInitOptions, TestCasesOptions {
1313
lang?: 'js' | 'ts'
@@ -25,8 +25,6 @@ const defaultFilenames = {
2525
ts: 'tests/eslint-plugin/fixtures/foo.ts',
2626
}
2727

28-
export const oxcResolver = path.resolve(cwd(), 'dist/index.cjs')
29-
3028
export function run(options: ExtendedRuleTesterOptions) {
3129
return _run({
3230
recursive: false,
@@ -35,15 +33,15 @@ export function run(options: ExtendedRuleTesterOptions) {
3533
configs: {
3634
settings: {
3735
...(options.lang === 'js' ? {} : { 'import-x/parsers': { [require.resolve('@typescript-eslint/parser')]: ['.ts'] } }),
38-
'import-x/resolver': {
39-
[oxcResolver]: {
36+
'import-x/resolver-next': [
37+
createOxcImportResolver({
4038
tsconfig: {
4139
configFile: path.resolve(FIXTURES_PATH, 'tsconfig.json'),
4240
references: 'auto',
4341
},
4442
roots: [FIXTURES_PATH],
45-
} satisfies NapiResolveOptions,
46-
},
43+
}),
44+
],
4745
},
4846
},
4947
...(options.lang === 'js' ? {} : { parser: tsParser as any }),

0 commit comments

Comments
 (0)