Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon-azerty committed Nov 17, 2023
1 parent d727f2e commit fb14880
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
87 changes: 46 additions & 41 deletions doc/createMarkdownFilesFromJsonArray.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { promises as fs } from 'fs';

let filename = '';
import { dirname } from 'path';

async function readFileContent(filePath) {
try {
Expand All @@ -11,49 +10,55 @@ async function readFileContent(filePath) {
}
}

async function saveToFile(path, content) {
async function saveToFile(filepath, content) {
try {
await fs.writeFile(path, content, 'utf8');
const directory = dirname(filepath);
await fs.mkdir(directory, { recursive: true });
await fs.writeFile(filepath, content, 'utf8');
} catch (err) {
console.error('Error creating the file:', err);
}
}

function id(value) {
filename = value;
return "";
}

function code(value) {
const blockCodeDelimiter = "```";
const langage = 'solidity';
return `${blockCodeDelimiter}${langage}\n${value}\n\n${blockCodeDelimiter}\n`;
}

const balises = ["id", "code"];
const functions = [id, code];
const parseJson = (rule) => {
let content = "";

function parseJSON(obj, depth) {
let content = '';
for (const prop in obj) {
const value = obj[prop];

if (typeof value === 'object') {
content += `${'#'.repeat(depth + 1)} ${prop}\n\n`;
content += parseJSON(value, depth + 1);
continue;
}
let isText = true;
for (const elem in balises) {
if (prop === balises[elem]) {
isText = false;
content += functions[elem](value);
}
}
if (isText) {
content += `${prop}: ${value}\n\n`;
}
const categoryFormated = rule.category.replace("-", "_");
content += `![](https://img.shields.io/badge/${categoryFormated}-green)\t`;
content += `![](https://img.shields.io/badge/Default%20Severity-${rule.severity}-yellow)\n\n`;
content += "## Description\n";
content += `${rule.description}\n\n`;
content += "## Options\n";
content += "description | default\n";
content += "------------ | -------------\n";
for (const example of rule.options) {
content += `${example.description} | ${example.default}\n`;
}
content += "## Example Config\n";
if (rule.example_config !== "") {
content += "```json\n";
content += `${rule.example_config}\n`;
content += "```\n\n";
}
content += "## Examples\n";
content += "### Good\n";
for (const example of rule.examples.good) {
content += `### ${example.description}\n`;
content += "```solidity\n";
content += `${example.code}\n`;
content += "```\n\n";
}
content += "### Bad\n";
for (const example of rule.examples.bad) {
content += `### ${example.description}\n`;
content += "```solidity\n";
content += `${example.code}\n`;
content += "```\n\n";
}

content += "## References\n";
content += `* [Rule source](${rule.source_link})\n`;
content += `* [Test](${rule.test_link})\n`;
return content;
}

Expand All @@ -63,10 +68,10 @@ async function createMarkdownFilesFromJsonArray(path) {
let depth = 1;
for (const key in jsonContent) {
let content = '';
const value = jsonContent[key];
const body = parseJSON(value, depth);
content += `# ${filename}\n\n` + body;
saveToFile(`./${filename}.md`, content);
const rule = jsonContent[key];
const body = parseJson(rule, depth);
content += `# ${rule.id}\n\n` + body;
saveToFile(`./${rule.category}/${rule.id}.md`, content);
}
}

Expand Down
2 changes: 1 addition & 1 deletion doc/docTree.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
{
"id": "custom-errors",
"description": "Enforces the use of Custom Errors over Require and Revert statements",
"category": "best-practises",
"category": "best-practices",
"example_config": "",
"source_link": "https://github.com/astrodevs-labs/osmium/blob/dev/toolchains/solidity/core/crates/linter-lib/src/rules/best_practices/custom_errors.rs",
"test_link": "https://github.com/astrodevs-labs/osmium/tree/dev/toolchains/solidity/core/crates/linter-lib/testdata/CustomErrors",
Expand Down

0 comments on commit fb14880

Please sign in to comment.