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) {
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);
}
});
}

@ -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;
};

@ -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');

Loading…
Cancel
Save