diff --git a/packages/jsts/tests/rules/index.test.ts b/packages/jsts/tests/rules/index.test.ts new file mode 100644 index 00000000000..50a65ec17dc --- /dev/null +++ b/packages/jsts/tests/rules/index.test.ts @@ -0,0 +1,43 @@ +/* + * SonarQube JavaScript Plugin + * Copyright (C) 2011-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import { rules as mapping } from '../../src/rules'; +import fs from 'fs'; +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 missing = []; + for (const sonarKey of sonarKeys) { + const { rule } = require(path.join(ruleFolder, sonarKey)); + if (!mappedRules.some(mapped => mapped === rule)) { + missing.push(sonarKey); + } + } + expect(missing).toHaveLength(0); + }); +});