Skip to content

feat: integrate eslint-import-context to get correct context.cwd #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/olive-turkeys-go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": minor
---

feat: integrate `eslint-import-context` to get correct `context.cwd`
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ jobs:
PARSER_NO_WATCH: true

- name: Codecov
uses: codecov/codecov-action@ad3126e916f78f00edff4ed0317cf185271ccc2d # v5.4.2
uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24 # v5.4.3
with:
token: ${{ secrets.CODECOV_TOKEN }}
3 changes: 0 additions & 3 deletions index.d.cts

This file was deleted.

48 changes: 25 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"node": "^16.17.0 || >=18.6.0"
},
"main": "lib/index.cjs",
"types": "lib/index.d.ts",
"types": "lib/index.d.cts",
"module": "lib/index.js",
"exports": {
".": {
Expand All @@ -24,14 +24,13 @@
"default": "./lib/index.js"
},
"require": {
"types": "./index.d.cts",
"types": "./lib/index.d.cts",
"default": "./lib/index.cjs"
}
},
"./package.json": "./package.json"
},
"files": [
"index.d.cts",
"lib",
"!**/*.tsbuildinfo"
],
Expand All @@ -44,12 +43,13 @@
],
"scripts": {
"build": "run-p -c 'build:*'",
"build:r": "r -f cjs",
"build:ts": "tsc -b",
"build:ts": "tsc -p src",
"build:tsdown": "tsdown --no-clean -d lib -f cjs src/index.ts",
"clean": "premove coverage lib .eslintcache",
"format": "prettier --write .",
"lint": "run-p 'lint:*'",
"lint:es": "eslint . --cache",
"lint:tsc": "tsc -b --noEmit",
"lint:tsc": "tsc --noEmit",
"prepare": "simple-git-hooks && yarn-berry-deduplicate || exit 0",
"release": "clean-pkg-json && changeset publish",
"test": "node tests/e2e/withJsExtension/test.cjs && vitest run",
Expand All @@ -69,45 +69,47 @@
}
},
"dependencies": {
"debug": "^4.4.0",
"get-tsconfig": "^4.10.0",
"debug": "^4.4.1",
"eslint-import-context": "^0.1.3",
"get-tsconfig": "^4.10.1",
"is-bun-module": "^2.0.0",
"stable-hash": "^0.0.5",
"tinyglobby": "^0.2.13",
"unrs-resolver": "^1.6.3"
"unrs-resolver": "^1.7.2"
},
"devDependencies": {
"@1stg/common-config": "^13.0.1",
"@1stg/common-config": "^14.0.0",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.29.2",
"@commitlint/cli": "^19.8.0",
"@changesets/cli": "^2.29.4",
"@commitlint/cli": "^19.8.1",
"@mozilla/glean": "^5.0.4",
"@pkgr/core": "^0.2.4",
"@pkgr/rollup": "^6.0.3",
"@total-typescript/ts-reset": "^0.6.1",
"@types/debug": "^4.1.12",
"@types/node": "^22.14.1",
"@types/node": "^22.15.21",
"@types/pnpapi": "^0.0.5",
"@types/unist": "^3.0.3",
"@vitest/coverage-v8": "3.1.2",
"@vitest/eslint-plugin": "^1.1.43",
"@vitest/coverage-v8": "3.1.4",
"@vitest/eslint-plugin": "^1.2.1",
"clean-pkg-json": "^1.3.0",
"dummy.js": "link:dummy.js",
"eslint": "^9.25.0",
"eslint": "^9.27.0",
"eslint-import-resolver-typescript": "workspace:*",
"eslint-plugin-import-x": "^4.10.6",
"eslint-plugin-import-x": "^4.13.0",
"nano-staged": "^0.8.0",
"npm-run-all2": "^7.0.2",
"path-serializer": "^0.3.4",
"npm-run-all2": "^8.0.3",
"path-serializer": "^0.4.0",
"premove": "^4.0.0",
"prettier": "^3.5.3",
"react": "^19.1.0",
"simple-git-hooks": "^2.12.1",
"simple-git-hooks": "^2.13.0",
"size-limit": "^11.2.0",
"size-limit-preset-node-lib": "^0.3.0",
"size-limit-preset-node-lib": "^0.4.0",
"tinyexec": "^1.0.1",
"tsdown": "^0.12.3",
"type-coverage": "^2.29.7",
"typescript": "^5.8.3",
"vitest": "^3.1.2",
"vitest": "^3.1.4",
"yarn-berry-deduplicate": "^6.1.3"
},
"resolutions": {
Expand Down
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isBuiltin } from 'node:module'
import path from 'node:path'

import type { ResolvedResult } from 'eslint-plugin-import-x/types'
import { useRuleContext, type ResolvedResult } from 'eslint-import-context'
import {
type FileMatcher,
type TsConfigJsonResolved,
Expand Down Expand Up @@ -81,10 +81,11 @@

if (!resolver) {
const optionsHash = stableHash(options)
const cwd = process.cwd()
const context = useRuleContext()
const cwd = context?.cwd || process.cwd()

Check warning on line 85 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L84-L85

Added lines #L84 - L85 were not covered by tests
options = normalizeOptions(options, cwd)
// take `cwd` into account -- #217
const cacheKey = `${optionsHash}:${cwd}`
const cacheKey = `${optionsHash}\0${cwd}`

Check warning on line 88 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L88

Added line #L88 was not covered by tests
let cached = resolverCache.get(cacheKey)
if (!cached && !options.project) {
resolverCache.set(cacheKey, (cached = new ResolverFactory(options)))
Expand Down Expand Up @@ -205,12 +206,23 @@
export const createTypeScriptImportResolver = (
options?: TypeScriptResolverOptions | null,
) => {
options = normalizeOptions(options)
const resolver = options.project ? null : new ResolverFactory(options)
let cwd = process.cwd()
options = normalizeOptions(options, cwd)
let resolver = options.project ? undefined : new ResolverFactory(options)
return {
interfaceVersion: 3,
name: IMPORT_RESOLVER_NAME,
resolve(source: string, file: string) {
const context = useRuleContext()
if (context && cwd !== context.cwd) {
cwd = context.cwd
options = normalizeOptions(options, cwd)
if (options.project) {
resolver = resolver
? resolver.cloneWithOptions(options)
: new ResolverFactory(options)
}
}

Check warning on line 225 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L218-L225

Added lines #L218 - L225 were not covered by tests
return resolve(source, file, options, resolver)
},
}
Expand Down
8 changes: 8 additions & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.lib",
"compilerOptions": {
"rootDir": ".",
"outDir": "../lib"
},
"include": ["."]
}
2 changes: 1 addition & 1 deletion tests/e2e/absoluteAlias/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../../tsconfig.base",
"extends": "../../../tsconfig.lib",
"compilerOptions": {
"paths": {
"/*": ["./public/*"]
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/withJsExtension/test.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('node:assert')
const path = require('node:path')

// eslint-disable-next-line import-x/extensions
const { resolve } = require('../../..')

const config = {
Expand Down
4 changes: 2 additions & 2 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../tsconfig.base",
"extends": "../tsconfig.lib",
"compilerOptions": {
"rootDir": "."
},
"include": [".", "../auto-imports.d.ts"]
"include": ["."]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/unit/pnp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"packageManager": "[email protected]",
"devDependencies": {
"@atlaskit/pragmatic-drag-and-drop": "^1.5.2",
"@atlaskit/pragmatic-drag-and-drop": "^1.7.0",
"lodash.zip": "^4.2.0"
}
}
Loading
Loading