Skip to content

Commit

Permalink
mergedToLatestComplierSupportV
Browse files Browse the repository at this point in the history
  • Loading branch information
Javadyakuza committed Aug 6, 2023
1 parent e424e7e commit 4f3dbe4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsolidity-docgen",
"version": "0.66.0-beta.01",
"name": "tsolidity-docgen-dev",
"version": "0.66.0-beta.04",
"description": "Fork from OpenZeppelin/solidity-docgen. Documentation generator for Ton-Solidity smart contracts.",
"repository": "github:cyace84/tsolidity-docgen",
"keywords": [
Expand Down
27 changes: 26 additions & 1 deletion src/ast-builder/compile-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import { execSync } from "child_process";
import { Config } from "../config";
import { getAstsFromSources, getContractsList } from "./getters";
import { basename, join, resolve } from "path";
import { basename, join, extname, resolve } from "path";

const createDirectoryIfNotExists = (dir: string) => {
if (!fs.existsSync(dir)) {
Expand Down Expand Up @@ -51,6 +51,7 @@ export const compileAst = async (config: Config) => {
deleteDirectoryIfExists(resolve(process.cwd(), astCachePath));
compileExternalAst(config);
renameAstFiles(astOutputPath);
wrapAstInArray(astOutputPath);
};

/**
Expand Down Expand Up @@ -104,3 +105,27 @@ const renameAstFiles = (dir: string) => {
}
});
};

const wrapAstInArray = (dir: string) => {
const files = fs.readdirSync(dir);

files.forEach((file) => {
if (extname(file) === ".json") {
const filePath = join(dir, file);
const content = fs.readFileSync(filePath, "utf8");
let jsonContent;

try {
jsonContent = JSON.parse(content);
} catch (error) {
console.error(`Error parsing file ${file}:`, error);
return;
}

if (!Array.isArray(jsonContent)) {
const wrappedContent = [jsonContent];
fs.writeFileSync(filePath, JSON.stringify(wrappedContent, null, 2));
}
}
});
};
50 changes: 25 additions & 25 deletions src/ast-builder/getters.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { isMainContract } from './builder';
import { SourceUnit } from 'solidity-ast';
import { isNodeType } from 'solidity-ast/utils';
import { FullSources, Sources } from './types';
import fs from 'fs';
import path from 'path';
import { renameAbsolutePaths } from './ast-updater';
import { isMainContract } from "./builder";
import { SourceUnit } from "solidity-ast";
import { isNodeType } from "solidity-ast/utils";
import { FullSources, Sources } from "./types";
import fs from "fs";
import path from "path";
import { renameAbsolutePaths } from "./ast-updater";

/**
* It takes a file path and returns the name of the contract
* @param {string} filePath - The path to the contract file.
* @returns The contract name
*/
export function getContractName(filePath: string): string {
const pathArray = filePath.split('/');
const contractName = pathArray[pathArray.length - 1]!.split('.')[0];
const pathArray = filePath.split("/");
const contractName = pathArray[pathArray.length - 1]!.split(".")[0];
if (!contractName) {
throw new Error('Contract name not found');
throw new Error("Contract name not found");
}
return contractName;
}
Expand All @@ -30,7 +30,7 @@ export function getContractName(filePath: string): string {
export const getMainAst = (
asts: SourceUnit[],
contractName: string,
astFullPath: string,
astFullPath: string
) => {
let mainContract = null;
for (const ast of asts) {
Expand All @@ -55,7 +55,7 @@ export const getMainAst = (
*/
export const getParentAstFromContractId = (
asts: SourceUnit[],
id: number,
id: number
): SourceUnit | undefined => {
for (const ast of asts) {
if (Object.values(ast.exportedSymbols).flat().includes(id)) {
Expand Down Expand Up @@ -87,17 +87,17 @@ export const getParentAstFromName = (asts: SourceUnit[], name: string) => {
*/
export const getDependenciesCount = (sources: SourceUnit[][]) => {
const dependencyCount = new Map<string, number>();
sources.forEach(source => {
sources.forEach((source) => {
for (const ast of source) {
const contractDef = ast.nodes.find(isNodeType('ContractDefinition'))!;
const contractDef = ast.nodes.find(isNodeType("ContractDefinition"))!;

const absolutePath = ast.absolutePath;
if (!dependencyCount.has(absolutePath)) {
dependencyCount.set(
absolutePath,
contractDef.contractDependencies
? contractDef.contractDependencies.length
: 0,
: 0
);
}
}
Expand All @@ -115,19 +115,19 @@ export const getDependenciesCount = (sources: SourceUnit[][]) => {
export const getAstsFromSources = (astDir: string, root: string) => {
const astSources = fs
.readdirSync(astDir)
.filter(file => file.endsWith('.ast.json'))
.map(file => path.join(root, astDir, file));
.filter((file) => file.endsWith(".ast.json"))
.map((file) => path.join(root, astDir, file));

const sources: Sources = {};
const fullSources: FullSources = {};
astSources.forEach(astSourceFullPath => {
const astContent: SourceUnit[] = [require(astSourceFullPath) as SourceUnit];
astSources.forEach((astSourceFullPath) => {
const astContent: SourceUnit[] = require(astSourceFullPath) as SourceUnit[];
const withNormalPathAsts = renameAbsolutePaths(root, astContent);
const astContractName = getContractName(astSourceFullPath)!;
const mainAst = getMainAst(
withNormalPathAsts,
astContractName,
astSourceFullPath,
astSourceFullPath
);
if (mainAst) {
sources[mainAst.absolutePath] = mainAst;
Expand All @@ -148,21 +148,21 @@ export const getAstsFromSources = (astDir: string, root: string) => {
*/
export const getContractsList = (
contractsDir: string,
exclude: string[] = [],
exclude: string[] = []
) => {
let contracts: string[] = [];

const searchForContracts = (dir: string) => {
fs.readdirSync(dir).forEach(file => {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory() && !exclude.includes(file)) {
searchForContracts(filePath);
} else if (file.endsWith('sol')) {
contracts.push(`${dir.slice(contractsDir.length) + '/'}${file}`);
} else if (file.endsWith("sol")) {
contracts.push(`${dir.slice(contractsDir.length) + "/"}${file}`);
}
});
};

searchForContracts(contractsDir);
return contracts.map(file => file.slice(0, file.length));
return contracts.map((file) => file.slice(0, file.length));
};

0 comments on commit 4f3dbe4

Please sign in to comment.