diff --git a/lib/app.js b/lib/app.js index 8020bbe..d1027df 100644 --- a/lib/app.js +++ b/lib/app.js @@ -286,9 +286,15 @@ class App { postProcessPure(env) { shell.ls(`${env}/**/*.sol`).forEach(file => { const contractPath = this.platformNeutralPath(file); - let contract = fs.readFileSync(contractPath).toString(); - contract = preprocessor.run(contract); - fs.writeFileSync(contractPath, contract); + const contract = fs.readFileSync(contractPath).toString(); + const contractProcessed = preprocessor.run(contract); + if (contractProcessed.name && contractProcessed.name === 'SyntaxError' && file.slice(-15) !== 'SimpleError.sol') { + console.log(`Warning: The file at ${file} was identified as a Solidity Contract, ` + + 'but did not parse correctly. You may ignore this warning if it is not a Solidity file, ' + + 'or your project does not use it'); + } else { + fs.writeFileSync(contractPath, contractProcessed); + } }); } diff --git a/lib/preprocessor.js b/lib/preprocessor.js index c6dcf22..8d551eb 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -26,42 +26,47 @@ module.exports.run = function r(contract) { let keepRunning = true; while (keepRunning) { - const ast = SolidityParser.parse(contract); - keepRunning = false; - SolExplore.traverse(ast, { - enter(node, parent) { // eslint-disable-line no-loop-func - // If consequents - if (node.type === 'IfStatement' && node.consequent.type !== 'BlockStatement') { - contract = blockWrap(contract, node.consequent); - keepRunning = true; - this.stopTraversal(); - // If alternates - } else if ( - node.type === 'IfStatement' && - node.alternate && - node.alternate.type !== 'BlockStatement') { - contract = blockWrap(contract, node.alternate); - keepRunning = true; - this.stopTraversal(); - // Loops - } else if ( - (node.type === 'ForStatement' || node.type === 'WhileStatement') && - node.body.type !== 'BlockStatement') { - contract = blockWrap(contract, node.body); - keepRunning = true; - this.stopTraversal(); - } else if (node.type === 'FunctionDeclaration' && node.modifiers) { - // We want to remove constant / pure / view from functions - for (let i = 0; i < node.modifiers.length; i++) { - if (['pure', 'constant', 'view'].indexOf(node.modifiers[i].name) > -1) { - contract = contract.slice(0, node.modifiers[i].start) + contract.slice(node.modifiers[i].end); - keepRunning = true; - this.stopTraversal(); + try { + const ast = SolidityParser.parse(contract); + keepRunning = false; + SolExplore.traverse(ast, { + enter(node, parent) { // eslint-disable-line no-loop-func + // If consequents + if (node.type === 'IfStatement' && node.consequent.type !== 'BlockStatement') { + contract = blockWrap(contract, node.consequent); + keepRunning = true; + this.stopTraversal(); + // If alternates + } else if ( + node.type === 'IfStatement' && + node.alternate && + node.alternate.type !== 'BlockStatement') { + contract = blockWrap(contract, node.alternate); + keepRunning = true; + this.stopTraversal(); + // Loops + } else if ( + (node.type === 'ForStatement' || node.type === 'WhileStatement') && + node.body.type !== 'BlockStatement') { + contract = blockWrap(contract, node.body); + keepRunning = true; + this.stopTraversal(); + } else if (node.type === 'FunctionDeclaration' && node.modifiers) { + // We want to remove constant / pure / view from functions + for (let i = 0; i < node.modifiers.length; i++) { + if (['pure', 'constant', 'view'].indexOf(node.modifiers[i].name) > -1) { + contract = contract.slice(0, node.modifiers[i].start) + contract.slice(node.modifiers[i].end); + keepRunning = true; + this.stopTraversal(); + } } } - } - }, - }); + }, + }); + } catch (err) { + contract = err; + keepRunning = false; + } } return contract; }; diff --git a/test/util/mockTruffle.js b/test/util/mockTruffle.js index c333a58..5d801e3 100644 --- a/test/util/mockTruffle.js +++ b/test/util/mockTruffle.js @@ -51,6 +51,7 @@ module.exports.install = function install(contract, test, config, _trufflejs, _t shell.mkdir('./mock/contracts'); shell.mkdir('./mock/migrations'); shell.mkdir('./mock/assets'); + shell.cp('./test/sources/cli/SimpleError.sol', './mock/assets/SimpleError.sol'); shell.mkdir('./mock/node_modules'); shell.mkdir('./mock/test');