From 57a220ce375bc333832492bf8ceb71018f191d09 Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 30 Mar 2023 18:00:47 +0200 Subject: [PATCH] fix generating inherited functions --- apps/doc-gen/src/app/docgen/common/helpers.ts | 13 ++++++ .../src/app/docgen/common/properties.ts | 44 ++++++++++++++++++- .../app/docgen/themes/markdown/contract.hbs | 40 ++++++++++++++++- .../src/app/docgen/themes/markdown/helpers.ts | 13 ++++++ 4 files changed, 108 insertions(+), 2 deletions(-) diff --git a/apps/doc-gen/src/app/docgen/common/helpers.ts b/apps/doc-gen/src/app/docgen/common/helpers.ts index 926e6df9da..3ee3dbe10a 100644 --- a/apps/doc-gen/src/app/docgen/common/helpers.ts +++ b/apps/doc-gen/src/app/docgen/common/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(', '); +}; diff --git a/apps/doc-gen/src/app/docgen/common/properties.ts b/apps/doc-gen/src/app/docgen/common/properties.ts index 460090a036..5366e8d7ef 100644 --- a/apps/doc-gen/src/app/docgen/common/properties.ts +++ b/apps/doc-gen/src/app/docgen/common/properties.ts @@ -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)), + })) +} \ No newline at end of file diff --git a/apps/doc-gen/src/app/docgen/themes/markdown/contract.hbs b/apps/doc-gen/src/app/docgen/themes/markdown/contract.hbs index e4ed15831c..0ea851c60a 100644 --- a/apps/doc-gen/src/app/docgen/themes/markdown/contract.hbs +++ b/apps/doc-gen/src/app/docgen/themes/markdown/contract.hbs @@ -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}} diff --git a/apps/doc-gen/src/app/docgen/themes/markdown/helpers.ts b/apps/doc-gen/src/app/docgen/themes/markdown/helpers.ts index bfd5cfbdd7..92492cf931 100644 --- a/apps/doc-gen/src/app/docgen/themes/markdown/helpers.ts +++ b/apps/doc-gen/src/app/docgen/themes/markdown/helpers.ts @@ -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. */