Skip to content

Commit

Permalink
asciiDoctorIntegrated
Browse files Browse the repository at this point in the history
  • Loading branch information
Javadyakuza committed Aug 6, 2023
1 parent a046ecf commit a827192
Show file tree
Hide file tree
Showing 9 changed files with 1,295 additions and 4,048 deletions.
5,096 changes: 1,123 additions & 3,973 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"test": "ava"
},
"dependencies": {
"asciidoctor": "^3.0.2",
"handlebars": "^4.7.7",
"solidity-ast": "^0.4.38"
},
Expand Down
74 changes: 37 additions & 37 deletions src/ast-builder/ast-updater.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { SourceUnit } from 'solidity-ast';
import { SolcOutput } from 'solidity-ast/solc';
import { findAll, isNodeType } from 'solidity-ast/utils';
import { Config } from '../config';
import { SourceUnit } from "solidity-ast";
import { SolcOutput } from "solidity-ast/solc";
import { findAll, isNodeType } from "solidity-ast/utils";
import { Config } from "../config";
import {
getDependenciesCount,
getParentAstFromContractId,
getParentAstFromName,
} from './getters';
import { FullSources } from './types';
} from "./getters";
import { FullSources } from "./types";

export const resetIds = (ast: any, idCounter: number = 0): number => {
// Recursively visit child nodes and update their ID's
for (const key in ast) {
if (typeof ast[key] === 'object' && ast[key] !== null) {
if (key === 'body') {
if (ast.hasOwnProperty('id') && ast.id === undefined) {
if (typeof ast[key] === "object" && ast[key] !== null) {
if (key === "body") {
if (ast.hasOwnProperty("id") && ast.id === undefined) {
idCounter = resetIds(ast[key], idCounter);
ast.id = idCounter;
idCounter++;
} else {
idCounter = resetIds(ast[key], idCounter);
}
} else {
if (ast.hasOwnProperty('id') && ast.id === undefined) {
if (ast.hasOwnProperty("id") && ast.id === undefined) {
idCounter = resetIds(ast[key], idCounter);
ast.id = idCounter;
idCounter++;
Expand All @@ -34,14 +34,14 @@ export const resetIds = (ast: any, idCounter: number = 0): number => {
}
// Update references to new ID's
for (const key in ast) {
if (typeof ast[key] === 'number' && key === 'id') {
if (typeof ast[key] === "number" && key === "id") {
if (ast[key] < idCounter) {
ast[key] = idCounter;
++idCounter;
}
}
}
if (ast.hasOwnProperty('exportedSymbols')) {
if (ast.hasOwnProperty("exportedSymbols")) {
const name = Object.keys(ast.exportedSymbols)[0]!;
ast.exportedSymbols[name] = [ast.id - 1];
}
Expand All @@ -58,12 +58,12 @@ const assignIds = (node: any, id: number) => {
};

export const replaceSrc = (obj: any, id: number) => {
if (!obj || typeof obj !== 'object') {
if (!obj || typeof obj !== "object") {
return;
}

if (obj.hasOwnProperty('src')) {
const splitedSrc = obj.src.split(':');
if (obj.hasOwnProperty("src")) {
const splitedSrc = obj.src.split(":");
obj.src = `${splitedSrc[0]}:${splitedSrc[1]}:${id}`;
}

Expand All @@ -76,15 +76,15 @@ export const replaceSrc = (obj: any, id: number) => {

export function updateDependices(
origSources: FullSources,
sources: SolcOutput['sources'],
config: Config,
sources: SolcOutput["sources"],
config: Config
) {
for (const contract of Object.keys(sources)) {
const ast = sources[contract]!.ast;
const origSource = origSources[contract]!;
updateImports(ast, sources, config.root!);
updateReferences(ast, sources, origSource.asts);
for (const contractDef of findAll('ContractDefinition', ast)) {
for (const contractDef of findAll("ContractDefinition", ast)) {
const dependencies = contractDef.contractDependencies;
ast.exportedSymbols[contractDef.name] = [contractDef.id];
if (dependencies.length === 0) {
Expand All @@ -96,21 +96,21 @@ export function updateDependices(
ast,
sources,
origSource.asts,
dependencies,
dependencies
);
const uniqParentIds = [...new Set(parentIds)];
contractDef.contractDependencies = uniqParentIds;
contractDef.linearizedBaseContracts = [contractDef.id].concat(
uniqParentIds,
uniqParentIds
);
}
}
}
const updateFunctions = (
ast: SourceUnit,
sources: SolcOutput['sources'],
sources: SolcOutput["sources"],
origAsts: SourceUnit[],
dependencies: number[],
dependencies: number[]
) => {
let parentIds: number[] = [];

Expand All @@ -120,15 +120,15 @@ const updateFunctions = (
if (origParentAst) {
const parentAst = sources[origParentAst.absolutePath]!.ast;

for (const func of findAll('FunctionDefinition', ast)) {
for (const func of findAll("FunctionDefinition", ast)) {
if (func.baseFunctions) {
const baseFuncId = func.baseFunctions[0]!;
let funcSrc: string | undefined;
let parentId: number | undefined;

for (const node of origParentAst.nodes) {
if (
isNodeType('FunctionDefinition', node) &&
isNodeType("FunctionDefinition", node) &&
node.id === baseFuncId
) {
funcSrc = node.src;
Expand All @@ -139,12 +139,12 @@ const updateFunctions = (
if (funcSrc) {
for (const node of parentAst.nodes) {
if (
isNodeType('FunctionDefinition', node) &&
isNodeType("FunctionDefinition", node) &&
node.src.slice(0, -1) == funcSrc!.slice(0, -1)
) {
func.baseFunctions = [node.id];
parentId = Object.values(parentAst.exportedSymbols).find(
id => id![0] === node.id,
(id) => id![0] === node.id
)![0];
break;
}
Expand All @@ -164,10 +164,10 @@ const updateFunctions = (

const updateImports = (
ast: SourceUnit,
sources: SolcOutput['sources'],
rootPath: string,
sources: SolcOutput["sources"],
rootPath: string
) => {
for (const imp of findAll('ImportDirective', ast)) {
for (const imp of findAll("ImportDirective", ast)) {
let absolutePath = imp.absolutePath;
if (imp.absolutePath.startsWith(rootPath)) {
absolutePath = imp.absolutePath.slice(rootPath.length + 1);
Expand All @@ -184,11 +184,11 @@ const updateImports = (

const updateReferences = (
ast: SourceUnit,
sources: SolcOutput['sources'],
origAsts: SourceUnit[],
sources: SolcOutput["sources"],
origAsts: SourceUnit[]
) => {
for (const imp of findAll('InheritanceSpecifier', ast)) {
if (!isNodeType('UserDefinedTypeName', imp.baseName)) {
for (const imp of findAll("InheritanceSpecifier", ast)) {
if (!isNodeType("UserDefinedTypeName", imp.baseName)) {
continue;
}
const baseName = imp.baseName.name!;
Expand All @@ -199,7 +199,7 @@ const updateReferences = (

imp.baseName.referencedDeclaration = realParentId;
const splitedtypeIdentifier =
imp.baseName.typeDescriptions.typeIdentifier!.split('$');
imp.baseName.typeDescriptions.typeIdentifier!.split("$");
imp.baseName.typeDescriptions.typeIdentifier = `${splitedtypeIdentifier[0]}${splitedtypeIdentifier[1]}$${realParentId}`;
}
};
Expand All @@ -219,15 +219,15 @@ export const renameAbsolutePaths = (root: string, asts: SourceUnit[]) => {

export function sortASTByDependency(
sources: SourceUnit[][],
solcOutput: SolcOutput,
solcOutput: SolcOutput
) {
// Count the number of times each contract is inherited
const dependencyCount: Map<string, number> = getDependenciesCount(sources);

const sortedDependencyCount = Array.from(dependencyCount).sort(
(a, b) => b[1] - a[1],
(a, b) => b[1] - a[1]
);
const sortedSources: SolcOutput['sources'] = {};
const sortedSources: SolcOutput["sources"] = {};
let id = 0;
let lastAstId = 0;
sortedDependencyCount.reverse().forEach(([path]) => {
Expand Down
32 changes: 16 additions & 16 deletions src/ast-builder/builder.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import fs from 'fs';
import { SolcInput, SolcOutput } from 'solidity-ast/solc';
import { Sources } from './types';
import { getContractName, getAstsFromSources } from './getters';
import fs from "fs";
import { SolcInput, SolcOutput } from "solidity-ast/solc";
import { Sources } from "./types";
import { getContractName, getAstsFromSources } from "./getters";
import {
replaceSrc,
sortASTByDependency,
updateDependices,
} from './ast-updater';
import { Config } from '../config';
import { Build } from '../site';
import path from 'path';
} from "./ast-updater";
import { Config } from "../config";
import { Build } from "../site";
import path from "path";

import { compileAst } from './compile-ast';
import { compileAst } from "./compile-ast";

const createRawOutput = (sources: Sources) => {
const output: SolcOutput = { sources: {} };
Expand All @@ -28,7 +28,7 @@ const createInput = (solcOutput: SolcOutput) => {
const sources = solcOutput.sources;
const SolcInput: SolcInput = { sources: {} };
for (const key of Object.keys(sources)) {
const fileContent = fs.readFileSync(key, 'utf8').toString();
const fileContent = fs.readFileSync(key, "utf8").toString();
SolcInput.sources[key] = { content: fileContent };
}
return SolcInput;
Expand All @@ -37,7 +37,7 @@ const createInput = (solcOutput: SolcOutput) => {
export function isMainContract(absolutePath: string, astPath: string) {
// Extract the contract name from absolute path
if (!fileExists(absolutePath) || !fileExists(astPath)) {
throw new Error('File does not exist');
throw new Error("File does not exist");
}
const contractName = getContractName(absolutePath);

Expand All @@ -52,7 +52,7 @@ function fileExists(filePath: string) {
try {
return fs.statSync(filePath).isFile();
} catch (err: any) {
if (err.code === 'ENOENT') {
if (err.code === "ENOENT") {
return false;
} else {
throw err;
Expand All @@ -62,19 +62,19 @@ function fileExists(filePath: string) {

export const makeBuild = async (
config: Config,
contractList: string[] = [],
contractList: string[] = []
) => {
compileAst(config);
const { sources: astSources, fullSources } = getAstsFromSources(
config.astOutputDir!,
config.root!,
config.root!
)!;
const solcOutput = createRawOutput(astSources);
const sourcesList = Object.values(fullSources).map(source => source.asts);
const sourcesList = Object.values(fullSources).map((source) => source.asts);

const sortedSources = sortASTByDependency(sourcesList, solcOutput);
updateDependices(fullSources, sortedSources, config);
const ph = path.join(config.root!, 'build/astBuild.json');
const ph = path.join(config.root!, "build/astBuild.json");
fs.writeFileSync(ph, JSON.stringify(sortedSources, null, 2));
solcOutput.sources = sortedSources;
const solcInput = createInput(solcOutput);
Expand Down
Loading

0 comments on commit a827192

Please sign in to comment.