import * as _ from 'lodash'; import * as React from 'react'; import {Type} from 'ts/pages/documentation/type'; import {Parameter, SolidityMethod, TypeDefinitionByName, TypescriptMethod} from 'ts/types'; interface MethodSignatureProps { method: TypescriptMethod|SolidityMethod; shouldHideMethodName?: boolean; shouldUseArrowSyntax?: boolean; typeDefinitionByName?: TypeDefinitionByName; } const defaultProps = { shouldHideMethodName: false, shouldUseArrowSyntax: false, }; export const MethodSignature: React.SFC = (props: MethodSignatureProps) => { const parameters = renderParameters(props.method, 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.typeDefinitionByName); return ( {props.method.callPath}{methodName}{typeParameterIfExists}({paramString}) {props.shouldUseArrowSyntax ? ' => ' : ': '} {' '} {props.method.returnType && } ); }; function renderParameters(method: TypescriptMethod|SolidityMethod, typeDefinitionByName?: TypeDefinitionByName) { const parameters = method.parameters; const params = _.map(parameters, (p: Parameter) => { const isOptional = p.isOptional; return ( {p.name}{isOptional && '?'}: ); }); return params; } function renderTypeParameter(method: TypescriptMethod, typeDefinitionByName?: TypeDefinitionByName) { const typeParameter = method.typeParameter; const typeParam = ( {`<${typeParameter.name} extends `} {`>`} ); return typeParam; }