forked from JoinColony/colonyNetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocgen.js
More file actions
executable file
·313 lines (256 loc) · 10.6 KB
/
docgen.js
File metadata and controls
executable file
·313 lines (256 loc) · 10.6 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env node
/* eslint-disable */
const fs = require('fs');
const path = require('path');
const parser = require('solidity-parser-antlr');
const INTERFACES = [
{
contractFile: path.resolve(__dirname, '..', 'contracts', 'IColony.sol'),
templateFile: path.resolve(__dirname, '..', 'docs', 'templates', '_Interface_IColony.md'),
outputFile: path.resolve(__dirname, '..', 'docs', '_Interface_IColony.md'),
},
{
contractFile: path.resolve(__dirname, '..', 'contracts', 'IColonyNetwork.sol'),
templateFile: path.resolve(__dirname, '..', 'docs', 'templates', '_Interface_IColonyNetwork.md'),
outputFile: path.resolve(__dirname, '..', 'docs', '_Interface_IColonyNetwork.md'),
},
{
contractFile: path.resolve(__dirname, '..', 'contracts', 'IEtherRouter.sol'),
templateFile: path.resolve(__dirname, '..', 'docs', 'templates', '_Interface_IEtherRouter.md'),
outputFile: path.resolve(__dirname, '..', 'docs', '_Interface_IEtherRouter.md'),
},
{
contractFile: path.resolve(__dirname, '..', 'contracts', 'IMetaColony.sol'),
templateFile: path.resolve(__dirname, '..', 'docs', 'templates', '_Interface_IMetaColony.md'),
outputFile: path.resolve(__dirname, '..', 'docs', '_Interface_IMetaColony.md'),
},
{
contractFile: path.resolve(__dirname, '..', 'contracts', 'IRecovery.sol'),
templateFile: path.resolve(__dirname, '..', 'docs', 'templates', '_Interface_IRecovery.md'),
outputFile: path.resolve(__dirname, '..', 'docs', '_Interface_IRecovery.md'),
},
{
contractFile: path.resolve(__dirname, '..', 'contracts', 'IReputationMiningCycle.sol'),
templateFile: path.resolve(__dirname, '..', 'docs', 'templates', '_Interface_IReputationMiningCycle.md'),
outputFile: path.resolve(__dirname, '..', 'docs', '_Interface_IReputationMiningCycle.md'),
},
{
contractFile: path.resolve(__dirname, '..', 'contracts', 'ITokenLocking.sol'),
templateFile: path.resolve(__dirname, '..', 'docs', 'templates', '_Interface_ITokenLocking.md'),
outputFile: path.resolve(__dirname, '..', 'docs', '_Interface_ITokenLocking.md'),
},
];
const generateMarkdown = ({ contractFile, templateFile, outputFile }) => {
const contractFileString = fs.readFileSync(contractFile).toString();
const templateFileString = fs.readFileSync(templateFile).toString();
const ast = parser.parse(contractFileString);
const contract = ast.children.find(child => {
return child.type === 'ContractDefinition'
});
const contractFileArray = contractFileString.split('\n');
function isValidAdditionalLine(index) {
return (
contractFileArray[index] &&
contractFileArray[index].includes('///') &&
!contractFileArray[index].includes(' @dev ') &&
!contractFileArray[index].includes(' @param ') &&
!contractFileArray[index].includes(' @return ')
);
}
const methods = [];
console.log(`Generating ${contract.name} documentation...`);
contract.subNodes.map(method => {
// Set the initial natspec values
const natspec = {
notice: null,
dev: null,
params: [],
returns: [],
}
// Get the index of the line in which the method is declared
const methodLineIndex = contractFileArray.findIndex(line => {
return line.includes(`function ${method.name}(`)
});
// Set the initial value for the natsepc notice
let noticeLineIndex = methodLineIndex - 1;
// Get the line index for the natspec notice
while(
contractFileArray[noticeLineIndex] &&
contractFileArray[noticeLineIndex].includes('///') &&
!contractFileArray[noticeLineIndex].includes(' @notice ')
) {
noticeLineIndex -= 1;
}
// Check whether the current line is a valid notice line
if (contractFileArray[noticeLineIndex].includes(' @notice ')) {
// Get additional natspec notice lines
const additionalNoticeLineIndexes = [];
let additionalNoticeLineIndex = noticeLineIndex + 1;
while(isValidAdditionalLine(additionalNoticeLineIndex)) {
additionalNoticeLineIndexes.push(additionalNoticeLineIndex);
additionalNoticeLineIndex += 1;
}
// Set natspec notice including additional natspec notice lines
natspec.notice = contractFileArray[noticeLineIndex].split(' @notice ')[1];
additionalNoticeLineIndexes.forEach(index => {
natspec.notice += contractFileArray[index].split('///')[1];
});
// Set the initial value for the dev line index
let devLineIndex = noticeLineIndex + 1;
// Get the natspec dev line index
while (
contractFileArray[devLineIndex] &&
contractFileArray[devLineIndex].includes('///') &&
!contractFileArray[devLineIndex].includes(' @dev ')
) {
devLineIndex += 1;
}
// Check whether the current line is a valid dev line
if (contractFileArray[devLineIndex].includes(' @dev ')) {
// Get additional natspec dev lines
const additionalDevLineIndexes = [];
let additionalDevLineIndex = devLineIndex + 1;
while(isValidAdditionalLine(additionalDevLineIndex)) {
additionalDevLineIndexes.push(additionalDevLineIndex);
additionalDevLineIndex += 1;
}
// Set natspec dev including additional natspec dev lines
natspec.dev = contractFileArray[devLineIndex].split(' @dev ')[1];
additionalDevLineIndexes.forEach(index => {
natspec.dev += contractFileArray[index].split('///')[1];
});
}
// Check whether the method has params
if (method.parameters) {
// Set the initial value for the param line index
let paramLineIndex = noticeLineIndex + 1;
// Get the natspec param line index for each param
while (
contractFileArray[paramLineIndex] &&
contractFileArray[paramLineIndex].includes('///') &&
natspec.params.length !== method.parameters.parameters.length
) {
// Check whether the current line is a valid param line
if (contractFileArray[paramLineIndex].includes(' @param ')) {
// Get additional natspec param lines
const additionalParamLineIndexes = [];
let additionalParamLineIndex = paramLineIndex + 1;
while(isValidAdditionalLine(additionalParamLineIndex)) {
additionalParamLineIndexes.push(additionalParamLineIndex);
additionalParamLineIndex += 1;
}
// Set natspec param including additional natspec param lines
let param = contractFileArray[paramLineIndex].split(' @param ')[1];
additionalParamLineIndexes.forEach(index => {
param += contractFileArray[index].split('///')[1];
});
// Push the param and continue loop
natspec.params.push(param);
}
// Incremenent the param line
paramLineIndex += 1;
}
}
// Check whether the method has returns
if (method.returnParameters) {
// Set the initial value for the return line index
let returnLineIndex = noticeLineIndex + 1;
// Get the natspec return line index for each return
while (
contractFileArray[returnLineIndex] &&
contractFileArray[returnLineIndex].includes('///') &&
natspec.returns.length !== method.returnParameters.parameters.length
) {
// Check whether the current line is a valid return line
if (contractFileArray[returnLineIndex].includes(' @return ')) {
// Get additional natspec return lines
const additionalReturnLineIndexes = [];
let additionalReturnLineIndex = returnLineIndex + 1;
while(isValidAdditionalLine(additionalReturnLineIndex)) {
additionalReturnLineIndexes.push(additionalReturnLineIndex);
additionalReturnLineIndex += 1;
}
// Set natspec return including additional natspec return lines
let param = contractFileArray[returnLineIndex].split(' @return ')[1];
additionalReturnLineIndexes.forEach(index => {
param += contractFileArray[index].split('///')[1];
});
// Push the return and continue loop
natspec.returns.push(param);
}
// Incremenent the return line
returnLineIndex += 1;
}
}
} else {
// Log warning for any methods without a valid notice line
console.warn(`WARNING: ${method.name} is missing a natspec @notice`);
}
// Push the method and append natspec
methods.push({ ...method, natspec });
});
const md = `
${templateFileString}
${printMethods(methods)}
`.trim();
fs.writeFileSync(outputFile, md);
};
INTERFACES.forEach(generateMarkdown);
function printMethods(methods) {
if (!methods.length) return '';
methods.sort((a, b) => {
var x = a.name.toLowerCase();
var y = b.name.toLowerCase();
if (x < y) return -1;
if (x > y) return 1;
return 0;
});
return `
## Interface Methods
` + methods.map(method => `
### \`${method.name}\`\n
${method.natspec.notice ? method.natspec.notice : ''}
${method.natspec.dev ? `
*Note: ${method.natspec.dev}*` : ''}
${method.parameters && method.parameters.parameters.length ? `
**Parameters**
` + printParams(method, method.parameters.parameters, method.natspec.params) : ''}
${method.returnParameters && method.returnParameters.parameters.length ? `
**Return Parameters**
` + printParams(method, method.returnParameters.parameters, method.natspec.returns) : ''}
`).join('');
}
function printParams(method, params, natspecParams) {
if (params.length) {
return `
|Name|Type|Description|
|---|---|---|
${params
.map((param, index) => {
const name = param.name || param.typeName.name;
let arrayType;
let userDefinedType;
if (param.typeName.type === 'ArrayTypeName') {
const length = param.typeName.length ? param.typeName.length.number : '';
arrayType = `${param.typeName.baseTypeName.name || param.typeName.baseTypeName.namePath}[${length}]`;
}
if (param.typeName.type === 'UserDefinedTypeName') {
userDefinedType = param.typeName.namePath;
}
const type = param.typeName.name || arrayType || userDefinedType;
let description = '';
const matchingDescription = (
natspecParams[index] &&
natspecParams[index].substring(0, name.length) === name
);
if (matchingDescription) {
description = natspecParams[index].slice(name.length + 1);
} else {
console.warn(`WARNING: ${method.name} ${name} has no matching natspec comment`);
}
return `|${name}|${type}|${description}`;
})
.join('\n')}`;
}
return '';
}