aboutsummaryrefslogtreecommitdiffstats
path: root/packages/react-docs/src/utils/doxity_utils.ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-03-16 00:57:27 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-03-16 00:57:27 +0800
commit76029cbf0915df36266bd5e51add07755297ddda (patch)
treee1692f8cc4ea4642292c61f65ba3911ded26de8e /packages/react-docs/src/utils/doxity_utils.ts
parentb9c1653c1cf6984d56b7825d8747b48d797fa39e (diff)
parent4a27a7dc581fc6c8a3d4e212ca3712c249a5b417 (diff)
downloaddexon-0x-contracts-76029cbf0915df36266bd5e51add07755297ddda.tar
dexon-0x-contracts-76029cbf0915df36266bd5e51add07755297ddda.tar.gz
dexon-0x-contracts-76029cbf0915df36266bd5e51add07755297ddda.tar.bz2
dexon-0x-contracts-76029cbf0915df36266bd5e51add07755297ddda.tar.lz
dexon-0x-contracts-76029cbf0915df36266bd5e51add07755297ddda.tar.xz
dexon-0x-contracts-76029cbf0915df36266bd5e51add07755297ddda.tar.zst
dexon-0x-contracts-76029cbf0915df36266bd5e51add07755297ddda.zip
Merge branch 'development' into feature/sra-report/collection-tests
* development: (97 commits) Keep console.log in monorepo-scripts Enable coverage for all other packages with tests Fix parallel coverage results reporting Fix linter issuesx Remove outdated comment Add script copying to build command Add postpublish script to sol-cov Move configuration into package.json configs section Transform input data before encoding for callAsync and getABIEncodedTransactionData Update CHANGELOGs Consolidate all console.log into the @0xproject/utils package Update coverage badge to show development coverage Configure post build hook Notify coveralls after all tasks have finished Address feedback Revert "Report all coverage reports together" Separate published packages and typescript typings on README Consolidate docs generation and uploading logic Use async/await instead of promise syntax Move changelog entry ...
Diffstat (limited to 'packages/react-docs/src/utils/doxity_utils.ts')
-rw-r--r--packages/react-docs/src/utils/doxity_utils.ts175
1 files changed, 175 insertions, 0 deletions
diff --git a/packages/react-docs/src/utils/doxity_utils.ts b/packages/react-docs/src/utils/doxity_utils.ts
new file mode 100644
index 000000000..26dea6966
--- /dev/null
+++ b/packages/react-docs/src/utils/doxity_utils.ts
@@ -0,0 +1,175 @@
+import * as _ from 'lodash';
+
+import {
+ AbiTypes,
+ DocAgnosticFormat,
+ DocSection,
+ DoxityAbiDoc,
+ DoxityContractObj,
+ DoxityDocObj,
+ DoxityInput,
+ EventArg,
+ Parameter,
+ Property,
+ SolidityMethod,
+ Type,
+ TypeDocTypes,
+} from '../types';
+
+export const doxityUtils = {
+ convertToDocAgnosticFormat(doxityDocObj: DoxityDocObj): DocAgnosticFormat {
+ const docAgnosticFormat: DocAgnosticFormat = {};
+ _.each(doxityDocObj, (doxityContractObj: DoxityContractObj, contractName: string) => {
+ const doxityConstructor = _.find(doxityContractObj.abiDocs, (abiDoc: DoxityAbiDoc) => {
+ return abiDoc.type === AbiTypes.Constructor;
+ });
+ const constructors = [];
+ if (!_.isUndefined(doxityConstructor)) {
+ const constructor = {
+ isConstructor: true,
+ name: doxityContractObj.name,
+ comment: doxityConstructor.details,
+ returnComment: doxityConstructor.return,
+ callPath: '',
+ parameters: this._convertParameters(doxityConstructor.inputs),
+ returnType: this._convertType(doxityContractObj.name),
+ };
+ constructors.push(constructor);
+ }
+
+ const doxityMethods: DoxityAbiDoc[] = _.filter<DoxityAbiDoc>(
+ doxityContractObj.abiDocs,
+ (abiDoc: DoxityAbiDoc) => {
+ return this._isMethod(abiDoc);
+ },
+ );
+ const methods: SolidityMethod[] = _.map<DoxityAbiDoc, SolidityMethod>(
+ doxityMethods,
+ (doxityMethod: DoxityAbiDoc) => {
+ const outputs = !_.isUndefined(doxityMethod.outputs) ? doxityMethod.outputs : [];
+ let returnTypeIfExists: Type;
+ if (outputs.length === 0) {
+ // no-op. It's already undefined
+ } else if (outputs.length === 1) {
+ const outputsType = outputs[0].type;
+ returnTypeIfExists = this._convertType(outputsType);
+ } else {
+ const outputsType = `[${_.map(outputs, output => output.type).join(', ')}]`;
+ returnTypeIfExists = this._convertType(outputsType);
+ }
+ // For ZRXToken, we want to convert it to zrxToken, rather then simply zRXToken
+ const callPath =
+ contractName !== 'ZRXToken'
+ ? `${contractName[0].toLowerCase()}${contractName.slice(1)}.`
+ : `${contractName.slice(0, 3).toLowerCase()}${contractName.slice(3)}.`;
+ const method = {
+ isConstructor: false,
+ isConstant: doxityMethod.constant,
+ isPayable: doxityMethod.payable,
+ name: doxityMethod.name,
+ comment: doxityMethod.details,
+ returnComment: doxityMethod.return,
+ callPath,
+ parameters: this._convertParameters(doxityMethod.inputs),
+ returnType: returnTypeIfExists,
+ };
+ return method;
+ },
+ );
+
+ const doxityProperties: DoxityAbiDoc[] = _.filter<DoxityAbiDoc>(
+ doxityContractObj.abiDocs,
+ (abiDoc: DoxityAbiDoc) => {
+ return this._isProperty(abiDoc);
+ },
+ );
+ const properties = _.map<DoxityAbiDoc, Property>(doxityProperties, (doxityProperty: DoxityAbiDoc) => {
+ // We assume that none of our functions return more then a single return value
+ let typeName = doxityProperty.outputs[0].type;
+ if (!_.isEmpty(doxityProperty.inputs)) {
+ // Properties never have more then a single input
+ typeName = `(${doxityProperty.inputs[0].type} => ${typeName})`;
+ }
+ const property = {
+ name: doxityProperty.name,
+ type: this._convertType(typeName),
+ comment: doxityProperty.details,
+ };
+ return property;
+ });
+
+ const doxityEvents = _.filter(
+ doxityContractObj.abiDocs,
+ (abiDoc: DoxityAbiDoc) => abiDoc.type === AbiTypes.Event,
+ );
+ const events = _.map(doxityEvents, doxityEvent => {
+ const event = {
+ name: doxityEvent.name,
+ eventArgs: this._convertEventArgs(doxityEvent.inputs),
+ };
+ return event;
+ });
+
+ const docSection: DocSection = {
+ comment: doxityContractObj.title,
+ constructors,
+ methods,
+ properties,
+ types: [],
+ events,
+ };
+ docAgnosticFormat[contractName] = docSection;
+ });
+ return docAgnosticFormat;
+ },
+ _convertParameters(inputs: DoxityInput[]): Parameter[] {
+ const parameters = _.map(inputs, input => {
+ const parameter = {
+ name: input.name,
+ comment: input.description,
+ isOptional: false,
+ type: this._convertType(input.type),
+ };
+ return parameter;
+ });
+ return parameters;
+ },
+ _convertType(typeName: string): Type {
+ const type = {
+ name: typeName,
+ typeDocType: TypeDocTypes.Intrinsic,
+ };
+ return type;
+ },
+ _isMethod(abiDoc: DoxityAbiDoc) {
+ if (abiDoc.type !== AbiTypes.Function) {
+ return false;
+ }
+ const hasInputs = !_.isEmpty(abiDoc.inputs);
+ const hasNamedOutputIfExists = !hasInputs || !_.isEmpty(abiDoc.inputs[0].name);
+ const isNameAllCaps = abiDoc.name === abiDoc.name.toUpperCase();
+ const isMethod = hasNamedOutputIfExists && !isNameAllCaps;
+ return isMethod;
+ },
+ _isProperty(abiDoc: DoxityAbiDoc) {
+ if (abiDoc.type !== AbiTypes.Function) {
+ return false;
+ }
+ const hasInputs = !_.isEmpty(abiDoc.inputs);
+ const hasNamedOutputIfExists = !hasInputs || !_.isEmpty(abiDoc.inputs[0].name);
+ const isNameAllCaps = abiDoc.name === abiDoc.name.toUpperCase();
+ const isProperty = !hasNamedOutputIfExists || isNameAllCaps;
+ return isProperty;
+ },
+ _convertEventArgs(inputs: DoxityInput[]): EventArg[] {
+ const eventArgs = _.map(inputs, input => {
+ const eventArg = {
+ isIndexed: input.indexed,
+ name: input.name,
+ type: this._convertType(input.type),
+ };
+ return eventArg;
+ });
+ return eventArgs;
+ },
+};