Skip to content

Commit

Permalink
Improve the time complexity of the sanity check for mapped rules
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Nov 2, 2023
1 parent 4044b82 commit b228090
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions packages/jsts/tests/rules/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ import path from 'path';

describe('index', () => {
it('should map keys to rules definitions', () => {
// FIXME: This test runs with a time complexity of O(n^2) where n is the number of rules.
// This is because it iterates over all rules and checks if they are mapped. Once
// we use Sonar rule keys as ESLint rule keys, we can make this test O(n) just by
// checking if the rule key is in the mapping.
const ruleFolder = path.join(__dirname, '../../src/rules');
const sonarKeys = fs.readdirSync(ruleFolder).filter(name => /^S\d+/.test(name));
const mappedRules = Object.values(mapping);
const mappedRules = new Map(Object.values(mapping).map(rule => [rule, true]));
const missing = [];
for (const sonarKey of sonarKeys) {
const { rule } = require(path.join(ruleFolder, sonarKey));
if (!mappedRules.some(mapped => mapped === rule)) {
if (!mappedRules.has(rule)) {
missing.push(sonarKey);
}
}
Expand Down

0 comments on commit b228090

Please sign in to comment.