11const SolExplore = require ( 'sol-explore' ) ;
2- const SolidityParser = require ( 'solidity-parser-sc ' ) ;
2+ const SolidityParser = require ( 'solidity-parser-antlr ' ) ;
33
44const crRegex = / [ \r \n ] + $ / g;
55/**
@@ -9,21 +9,27 @@ const crRegex = /[\r\n ]+$/g;
99 * @return {String } contract
1010 */
1111function blockWrap ( contract , expression ) {
12- return contract . slice ( 0 , expression . start ) + '{' + contract . slice ( expression . start , expression . end ) + '}' + contract . slice ( expression . end ) ;
12+ return contract . slice ( 0 , expression . range [ 0 ] ) + '{' + contract . slice ( expression . range [ 0 ] , expression . range [ 1 ] + 1 ) + '}' + contract . slice ( expression . range [ 1 ] + 1 ) ;
1313}
1414
15-
16- /**
17- * Captures carriage returns at modifiers we'll remove. These need to be re-injected into the
18- * source to keep line report alignments accurate.
15+ /** Remove 'pure' and 'view' from the function declaration.
1916 * @param {String } contract solidity source
20- * @param {Object } modifier AST node
21- * @return {String } whitespace around the modifier
22- */
23- function getModifierWhitespace ( contract , modifier ) {
24- const source = contract . slice ( modifier . start , modifier . end ) ;
25- const whitespace = source . match ( crRegex ) || [ ] ;
26- return whitespace . join ( '' ) ;
17+ * @param {Object } function AST node
18+ * @return {String } contract with the modifiers removed from the given function.
19+ */
20+ function removePureView ( contract , node ) {
21+ let fDefStart = node . range [ 0 ] ;
22+ if ( node . body ) {
23+ fDefEnd = node . body . range [ 0 ] ;
24+ } else if ( node . returnParameters ) {
25+ fDefEnd = node . returnParameters . range [ 0 ] ;
26+ } else {
27+ fDefEnd = node . range [ 1 ] ;
28+ }
29+ let fDef = contract . slice ( fDefStart , fDefEnd + 1 ) ;
30+ fDef = fDef . replace ( / \b v i e w \b / i, ' ' ) ;
31+ fDef = fDef . replace ( / \b p u r e \b / i, ' ' ) ;
32+ return contract . slice ( 0 , fDefStart ) + fDef + contract . slice ( fDefEnd + 1 ) ;
2733}
2834
2935/**
@@ -42,47 +48,42 @@ module.exports.run = function r(contract) {
4248
4349 while ( keepRunning ) {
4450 try {
45- const ast = SolidityParser . parse ( contract ) ;
51+ const ast = SolidityParser . parse ( contract , { range : true } ) ;
4652 keepRunning = false ;
47- SolExplore . traverse ( ast , {
48- enter ( node , parent ) { // eslint-disable-line no-loop-func
49- // If consequents
50- if ( node . type === 'IfStatement' && node . consequent . type !== 'BlockStatement' ) {
51- contract = blockWrap ( contract , node . consequent ) ;
53+ SolidityParser . visit ( ast , {
54+ IfStatement : function ( node ) {
55+ if ( node . trueBody . type !== 'Block' ) {
56+ contract = blockWrap ( contract , node . trueBody ) ;
5257 keepRunning = true ;
53- this . stopTraversal ( ) ;
54- // If alternates
55- } else if (
56- node . type === 'IfStatement' &&
57- node . alternate &&
58- node . alternate . type !== 'BlockStatement' ) {
59- contract = blockWrap ( contract , node . alternate ) ;
58+ return false ;
59+ } else if ( node . falseBody && node . falseBody . type !== 'Block' ) {
60+ contract = blockWrap ( contract , node . falseBody ) ;
6061 keepRunning = true ;
61- this . stopTraversal ( ) ;
62- // Loops
63- } else if (
64- ( node . type === 'ForStatement' || node . type === 'WhileStatement' ) &&
65- node . body . type !== 'BlockStatement' ) {
62+ return false ;
63+ }
64+ } ,
65+ ForStatement : function ( node ) {
66+ if ( node . body . type !== 'Block' ) {
6667 contract = blockWrap ( contract , node . body ) ;
6768 keepRunning = true ;
68- this . stopTraversal ( ) ;
69- } else if ( node . type === 'FunctionDeclaration' && node . modifiers ) {
70- // We want to remove constant / pure / view from functions
71- for ( let i = 0 ; i < node . modifiers . length ; i ++ ) {
72- if ( [ 'pure' , 'constant' , 'view' ] . indexOf ( node . modifiers [ i ] . name ) > - 1 ) {
73- let whitespace = getModifierWhitespace ( contract , node . modifiers [ i ] ) ;
74-
75- contract = contract . slice ( 0 , node . modifiers [ i ] . start ) +
76- whitespace +
77- contract . slice ( node . modifiers [ i ] . end ) ;
78-
79- keepRunning = true ;
80- this . stopTraversal ( ) ;
81- }
82- }
69+ return false ;
8370 }
8471 } ,
85- } ) ;
72+ WhileStatement : function ( node ) {
73+ if ( node . body . type !== 'Block' ) {
74+ contract = blockWrap ( contract , node . body ) ;
75+ keepRunning = true ;
76+ return false ;
77+ }
78+ } ,
79+ FunctionDefinition : function ( node ) {
80+ if ( node . stateMutability === 'view' || node . stateMutability === 'pure' ) {
81+ contract = removePureView ( contract , node ) ;
82+ keepRunning = true ;
83+ return false ;
84+ }
85+ }
86+ } )
8687 } catch ( err ) {
8788 contract = err ;
8889 keepRunning = false ;
0 commit comments