Skip to content

Commit bfcd8f2

Browse files
committed
feat: rewrite, speed up by using oxc-resolver under the hood
1 parent 7d63ec7 commit bfcd8f2

11 files changed

+2076
-1958
lines changed

package.json

+8-12
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@
1010
],
1111
"funding": "https://opencollective.com/unts/projects/eslint-import-resolver-ts",
1212
"license": "ISC",
13-
"packageManager": "yarn@4.0.2",
13+
"packageManager": "yarn@4.7.0",
1414
"engines": {
15-
"node": "^14.18.0 || >=16.0.0"
15+
"node": "^16.17.0 || >=18.6.0"
1616
},
1717
"main": "lib/index.cjs",
1818
"module": "lib/index.js",
1919
"exports": {
2020
".": {
2121
"types": "./lib/index.d.ts",
22-
"es2020": "./lib/index.es2020.mjs",
23-
"fesm2020": "./lib/index.es2020.mjs",
2422
"import": "./lib/index.js",
2523
"require": "./lib/index.cjs"
2624
},
2725
"./package.json": "./package.json"
2826
},
29-
"es2020": "lib/index.es2020.mjs",
30-
"fesm2020": "lib/index.es2020.mjs",
3127
"types": "lib/index.d.ts",
3228
"files": [
3329
"lib",
@@ -42,15 +38,15 @@
4238
"plugin"
4339
],
4440
"scripts": {
45-
"build": "run-p 'build:*'",
41+
"build": "run-p -c 'build:*'",
4642
"build:r": "r -f cjs,es2020",
4743
"build:ts": "tsc -b",
48-
"lint": "run-p 'lint:*'",
44+
"lint": "run-p -c 'lint:*'",
4945
"lint:es": "eslint src --cache -f friendly",
5046
"lint:tsc": "tsc --noEmit",
51-
"prepare": "simple-git-hooks",
47+
"prepare": "patch-package && simple-git-hooks",
5248
"release": "changeset publish",
53-
"test": "run-p 'test:*'",
49+
"test": "run-p -c 'test:*'",
5450
"test:dotInclude": "eslint --ext ts,tsx tests/dotInclude --ignore-pattern \"!.dot\"",
5551
"test:dotPaths": "eslint --ext ts,tsx tests/dotPaths --ignore-pattern \"!.dot\"",
5652
"test:dotProject": "eslint --ext ts,tsx tests/dotProject --ignore-pattern \"!.dot\"",
@@ -80,11 +76,10 @@
8076
}
8177
},
8278
"dependencies": {
83-
"@nolyfill/is-core-module": "1.0.39",
8479
"debug": "^4.3.7",
85-
"enhanced-resolve": "^5.15.0",
8680
"get-tsconfig": "^4.10.0",
8781
"is-bun-module": "^1.0.2",
82+
"oxc-resolver": "^5.0.0",
8883
"stable-hash": "^0.0.4",
8984
"tinyglobby": "^0.2.12"
9085
},
@@ -99,6 +94,7 @@
9994
"@types/debug": "^4.1.12",
10095
"@types/node": "^18.19.78",
10196
"@types/unist": "^2.0.11",
97+
"@unts/patch-package": "^8.1.1",
10298
"cross-env": "^7.0.3",
10399
"dummy.js": "link:dummy.js",
104100
"eslint": "^8.57.1",
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/node_modules/@1stg/eslint-config/overrides.js b/node_modules/@1stg/eslint-config/overrides.js
2+
index 3c54ff2..d8aa880 100644
3+
--- a/node_modules/@1stg/eslint-config/overrides.js
4+
+++ b/node_modules/@1stg/eslint-config/overrides.js
5+
@@ -88,7 +88,7 @@ const resolveSettings = {
6+
'import/resolver': {
7+
typescript: {
8+
alwaysTryTypes: true,
9+
- project,
10+
+ tsconfig: { configFile: project },
11+
},
12+
},
13+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
diff --git a/node_modules/eslint-plugin-import-x/lib/index.d.ts b/node_modules/eslint-plugin-import-x/lib/index.d.ts
2+
index cd65cba..cebba7a 100644
3+
--- a/node_modules/eslint-plugin-import-x/lib/index.d.ts
4+
+++ b/node_modules/eslint-plugin-import-x/lib/index.d.ts
5+
@@ -410,4 +410,5 @@ declare const _default: {
6+
};
7+
importXResolverCompat: typeof importXResolverCompat;
8+
};
9+
+export type * from './types';
10+
export = _default;

