pull/3542/head
Defi Boy 4 years ago committed by Joseph Izang
parent 46e70e1f3e
commit c4d02aac55
  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();
setClientInstance(client);
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(
"compilationFinished",
(

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

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

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

@ -3,14 +3,10 @@ import {
CompiledContract,
FunctionDescription,
ABIParameter,
ABIDescription
ABIDescription,
} from "@remixproject/plugin";
import {
FileName,
Documentation,
ContractName,
} from "../types";
import { FileName, Documentation, ContractName } from "../types";
import { template } from "./template";
import {
ContractDocumentation,
@ -42,8 +38,8 @@ export const getContractDoc = (name: string, contract: CompiledContract) => {
const contractDoc: ContractDocumentation = getContractDocumentation(contract);
const onlyFunctions = contract.abi.filter((item) => {
return item.type !== "event"
})
return item.type !== "event";
});
const functionsDocumentation = onlyFunctions.map((def: ABIDescription) => {
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
const methodDoc = {
...(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);
} else {

@ -19,7 +19,10 @@ export const ErrorView: React.FC = () => {
<h5>Sorry, something unexpected happened. </h5>
<h5>
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
</a>
</h5>

Loading…
Cancel
Save