From 0a604ef39628a94e77ca9ec0920a300efaa85aac Mon Sep 17 00:00:00 2001 From: Solid Studio Date: Fri, 10 Jul 2020 10:21:43 +0100 Subject: [PATCH] fix rendering and document generation --- src/utils/template.ts | 73 ++++++++++++++------------------- src/utils/utils.test.ts | 89 ++++++++++++++++++++++++++++++++++++++++- src/utils/utils.ts | 8 +++- 3 files changed, 123 insertions(+), 47 deletions(-) diff --git a/src/utils/template.ts b/src/utils/template.ts index c0682844b8..a8c3b24d0b 100644 --- a/src/utils/template.ts +++ b/src/utils/template.ts @@ -7,22 +7,24 @@ import { } from "./types"; type HTMLContent = string; -export const htmlTemplate = (content: HTMLContent) => ` - - - - - - - - - ${content} - - -`; +export const htmlTemplate = (content: HTMLContent) => { + return ` + + + + + + + + + ${content} + + +` +}; export const template = ( name: string, @@ -69,7 +71,6 @@ export const template = (

Returns:

${renderParameterDocumentation(item.outputs)} - ` ) .join("\n")} @@ -77,22 +78,11 @@ export const template = ( `; -// const contractDocTemplate: TemplateDoc = { -// author: (author: string) => '',//`Author: ${author}`, -// details: (details: string) => `

${details}

`, -// title: (title: string) => { -// return title ? -// `${title}` -// : '' -// }, -// notice: (notice: string) => `

${notice}

`, -// methods: () => '' // Methods is managed by getMethod() -// } const devMethodDocTemplate: Partial> = { author: (author: string) => `

Created By ${author}

`, details: (details: string) => `

${details}

`, return: (value: string) => `

Return : ${value}

`, - notice: (notice: string) => `

${notice}

`, + notice: (notice: string) => notice ? `

${notice}

` : '', // returns: () => '', // Implemented by getParams() params: () => "", // Implemented by getParams() }; @@ -114,11 +104,9 @@ export const renderHeader = ( export const renderParameterDocumentation = ( parameters: ParameterDocumentation[] -) => ` - ${ +) => `${ parameters.length > 0 - ? ` - + ? `
@@ -127,18 +115,17 @@ export const renderParameterDocumentation = ( - ${parameters.map( + ${parameters.map( (output) => ` - - - - ` - )} - -
Name
${output.name}${output.type}${output.description}
` + ${output.name} + ${output.type} + ${output.description} + ` + ).join("")} + + ` : "

No parameters

" - } -`; + }`; export const getMethodDetails = (devMethod?: Partial) => { return !devMethod diff --git a/src/utils/utils.test.ts b/src/utils/utils.test.ts index 3576079dcb..399a002a1b 100644 --- a/src/utils/utils.test.ts +++ b/src/utils/utils.test.ts @@ -45,10 +45,95 @@ describe("Publisher tests", () => { buildFakeArtifactWithComments() ); - console.log("Template", template); - 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", () => { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 948e1e557e..3e02811e46 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -41,13 +41,17 @@ export const createDocumentation = ( export const getContractDoc = (name: string, contract: CompiledContract) => { 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") { def.name = "constructor"; // because "constructor" is a string and not a { notice } object for userdoc we need to do that const methodDoc = { ...(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); } else {