Skip to content

Commit 6d8b6be

Browse files
committed
Add tooling script to generate Sonar rules file
1 parent 6d9a722 commit 6d8b6be

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
*-report.json
77
.nyc_output
88
coverage
9+
generated-rules.json

eslint-plugin/lib/rules/no-multiple-access-dom-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/** @type {import("eslint").Rule.RuleModule} */
2020
module.exports = {
2121
meta: {
22-
type: "problem",
22+
type: "suggestion",
2323
docs: {
2424
description: "Disallow multiple access of same DOM element.",
2525
category: "eco-design",

eslint-plugin/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"main": "./lib/index.js",
1919
"exports": "./lib/index.js",
2020
"scripts": {
21+
"generate-sonar-rules": "node tools/generate-sonar-rules.js",
2122
"lint": "npm-run-all \"lint:*\"",
2223
"lint:eslint-docs": "eslint-doc-generator --check",
2324
"lint:js": "eslint .",
@@ -32,6 +33,7 @@
3233
"eslint-plugin-eslint-plugin": "^5.0.0",
3334
"eslint-plugin-node": "^11.1.0",
3435
"eslint-plugin-prettier": "^4.2.1",
36+
"marked": "^4.2.12",
3537
"mocha": "^10.0.0",
3638
"npm-run-all": "^4.1.5",
3739
"nyc": "^15.1.0",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (C) 2023 Green Code Initiative
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/**
19+
* @fileoverview Generates the rules file to be imported into Sonar
20+
* @author Green Code Initiative
21+
*/
22+
"use strict";
23+
24+
const { readFileSync, writeFileSync } = require("fs");
25+
// eslint-disable-next-line node/no-unpublished-require
26+
const { marked } = require("marked");
27+
const { rules, configs } = require("../lib/index");
28+
29+
const DEFAULT_SEVERITY = "MINOR";
30+
const DEFAULT_CONSTANT_DEBT_MINUTES = 5;
31+
32+
const DOCUMENTATION_PATH = "docs/rules/";
33+
const DOCUMENTATION_SEPARATOR = "<!-- end auto-generated rule header -->";
34+
const OUTPUT_FILE = `${__dirname}/generated-rules.json`;
35+
const PLUGIN_NAME = configs.recommended.plugins[0];
36+
const REPO_BLOB_URL =
37+
"https://github.com/green-code-initiative/ecoCode-linter/blob/main/eslint-plugin/";
38+
39+
/**
40+
* Retrieves documentation of a rule from its Markdown file.
41+
* Also converts Markdown to HTML to display it in SonarQube interface.
42+
*
43+
* @param ruleName name of rule to retrieve documentation
44+
*/
45+
const retrieveDocumentation = (ruleName) => {
46+
const markdownDoc = readFileSync(
47+
`${__dirname}/../${DOCUMENTATION_PATH}${ruleName}.md`,
48+
"utf8"
49+
);
50+
const htmlDoc = marked.parse(markdownDoc);
51+
const htmlParts = htmlDoc.split(DOCUMENTATION_SEPARATOR);
52+
const content = htmlParts.length > 1 ? htmlParts[1] : htmlParts[0];
53+
const livingDocUrl = REPO_BLOB_URL + DOCUMENTATION_PATH + ruleName + ".md";
54+
return `${content}<br/><a href="${livingDocUrl}">Click here to access the rule details.</a>`;
55+
};
56+
57+
// Browse all exported rules to create their Sonar equivalent.
58+
const sonarRules = Object.entries(rules).map(([ruleName, rule]) => ({
59+
key: `${PLUGIN_NAME}/${ruleName}`,
60+
type: "CODE_SMELL",
61+
name: rule.meta.docs.description.replace(/.$/, ""),
62+
description: retrieveDocumentation(ruleName),
63+
constantDebtMinutes:
64+
rule.meta.docs.constantDebtMinutes != null
65+
? rule.meta.docs.constantDebtMinutes
66+
: DEFAULT_CONSTANT_DEBT_MINUTES,
67+
severity:
68+
rule.meta.docs.severity != null
69+
? rule.meta.docs.severity
70+
: DEFAULT_SEVERITY,
71+
tags: [rule.meta.docs.category],
72+
}));
73+
74+
writeFileSync(OUTPUT_FILE, JSON.stringify(sonarRules, null, 2), {
75+
encoding: "utf8",
76+
});
77+
78+
console.log(`Generated ${sonarRules.length} rules in ${OUTPUT_FILE}.`);

eslint-plugin/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,11 @@ markdown-table@^3.0.3:
18351835
resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-3.0.3.tgz#e6331d30e493127e031dd385488b5bd326e4a6bd"
18361836
integrity sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==
18371837

1838+
marked@^4.2.12:
1839+
version "4.2.12"
1840+
resolved "https://registry.yarnpkg.com/marked/-/marked-4.2.12.tgz#d69a64e21d71b06250da995dcd065c11083bebb5"
1841+
integrity sha512-yr8hSKa3Fv4D3jdZmtMMPghgVt6TWbk86WQaWhDloQjRSQhMMYCAro7jP7VDJrjjdV8pxVxMssXS8B8Y5DZ5aw==
1842+
18381843
memorystream@^0.3.1:
18391844
version "0.3.1"
18401845
resolved "https://registry.yarnpkg.com/memorystream/-/memorystream-0.3.1.tgz#86d7090b30ce455d63fbae12dda51a47ddcaf9b2"

0 commit comments

Comments
 (0)