Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 25 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ import { normalizeOptions } from './normalizeOptions'
import { hashObject } from './utils'

let cacheOptionsHash: string | undefined
let resolver: ResolverFactory | undefined
export function resolve(source: string, file: string, options?: NapiResolveOptions | null): { found: boolean, path: string | null | undefined } {
let cachedResolver: ResolverFactory | undefined

export function resolve(source: string, file: string, options?: NapiResolveOptions | null, resolver: ResolverFactory | null = null): { found: boolean, path: string | null | undefined } {
if (isBuiltin(source))
return { found: true, path: null }

options ??= {}
const optionsHash = hashObject(options)
if (resolver == null) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With interface version 3, the resolver will not be null, so all those hashing and caching below would be skipped~

options ??= {}
const optionsHash = hashObject(options)

if (!cachedResolver || cacheOptionsHash !== optionsHash) {
options = normalizeOptions(options)
cachedResolver = new ResolverFactory(options)
cacheOptionsHash = optionsHash
}

if (!resolver || cacheOptionsHash !== optionsHash) {
options = normalizeOptions(options)
resolver = new ResolverFactory(options)
cacheOptionsHash = optionsHash
resolver = cachedResolver
}

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

export const interfaceVersion = 2

export function createOxcResolver(options?: NapiResolveOptions | null) {
const resolver = new ResolverFactory(options)

return {
interfaceVersion: 3,
name: 'eslint-import-resolver-oxc',
resolve(source: string, file: string) {
return resolve(source, file, null, resolver)
},
}
}
3 changes: 2 additions & 1 deletion tests/eslint-plugin/rules/no-cycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { run } from '../utils'

function createCycleSourceError(p: string) {
return {
message: `Dependency cycle via ${p}`,
messageId: 'cycleSource',
data: { source: p },
}
}
Comment on lines 3 to 8
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[email protected] has removed all the message and has migrated them all to the messgaeId. So the error has also been updated.


Expand Down
11 changes: 6 additions & 5 deletions tests/eslint-plugin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { RuleTesterInitOptions, TestCasesOptions } from 'eslint-vitest-rule-tester'
import type { NapiResolveOptions } from 'oxc-resolver'
import path from 'node:path'
import { cwd } from 'node:process'
import tsParser from '@typescript-eslint/parser'
import { rules } from 'eslint-plugin-import-x'
import { run as _run } from 'eslint-vitest-rule-tester'

import { createOxcResolver } from '../../src'

export * from 'eslint-vitest-rule-tester'
export { unindent as $ } from 'eslint-vitest-rule-tester'

Expand Down Expand Up @@ -35,15 +36,15 @@ export function run(options: ExtendedRuleTesterOptions) {
configs: {
settings: {
...(options.lang === 'js' ? {} : { 'import-x/parsers': { [require.resolve('@typescript-eslint/parser')]: ['.ts'] } }),
'import-x/resolver': {
[oxcResolver]: {
'import-x/resolver-next': [
createOxcResolver({
tsconfig: {
configFile: path.resolve(FIXTURES_PATH, 'tsconfig.json'),
references: 'auto',
},
roots: [FIXTURES_PATH],
} satisfies NapiResolveOptions,
},
}),
],
},
},
...(options.lang === 'js' ? {} : { parser: tsParser as any }),
Expand Down