Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solhint rule to check custom error names #4543

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions scripts/solhint-custom/index.js
Expand Up @@ -81,4 +81,34 @@ module.exports = [
// }
// }
// },

class extends Base {
static ruleId = 'custom-error-domains';

ContractDefinition(node) {
const domains = [node?.name, ...(node?.baseContracts?.map(({ baseName }) => baseName.namePath) ?? [])].map(
this._getDomainFromContractName,
);

node?.subNodes
?.filter(
({ type, name }) => type === 'CustomErrorDefinition' && !domains.find(domain => name.startsWith(domain)),
)
.forEach(customError => this.error(customError, 'Custom errors should contain corresponding domain prefix'));
}

_getDomainFromContractName(contractName) {
let domainName = contractName;

if (/^I[A-Z]/.test(domainName)) {
domainName = contractName.substring(1);
}

if (/^ERC\d+/.test(domainName)) {
domainName = domainName.match(/^ERC\d+/)[0];
}

return domainName;
}
},
];