Skip to content

Commit 7cb6fcd

Browse files
Nokel81ljharb
authored andcommitted
[Refactor] no-cycle: Add per-run caching of traversed paths
- This leads to about a 5x speed up Signed-off-by: Sebastian Malton <[email protected]>
1 parent d45fe21 commit 7cb6fcd

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2626
- [meta] replace git.io link in comments with the original URL ([#2444], thanks [@liby])
2727
- [Docs] remove global install in readme ([#2412], thanks [@aladdin-add])
2828
- [readme] clarify `eslint-import-resolver-typescript` usage ([#2503], thanks [@JounQin])
29+
- [Refactor] `no-cycle`: Add per-run caching of traversed paths ([#2419], thanks [@nokel81])
2930

3031
## [2.26.0] - 2022-04-05
3132

@@ -1004,6 +1005,7 @@ for info on changes for earlier releases.
10041005
[#2466]: https://github.com/import-js/eslint-plugin-import/pull/2466
10051006
[#2440]: https://github.com/import-js/eslint-plugin-import/pull/2440
10061007
[#2427]: https://github.com/import-js/eslint-plugin-import/pull/2427
1008+
[#2419]: https://github.com/import-js/eslint-plugin-import/pull/2419
10071009
[#2417]: https://github.com/import-js/eslint-plugin-import/pull/2417
10081010
[#2411]: https://github.com/import-js/eslint-plugin-import/pull/2411
10091011
[#2399]: https://github.com/import-js/eslint-plugin-import/pull/2399

src/rules/no-cycle.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { isExternalModule } from '../core/importType';
99
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
1010
import docsUrl from '../docsUrl';
1111

12-
// todo: cache cycles / deep relationships for faster repeat evaluation
12+
const traversed = new Set();
13+
1314
module.exports = {
1415
meta: {
1516
type: 'suggestion',
@@ -87,7 +88,6 @@ module.exports = {
8788
}
8889

8990
const untraversed = [{ mget: () => imported, route:[] }];
90-
const traversed = new Set();
9191
function detectCycle({ mget, route }) {
9292
const m = mget();
9393
if (m == null) return;
@@ -101,7 +101,7 @@ module.exports = {
101101
// Ignore only type imports
102102
!isOnlyImportingTypes,
103103
);
104-
104+
105105
/*
106106
If cyclic dependency is allowed via dynamic import, skip checking if any module is imported dynamically
107107
*/
@@ -138,7 +138,11 @@ module.exports = {
138138
}
139139
}
140140

141-
return moduleVisitor(checkSourceValue, context.options[0]);
141+
return Object.assign(moduleVisitor(checkSourceValue, context.options[0]), {
142+
'Program:exit': () => {
143+
traversed.clear();
144+
},
145+
});
142146
},
143147
};
144148

0 commit comments

Comments
 (0)