aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/pages/documentation/method_signature.tsx
blob: 041dcd0937921e45c83d4632d377965216ddd157 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import * as _ from 'lodash';
import * as React from 'react';
import { DocsInfo } from 'ts/pages/documentation/docs_info';
import { Type } from 'ts/pages/documentation/type';
import { Parameter, SolidityMethod, TypeDefinitionByName, TypescriptMethod } from 'ts/types';
import { constants } from 'ts/utils/constants';

interface MethodSignatureProps {
    method: TypescriptMethod | SolidityMethod;
    sectionName: string;
    shouldHideMethodName?: boolean;
    shouldUseArrowSyntax?: boolean;
    typeDefinitionByName?: TypeDefinitionByName;
    docsInfo: DocsInfo;
}

const defaultProps = {
    shouldHideMethodName: false,
    shouldUseArrowSyntax: false,
};

export const MethodSignature: React.SFC<MethodSignatureProps> = (props: MethodSignatureProps) => {
    const sectionName = constants.TYPES_SECTION_NAME;
    const parameters = renderParameters(props.method, props.docsInfo, sectionName, props.typeDefinitionByName);
    const paramString = _.reduce(parameters, (prev: React.ReactNode, curr: React.ReactNode) => {
        return [prev, ', ', curr];
    });
    const methodName = props.shouldHideMethodName ? '' : props.method.name;
    const typeParameterIfExists = _.isUndefined((props.method as TypescriptMethod).typeParameter)
        ? undefined
        : renderTypeParameter(props.method, props.docsInfo, sectionName, props.typeDefinitionByName);
    return (
        <span>
            {props.method.callPath}
            {methodName}
            {typeParameterIfExists}({paramString})
            {props.shouldUseArrowSyntax ? ' => ' : ': '}{' '}
            {props.method.returnType && (
                <Type
                    type={props.method.returnType}
                    sectionName={sectionName}
                    typeDefinitionByName={props.typeDefinitionByName}
                    docsInfo={props.docsInfo}
                />
            )}
        </span>
    );
};

MethodSignature.defaultProps = defaultProps;

function renderParameters(
    method: TypescriptMethod | SolidityMethod,
    docsInfo: DocsInfo,
    sectionName: string,
    typeDefinitionByName?: TypeDefinitionByName,
) {
    const parameters = method.parameters;
    const params = _.map(parameters, (p: Parameter) => {
        const isOptional = p.isOptional;
        const type = (
            <Type
                type={p.type}
                sectionName={sectionName}
                typeDefinitionByName={typeDefinitionByName}
                docsInfo={docsInfo}
            />
        );
        return (
            <span key={`param-${p.type}-${p.name}`}>
                {p.name}
                {isOptional && '?'}: {type}
            </span>
        );
    });
    return params;
}

function renderTypeParameter(
    method: TypescriptMethod,
    docsInfo: DocsInfo,
    sectionName: string,
    typeDefinitionByName?: TypeDefinitionByName,
) {
    const typeParameter = method.typeParameter;
    const typeParam = (
        <span>
            {`<${typeParameter.name} extends `}
            <Type
                type={typeParameter.type}
                sectionName={sectionName}
                typeDefinitionByName={typeDefinitionByName}
                docsInfo={docsInfo}
            />
            {`>`}
        </span>
    );
    return typeParam;
}