Skip to content
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

[Refactor] no-cycle: Add per-run caching of traversed paths #2419

Merged
merged 1 commit into from
Aug 23, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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