Skip to content

Commit

Permalink
[Refactor] no-cycle: Add per-run caching of traversed paths
Browse files Browse the repository at this point in the history
 - This leads to about a 5x speed up

Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 authored and ljharb committed Mar 30, 2022
1 parent d45fe21 commit 7cb6fcd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions src/rules/no-cycle.js
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -138,7 +138,11 @@ module.exports = {
}
}

return moduleVisitor(checkSourceValue, context.options[0]);
return Object.assign(moduleVisitor(checkSourceValue, context.options[0]), {
'Program:exit': () => {
traversed.clear();
},
});
},
};

Expand Down

0 comments on commit 7cb6fcd

Please sign in to comment.