Skip to content

feat: rewrite, speed up by using rspack-resolver under the hood #368

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 7 commits into from
Mar 16, 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
12 changes: 12 additions & 0 deletions .changeset/friendly-weeks-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"eslint-import-resolver-typescript": major
---

feat!: rewrite, speed up by using [`rspack-resolver`](https://github.com/unrs/rspack-resolver) which supports `references` natively under the hood

BREAKING CHANGES:

- drop Node 14 support, Node `^16.17.0 || >=18.6` is now required
- `alwaysTryTypes` is enabled by default, you can set it as `true` to opt-out
- array type of `project` is discouraged but still supported, single `project` with `references` are encouraged for better performance, you can enable `noWarnOnMultipleProjects` option to supress the warning message
- root `tsconfig.json` or `jsconfig.json` will be used automatically if no `project` provided
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"path": "./lib/index.js",
"limit": "3.1kB"
"limit": "1.5kB"
}
]
7 changes: 7 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ const config = [
},
},
},
{
files: ['src/*'],
rules: {
'prefer-const': ['error', { destructuring: 'all' }],
'sonarjs/no-nested-assignment': 'off',
},
},
]

export default config
24 changes: 8 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,21 @@
"license": "ISC",
"packageManager": "[email protected]",
"engines": {
"node": "^14.18.0 || >=16.0.0"
"node": "^16.17.0 || >=18.6.0"
},
"main": "lib/index.cjs",
"module": "lib/index.js",
"exports": {
".": {
"types": "./lib/index.d.ts",
"es2020": "./lib/index.es2020.mjs",
"fesm2020": "./lib/index.es2020.mjs",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
"require": "./lib/index.cjs",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
},
"es2020": "lib/index.es2020.mjs",
"fesm2020": "lib/index.es2020.mjs",
"types": "lib/index.d.ts",
"files": [
"lib",
"shim.d.ts",
"!**/*.tsbuildinfo"
],
"keywords": [
Expand All @@ -42,10 +37,10 @@
"plugin"
],
"scripts": {
"build": "run-p 'build:*'",
"build:r": "r -f cjs,es2020",
"build": "run-p -c 'build:*'",
"build:r": "r -f cjs",
"build:ts": "tsc -b",
"eslint": "cross-env ESLINT_USE_FLAT_CONFIG=false eslint --cache",
"eslint": "ESLINT_USE_FLAT_CONFIG=false eslint",
"lint": "run-p 'lint:*'",
"lint:es": "eslint . --cache",
"lint:tsc": "tsc --noEmit",
Expand All @@ -55,7 +50,7 @@
"test:dotInclude": "yarn eslint --ext ts,tsx tests/dotInclude --ignore-pattern \"!.dot\"",
"test:dotPaths": "yarn eslint --ext ts,tsx tests/dotPaths --ignore-pattern \"!.dot\"",
"test:dotProject": "yarn eslint --ext ts,tsx tests/dotProject --ignore-pattern \"!.dot\"",
"test:importXResolverV3": "eslint --cache --config=tests/importXResolverV3/eslint.config.cjs tests/importXResolverV3",
"test:importXResolverV3": "eslint --config=tests/importXResolverV3/eslint.config.cjs tests/importXResolverV3",
"test:multipleEslintrcs": "yarn eslint --ext ts,tsx tests/multipleEslintrcs",
"test:multipleTsconfigs": "yarn eslint --ext ts,tsx tests/multipleTsconfigs",
"test:nearestTsconfig": "yarn eslint --ext ts,tsx tests/nearestTsconfig",
Expand All @@ -81,11 +76,10 @@
}
},
"dependencies": {
"@nolyfill/is-core-module": "1.0.39",
"debug": "^4.4.0",
"get-tsconfig": "^4.10.0",
"is-bun-module": "^1.3.0",
"rspack-resolver": "^1.1.0",
"rspack-resolver": "^1.1.2",
"stable-hash": "^0.0.5",
"tinyglobby": "^0.2.12"
},
Expand All @@ -102,7 +96,6 @@
"@types/pnpapi": "^0.0.5",
"@types/unist": "^3.0.3",
"clean-pkg-json": "^1.2.1",
"cross-env": "^7.0.3",
"dummy.js": "link:dummy.js",
"eslint": "^9.22.0",
"eslint-import-resolver-typescript": "link:.",
Expand All @@ -115,7 +108,6 @@
"size-limit": "^11.2.0",
"size-limit-preset-node-lib": "^0.3.0",
"type-coverage": "^2.29.7",
"type-fest": "^4.37.0",
"typescript": "~5.8.2",
"yarn-berry-deduplicate": "^6.1.1"
},
Expand Down
75 changes: 75 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
export const defaultConditionNames = [
'types',
'import',

// APF: https://angular.io/guide/angular-package-format
'esm2020',
'es2020',
'es2015',

'require',
'node',
'node-addons',
'browser',
'default',
]

