Skip to content

Commit

Permalink
Rewrite without solidity-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Aug 29, 2023
1 parent 054c2b7 commit ee032e0
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions scripts/checks/custom-errors-duplication.js
@@ -1,46 +1,55 @@
const fs = require('fs');
const path = require('path');
const { GlobSync } = require('glob'); // used by solhint
const parser = require('@solidity-parser/parser');

// Files matching these patterns will be ignored unless a rule has `static global = true`
const excludedDirs = ['contracts/mocks/**/*', 'test/**/*'];
const { findAll } = require('solidity-ast/utils');
const { _: artifacts } = require('yargs').argv;

const excludedDirs = ['contracts/mocks/', 'test/', 'contracts-exposed'];
const ROOT_PATH = path.resolve(__dirname, '../..');

function main() {
const reports = processPath('contracts/**/*.sol');
const reports = processArtifacts(artifacts);

if (reports.length > 0) {
console.log('Found duplicates in custom errors:');
for (const report of reports) {
let output = '';
for (const file of report.files) {
output += `${report.name}: declared in ${file.file}, line: ${file.node.loc.start.line}, column: ${file.node.loc.start.column}\n`;
output += `${report.name}: declared in ${file.file}\n`;
}
console.log(output, '\n');
}
}
exitWithCode(reports);

if (reports.length > 0) process.exitCode = 1;

if (!process.exitCode) {
console.log('No duplicate custom errors found.');
}
}

function processPath(expression) {
const allFiles = GlobSync(expression, { nodir: true, cwd: ROOT_PATH, ignore: excludedDirs }).found;
function processArtifacts(artifacts) {

const filesContainingCustomError = {};

allFiles.map(curFile => {
const customErrors = processFile(path.resolve(ROOT_PATH, curFile));
if (customErrors.length > 0) {
for (const customError of customErrors) {
if (!filesContainingCustomError[customError.name]) {
filesContainingCustomError[customError.name] = [{ file: curFile, node: customError }];
} else {
filesContainingCustomError[customError.name].push({ file: curFile, node: customError });
for (const artifact of artifacts) {
const { output: solcOutput } = require(path.resolve(ROOT_PATH, artifact));

for (const source in solcOutput.contracts) {
if (excludedDirs.some(pattern => source.startsWith(pattern))) {
continue;
}

const customErrors = processSource(source, solcOutput);
if (customErrors.length > 0) {
for (const customError of customErrors) {
if (!filesContainingCustomError[customError.name]) {
filesContainingCustomError[customError.name] = [{ file: source, node: customError }];
} else {
filesContainingCustomError[customError.name].push({ file: source, node: customError });
}
}
}
}
});
}

const duplicates = [];
for (const customError of Object.keys(filesContainingCustomError)) {
Expand All @@ -52,29 +61,12 @@ function processPath(expression) {
return duplicates;
}

function processFile(filePath) {
const ast = parseInput(fs.readFileSync(filePath).toString());
function processSource(source, solcOutput) {
const customErrors = [];
parser.visit(ast, {
CustomErrorDefinition: node => {
customErrors.push(node);
},
});
return customErrors;
}

function parseInput(inputStr) {
try {
// first we try to parse the string as we normally do
return parser.parse(inputStr, { loc: true, range: true });
} catch (e) {
// using 'loc' may throw when inputStr is empty or only has comments
return parser.parse(inputStr, {});
for (const customError of findAll('ErrorDefinition', solcOutput.sources[source].ast)) {
customErrors.push(customError);
}
}

function exitWithCode(reports) {
process.exit(reports.length > 0 ? 1 : 0);
return customErrors;
}

main();

0 comments on commit ee032e0

Please sign in to comment.