Skip to content

Commit 5bc8976

Browse files
authored
fix: use rspack-resolver fork for pnp support (#243)
close #242
1 parent d2512df commit 5bc8976

22 files changed

+1987
-25
lines changed

.changeset/silly-crabs-smile.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
fix: use [`rspack-resolver`](https://github.com/unrs/rspack-resolver) fork for pnp support

.env.yarn

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
ESLINT_USE_FLAT_CONFIG=false
2+
NODE_OPTIONS="--no-warnings=ESLintRCWarning"

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ test/fixtures/malformed.js
66
test/fixtures/with-syntax-error
77
test/fixtures/just-json-files/invalid.json
88
test/fixtures/typescript-d-ts/
9+
test/fixtures/yarn-pnp/
910
# we want to ignore "test/fixtures" here, but unfortunately doing so would
1011
# interfere with unit test and fail it for some reason.
1112
# test/fixtures

jest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export default {
1616
snapshotSerializers: ['<rootDir>/test/jest.serializer.ts'],
1717
testMatch: ['<rootDir>/test/**/*.spec.ts'],
1818
transform: {
19-
'^.+\\.(t|j)sx?$': ['@swc-node/jest', {} satisfies SwcOptions],
19+
'^.+\\.tsx?$': ['@swc-node/jest', {} satisfies SwcOptions],
2020
},
2121
} satisfies Config

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "eslint-plugin-import-x",
33
"version": "4.7.0",
4+
"type": "commonjs",
45
"description": "Import with sanity.",
56
"repository": "git+https://github.com/un-ts/eslint-plugin-import-x",
67
"author": "JounQin <[email protected]> (https://www.1stG.me)",
@@ -37,7 +38,7 @@
3738
"lint:es": "eslint . --cache",
3839
"lint:tsc": "tsc -p tsconfig.base.json --noEmit",
3940
"prepare": "patch-package && simple-git-hooks && yarn-berry-deduplicate || exit 0",
40-
"release": "changeset publish",
41+
"release": "clean-pkg-json && changeset publish",
4142
"test": "jest",
4243
"test-compiled": "yarn build && cross-env TEST_COMPILED=1 jest",
4344
"update:eslint-docs": "eslint-doc-generator --rule-doc-title-format prefix-name --rule-doc-section-options false --rule-list-split meta.docs.category --ignore-config stage-0 --config-emoji recommended,☑️",
@@ -55,7 +56,7 @@
5556
"get-tsconfig": "^4.10.0",
5657
"is-glob": "^4.0.3",
5758
"minimatch": "^10.0.1",
58-
"oxc-resolver": "^5.0.0",
59+
"rspack-resolver": "^1.1.0",
5960
"semver": "^7.7.1",
6061
"stable-hash": "^0.0.5",
6162
"tslib": "^2.8.1"
@@ -98,12 +99,13 @@
9899
"@typescript-eslint/parser": "^8.26.1",
99100
"@typescript-eslint/rule-tester": "^8.26.1",
100101
"@unts/patch-package": "^8.1.1",
102+
"clean-pkg-json": "^1.2.1",
101103
"cross-env": "^7.0.3",
102104
"escope": "^4.0.0",
103105
"eslint": "^9.22.0",
104106
"eslint-config-prettier": "^10.1.1",
105107
"eslint-doc-generator": "^2.1.1",
106-
"eslint-import-resolver-typescript": "^3.8.7",
108+
"eslint-import-resolver-typescript": "^3.9.0",
107109
"eslint-import-resolver-webpack": "^0.13.10",
108110
"eslint-import-test-order-redirect": "link:./test/fixtures/order-redirect",
109111
"eslint-plugin-eslint-plugin": "^6.4.0",
@@ -127,6 +129,7 @@
127129
"redux": "^5.0.1",
128130
"rimraf": "^6.0.1",
129131
"simple-git-hooks": "^2.11.1",
132+
"tinyexec": "^0.3.2",
130133
"ts-node": "^10.9.2",
131134
"type-fest": "^4.37.0",
132135
"typescript": "^5.8.2",

src/node-resolver.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { isBuiltin } from 'node:module'
22
import path from 'node:path'
33

4-
import { ResolverFactory } from 'oxc-resolver'
5-
import type { NapiResolveOptions as ResolveOptions } from 'oxc-resolver'
4+
import { ResolverFactory } from 'rspack-resolver'
5+
import type { NapiResolveOptions as ResolveOptions } from 'rspack-resolver'
66

77
import type { NewResolver } from './types'
88

@@ -49,7 +49,10 @@ export function createNodeResolver({
4949
interfaceVersion: 3,
5050
name: 'eslint-plugin-import-x built-in node resolver',
5151
resolve(modulePath, sourceFile) {
52-
if (isBuiltin(modulePath)) {
52+
if (
53+
isBuiltin(modulePath) ||
54+
(process.versions.pnp && modulePath === 'pnpapi')
55+
) {
5356
return { found: true, path: null }
5457
}
5558

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
22
import type { MinimatchOptions } from 'minimatch'
3-
import type { NapiResolveOptions as ResolveOptions } from 'oxc-resolver'
3+
import type { NapiResolveOptions as ResolveOptions } from 'rspack-resolver'
44
import type { KebabCase } from 'type-fest'
55

66
import type { ImportType as ImportType_, PluginName } from './utils'
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`yarn pnp should just work 1`] = `
4+
{
5+
"exitCode": 0,
6+
"stderr": "",
7+
"stdout": "",
8+
}
9+
`;

test/fixtures.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import path from 'node:path'
2+
3+
import { exec } from 'tinyexec'
4+
5+
describe('yarn pnp', () => {
6+
const yarnPnpDir = path.resolve('test/fixtures/yarn-pnp')
7+
const execOptions = { nodeOptions: { cwd: yarnPnpDir } }
8+
9+
it('should just work', async () => {
10+
await exec('yarn', ['install', '--immutable'], execOptions)
11+
const result = await exec('yarn', ['lint'], execOptions)
12+
expect(result).toMatchSnapshot()
13+
}, 10_000)
14+
})

test/fixtures/yarn-pnp/.editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root=true
2+
3+
[*]
4+
charset=utf-8
5+
end_of_line=lf
6+
indent_size=2
7+
indent_style=space
8+
insert_final_newline=true

test/fixtures/yarn-pnp/.env.yarn

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ESLINT_USE_FLAT_CONFIG=true

test/fixtures/yarn-pnp/.gitattributes

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.yarn/** linguist-vendored
2+
/.yarn/releases/* binary
3+
/.yarn/plugins/**/* binary
4+
/.pnp.* binary linguist-generated

test/fixtures/yarn-pnp/.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.yarn/*
2+
!.yarn/patches
3+
!.yarn/plugins
4+
!.yarn/releases
5+
!.yarn/sdks
6+
!.yarn/versions
7+
8+
# Whether you use PnP or not, the node_modules folder is often used to store
9+
# build artifacts that should be gitignored
10+
node_modules
11+
12+
# Swap the comments on the following lines if you wish to use zero-installs
13+
# In that case, don't forget to run `yarn config set enableGlobalCache false`!
14+
# Documentation here: https://yarnpkg.com/features/caching#zero-installs
15+
16+
#!.yarn/cache
17+
.pnp.*

test/fixtures/yarn-pnp/.yarn/releases/yarn-4.7.0.cjs

+935
Large diffs are not rendered by default.

test/fixtures/yarn-pnp/.yarnrc.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nodeLinker: pnp
2+
3+
yarnPath: .yarn/releases/yarn-4.7.0.cjs

test/fixtures/yarn-pnp/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# yarn-pnp

test/fixtures/yarn-pnp/add.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default (a, b) => a + b
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import importPlugin from 'eslint-plugin-import-x'
2+
3+
export default [
4+
{ ignores: ['.yarn', '.pnp.*'] },
5+
{
6+
settings: {
7+
'import-x/resolver-next': [importPlugin.createNodeResolver()],
8+
},
9+
plugins: {
10+
'import-x': importPlugin,
11+
},
12+
rules: {
13+
...importPlugin.configs.recommended.rules,
14+
},
15+
},
16+
]

test/fixtures/yarn-pnp/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pnp from 'pnpapi'
2+
3+
import add from 'epix-oxc/add'
4+
5+
console.log(add)
6+
7+
console.log(pnp)

test/fixtures/yarn-pnp/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "yarn-pnp",
3+
"type": "module",
4+
"packageManager": "[email protected]",
5+
"exports": {
6+
".": "./index.js",
7+
"./add": "./add.js?custom"
8+
},
9+
"scripts": {
10+
"lint": "eslint ."
11+
},
12+
"devDependencies": {
13+
"epix-oxc": "link:.",
14+
"eslint": "^9.22.0",
15+
"eslint-plugin-import-x": "link:../../.."
16+
}
17+
}

0 commit comments

Comments
 (0)