Skip to content

Commit ba64b37

Browse files
committed
docgen wip
1 parent ec8fdb0 commit ba64b37

50 files changed

Lines changed: 1382 additions & 44 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/config.js renamed to docs/config.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const path = require('path');
2-
const fs = require('fs');
1+
import path from 'path';
2+
import fs from 'fs';
33

44
/** @type import('solidity-docgen/dist/config').UserConfig */
5-
module.exports = {
5+
export default {
66
outputDir: 'docs/modules/api/pages',
77
templates: 'docs/templates',
88
exclude: ['mocks'],

docs/templates/contract.hbs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[.contract]
1010
[[{{anchor}}]]
11-
=== `++{{name}}++` link:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v{{oz-version}}/{{__item_context.file.absolutePath}}[{github-icon},role=heading-link]
11+
=== `++{{name}}++` link:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v{{ozVersion}}/{{__item_context.file.absolutePath}}[{github-icon},role=heading-link]
1212

1313
[.hljs-theme-light.nopadding]
1414
```solidity
@@ -27,11 +27,11 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
2727
--
2828
{{/if}}
2929

30-
{{#if has-functions}}
30+
{{#if hasFunctions}}
3131
[.contract-index]
3232
.Functions
3333
--
34-
{{#each inherited-functions}}
34+
{{#each inheritedFunctions}}
3535
{{#unless @first}}
3636
[.contract-subindex-inherited]
3737
.{{contract.name}}
@@ -44,7 +44,7 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
4444
--
4545
{{/if}}
4646

47-
{{#if has-events}}
47+
{{#if hasEvents}}
4848
[.contract-index]
4949
.Events
5050
--
@@ -61,7 +61,7 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
6161
--
6262
{{/if}}
6363

64-
{{#if has-errors}}
64+
{{#if hasErrors}}
6565
[.contract-index]
6666
.Errors
6767
--
@@ -78,7 +78,7 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
7878
--
7979
{{/if}}
8080

81-
{{#if has-internal-variables}}
81+
{{#if hasInternalVariables}}
8282
[.contract-index]
8383
.Internal Variables
8484
--
@@ -87,7 +87,7 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
8787
[.contract-subindex-inherited]
8888
.{{name}}
8989
{{/unless}}
90-
{{#each internal-variables}}
90+
{{#each internalVariables}}
9191
* {xref-{{anchor~}} }[`++{{typeDescriptions.typeString}} {{#if constant}}constant{{/if}} {{name}}++`]
9292
{{/each}}
9393

@@ -98,7 +98,7 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
9898
{{#each modifiers}}
9999
[.contract-item]
100100
[[{{anchor}}]]
101-
==== `[.contract-item-name]#++{{name}}++#++({{typed-params params}})++` [.item-kind]#modifier#
101+
==== `[.contract-item-name]#++{{name}}++#++({{typedParams params}})++` [.item-kind]#modifier#
102102

103103
{{{natspec.dev}}}
104104

@@ -107,7 +107,7 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
107107
{{#each functions}}
108108
[.contract-item]
109109
[[{{anchor}}]]
110-
==== `[.contract-item-name]#++{{name}}++#++({{typed-params params}}){{#if returns2}}{{typed-params returns2}}{{/if}}++` [.item-kind]#{{visibility}}#
110+
==== `[.contract-item-name]#++{{name}}++#++({{typedParams params}}){{#if returns2}}{{typedParams returns2}}{{/if}}++` [.item-kind]#{{visibility}}#
111111

112112
{{{natspec.dev}}}
113113

@@ -116,7 +116,7 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
116116
{{#each events}}
117117
[.contract-item]
118118
[[{{anchor}}]]
119-
==== `[.contract-item-name]#++{{name}}++#++({{typed-params params}})++` [.item-kind]#event#
119+
==== `[.contract-item-name]#++{{name}}++#++({{typedParams params}})++` [.item-kind]#event#
120120

121121
{{{natspec.dev}}}
122122

@@ -125,13 +125,13 @@ import "@openzeppelin/{{__item_context.file.absolutePath}}";
125125
{{#each errors}}
126126
[.contract-item]
127127
[[{{anchor}}]]
128-
==== `[.contract-item-name]#++{{name}}++#++({{typed-params params}})++` [.item-kind]#error#
128+
==== `[.contract-item-name]#++{{name}}++#++({{typedParams params}})++` [.item-kind]#error#
129129

130130
{{{natspec.dev}}}
131131

132132
{{/each}}
133133

134-
{{#each internal-variables}}
134+
{{#each internalVariables}}
135135
[.contract-item]
136136
[[{{anchor}}]]
137137
==== `{{typeDescriptions.typeString}} [.contract-item-name]#++{{name}}++#` [.item-kind]#internal{{#if constant}} constant{{/if}}#
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
const { version } = require('../../package.json');
1+
import { version } from '../../package.json';
22

3-
module.exports['oz-version'] = () => version;
3+
export const ozVersion = () => version;
44

5-
module.exports['readme-path'] = opts => {
5+
export const readmePath = opts => {
66
return 'contracts/' + opts.data.root.id.replace(/\.adoc$/, '') + '/README.adoc';
77
};
88

9-
module.exports.names = params => params?.map(p => p.name).join(', ');
9+
export const names = params => params?.map(p => p.name).join(', ');
1010

11-
module.exports['typed-params'] = params => {
11+
export const typedParams = params => {
1212
return params?.map(p => `${p.type}${p.indexed ? ' indexed' : ''}${p.name ? ' ' + p.name : ''}`).join(', ');
1313
};
1414

15-
const slug = (module.exports.slug = str => {
15+
export const slug = str => {
1616
if (str === undefined) {
1717
throw new Error('Missing argument');
1818
}
1919
return str.replace(/\W/g, '-');
20-
});
20+
};
2121

2222
const linksCache = new WeakMap();
2323

@@ -34,7 +34,7 @@ function getAllLinks(items) {
3434
return res;
3535
}
3636

37-
module.exports['with-prelude'] = opts => {
37+
export const withPrelude = opts => {
3838
const links = getAllLinks(opts.data.site.items);
3939
const contents = opts.fn();
4040
const neededLinks = contents

docs/templates/page.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
:github-icon: pass:[<svg class="icon"><use href="#github-icon"/></svg>]
2-
{{#with-prelude}}
3-
{{readme (readme-path)}}
4-
{{/with-prelude}}
2+
{{#withPrelude}}
3+
{{readme (readmePath)}}
4+
{{/withPrelude}}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { isNodeType, findAll } = require('solidity-ast/utils');
2-
const { slug } = require('./helpers');
1+
import { isNodeType, findAll } from 'solidity-ast/utils.js';
2+
import { slug } from './helpers';
33

4-
module.exports.anchor = function anchor({ item, contract }) {
4+
export const anchor = function anchor({ item, contract }) {
55
let res = '';
66
if (contract) {
77
res += contract.name + '-';
@@ -17,7 +17,7 @@ module.exports.anchor = function anchor({ item, contract }) {
1717
return res;
1818
};
1919

20-
module.exports.fullname = function fullname({ item }) {
20+
export const fullname = function fullname({ item }) {
2121
let res = '';
2222
res += item.name;
2323
if ('parameters' in item) {
@@ -33,7 +33,7 @@ module.exports.fullname = function fullname({ item }) {
3333
return res;
3434
};
3535

36-
module.exports.inheritance = function ({ item, build }) {
36+
export const inheritance = function ({ item, build }) {
3737
if (!isNodeType('ContractDefinition', item)) {
3838
throw new Error('inheritance modifier used on non-contract');
3939
}
@@ -43,42 +43,42 @@ module.exports.inheritance = function ({ item, build }) {
4343
.filter((c, i) => c.name !== 'Context' || i === 0);
4444
};
4545

46-
module.exports['has-functions'] = function ({ item }) {
46+
export const hasFunctions = function ({ item }) {
4747
return item.inheritance.some(c => c.functions.length > 0);
4848
};
4949

50-
module.exports['has-events'] = function ({ item }) {
50+
export const hasEvents = function ({ item }) {
5151
return item.inheritance.some(c => c.events.length > 0);
5252
};
5353

54-
module.exports['has-errors'] = function ({ item }) {
54+
export const hasErrors = function ({ item }) {
5555
return item.inheritance.some(c => c.errors.length > 0);
5656
};
5757

58-
module.exports['internal-variables'] = function ({ item }) {
58+
export const internalVariables = function ({ item }) {
5959
return item.variables.filter(({ visibility }) => visibility === 'internal');
6060
};
6161

62-
module.exports['has-internal-variables'] = function ({ item }) {
63-
return module.exports['internal-variables']({ item }).length > 0;
62+
export const hasInternalVariables = function ({ item }) {
63+
return internalVariables({ item }).length > 0;
6464
};
6565

66-
module.exports.functions = function ({ item }) {
66+
export const functions = function ({ item }) {
6767
return [
6868
...[...findAll('FunctionDefinition', item)].filter(f => f.visibility !== 'private'),
6969
...[...findAll('VariableDeclaration', item)].filter(f => f.visibility === 'public'),
7070
];
7171
};
7272

73-
module.exports.returns2 = function ({ item }) {
73+
export const returns2 = function ({ item }) {
7474
if (isNodeType('VariableDeclaration', item)) {
7575
return [{ type: item.typeName.typeDescriptions.typeString }];
7676
} else {
7777
return item.returns;
7878
}
7979
};
8080

81-
module.exports['inherited-functions'] = function ({ item }) {
81+
export const inheritedFunctions = function ({ item }) {
8282
const { inheritance } = item;
8383
const baseFunctions = new Set(inheritance.flatMap(c => c.functions.flatMap(f => f.baseFunctions ?? [])));
8484
return inheritance.map((contract, i) => ({

hardhat.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import hardhatIgnoreWarnings from 'hardhat-ignore-warnings';
77
import hardhatMocha from '@nomicfoundation/hardhat-mocha';
88
import hardhatNetworkHelpers from '@nomicfoundation/hardhat-network-helpers';
99
import hardhatPredeploy from 'hardhat-predeploy';
10+
import hardhatDocgen from './hardhat/hardhat-solidity-docgen/plugin.ts';
1011
import hardhatExposed from './hardhat/hardhat-exposed/plugin.ts';
1112
import hardhatTranspiler from './hardhat/hardhat-transpiler/plugin.ts';
1213
import hardhatOzContractsHelpers from './hardhat/hardhat-oz-contracts-helpers/plugin.ts';
@@ -36,6 +37,7 @@ export default defineConfig({
3637
hardhatNetworkHelpers,
3738
hardhatPredeploy,
3839
// Local plugins
40+
hardhatDocgen,
3941
hardhatExposed,
4042
hardhatTranspiler,
4143
hardhatOzContractsHelpers,
@@ -92,4 +94,5 @@ export default defineConfig({
9294
include: ['contracts/**/*.sol'],
9395
exclude: ['**/*WithInit.sol'],
9496
},
97+
docgen: await import('./docs/config.mjs').then(m => m.default),
9598
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import path from 'node:path';
2+
3+
import type { ConfigHooks } from 'hardhat/types/hooks';
4+
import { defaults } from '../internal/config.ts';
5+
6+
export type * from '../type-extensions.ts';
7+
8+
export default async (): Promise<Partial<ConfigHooks>> => ({
9+
resolveUserConfig: (userConfig, resolveConfigurationVariable, next) =>
10+
next(userConfig, resolveConfigurationVariable).then(config => {
11+
config.docgen ??= userConfig.docgen ?? defaults;
12+
config.docgen.root = config.paths.root;
13+
config.docgen.sourcesDir = path
14+
.relative(config.paths.root, config.paths.sources.solidity[0]) // TODO: support multiple source directories
15+
.split(path.sep)
16+
.join(path.posix.sep);
17+
return config;
18+
}),
19+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { VariableDeclaration } from 'solidity-ast';
2+
3+
export function trim(text: string) {
4+
if (typeof text === 'string') {
5+
return text.trim();
6+
}
7+
}
8+
9+
export function joinLines(text?: string) {
10+
if (typeof text === 'string') {
11+
return text.replace(/[\r\n]+/g, ' ');
12+
}
13+
}
14+
15+
/**
16+
* Format a variable as its type followed by its name, if available.
17+
*/
18+
export function formatVariable(v: VariableDeclaration): string {
19+
return [v.typeName?.typeDescriptions.typeString!].concat(v.name || []).join(' ');
20+
}
21+
22+
export const eq = (a: unknown, b: unknown) => a === b;

0 commit comments

Comments
 (0)