Merge pull request #156 from sc-forks/fix/parse-everything

Handle *.sol files that are invalid Solidity
pull/117/merge
c-g-e-w-e-k-e- 7 years ago committed by GitHub
commit 4342743e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      lib/app.js
  2. 73
      lib/preprocessor.js
  3. 1
      test/util/mockTruffle.js

@ -286,9 +286,15 @@ class App {
postProcessPure(env) { postProcessPure(env) {
shell.ls(`${env}/**/*.sol`).forEach(file => { shell.ls(`${env}/**/*.sol`).forEach(file => {
const contractPath = this.platformNeutralPath(file); const contractPath = this.platformNeutralPath(file);
let contract = fs.readFileSync(contractPath).toString(); const contract = fs.readFileSync(contractPath).toString();
contract = preprocessor.run(contract); const contractProcessed = preprocessor.run(contract);
fs.writeFileSync(contractPath, 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);
}
}); });
} }

@ -26,42 +26,47 @@ module.exports.run = function r(contract) {
let keepRunning = true; let keepRunning = true;
while (keepRunning) { while (keepRunning) {
const ast = SolidityParser.parse(contract); try {
keepRunning = false; const ast = SolidityParser.parse(contract);
SolExplore.traverse(ast, { keepRunning = false;
enter(node, parent) { // eslint-disable-line no-loop-func SolExplore.traverse(ast, {
// If consequents enter(node, parent) { // eslint-disable-line no-loop-func
if (node.type === 'IfStatement' && node.consequent.type !== 'BlockStatement') { // If consequents
contract = blockWrap(contract, node.consequent); if (node.type === 'IfStatement' && node.consequent.type !== 'BlockStatement') {
keepRunning = true; contract = blockWrap(contract, node.consequent);
this.stopTraversal(); keepRunning = true;
// If alternates this.stopTraversal();
} else if ( // If alternates
node.type === 'IfStatement' && } else if (
node.alternate && node.type === 'IfStatement' &&
node.alternate.type !== 'BlockStatement') { node.alternate &&
contract = blockWrap(contract, node.alternate); node.alternate.type !== 'BlockStatement') {
keepRunning = true; contract = blockWrap(contract, node.alternate);
this.stopTraversal(); keepRunning = true;
// Loops this.stopTraversal();
} else if ( // Loops
(node.type === 'ForStatement' || node.type === 'WhileStatement') && } else if (
node.body.type !== 'BlockStatement') { (node.type === 'ForStatement' || node.type === 'WhileStatement') &&
contract = blockWrap(contract, node.body); node.body.type !== 'BlockStatement') {
keepRunning = true; contract = blockWrap(contract, node.body);
this.stopTraversal(); keepRunning = true;
} else if (node.type === 'FunctionDeclaration' && node.modifiers) { this.stopTraversal();
// We want to remove constant / pure / view from functions } else if (node.type === 'FunctionDeclaration' && node.modifiers) {
for (let i = 0; i < node.modifiers.length; i++) { // We want to remove constant / pure / view from functions
if (['pure', 'constant', 'view'].indexOf(node.modifiers[i].name) > -1) { for (let i = 0; i < node.modifiers.length; i++) {
contract = contract.slice(0, node.modifiers[i].start) + contract.slice(node.modifiers[i].end); if (['pure', 'constant', 'view'].indexOf(node.modifiers[i].name) > -1) {
keepRunning = true; contract = contract.slice(0, node.modifiers[i].start) + contract.slice(node.modifiers[i].end);
this.stopTraversal(); keepRunning = true;
this.stopTraversal();
}
} }
} }
} },
}, });
}); } catch (err) {
contract = err;
keepRunning = false;
}
} }
return contract; return contract;
}; };

@ -51,6 +51,7 @@ module.exports.install = function install(contract, test, config, _trufflejs, _t
shell.mkdir('./mock/contracts'); shell.mkdir('./mock/contracts');
shell.mkdir('./mock/migrations'); shell.mkdir('./mock/migrations');
shell.mkdir('./mock/assets'); shell.mkdir('./mock/assets');
shell.cp('./test/sources/cli/SimpleError.sol', './mock/assets/SimpleError.sol');
shell.mkdir('./mock/node_modules'); shell.mkdir('./mock/node_modules');
shell.mkdir('./mock/test'); shell.mkdir('./mock/test');

Loading…
Cancel
Save