src/.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
extends: require.resolve('@1stg/eslint-config'),
2+
extends: '@1stg',
33
}

src/constants.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export const defaultConditionNames = [
2+
'types',
3+
'import',
4+
5+
// APF: https://angular.io/guide/angular-package-format
6+
'esm2020',
7+
'es2020',
8+
'es2015',
9+
10+
'require',
11+
'node',
12+
'node-addons',
13+
'browser',
14+
'default',
15+
]
16+
17+
/**
18+
* `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
19+
*/
20+
export const defaultExtensions = [
21+
'.ts',
22+
'.tsx',
23+
'.d.ts',
24+
'.js',
25+
'.jsx',
26+
'.json',
27+
'.node',
28+
]
29+
30+
export const defaultExtensionAlias = {
31+
'.js': [
32+
'.ts',
33+
// `.tsx` can also be compiled as `.js`
34+
'.tsx',
35+
'.d.ts',
36+
'.js',
37+
],
38+
'.jsx': ['.tsx', '.d.ts', '.jsx'],
39+
'.cjs': ['.cts', '.d.cts', '.cjs'],
40+
'.mjs': ['.mts', '.d.mts', '.mjs'],
41+
}
42+
43+
export const defaultMainFields = [
44+
'types',
45+
'typings',
46+
47+
// APF: https://angular.io/guide/angular-package-format
48+
'fesm2020',
49+
'fesm2015',
50+
'esm2020',
51+
'es2020',
52+
53+
'module',
54+
'jsnext:main',
55+
56+
'main',
57+
]
58+
59+
export const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/
60+
61+
export const IMPORT_RESOLVER_NAME = 'eslint-import-resolver-typescript'
62+
63+
export const interfaceVersion = 2
64+
65+
export const DEFAULT_TSCONFIG = 'tsconfig.json'
66+
67+
export const DEFAULT_JSCONFIG = 'jsconfig.json'
68+
69+
export const DEFAULT_CONFIGS = [DEFAULT_TSCONFIG, DEFAULT_JSCONFIG]
70+
71+
export const DEFAULT_TRY_PATHS = ['', ...DEFAULT_CONFIGS]
72+
73+
export const MATCH_ALL = '**'
74+
75+
export const DEFAULT_IGNORE = [MATCH_ALL, 'node_modules', MATCH_ALL].join('/')

src/helpers.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
3+
4+
/**
5+
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
6+
*/
7+
export function mangleScopedPackage(moduleName: string) {
8+
if (moduleName.startsWith('@')) {
9+
const replaceSlash = moduleName.replace('/', '__')
10+
if (replaceSlash !== moduleName) {
11+
return replaceSlash.slice(1) // Take off the "@"
12+
}
13+
}
14+
return moduleName
15+
}
16+
17+
/** Remove any trailing querystring from module id. */
18+
export function removeQuerystring(id: string) {
19+
const querystringIndex = id.lastIndexOf('?')
20+
if (querystringIndex >= 0) {
21+
return id.slice(0, querystringIndex)
22+
}
23+
return id
24+
}
25+
26+
export const tryFile = (
27+
filename?: string[] | string,
28+
includeDir = false,
29+
base = process.cwd(),
30+
): string => {
31+
if (typeof filename === 'string') {
32+
const filepath = path.resolve(base, filename)
33+
return fs.existsSync(filepath) &&
34+
(includeDir || fs.statSync(filepath).isFile())
35+
? filepath
36+
: ''
37+
}
38+
39+
for (const file of filename ?? []) {
40+
const filepath = tryFile(file, includeDir, base)
41+
if (filepath) {
42+
return filepath
43+
}
44+
}
45+
46+
return ''
47+
}
48+
49+
const computeAffinity = (projectDir: string, targetDir: string): number => {
50+
const a = projectDir.split(path.sep)
51+
const b = targetDir.split(path.sep)
52+
let lca = 0
53+
while (lca < a.length && lca < b.length && a[lca] === b[lca]) {
54+
lca++
55+
}
56+
return a.length - lca + (b.length - lca)
57+
}
58+
59+
export const sortProjectsByAffinity = (projects: string[], file: string) => {
60+
const fileDir = path.dirname(file)
61+
return projects
62+
.map(project => ({
63+
project,
64+
affinity: computeAffinity(path.dirname(project), fileDir),
65+
}))
66+
.sort((a, b) => a.affinity - b.affinity)
67+
.map(item => item.project)
68+
}

0 commit comments

Comments
 (0)