|
| 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}.`); |
0 commit comments