diff --git a/CHANGELOG.md b/CHANGELOG.md index 3102fc869..d7b7917e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange - [meta] replace git.io link in comments with the original URL ([#2444], thanks [@liby]) - [Docs] remove global install in readme ([#2412], thanks [@aladdin-add]) - [readme] clarify `eslint-import-resolver-typescript` usage ([#2503], thanks [@JounQin]) +- [Refactor] `no-cycle`: Add per-run caching of traversed paths ([#2419], thanks [@nokel81]) ## [2.26.0] - 2022-04-05 @@ -1004,6 +1005,7 @@ for info on changes for earlier releases. [#2466]: https://github.com/import-js/eslint-plugin-import/pull/2466 [#2440]: https://github.com/import-js/eslint-plugin-import/pull/2440 [#2427]: https://github.com/import-js/eslint-plugin-import/pull/2427 +[#2419]: https://github.com/import-js/eslint-plugin-import/pull/2419 [#2417]: https://github.com/import-js/eslint-plugin-import/pull/2417 [#2411]: https://github.com/import-js/eslint-plugin-import/pull/2411 [#2399]: https://github.com/import-js/eslint-plugin-import/pull/2399 diff --git a/src/rules/no-cycle.js b/src/rules/no-cycle.js index 0aa362682..e261ac40b 100644 --- a/src/rules/no-cycle.js +++ b/src/rules/no-cycle.js @@ -9,7 +9,8 @@ import { isExternalModule } from '../core/importType'; import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor'; import docsUrl from '../docsUrl'; -// todo: cache cycles / deep relationships for faster repeat evaluation +const traversed = new Set(); + module.exports = { meta: { type: 'suggestion', @@ -87,7 +88,6 @@ module.exports = { } const untraversed = [{ mget: () => imported, route:[] }]; - const traversed = new Set(); function detectCycle({ mget, route }) { const m = mget(); if (m == null) return; @@ -101,7 +101,7 @@ module.exports = { // Ignore only type imports !isOnlyImportingTypes, ); - + /* If cyclic dependency is allowed via dynamic import, skip checking if any module is imported dynamically */ @@ -138,7 +138,11 @@ module.exports = { } } - return moduleVisitor(checkSourceValue, context.options[0]); + return Object.assign(moduleVisitor(checkSourceValue, context.options[0]), { + 'Program:exit': () => { + traversed.clear(); + }, + }); }, };