Skip to content

Commit

Permalink
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 committed Mar 30, 2022
1 parent d8633c3 commit 2c84947
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/rules/no-cycle.js
Original file line number Diff line number Diff line change
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 All @@ -35,8 +37,9 @@ module.exports = {
},
})],
},

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

Expand Down Expand Up @@ -75,7 +78,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parsers, test as _test, testFilePath } from '../utils';

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 @@ -14,6 +15,10 @@ const test = def => _test(Object.assign(def, {

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 2c84947

Please sign in to comment.