From 0bf41c2af1dbb5170f75fe90e950dcb3dd534545 Mon Sep 17 00:00:00 2001 From: Ivan Rubinson Date: Mon, 26 Aug 2024 18:59:38 +0300 Subject: [PATCH] add option to skip scc --- src/rules/no-cycle.js | 7 ++++++- tests/src/rules/no-cycle.js | 28 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/rules/no-cycle.js b/src/rules/no-cycle.js index c8cf17cf1e..e3a9166d16 100644 --- a/src/rules/no-cycle.js +++ b/src/rules/no-cycle.js @@ -48,6 +48,11 @@ module.exports = { type: 'boolean', default: false, }, + disableScc: { + description: 'When true, don\'t calculate a strongly-connected-components graph. SCC is used to reduce the time-complexity of cycle detection, but adds overhead.', + type: 'boolean', + default: false, + }, })], }, @@ -63,7 +68,7 @@ module.exports = { context, ); - const scc = StronglyConnectedComponentsBuilder.get(myPath, context); + const scc = options.disableScc ? {} : StronglyConnectedComponentsBuilder.get(myPath, context); function checkSourceValue(sourceNode, importer) { if (ignoreModule(sourceNode.value)) { diff --git a/tests/src/rules/no-cycle.js b/tests/src/rules/no-cycle.js index d2adbf61f9..efc0fb6eb9 100644 --- a/tests/src/rules/no-cycle.js +++ b/tests/src/rules/no-cycle.js @@ -17,7 +17,7 @@ const testVersion = (specifier, t) => _testVersion(specifier, () => Object.assig const testDialects = ['es6']; -ruleTester.run('no-cycle', rule, { +const cases = { valid: [].concat( // this rule doesn't care if the cycle length is 0 test({ code: 'import foo from "./foo.js"' }), @@ -290,4 +290,30 @@ ruleTester.run('no-cycle', rule, { ], }), ), +}; + +ruleTester.run('no-cycle', rule, { + valid: flatMap(cases.valid, (testCase) => [ + testCase, + { + ...testCase, + code: `${testCase.code} // disableScc=true`, + options: [{ + ...testCase.options && testCase.options[0] || {}, + disableScc: true, + }], + }, + ]), + + invalid: flatMap(cases.invalid, (testCase) => [ + testCase, + { + ...testCase, + code: `${testCase.code} // disableScc=true`, + options: [{ + ...testCase.options && testCase.options[0] || {}, + disableScc: true, + }], + }, + ]), });