add simple algorithm flawed example for bracketing,

Example: https://www.diffchecker.com/OWtBlP0x
add blockWrap function
pull/259/head
panos 6 years ago
parent 71320c06a1
commit 7241af865e
  1. 16
      lib/preprocessor.js

@ -1,6 +1,16 @@
const SolExplore = require('sol-explore'); const SolExplore = require('sol-explore');
const SolidityParser = require('solidity-parser-sc'); const SolidityParser = require('solidity-parser-sc');
/**
* Splices enclosing brackets into `contract` around `expression`;
* @param {String} contract solidity source
* @param {Object} expression 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);
}
/** /**
* Parses the AST tree to remove pure, constant and view modifiers * Parses the AST tree to remove pure, constant and view modifiers
* @param {Object} ast AST tree * @param {Object} ast AST tree
@ -34,6 +44,7 @@ function removeViewPureConst(ast, contract) {
*/ */
function bracketUnbStatements(ast, contract) { function bracketUnbStatements(ast, contract) {
const brkList = []; const brkList = [];
let offset = 0;
SolExplore.traverse(ast, { SolExplore.traverse(ast, {
enter(node, parent) { // eslint-disable-line no-loop-func enter(node, parent) { // eslint-disable-line no-loop-func
// If consequents // If consequents
@ -83,6 +94,11 @@ function bracketUnbStatements(ast, contract) {
check = brkList[i].end <= brkList[i - 1].end; check = brkList[i].end <= brkList[i - 1].end;
} }
brkList[i].nested = check; brkList[i].nested = check;
const elem = Object.assign(brkList[i]);
elem.start += offset;
elem.end += offset;
contract = blockWrap(contract, elem);
offset += 2;
} }
return contract; return contract;
} }

Loading…
Cancel
Save