@@ -2,14 +2,17 @@ const SolExplore = require('sol-explore');
22const SolidityParser = require ( 'solidity-parser-antlr' ) ;
33
44const crRegex = / [ \r \n ] + $ / g;
5+ const OPEN = '{' ;
6+ const CLOSE = '}' ;
7+
58/**
69 * Splices enclosing brackets into `contract` around `expression`;
710 * @param {String } contract solidity source
811 * @param {Object } node AST node to bracket
912 * @return {String } contract
1013 */
11- function blockWrap ( contract , expression ) {
12- return contract . slice ( 0 , expression . range [ 0 ] ) + '{' + contract . slice ( expression . range [ 0 ] , expression . range [ 1 ] + 1 ) + '}' + contract . slice ( expression . range [ 1 ] + 1 ) ;
14+ function insertBrace ( contract , item , offset ) {
15+ return contract . slice ( 0 , item . pos + offset ) + item . type + contract . slice ( item . pos + offset )
1316}
1417
1518/** Remove 'pure' and 'view' from the function declaration.
@@ -47,24 +50,28 @@ module.exports.run = function r(contract) {
4750
4851 try {
4952 const ast = SolidityParser . parse ( contract , { range : true } ) ;
50- blocksToWrap = [ ] ;
53+ insertions = [ ] ;
5154 viewPureToRemove = [ ] ;
5255 SolidityParser . visit ( ast , {
5356 IfStatement : function ( node ) {
5457 if ( node . trueBody . type !== 'Block' ) {
55- blocksToWrap . push ( node . trueBody ) ;
56- } else if ( node . falseBody && node . falseBody . type !== 'Block' ) {
57- blocksToWrap . push ( node . falseBody ) ;
58+ insertions . push ( { type : OPEN , pos : node . trueBody . range [ 0 ] } ) ;
59+ insertions . push ( { type : CLOSE , pos : node . trueBody . range [ 1 ] + 1 } ) ;
60+ } else if ( node . falseBody && node . falseBody . type !== 'Block' ) {
61+ insertions . push ( { type : OPEN , pos : node . falseBody . range [ 0 ] } ) ;
62+ insertions . push ( { type : CLOSE , pos : node . falseBody . range [ 1 ] + 1 } ) ;
5863 }
5964 } ,
6065 ForStatement : function ( node ) {
6166 if ( node . body . type !== 'Block' ) {
62- blocksToWrap . push ( node . body ) ;
67+ insertions . push ( { type : OPEN , pos : node . body . range [ 0 ] } ) ;
68+ insertions . push ( { type : CLOSE , pos : node . body . range [ 1 ] + 1 } ) ;
6369 }
6470 } ,
6571 WhileStatement : function ( node ) {
6672 if ( node . body . type !== 'Block' ) {
67- blocksToWrap . push ( node . body ) ;
73+ insertions . push ( { type : OPEN , pos : node . body . range [ 0 ] } ) ;
74+ insertions . push ( { type : CLOSE , pos : node . body . range [ 1 ] + 1 } ) ;
6875 }
6976 } ,
7077 FunctionDefinition : function ( node ) {
@@ -73,12 +80,13 @@ module.exports.run = function r(contract) {
7380 }
7481 }
7582 } )
76- // Firstly, remove pures and views. Note that we replace 'pure' and 'view' with spaces, so
83+ // Firstly, remove pures and views. Note that we replace 'pure' and 'view' with spaces, so
7784 // character counts remain the same, so we can do this in any order
7885 viewPureToRemove . forEach ( node => contract = removePureView ( contract , node ) ) ;
79- // We apply the blocks we found in reverse order to avoid extra characters messing things up.
80- blocksToWrap . sort ( ( a , b ) => a . range [ 0 ] < b . range [ 0 ] ) ;
81- blocksToWrap . forEach ( block => contract = blockWrap ( contract , block ) )
86+ // Sort the insertion points.
87+ insertions . sort ( ( a , b ) => a . pos - b . pos ) ;
88+ insertions . forEach ( ( item , idx ) => contract = insertBrace ( contract , item , idx ) ) ;
89+
8290 } catch ( err ) {
8391 contract = err ;
8492 keepRunning = false ;
0 commit comments