-
Notifications
You must be signed in to change notification settings - Fork 8
/
preprocessor.js
55 lines (52 loc) · 2.02 KB
/
preprocessor.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
const SolExplore = require('sol-explore');
const SolidityParser = require('solidity-parser');
/**
* Splices enclosing brackets into `contract` around `expression`;
* @param {String} contract solidity code
* @param {Object} node AST node to bracket
* @return {String} contract
*/
function blockWrap(contract, expression) {
return contract.slice(0, expression.start) + '{' + contract.slice(expression.start, expression.end) + '}' + contract.slice(expression.end);
}
/**
* Locates unbracketed singleton statements attached to if, else, for and while statements
* and brackets them. Instrumenter needs to inject events at these locations and having
* them pre-bracketed simplifies the process. Each time a modification is made the contract
* is passed back to the parser and re-walked because all the starts and ends get shifted.
* @param {String} contract solidity code
* @return {String} contract
*/
module.exports.run = function r(contract) {
let keepRunning = true;
while (keepRunning) {
const ast = SolidityParser.parse(contract);
keepRunning = false;
SolExplore.traverse(ast, {
enter(node, parent) { // eslint-disable-line no-loop-func
// If consequents
if (node.type === 'IfStatement' && node.consequent.type !== 'BlockStatement') {
contract = blockWrap(contract, node.consequent);
keepRunning = true;
this.stopTraversal();
// If alternates
} else if (
node.type === 'IfStatement' &&
node.alternate &&
node.alternate.type !== 'BlockStatement') {
contract = blockWrap(contract, node.alternate);
keepRunning = true;
this.stopTraversal();
// Loops
} else if (
(node.type === 'ForStatement' || node.type === 'WhileStatement') &&
node.body.type !== 'BlockStatement') {
contract = blockWrap(contract, node.body);
keepRunning = true;
this.stopTraversal();
}
},
});
}
return contract;
};