diff --git a/.github/workflows/autofix.yml b/.github/workflows/autofix.yml index e7116bb..a023f64 100644 --- a/.github/workflows/autofix.yml +++ b/.github/workflows/autofix.yml @@ -31,6 +31,6 @@ jobs: run: yarn format - name: Apply autofix.ci - uses: autofix-ci/action@2891949f3779a1cafafae1523058501de3d4e944 # v1.3.1 + uses: autofix-ci/action@635ffb0c9798bd160680f18fd73371e355b85f27 # v1.3.2 with: fail-fast: false diff --git a/.nvmrc b/.nvmrc index 08b7109..fc37597 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -18.20.8 +22.17.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b580e9..5930955 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 4.4.4 + +### Patch Changes + +- [#468](https://github.com/import-js/eslint-import-resolver-typescript/pull/468) [`93b39d2`](https://github.com/import-js/eslint-import-resolver-typescript/commit/93b39d2c4f6c67683e5eabed9fe3b7d61565d263) Thanks [@renovate](https://github.com/apps/renovate)! - chore(deps): bump `stable-hash-x` v0.2.0 + +- [#466](https://github.com/import-js/eslint-import-resolver-typescript/pull/466) [`799f1ce`](https://github.com/import-js/eslint-import-resolver-typescript/commit/799f1ce2b23487c371470cac048ff76a736faf93) Thanks [@anomiex](https://github.com/anomiex)! - fix: include options hash in cache key for options normalization + ## 4.4.3 ### Patch Changes diff --git a/README.md b/README.md index 4405eee..51a2979 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,14 @@ [![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![changesets](https://img.shields.io/badge/maintained%20with-changesets-176de3.svg)](https://github.com/changesets/changesets) -This is a resolver for `eslint-plugin-import(-x)` plugin, not an ESLint plugin itself, it adds [`TypeScript`][] support to [`eslint-plugin-import`][] (Or maybe you want to try [`eslint-plugin-import-x`][] for faster speed) +This is a resolver for `eslint-plugin-import(-x)` plugin, not an ESLint plugin itself, it adds [`TypeScript`][] support to [`eslint-plugin-import`][]. (Or maybe you want to try [`eslint-plugin-import-x`][] for faster speed) This means you can: - `import`/`require` files with extension `.cts`/`.mts`/`.ts`/`.tsx`/`.d.cts`/`.d.mts`/`.d.ts` - Use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) defined in `tsconfig.json` - Prefer resolving `@types/*` definitions over plain `.js`/`.jsx` -- Multiple tsconfigs support just like normal +- Multiple tsconfigs support, just like normal - `imports/exports` fields support in `package.json` ## TOC @@ -50,9 +50,9 @@ This means you can: ## Notice -After version 2.0.0, `.d.ts` will take higher priority then normal `.js`/`.jsx` files on resolving `node_modules` packages in favor of `@types/*` definitions or its own definition. +After version 2.0.0, `.d.ts` will take higher priority than normal `.js`/`.jsx` files on resolving `node_modules` packages in favor of `@types/*` definitions or its own definition. -If you're facing some problems on rules `import/default` or `import/named` from [`eslint-plugin-import`][], do not post any issue here, because they are just working exactly as [expected](https://github.com/import-js/eslint-import-resolver-typescript/issues/31#issuecomment-539751607) on our sides, take [import-js/eslint-plugin-import#1525](https://github.com/import-js/eslint-plugin-import/issues/1525) as reference or post a new issue to [`eslint-plugin-import`][] instead. +If you're facing some problems with rules `import/default` or `import/named` from [`eslint-plugin-import`][], do not post any issue here, because they are working exactly as [expected](https://github.com/import-js/eslint-import-resolver-typescript/issues/31#issuecomment-539751607) on our side. Take [import-js/eslint-plugin-import#1525](https://github.com/import-js/eslint-plugin-import/issues/1525) as reference or post a new issue on [`eslint-plugin-import`][] instead. ## Installation @@ -67,6 +67,9 @@ pnpm i -D eslint-plugin-import-x eslint-import-resolver-typescript # yarn yarn add -D eslint-plugin-import-x eslint-import-resolver-typescript + +# bun +bun add -d eslint-plugin-import-x eslint-import-resolver-typescript ``` ### `eslint-plugin-import` @@ -80,16 +83,19 @@ pnpm i -D eslint-plugin-import eslint-import-resolver-typescript # yarn yarn add -D eslint-plugin-import eslint-import-resolver-typescript + +# bun +bun add -d eslint-plugin-import eslint-import-resolver-typescript ``` ## Configuration ### `eslint.config.js` -If you are using `eslint-plugin-import-x@>=4.5.0`, you can use import/require to reference `eslint-import-resolver-typescript` directly in your ESLint flat config: +If you are using `eslint-plugin-import-x@>=4.5.0`, you can use `import`/`require` to reference `eslint-import-resolver-typescript` directly in your ESLint flat config: ```js -// eslint.config.js, CommonJS is also supported +// eslint.config.js (CommonJS is also supported) import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript' export default [ @@ -97,27 +103,27 @@ export default [ settings: { 'import-x/resolver-next': [ createTypeScriptImportResolver({ - alwaysTryTypes: true, // always try to resolve types under `@types` directory even it doesn't contain any source code, like `@types/unist` + alwaysTryTypes: true, // Always try to resolve types under `@types` directory even if it doesn't contain any source code, like `@types/unist` - bun: true, // resolve Bun modules https://github.com/import-js/eslint-import-resolver-typescript#bun + bun: true, // Resolve Bun modules (https://github.com/import-js/eslint-import-resolver-typescript#bun) // Choose from one of the "project" configs below or omit to use /tsconfig.json or /jsconfig.json by default - // use /path/to/folder/tsconfig.json or /path/to/folder/jsconfig.json + // Use /path/to/folder/tsconfig.json or /path/to/folder/jsconfig.json project: 'path/to/folder', // Multiple tsconfigs/jsconfigs (Useful for monorepos, but discouraged in favor of `references` supported) - // use a glob pattern + // Use a glob pattern project: 'packages/*/{ts,js}config.json', - // use an array + // Use an array project: [ 'packages/module-a/tsconfig.json', 'packages/module-b/jsconfig.json', ], - // use an array of glob patterns + // Use an array of glob patterns project: [ 'packages/*/tsconfig.json', 'other-packages/*/jsconfig.json', @@ -129,36 +135,36 @@ export default [ ] ``` -But if you are using `eslint-plugin-import` or the older version of `eslint-plugin-import-x`, you can't use require/import: +But if you are using `eslint-plugin-import` or the older version of `eslint-plugin-import-x`, you can't use `require`/`import`: ```js -// eslint.config.js, CommonJS is also supported +// eslint.config.js (CommonJS is also supported) export default [ { settings: { 'import/resolver': { typescript: { - alwaysTryTypes: true, // always try to resolve types under `@types` directory even it doesn't contain any source code, like `@types/unist` + alwaysTryTypes: true, // Always try to resolve types under `@types` directory even if it doesn't contain any source code, like `@types/unist` - bun: true, // resolve Bun modules https://github.com/import-js/eslint-import-resolver-typescript#bun + bun: true, // Resolve Bun modules (https://github.com/import-js/eslint-import-resolver-typescript#bun) // Choose from one of the "project" configs below or omit to use /tsconfig.json or /jsconfig.json by default - // use /path/to/folder/tsconfig.json or /path/to/folder/jsconfig.json + // Use /path/to/folder/tsconfig.json or /path/to/folder/jsconfig.json project: 'path/to/folder', // Multiple tsconfigs/jsconfigs (Useful for monorepos, but discouraged in favor of `references` supported) - // use a glob pattern + // Use a glob pattern project: 'packages/*/{ts,js}config.json', - // use an array + // Use an array project: [ 'packages/module-a/tsconfig.json', 'packages/module-b/jsconfig.json', ], - // use an array of glob patterns + // Use an array of glob patterns project: [ 'packages/*/tsconfig.json', 'other-packages/*/jsconfig.json', @@ -178,7 +184,7 @@ Add the following to your `.eslintrc` config: { "plugins": ["import"], "rules": { - // turn on errors for missing imports + // Turn on errors for missing imports "import/no-unresolved": "error", }, "settings": { @@ -187,27 +193,27 @@ Add the following to your `.eslintrc` config: }, "import/resolver": { "typescript": { - "alwaysTryTypes": true, // always try to resolve types under `@types` directory even it doesn't contain any source code, like `@types/unist` + "alwaysTryTypes": true, // Always try to resolve types under `@types` directory even if it doesn't contain any source code, like `@types/unist` - "bun": true, // resolve Bun modules https://github.com/import-js/eslint-import-resolver-typescript#bun + "bun": true, // Resolve Bun modules (https://github.com/import-js/eslint-import-resolver-typescript#bun) // Choose from one of the "project" configs below or omit to use /tsconfig.json or /jsconfig.json by default - // use /path/to/folder/tsconfig.json or /path/to/folder/jsconfig.json + // Use /path/to/folder/tsconfig.json or /path/to/folder/jsconfig.json "project": "path/to/folder", // Multiple tsconfigs (Useful for monorepos, but discouraged in favor of `references` supported) - // use a glob pattern + // Use a glob pattern "project": "packages/*/{ts,js}config.json", - // use an array + // Use an array "project": [ "packages/module-a/tsconfig.json", "packages/module-b/jsconfig.json", ], - // use an array of glob patterns + // Use an array of glob patterns "project": [ "packages/*/tsconfig.json", "other-packages/*/jsconfig.json", @@ -226,9 +232,9 @@ Add the following to your `.eslintrc` config: Enable Bun built-in module resolution by choosing 1 out of these 3 options: -- Set the `bun: true` option, as shown in [Configuration](#configuration) above -- Run ESLint with `bun --bun eslint` -- [Configure `run.bun` in `bunfig.toml`](https://bun.sh/docs/runtime/bunfig#run-bun-auto-alias-node-to-bun) +- Set the `bun: true` option, as shown in [Configuration](#configuration) above. +- Run ESLint with `bun --bun eslint`. +- [Configure `run.bun` in `bunfig.toml`](https://bun.sh/docs/runtime/bunfig#run-bun-auto-alias-node-to-bun). ## Options from [`unrs-resolver`][] @@ -324,11 +330,11 @@ Default: ### Other options -You can pass through other options of [`unrs-resolver`][] directly +You can pass through other options of [`unrs-resolver`][] directly. ### Default options -You can reuse `defaultConditionNames`, `defaultExtensions`, `defaultExtensionAlias` and `defaultMainFields` by `require/import` them directly +You can reuse `defaultConditionNames`, `defaultExtensions`, `defaultExtensionAlias`, and `defaultMainFields` by directly using `require`/`import`. ## Contributing @@ -337,7 +343,7 @@ You can reuse `defaultConditionNames`, `defaultExtensions`, `defaultExtensionAli - Make sure that `yarn lint` passes without conflicts. - Make sure your code changes match our [type-coverage](https://github.com/plantain-00/type-coverage) settings: `yarn type-coverage`. -We have [GitHub Actions](https://github.com/import-js/eslint-import-resolver-typescript/actions) which will run the above commands on your PRs. +We have [GitHub Actions](https://github.com/import-js/eslint-import-resolver-typescript/actions), which will run the above commands on your PRs. If either fails, we won't be able to merge your PR until it's fixed. diff --git a/package.json b/package.json index a4ef7d6..2fd9e03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-import-resolver-typescript", - "version": "4.4.3", + "version": "4.4.4", "type": "module", "description": "This plugin adds `TypeScript` support to `eslint-plugin-import`", "repository": "https://github.com/import-js/eslint-import-resolver-typescript", @@ -73,7 +73,7 @@ "eslint-import-context": "^0.1.8", "get-tsconfig": "^4.10.1", "is-bun-module": "^2.0.0", - "stable-hash-x": "^0.1.1", + "stable-hash-x": "^0.2.0", "tinyglobby": "^0.2.14", "unrs-resolver": "^1.7.11" }, @@ -98,7 +98,7 @@ "eslint-plugin-import-x": "^4.15.1", "nano-staged": "^0.8.0", "npm-run-all2": "^8.0.4", - "path-serializer": "^0.4.0", + "path-serializer": "^0.5.0", "premove": "^4.0.0", "prettier": "^3.5.3", "react": "^19.1.0", diff --git a/src/normalize-options.ts b/src/normalize-options.ts index ba675eb..11b96a5 100644 --- a/src/normalize-options.ts +++ b/src/normalize-options.ts @@ -1,3 +1,4 @@ +import { stableHash } from 'stable-hash-x' import { globSync, isDynamicPattern } from 'tinyglobby' import type { TsconfigOptions } from 'unrs-resolver' @@ -76,10 +77,12 @@ export function normalizeOptions( ensured = true } + const optionsHash = stableHash(options) + const cacheKey = `${configFile}\0${optionsHash}` if (configFile) { - const cachedOptions = configFileMapping.get(configFile) + const cachedOptions = configFileMapping.get(cacheKey) if (cachedOptions) { - log('using cached options for', configFile) + log('using cached options for', configFile, 'with options', options) return cachedOptions } } @@ -101,7 +104,7 @@ export function normalizeOptions( } if (configFile) { - configFileMapping.set(configFile, options) + configFileMapping.set(cacheKey, options) } return options diff --git a/tests/e2e/__snapshots__/e2e.spec.ts.snap b/tests/e2e/__snapshots__/e2e.spec.ts.snap index 622d64a..b48beee 100644 --- a/tests/e2e/__snapshots__/e2e.spec.ts.snap +++ b/tests/e2e/__snapshots__/e2e.spec.ts.snap @@ -32,6 +32,14 @@ exports[`e2e cases > should exec eslint successfully > dotProject 1`] = ` } `; +exports[`e2e cases > should exec eslint successfully > filesWithDifferentOptions 1`] = ` +{ + "exitCode": 0, + "stderr": "", + "stdout": "", +} +`; + exports[`e2e cases > should exec eslint successfully > importXResolverV3 1`] = ` { "exitCode": 0, diff --git a/tests/e2e/filesWithDifferentOptions/eslint.config.js b/tests/e2e/filesWithDifferentOptions/eslint.config.js new file mode 100644 index 0000000..c1adf02 --- /dev/null +++ b/tests/e2e/filesWithDifferentOptions/eslint.config.js @@ -0,0 +1,44 @@ +import { defineConfig, globalIgnores } from 'eslint/config' +import { defaultExtensions } from 'eslint-import-resolver-typescript' +import importX, { flatConfigs } from 'eslint-plugin-import-x' + +export default defineConfig( + globalIgnores(['eslint.config.js']), + { + files: ['**/*.foo.js', '**/*.bar.js'], + plugins: { + 'import-x': importX, + }, + settings: { + ...flatConfigs.typescript.settings, + 'import-x/resolver': { + typescript: {}, + }, + }, + rules: { + 'import-x/no-unresolved': 'error', + }, + }, + // .foo.js files should prefer importing other .foo.js files. + { + files: ['**/*.foo.js'], + settings: { + 'import-x/resolver': { + typescript: { + extensions: ['.foo.js', ...defaultExtensions], + }, + }, + }, + }, + // .bar.js files should prefer importing other .bar.js files. + { + files: ['**/*.bar.js'], + settings: { + 'import-x/resolver': { + typescript: { + extensions: ['.bar.js', ...defaultExtensions], + }, + }, + }, + }, +) diff --git a/tests/e2e/filesWithDifferentOptions/src/a.bar.js b/tests/e2e/filesWithDifferentOptions/src/a.bar.js new file mode 100644 index 0000000..ec54c7f --- /dev/null +++ b/tests/e2e/filesWithDifferentOptions/src/a.bar.js @@ -0,0 +1 @@ +import y from './y' diff --git a/tests/e2e/filesWithDifferentOptions/src/a.foo.js b/tests/e2e/filesWithDifferentOptions/src/a.foo.js new file mode 100644 index 0000000..849aac5 --- /dev/null +++ b/tests/e2e/filesWithDifferentOptions/src/a.foo.js @@ -0,0 +1 @@ +import x from './x' diff --git a/tests/e2e/filesWithDifferentOptions/src/x.foo.js b/tests/e2e/filesWithDifferentOptions/src/x.foo.js new file mode 100644 index 0000000..80bf0a9 --- /dev/null +++ b/tests/e2e/filesWithDifferentOptions/src/x.foo.js @@ -0,0 +1 @@ +export const x = 'x' diff --git a/tests/e2e/filesWithDifferentOptions/src/y.bar.js b/tests/e2e/filesWithDifferentOptions/src/y.bar.js new file mode 100644 index 0000000..6180eb9 --- /dev/null +++ b/tests/e2e/filesWithDifferentOptions/src/y.bar.js @@ -0,0 +1 @@ +export const y = 'y' diff --git a/tests/e2e/filesWithDifferentOptions/tsconfig.json b/tests/e2e/filesWithDifferentOptions/tsconfig.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/e2e/filesWithDifferentOptions/tsconfig.json @@ -0,0 +1 @@ +{} diff --git a/yarn.lock b/yarn.lock index 0248338..916c1cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6089,14 +6089,14 @@ __metadata: is-bun-module: "npm:^2.0.0" nano-staged: "npm:^0.8.0" npm-run-all2: "npm:^8.0.4" - path-serializer: "npm:^0.4.0" + path-serializer: "npm:^0.5.0" premove: "npm:^4.0.0" prettier: "npm:^3.5.3" react: "npm:^19.1.0" simple-git-hooks: "npm:^2.13.0" size-limit: "npm:^11.2.0" size-limit-preset-node-lib: "npm:^0.4.0" - stable-hash-x: "npm:^0.1.1" + stable-hash-x: "npm:^0.2.0" tinyexec: "npm:^1.0.1" tinyglobby: "npm:^0.2.14" tsdown: "npm:^0.12.7" @@ -10150,10 +10150,10 @@ __metadata: languageName: node linkType: hard -"path-serializer@npm:^0.4.0": - version: 0.4.0 - resolution: "path-serializer@npm:0.4.0" - checksum: 10c0/fc617221253e5ac12a4a012ec7c442a5a02dbc557047ae7b2ba52837d59322fd59a827ea1eee06366d465f624673613d23b282a325cf378d8bfe3c309b7da2b8 +"path-serializer@npm:^0.5.0": + version: 0.5.0 + resolution: "path-serializer@npm:0.5.0" + checksum: 10c0/822ca37d5cc9725b717b31a14a5b3094354315e156d05d3224364329813f2fe5efa6e942e14289cb4d2a0b467299f506521a1191150a057ed4a1d04f0d7ee528 languageName: node linkType: hard @@ -12297,6 +12297,13 @@ __metadata: languageName: node linkType: hard +"stable-hash-x@npm:^0.2.0": + version: 0.2.0 + resolution: "stable-hash-x@npm:0.2.0" + checksum: 10c0/c757df58366ee4bb266a9486b8932eab7c1ba730469eaf4b68d2dee404814e9f84089c44c9b5205f8c7d99a0ab036cce2af69139ce5ed44b635923c011a8aea8 + languageName: node + linkType: hard + "stackback@npm:0.0.2": version: 0.0.2 resolution: "stackback@npm:0.0.2" pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy