Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
9romise committed Dec 3, 2024
1 parent 866e1ec commit 1a51670
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ResolverFactory } from 'oxc-resolver'
import { normalizeOptions } from './normalizeOptions'
import { hashObject } from './utils'

let cacheOptionsHash: string | undefined
let cachedOptionsHash: string | 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 } {
Expand All @@ -16,10 +16,10 @@ export function resolve(source: string, file: string, options?: NapiResolveOptio
options ??= {}
const optionsHash = hashObject(options)

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

resolver = cachedResolver
Expand All @@ -37,7 +37,7 @@ export function resolve(source: string, file: string, options?: NapiResolveOptio
export const interfaceVersion = 2

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

return {
interfaceVersion: 3,
Expand Down
19 changes: 9 additions & 10 deletions tests/eslint-plugin/rules/no-extraneous-dependencies.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { NapiResolveOptions } from 'oxc-resolver'
import { oxcResolver, run, testFilePath } from '../utils'
import { createOxcImportResolver, run, testFilePath } from '../utils'

run({
rule: 'no-extraneous-dependencies',
Expand All @@ -13,27 +12,27 @@ run({
{
code: 'import "@custom-internal-alias/api/service";',
settings: {
'import-x/resolver': {
[oxcResolver]: {
'import-x/resolver-next': [
createOxcImportResolver({
alias: {
'@custom-internal-alias': [testFilePath('internal-modules')],
},
} satisfies NapiResolveOptions,
},
}),
],
},
},
],
invalid: [
{
code: 'import jest from "alias/jest";',
settings: {
'import-x/resolver': {
[oxcResolver]: {
'import-x/resolver-next': [
createOxcImportResolver({
alias: {
'alias/jest': ['jest'],
},
} satisfies NapiResolveOptions,
},
}),
],
},
errors: [
// missing dependency is jest not alias
Expand Down
11 changes: 5 additions & 6 deletions tests/eslint-plugin/rules/no-internal-modules.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { NapiResolveOptions } from 'oxc-resolver'
import { oxcResolver, run, testFilePath } from '../utils'
import { createOxcImportResolver, run, testFilePath } from '../utils'

run({
rule: 'no-internal-modules',
Expand All @@ -19,13 +18,13 @@ run({
},
],
settings: {
'import-x/resolver': {
[oxcResolver]: {
'import-x/resolver-next': [
createOxcImportResolver({
alias: {
'@': [testFilePath('internal-modules')],
},
} satisfies NapiResolveOptions,
},
}),
],
},
},
],
Expand Down
11 changes: 5 additions & 6 deletions tests/eslint-plugin/rules/order.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { NapiResolveOptions } from 'oxc-resolver'
import { oxcResolver, run, testFilePath } from '../utils'
import { createOxcImportResolver, run, testFilePath } from '../utils'

run({
rule: 'order',
Expand Down Expand Up @@ -54,13 +53,13 @@ run({
},
],
settings: {
'import-x/resolver': {
[oxcResolver]: {
'import-x/resolver-next': [
createOxcImportResolver({
alias: {
'@': [testFilePath('internal-modules')],
},
} satisfies NapiResolveOptions,
},
}),
],
},
},
],
Expand Down
11 changes: 4 additions & 7 deletions tests/eslint-plugin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { RuleTesterInitOptions, TestCasesOptions } from 'eslint-vitest-rule-tester'
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 { createOxcImportResolver } from '../../src'

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

export * from 'eslint-vitest-rule-tester'
export { createOxcImportResolver } from '../../src'
export { unindent as $ } from 'eslint-vitest-rule-tester'
export * from 'eslint-vitest-rule-tester'

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

export const oxcResolver = path.resolve(cwd(), 'dist/index.cjs')

export function run(options: ExtendedRuleTesterOptions) {
return _run({
recursive: false,
Expand All @@ -37,7 +34,7 @@ export function run(options: ExtendedRuleTesterOptions) {
settings: {
...(options.lang === 'js' ? {} : { 'import-x/parsers': { [require.resolve('@typescript-eslint/parser')]: ['.ts'] } }),
'import-x/resolver-next': [
createOxcResolver({
createOxcImportResolver({
tsconfig: {
configFile: path.resolve(FIXTURES_PATH, 'tsconfig.json'),
references: 'auto',
Expand Down

0 comments on commit 1a51670

Please sign in to comment.