From 7241af865ec386774f9b82ca66faaeb8072bb3c8 Mon Sep 17 00:00:00 2001 From: panos Date: Sat, 14 Jul 2018 03:51:15 +0300 Subject: [PATCH] add simple algorithm flawed example for bracketing, Example: https://www.diffchecker.com/OWtBlP0x add blockWrap function --- lib/preprocessor.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 1a64e80..e4a6601 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -1,6 +1,16 @@ const SolExplore = require('sol-explore'); 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 * @param {Object} ast AST tree @@ -34,6 +44,7 @@ function removeViewPureConst(ast, contract) { */ function bracketUnbStatements(ast, contract) { const brkList = []; + let offset = 0; SolExplore.traverse(ast, { enter(node, parent) { // eslint-disable-line no-loop-func // If consequents @@ -83,6 +94,11 @@ function bracketUnbStatements(ast, contract) { check = brkList[i].end <= brkList[i - 1].end; } brkList[i].nested = check; + const elem = Object.assign(brkList[i]); + elem.start += offset; + elem.end += offset; + contract = blockWrap(contract, elem); + offset += 2; } return contract; }