fix rendering and document generation

pull/5370/head
Solid Studio 4 years ago committed by Joseph Izang
parent d44e290dfa
commit 0a604ef396
  1. 31
      src/utils/template.ts
  2. 89
      src/utils/utils.test.ts
  3. 8
      src/utils/utils.ts

@ -7,7 +7,8 @@ import {
} from "./types"; } from "./types";
type HTMLContent = string; type HTMLContent = string;
export const htmlTemplate = (content: HTMLContent) => ` export const htmlTemplate = (content: HTMLContent) => {
return `
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@ -22,7 +23,8 @@ export const htmlTemplate = (content: HTMLContent) => `
${content} ${content}
</body> </body>
</html> </html>
`; `
};
export const template = ( export const template = (
name: string, name: string,
@ -69,7 +71,6 @@ export const template = (
<p>Returns:</p> <p>Returns:</p>
${renderParameterDocumentation(item.outputs)} ${renderParameterDocumentation(item.outputs)}
` `
) )
.join("\n")} .join("\n")}
@ -77,22 +78,11 @@ export const template = (
</div> </div>
`; `;
// const contractDocTemplate: TemplateDoc<ContractDoc> = {
// author: (author: string) => '',//`Author: ${author}`,
// details: (details: string) => `<p class="lead text-muted">${details}</p>`,
// title: (title: string) => {
// return title ?
// `<small>${title}</small>`
// : ''
// },
// notice: (notice: string) => `<p class="lead text-muted">${notice}</p>`,
// methods: () => '' // Methods is managed by getMethod()
// }
const devMethodDocTemplate: Partial<TemplateDoc<MethodDoc>> = { const devMethodDocTemplate: Partial<TemplateDoc<MethodDoc>> = {
author: (author: string) => `<p>Created By ${author}</p>`, author: (author: string) => `<p>Created By ${author}</p>`,
details: (details: string) => `<p>${details}</p>`, details: (details: string) => `<p>${details}</p>`,
return: (value: string) => `<p>Return : ${value}</p>`, return: (value: string) => `<p>Return : ${value}</p>`,
notice: (notice: string) => `<p>${notice}</p>`, notice: (notice: string) => notice ? `<p>${notice}</p>` : '',
// returns: () => '', // Implemented by getParams() // returns: () => '', // Implemented by getParams()
params: () => "", // Implemented by getParams() params: () => "", // Implemented by getParams()
}; };
@ -114,11 +104,9 @@ export const renderHeader = (
export const renderParameterDocumentation = ( export const renderParameterDocumentation = (
parameters: ParameterDocumentation[] parameters: ParameterDocumentation[]
) => ` ) => `${
${
parameters.length > 0 parameters.length > 0
? ` ? `<table class="table table-sm table-bordered table-striped">
<table class="table table-sm table-bordered table-striped">
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
@ -133,12 +121,11 @@ export const renderParameterDocumentation = (
<td>${output.type}</td> <td>${output.type}</td>
<td>${output.description}</td> <td>${output.description}</td>
</tr>` </tr>`
)} ).join("")}
</tbody> </tbody>
</table>` </table>`
: "<p>No parameters</p>" : "<p>No parameters</p>"
} }`;
`;
export const getMethodDetails = (devMethod?: Partial<MethodDoc>) => { export const getMethodDetails = (devMethod?: Partial<MethodDoc>) => {
return !devMethod return !devMethod

@ -45,10 +45,95 @@ describe("Publisher tests", () => {
buildFakeArtifactWithComments() buildFakeArtifactWithComments()
); );
console.log("Template", template);
expect(template).toBeDefined(); expect(template).toBeDefined();
}); });
test('getContractDoc', () => {
const template = getContractDoc(
'Owner',
{
...buildFakeArtifactWithComments(),
abi: [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "oldOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerSet",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "changeOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getOwner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
] as any,
devdoc: {
"details": "Set & change owner",
"methods": {
"changeOwner(address)": {
"details": "Change owner",
"params": {
"newOwner": "address of new owner"
}
},
"constructor": {
"details": "Set contract deployer as owner"
},
"getOwner()": {
"details": "Return owner address ",
"returns": {
"_0": "address of owner"
}
}
},
"title": "Owner"
} as any,
userdoc: {
"methods": {}
} as any
}
)
expect(template).toBeDefined();
})
}); });
describe("getFunctionDocumentation", () => { describe("getFunctionDocumentation", () => {

@ -41,13 +41,17 @@ export const createDocumentation = (
export const getContractDoc = (name: string, contract: CompiledContract) => { export const getContractDoc = (name: string, contract: CompiledContract) => {
const contractDoc: ContractDocumentation = getContractDocumentation(contract); const contractDoc: ContractDocumentation = getContractDocumentation(contract);
const functionsDocumentation = contract.abi.map((def: ABIDescription) => { const onlyFunctions = contract.abi.filter((item) => {
return item.type !== "event"
})
const functionsDocumentation = onlyFunctions.map((def: ABIDescription) => {
if (def.type === "constructor") { if (def.type === "constructor") {
def.name = "constructor"; def.name = "constructor";
// because "constructor" is a string and not a { notice } object for userdoc we need to do that // because "constructor" is a string and not a { notice } object for userdoc we need to do that
const methodDoc = { const methodDoc = {
...(contract.devdoc.methods.constructor || {}), ...(contract.devdoc.methods.constructor || {}),
notice: contract.userdoc.methods.constructor as string, notice: Object.keys(contract.userdoc.methods['constructor']).length > 0 ? contract.userdoc.methods['constructor'] as string : "",
}; };
return getFunctionDocumentation(def, methodDoc); return getFunctionDocumentation(def, methodDoc);
} else { } else {

Loading…
Cancel
Save