forked from agonzalezv/sol-function-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (60 loc) · 1.77 KB
/
index.js
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
const fs = require('fs')
const asciiTable = require('ascii-table')
const parser = require('solidity-parser-antlr')
if (process.argv.length < 3) {
console.log('Error: Missing argument for sol file to scan')
process.exit(1)
}
const target = process.argv[2]
fs.readFile(target, 'utf8', (err, data) => {
if (err) throw err
try {
generateReport(parser.parse(data))
} catch (e) {
if (e instanceof parser.ParserError) {
console.log(e.errors)
}
}
})
function generateReport (contract) {
const doc = contract.children
.filter(child => child.type === 'ContractDefinition')
.map(contract => ({
name: contract.name,
functions: contract.subNodes
.filter(node => node.type === 'FunctionDefinition')
.map(func => parseFunction(func))
}))
doc.map(contract => {
const table = new asciiTable(target + ": " + contract.name)
table.setHeading(
'Function',
'Visibility',
'Mutability',
'Modifiers',
'Returns')
contract.functions.map(
func => table.addRow(
`${func.name}(${func.params})`,
func.visibility,
func.mutability,
func.modifiers,
func.returns
)
)
console.log(table.toString() + '\n')
})
}
function parseFunction (node) {
return {
name: node.isConstructor ? 'constructor' : node.name || '',
params: node.parameters.parameters
.map(({ typeName }) => typeName.name || typeName.baseTypeName.namePath + "[]") || [],
visibility: node.visibility || '',
modifiers: node.modifiers
.map(modifier => modifier.name) || [],
mutability: node.stateMutability || '',
returns: node.returnParameters && node.returnParameters.parameters
.map(({ typeName }) => typeName.name || typeName.baseTypeName.name + "[]") || []
}
}