pull/3542/head
Defi Boy 5 years ago committed by Joseph Izang
parent ef8eed76ec
commit 01b51a1bf3
  1. 2
      src/App.tsx
  2. 7
      src/AppContext.tsx
  3. 49
      src/utils/template.ts
  4. 157
      src/utils/utils.test.ts
  5. 17
      src/utils/utils.ts
  6. 5
      src/views/ErrorView.tsx

@ -47,7 +47,7 @@ const App = () => {
await client.onload(); await client.onload();
setClientInstance(client); setClientInstance(client);
console.log("Remix EthDoc Plugin has been loaded"); console.log("Remix EthDoc Plugin has been loaded");
await client.call('manager' as any, 'activatePlugin', "ethdoc-viewer") await client.call("manager" as any, "activatePlugin", "ethdoc-viewer");
client.solidity.on( client.solidity.on(
"compilationFinished", "compilationFinished",
( (

@ -1,10 +1,5 @@
import React from "react"; import React from "react";
import { import { PluginApi, IRemixApi, Api, PluginClient } from "@remixproject/plugin";
PluginApi,
IRemixApi,
Api,
PluginClient
} from "@remixproject/plugin";
import { ContractName, Documentation, PublishedSite } from "./types"; import { ContractName, Documentation, PublishedSite } from "./types";

@ -23,7 +23,7 @@ export const htmlTemplate = (content: HTMLContent) => {
${content} ${content}
</body> </body>
</html> </html>
` `;
}; };
export const template = ( export const template = (
@ -54,14 +54,14 @@ export const template = (
<div id="ethdoc-viewer"> <div id="ethdoc-viewer">
${ ${
functions.length === 0 functions.length === 0
? "No contract to display" ? "No contract to display"
: renderHeader(name, contractDoc) : renderHeader(name, contractDoc)
} }
${functions ${functions
.map( .map(
(item) => ` (item) => `
<h6>${item.name} - ${item.type}</h6> <h6>${item.name} - ${item.type}</h6>
<hr> <hr>
${renderParameterDocumentation(item.inputs)} ${renderParameterDocumentation(item.inputs)}
@ -72,8 +72,8 @@ export const template = (
${renderParameterDocumentation(item.outputs)} ${renderParameterDocumentation(item.outputs)}
` `
) )
.join("\n")} .join("\n")}
</div> </div>
`; `;
@ -82,7 +82,7 @@ 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) => notice ? `<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()
}; };
@ -93,7 +93,7 @@ export const renderHeader = (
) => ` ) => `
<h3>${name} ${ <h3>${name} ${
contractDoc.title ? `<small>: ${contractDoc.title}</small>` : "" contractDoc.title ? `<small>: ${contractDoc.title}</small>` : ""
}</h3> }</h3>
${contractDoc.notice ? `<p class="lead">${contractDoc.notice}</p>` : ""} ${contractDoc.notice ? `<p class="lead">${contractDoc.notice}</p>` : ""}
@ -104,9 +104,10 @@ export const renderHeader = (
export const renderParameterDocumentation = ( export const renderParameterDocumentation = (
parameters: ParameterDocumentation[] parameters: ParameterDocumentation[]
) => `${ ) =>
parameters.length > 0 `${
? `<table class="table table-sm table-bordered table-striped"> parameters.length > 0
? `<table class="table table-sm table-bordered table-striped">
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
@ -115,25 +116,27 @@ export const renderParameterDocumentation = (
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
${parameters.map( ${parameters
(output) => `<tr> .map(
(output) => `<tr>
<td>${output.name}</td> <td>${output.name}</td>
<td>${output.type}</td> <td>${output.type}</td>
<td>${output.description}</td> <td>${output.description}</td>
</tr>` </tr>`
).join("")} )
.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
? "<p><strong>**Add Documentation for the method here**</strong></p>" ? "<p><strong>**Add Documentation for the method here**</strong></p>"
: Object.keys(devMethod) : Object.keys(devMethod)
.filter((key) => key !== "params") .filter((key) => key !== "params")
.map((key) => { .map((key) => {
return (devMethodDocTemplate as any)[key]((devMethod as any)[key]); return (devMethodDocTemplate as any)[key]((devMethod as any)[key]);
}) })
.join("\n"); .join("\n");
}; };

@ -48,92 +48,89 @@ describe("Publisher tests", () => {
expect(template).toBeDefined(); expect(template).toBeDefined();
}); });
test('getContractDoc', () => { test("getContractDoc", () => {
const template = getContractDoc( const template = getContractDoc("Owner", {
'Owner', ...buildFakeArtifactWithComments(),
{ abi: [
...buildFakeArtifactWithComments(), {
abi: [ inputs: [],
{ stateMutability: "nonpayable",
"inputs": [], type: "constructor",
"stateMutability": "nonpayable", },
"type": "constructor" {
}, anonymous: false,
{ inputs: [
"anonymous": false, {
"inputs": [ indexed: true,
{ internalType: "address",
"indexed": true, name: "oldOwner",
"internalType": "address", type: "address",
"name": "oldOwner", },
"type": "address" {
}, indexed: true,
{ internalType: "address",
"indexed": true, name: "newOwner",
"internalType": "address", type: "address",
"name": "newOwner", },
"type": "address" ],
} name: "OwnerSet",
], type: "event",
"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: {
"inputs": [ details: "Set contract deployer as owner",
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "changeOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}, },
{ "getOwner()": {
"inputs": [], details: "Return owner address ",
"name": "getOwner", returns: {
"outputs": [ _0: "address of owner",
{
"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, title: "Owner",
userdoc: { } as any,
"methods": {} userdoc: {
} as any methods: {},
} } as any,
) });
expect(template).toBeDefined(); expect(template).toBeDefined();
}) });
}); });
describe("getFunctionDocumentation", () => { describe("getFunctionDocumentation", () => {

@ -3,14 +3,10 @@ import {
CompiledContract, CompiledContract,
FunctionDescription, FunctionDescription,
ABIParameter, ABIParameter,
ABIDescription ABIDescription,
} from "@remixproject/plugin"; } from "@remixproject/plugin";
import { import { FileName, Documentation, ContractName } from "../types";
FileName,
Documentation,
ContractName,
} from "../types";
import { template } from "./template"; import { template } from "./template";
import { import {
ContractDocumentation, ContractDocumentation,
@ -42,8 +38,8 @@ export const getContractDoc = (name: string, contract: CompiledContract) => {
const contractDoc: ContractDocumentation = getContractDocumentation(contract); const contractDoc: ContractDocumentation = getContractDocumentation(contract);
const onlyFunctions = contract.abi.filter((item) => { const onlyFunctions = contract.abi.filter((item) => {
return item.type !== "event" return item.type !== "event";
}) });
const functionsDocumentation = onlyFunctions.map((def: ABIDescription) => { const functionsDocumentation = onlyFunctions.map((def: ABIDescription) => {
if (def.type === "constructor") { if (def.type === "constructor") {
@ -51,7 +47,10 @@ export const getContractDoc = (name: string, contract: CompiledContract) => {
// 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: Object.keys(contract.userdoc.methods['constructor']).length > 0 ? 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 {

@ -19,7 +19,10 @@ export const ErrorView: React.FC = () => {
<h5>Sorry, something unexpected happened. </h5> <h5>Sorry, something unexpected happened. </h5>
<h5> <h5>
Please raise an issue:{" "} Please raise an issue:{" "}
<a style={{ color: "red" }} href="https://github.com/Machinalabs/remix-ethdoc-plugin/issues"> <a
style={{ color: "red" }}
href="https://github.com/Machinalabs/remix-ethdoc-plugin/issues"
>
Here Here
</a> </a>
</h5> </h5>

Loading…
Cancel
Save