/**
* `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
*/
export const defaultExtensions = [
'.ts',
'.tsx',
'.d.ts',
'.js',
'.jsx',
'.json',
'.node',
]

export const defaultExtensionAlias = {
'.js': [
'.ts',
// `.tsx` can also be compiled as `.js`
'.tsx',
'.d.ts',
'.js',
],
'.jsx': ['.tsx', '.d.ts', '.jsx'],
'.cjs': ['.cts', '.d.cts', '.cjs'],
'.mjs': ['.mts', '.d.mts', '.mjs'],
}

export const defaultMainFields = [
'types',
'typings',

// APF: https://angular.io/guide/angular-package-format
'fesm2020',
'fesm2015',
'esm2020',
'es2020',

'module',
'jsnext:main',

'main',
]

export const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/

export const IMPORT_RESOLVER_NAME = 'eslint-import-resolver-typescript'

export const interfaceVersion = 2

export const DEFAULT_TSCONFIG = 'tsconfig.json'

export const DEFAULT_JSCONFIG = 'jsconfig.json'

export const DEFAULT_CONFIGS = [DEFAULT_TSCONFIG, DEFAULT_JSCONFIG]

export const DEFAULT_TRY_PATHS = ['', ...DEFAULT_CONFIGS]

export const MATCH_ALL = '**'

export const DEFAULT_IGNORE = [MATCH_ALL, 'node_modules', MATCH_ALL].join('/')
73 changes: 73 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fs from 'node:fs'
import path from 'node:path'

/**
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
*/
export function mangleScopedPackage(moduleName: string) {
if (moduleName.startsWith('@')) {
const replaceSlash = moduleName.replace('/', '__')
if (replaceSlash !== moduleName) {
return replaceSlash.slice(1) // Take off the "@"
}
}
return moduleName
}

/** Remove any trailing querystring from module id. */
export function removeQuerystring(id: string) {
const querystringIndex = id.lastIndexOf('?')
if (querystringIndex !== -1) {
return id.slice(0, querystringIndex)
}
return id
}

export const tryFile = (
filename?: string[] | string,
includeDir = false,
base = process.cwd(),
): string => {
if (typeof filename === 'string') {
const filepath = path.resolve(base, filename)
return fs.existsSync(filepath) &&
(includeDir || fs.statSync(filepath).isFile())
? filepath
: ''
}

for (const file of filename ?? []) {
const filepath = tryFile(file, includeDir, base)
if (filepath) {
return filepath
}
}

return ''
}

const computeAffinity = (projectDir: string, targetDir: string): number => {
const a = projectDir.split(path.sep)
const b = targetDir.split(path.sep)
let lca = 0
while (lca < a.length && lca < b.length && a[lca] === b[lca]) {
lca++
}
return a.length - lca + (b.length - lca)
}

export const sortProjectsByAffinity = (projects: string[], file: string) => {
const fileDir = path.dirname(file)
return projects
.map(project => ({
project,
affinity: computeAffinity(path.dirname(project), fileDir),
}))
.sort((a, b) => a.affinity - b.affinity)
.map(item => item.project)
}

export const toGlobPath = (pathname: string) => pathname.replaceAll('\\', '/')

export const toNativePath = (pathname: string) =>
'/' === path.sep ? pathname : pathname.replaceAll('/', '\\')
Loading