|
|
|
@ -2,14 +2,17 @@ const SolExplore = require('sol-explore'); |
|
|
|
|
const SolidityParser = require('solidity-parser-antlr'); |
|
|
|
|
|
|
|
|
|
const crRegex = /[\r\n ]+$/g; |
|
|
|
|
const OPEN = '{'; |
|
|
|
|
const CLOSE = '}'; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Splices enclosing brackets into `contract` around `expression`; |
|
|
|
|
* @param {String} contract solidity source |
|
|
|
|
* @param {Object} node AST node to bracket |
|
|
|
|
* @return {String} contract |
|
|
|
|
*/ |
|
|
|
|
function blockWrap(contract, expression) { |
|
|
|
|
return contract.slice(0, expression.range[0]) + '{' + contract.slice(expression.range[0], expression.range[1] + 1) + '}' + contract.slice(expression.range[1] + 1); |
|
|
|
|
function insertBrace(contract, item, offset) { |
|
|
|
|
return contract.slice(0,item.pos + offset) + item.type + contract.slice(item.pos + offset) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Remove 'pure' and 'view' from the function declaration. |
|
|
|
@ -47,24 +50,28 @@ module.exports.run = function r(contract) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
const ast = SolidityParser.parse(contract, { range: true }); |
|
|
|
|
blocksToWrap = []; |
|
|
|
|
insertions = []; |
|
|
|
|
viewPureToRemove = []; |
|
|
|
|
SolidityParser.visit(ast, { |
|
|
|
|
IfStatement: function(node) { |
|
|
|
|
if (node.trueBody.type !== 'Block') { |
|
|
|
|
blocksToWrap.push(node.trueBody); |
|
|
|
|
} else if (node.falseBody && node.falseBody.type !== 'Block'){ |
|
|
|
|
blocksToWrap.push(node.falseBody); |
|
|
|
|
insertions.push({type: OPEN, pos: node.trueBody.range[0]}); |
|
|
|
|
insertions.push({type: CLOSE, pos: node.trueBody.range[1] + 1}); |
|
|
|
|
} else if ( node.falseBody && node.falseBody.type !== 'Block' ) { |
|
|
|
|
insertions.push({type: OPEN, pos: node.falseBody.range[0]}); |
|
|
|
|
insertions.push({type: CLOSE, pos: node.falseBody.range[1] + 1}); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
ForStatement: function(node){ |
|
|
|
|
if (node.body.type !== 'Block'){ |
|
|
|
|
blocksToWrap.push(node.body); |
|
|
|
|
insertions.push({type: OPEN, pos: node.body.range[0]}); |
|
|
|
|
insertions.push({type: CLOSE, pos: node.body.range[1] + 1}); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
WhileStatement: function(node){ |
|
|
|
|
if (node.body.type !== 'Block'){ |
|
|
|
|
blocksToWrap.push(node.body); |
|
|
|
|
insertions.push({type: OPEN, pos: node.body.range[0]}); |
|
|
|
|
insertions.push({type: CLOSE, pos: node.body.range[1] + 1}); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
FunctionDefinition: function(node){ |
|
|
|
@ -73,12 +80,13 @@ module.exports.run = function r(contract) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
// Firstly, remove pures and views. Note that we replace 'pure' and 'view' with spaces, so
|
|
|
|
|
// Firstly, remove pures and views. Note that we replace 'pure' and 'view' with spaces, so
|
|
|
|
|
// character counts remain the same, so we can do this in any order
|
|
|
|
|
viewPureToRemove.forEach(node => contract = removePureView(contract, node)); |
|
|
|
|
// We apply the blocks we found in reverse order to avoid extra characters messing things up.
|
|
|
|
|
blocksToWrap.sort((a,b) => a.range[0] < b.range[0]); |
|
|
|
|
blocksToWrap.forEach(block => contract = blockWrap(contract, block)) |
|
|
|
|
// Sort the insertion points.
|
|
|
|
|
insertions.sort((a,b) => a.pos - b.pos); |
|
|
|
|
insertions.forEach((item, idx) => contract = insertBrace(contract, item, idx)); |
|
|
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
|
contract = err; |
|
|
|
|
keepRunning = false; |
|
|
|
|