fix generating inherited functions

pull/3586/head^2
yann300 2 years ago committed by Aniket
parent 2de6460c88
commit 57a220ce37
  1. 13
      apps/doc-gen/src/app/docgen/common/helpers.ts
  2. 44
      apps/doc-gen/src/app/docgen/common/properties.ts
  3. 40
      apps/doc-gen/src/app/docgen/themes/markdown/contract.hbs
  4. 13
      apps/doc-gen/src/app/docgen/themes/markdown/helpers.ts

@ -20,3 +20,16 @@ export function formatVariable(v: VariableDeclaration): string {
}
export const eq = (a: unknown, b: unknown) => a === b;
export const slug = (str) => {
if (str === undefined) {
throw new Error('Missing argument');
}
return str.replace(/\W/g, '-');
}
export const names = params => params.map(p => p.name).join(', ');
export const typedParams = params => {
return params?.map(p => `${p.type}${p.indexed ? ' indexed' : ''}${p.name ? ' ' + p.name : ''}`).join(', ');
};

@ -4,7 +4,7 @@ import { NatSpec, parseNatspec } from '../utils/natspec';
import { DocItemContext, DOC_ITEM_CONTEXT } from '../site';
import { mapValues } from '../utils/map-values';
import { DocItem, docItemTypes } from '../doc-item';
import { formatVariable } from './helpers';
import { formatVariable, slug } from './helpers';
import { PropertyGetter } from '../templates';
import { itemType } from '../utils/item-type';
@ -136,3 +136,45 @@ export function variables ({ item }: DocItemContext): VariableDeclaration[] | un
export function types ({ item }: DocItemContext): TypeDefinition[] | undefined {
return [...findAll(['StructDefinition', 'EnumDefinition', 'UserDefinedValueTypeDefinition'], item)];
}
export function anchor({ item, contract }: DocItemContext) {
let res = '';
if (contract) {
res += contract.name + '-';
}
res += item.name;
if ('parameters' in item) {
const signature = item.parameters.parameters.map(v => v.typeName.typeDescriptions.typeString).join(',');
res += slug('(' + signature + ')');
}
if (isNodeType('VariableDeclaration', item)) {
res += '-' + slug(item.typeName.typeDescriptions.typeString);
}
return res;
}
export function inheritance ({ item, build }: DocItemContext) {
if (!isNodeType('ContractDefinition', item)) {
throw new Error('used inherited-items on non-contract');
}
return item.linearizedBaseContracts
.map(id => build.deref('ContractDefinition', id))
.filter((c, i) => c.name !== 'Context' || i === 0);
}
export function hasfunctions ({ item }: DocItemContext) {
return (item as any).inheritance.some(c => c.functions.length > 0);
}
export function hasevents ({ item }: DocItemContext) {
return (item as any).inheritance.some(c => c.events.length > 0);
}
export function inheritedfunctions ({ item }: DocItemContext) {
const { inheritance } = (item as any)
const baseFunctions = new Set(inheritance.flatMap(c => c.functions.flatMap(f => f.baseFunctions ?? [])));
return inheritance.map((contract, i) => ({
contract,
functions: contract.functions.filter(f => !baseFunctions.has(f.id) && (f.name !== 'constructor' || i === 0)),
}))
}

@ -1,8 +1,46 @@
{{>common}}
{{#each items}}
{{h 2}} .Contract
{{name}} : {{__item_context.file.absolutePath}}
{{{natspec.dev}}}
{{#if modifiers}}
{{s}}
{{h 2}} Modifiers:
{{#each modifiers}}
{{#hsection}}
{{>item}}
{{/hsection}}
{{/each}}
{{/if}}
{{#if hasfunctions}}
{{s}}
{{h 2}} Functions:
{{#each inheritedfunctions}}
{{#unless @first}}
inherits {{contract.name}}:
{{/unless}}
{{#each functions}}
{{#hsection}}
{{>item}}
{{/hsection}}
{{/each}}
{{/each}}
{{/if}}
{{#if hasevents}}
{{s}}
{{h 2}} Events:
{{#each inheritance}}
{{#unless @first}}
inherits {{name}}:
{{/unless}}
{{#each events}}
{{#hsection}}
{{>item}}
{{/hsection}}
{{/each}}
{{/each}}
{{/if}}

@ -33,6 +33,19 @@ export function hsection(this: unknown, hsublevel: number | HelperOptions, opts?
return opts.fn(this as unknown, opts);
}
/**
* Returns a Markdown heading marker. An optional number increases the heading level.
*
* Input Output
* {{h}} {{name}} # Name
* {{h 2}} {{name}} ## Name
*/
export function s(opts: HelperOptions): string;
export function s(hsublevel: number, opts: HelperOptions): string;
export function s(hsublevel: number | HelperOptions, opts?: HelperOptions) {
return ' --- '
};
/**
* Helper for dealing with the optional hsublevel argument.
*/

Loading…
Cancel
Save