Skip to content

Update OZ solhint custom rules [WIP] #5794

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

Draft
wants to merge 7 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions scripts/solhint-custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Base {

module.exports = [
class extends Base {
static ruleId = 'interface-names';
static ruleId = 'interface-only-external-functions';

ContractDefinition(node) {
if (node.kind === 'interface' && !/^I[A-Z]/.test(node.name)) {
this.error(node, 'Interface names should have a capital I prefix');
FunctionDefinition(node) {
if (node.parent.kind === 'interface' && node.visibility !== 'external') {
this.error(node, 'Interface functions must be external');
}
}
},
Expand All @@ -36,8 +36,7 @@ module.exports = [
static ruleId = 'private-variables';

VariableDeclaration(node) {
const constantOrImmutable = node.isDeclaredConst || node.isImmutable;
if (node.isStateVar && !constantOrImmutable && node.visibility !== 'private') {
if (node.isStateVar && !node.isDeclaredConst && !node.isImmutable && node.visibility !== 'private') {
this.error(node, 'State variables must be private');
}
}
Expand All @@ -47,38 +46,49 @@ module.exports = [
static ruleId = 'leading-underscore';

VariableDeclaration(node) {
if (node.isDeclaredConst) {
// TODO: expand visibility and fix
if (node.visibility === 'private' && /^_/.test(node.name)) {
this.error(node, 'Constant variables should not have leading underscore');
}
} else if (node.visibility === 'private' && !/^_/.test(node.name)) {
this.error(node, 'Non-constant private variables must have leading underscore');
if (node.isDeclaredConst && node.name.startsWith('_')) {
this.error(node, 'Constant variables should not have leading underscore');
}
if (node.isImmutable && node.name.startsWith('_')) {
this.error(node, 'Immutable variables should not have leading underscore');
}
if (node.isStateVar && node.visibility === 'private' && !node.name.startsWith('_')) {
this.error(node, 'Private state variables must have leading underscore');
}
if (node.isStateVar && node.visibility === 'internal' && !node.name.startsWith('_')) {
this.error(node, 'Internal state variables must have leading underscore');
}
if (node.isStateVar && node.visibility === 'public' && node.name.startsWith('_')) {
this.error(node, 'Public state variables should not have leading underscore');
}
}

FunctionDefinition(node) {
if (node.visibility === 'private' || (node.visibility === 'internal' && node.parent.kind !== 'library')) {
if (!/^_/.test(node.name)) {
this.error(node, 'Private and internal functions must have leading underscore');
}
if (node.visibility === 'private' && !node.name.startsWith('_')) {
this.error(node, 'Private functions must have leading underscore');
}
if (node.visibility === 'internal' && node.parent.kind !== 'library' && !node.name.startsWith('_')) {
this.error(node, 'Non-library internal functions must have leading underscore');
}
if (node.visibility === 'internal' && node.parent.kind === 'library' && node.name.startsWith('_')) {
this.error(node, 'Library internal functions should not have leading underscore');
}
if (node.visibility === 'internal' && node.parent.kind === 'library') {
if (/^_/.test(node.name)) {
this.error(node, 'Library internal functions should not have leading underscore');
}
if (node.visibility === 'public' && node.name.startsWith('_')) {
this.error(node, 'Public functions should not have leading underscore');
}
if (node.visibility === 'external' && node.name.startsWith('_')) {
this.error(node, 'External functions should not have leading underscore');
}
}
},

// TODO: re-enable and fix
// class extends Base {
// static ruleId = 'no-external-virtual';
//
// FunctionDefinition(node) {
// if (node.visibility == 'external' && node.isVirtual) {
// this.error(node, 'Functions should not be external and virtual');
// }
// }
// },
class extends Base {
static ruleId = 'no-external-virtual';

FunctionDefinition(node) {
if (node.visibility == 'external' && node.isVirtual) {
this.error(node, 'Functions should not be external and virtual');
}
}
},
];
Loading