Skip to content

Commit

Permalink
[Refactor] no-cycle: Add caching of traversed paths to no-cycle
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 84008f1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Expand Up @@ -20,12 +20,13 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`order`]: leave more space in rankings for consecutive path groups ([#2506], thanks [@Pearce-Ropion])

### Changed
- [Tests] `named`: Run all TypeScript test ([#2427], thanks [@ProdigySim])
- [Tests] [`named`]: Run all TypeScript test ([#2427], thanks [@ProdigySim])
- [readme] note use of typescript in readme `import/extensions` section ([#2440], thanks [@OutdatedVersion])
- [Docs] `order`: use correct default value ([#2392], thanks [@hyperupcall])
- [Docs] [`order`]: use correct default value ([#2392], thanks [@hyperupcall])
- [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`]: Improve performance by using caching ([#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
7 changes: 4 additions & 3 deletions src/rules/no-cycle.js
Expand Up @@ -9,6 +9,8 @@ import { isExternalModule } from '../core/importType';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
import docsUrl from '../docsUrl';

const traversed = new Set();

// todo: cache cycles / deep relationships for faster repeat evaluation
module.exports = {
meta: {
Expand Down Expand Up @@ -40,8 +42,8 @@ module.exports = {
},
})],
},

create(context) {
_traversed: traversed,
create: (context) => {
const myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
if (myPath === '<text>') return {}; // can't cycle-check a non-file

Expand Down Expand Up @@ -87,7 +89,6 @@ module.exports = {
}

const untraversed = [{ mget: () => imported, route:[] }];
const traversed = new Set();
function detectCycle({ mget, route }) {
const m = mget();
if (m == null) return;
Expand Down
5 changes: 5 additions & 0 deletions tests/src/rules/no-cycle.js
Expand Up @@ -2,6 +2,7 @@ import { parsers, test as _test, testFilePath, testVersion as _testVersion } fro

import { RuleTester } from 'eslint';
import flatMap from 'array.prototype.flatmap';
import { beforeEach } from 'mocha';

const ruleTester = new RuleTester();
const rule = require('rules/no-cycle');
Expand All @@ -17,6 +18,10 @@ const testVersion = (specifier, t) => _testVersion(specifier, () => Object.assig

const testDialects = ['es6'];

beforeEach(() => {
rule._traversed.clear();
});

ruleTester.run('no-cycle', rule, {
valid: [].concat(
// this rule doesn't care if the cycle length is 0
Expand Down

0 comments on commit 84008f1

Please sign in to comment.