Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Aug 29, 2023
1 parent c521f48 commit 24a8eca
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions scripts/checks/custom-errors-duplication.js
@@ -1,8 +1,7 @@
const fs = require('fs');
const path = require('path');
const { GlobSync } = require('glob'); // used by solhint
const { GlobSync } = require('glob'); // used by solhint
const parser = require('@solidity-parser/parser');
const { findAll } = require('solidity-ast/utils');

// Files matching these patterns will be ignored unless a rule has `static global = true`
const excludedDirs = ['contracts/mocks/**/*', 'test/**/*'];
Expand All @@ -12,7 +11,6 @@ const ROOT_PATH = path.resolve(__dirname, '../..');
function main() {
const reports = processPath('contracts/**/*.sol');

console.log(reports)
if (reports.length > 0) {
console.log('Found duplicates in custom errors:');
for (const report of reports) {
Expand All @@ -27,56 +25,56 @@ function main() {
}

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

const filesContainingCustomError = {};
allFiles.map((curFile) => {

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}];
filesContainingCustomError[customError.name] = [{ file: curFile, node: customError }];
} else {
filesContainingCustomError[customError.name].push({file: curFile, node: customError});
filesContainingCustomError[customError.name].push({ file: curFile, node: customError });
}
}
}
})
});

const duplicates = [];
for (const customError of Object.keys(filesContainingCustomError)) {
if (filesContainingCustomError[customError].length > 1) {
duplicates.push({name: customError, files: filesContainingCustomError[customError]});
duplicates.push({ name: customError, files: filesContainingCustomError[customError] });
}
}

return duplicates;
}

function processFile(filePath) {
const ast = parseInput(fs.readFileSync(filePath).toString())
const ast = parseInput(fs.readFileSync(filePath).toString());
const customErrors = [];
parser.visit(ast, {
CustomErrorDefinition: (node) => {
CustomErrorDefinition: node => {
customErrors.push(node);
}
})
return customErrors
},
});
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 })
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, {})
return parser.parse(inputStr, {});
}
}

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

main();
main();

0 comments on commit 24a8eca

Please sign in to comment.