Skip to content

Commit 101f119

Browse files
committed
improve
1 parent add0ff0 commit 101f119

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

scripts/solhint-custom/index.js

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Base {
1414
}
1515
}
1616

17-
error(node, message) {
18-
if (!this.ignored) {
17+
require(condition, node, message) {
18+
if (!condition && !this.ignored) {
1919
this.reporter.error(node, this.ruleId, message);
2020
}
2121
}
@@ -26,8 +26,8 @@ module.exports = [
2626
static ruleId = 'interface-only-external-functions';
2727

2828
FunctionDefinition(node) {
29-
if (node.parent.kind === 'interface' && node.visibility !== 'external') {
30-
this.error(node, 'Interface functions must be external');
29+
if (node.parent.kind === 'interface') {
30+
this.require(node.visibility === 'external', node, 'Interface functions must be external');
3131
}
3232
}
3333
},
@@ -36,8 +36,12 @@ module.exports = [
3636
static ruleId = 'private-variables';
3737

3838
VariableDeclaration(node) {
39-
if (node.isStateVar && !node.isDeclaredConst && !node.isImmutable && node.visibility !== 'private') {
40-
this.error(node, 'State variables must be private');
39+
if (node.isStateVar) {
40+
this.require(
41+
node.isDeclaredConst || node.isImmutable || node.visibility === 'private',
42+
node,
43+
'State variables must be private',
44+
);
4145
}
4246
}
4347
},
@@ -46,38 +50,45 @@ module.exports = [
4650
static ruleId = 'leading-underscore';
4751

4852
VariableDeclaration(node) {
49-
if (node.isDeclaredConst && node.name.startsWith('_')) {
50-
this.error(node, 'Constant variables should not have leading underscore');
51-
}
52-
if (node.isImmutable && node.name.startsWith('_')) {
53-
this.error(node, 'Immutable variables should not have leading underscore');
54-
}
55-
if (node.isStateVar && node.visibility === 'private' && !node.name.startsWith('_')) {
56-
this.error(node, 'Private state variables must have leading underscore');
57-
}
58-
if (node.isStateVar && node.visibility === 'internal' && !node.name.startsWith('_')) {
59-
this.error(node, 'Internal state variables must have leading underscore');
60-
}
61-
if (node.isStateVar && node.visibility === 'public' && node.name.startsWith('_')) {
62-
this.error(node, 'Public state variables should not have leading underscore');
53+
if (node.isDeclaredConst) {
54+
this.require(!node.name.startsWith('_'), node, 'Constant variables should not have leading underscore');
55+
} else if (node.isImmutable) {
56+
this.require(!node.name.startsWith('_'), node, 'Immutable variables should not have leading underscore');
57+
} else if (node.isStateVar) {
58+
switch (node.visibility) {
59+
case 'private':
60+
this.require(node.name.startsWith('_'), node, 'Private state variables must have leading underscore');
61+
break;
62+
case 'internal':
63+
this.require(node.name.startsWith('_'), node, 'Internal state variables must have leading underscore');
64+
break;
65+
case 'public':
66+
this.require(!node.name.startsWith('_'), node, 'Public state variables should not have leading underscore');
67+
break;
68+
}
6369
}
6470
}
6571

6672
FunctionDefinition(node) {
67-
if (node.visibility === 'private' && !node.name.startsWith('_')) {
68-
this.error(node, 'Private functions must have leading underscore');
69-
}
70-
if (node.visibility === 'internal' && node.parent.kind !== 'library' && !node.name.startsWith('_')) {
71-
this.error(node, 'Non-library internal functions must have leading underscore');
72-
}
73-
if (node.visibility === 'internal' && node.parent.kind === 'library' && node.name.startsWith('_')) {
74-
this.error(node, 'Library internal functions should not have leading underscore');
75-
}
76-
if (node.visibility === 'public' && node.name.startsWith('_')) {
77-
this.error(node, 'Public functions should not have leading underscore');
78-
}
79-
if (node.visibility === 'external' && node.name.startsWith('_')) {
80-
this.error(node, 'External functions should not have leading underscore');
73+
switch (node.visibility) {
74+
case 'external':
75+
this.require(!node.name.startsWith('_'), node, 'External functions should not have leading underscore');
76+
break;
77+
case 'public':
78+
this.require(!node.name.startsWith('_'), node, 'Public functions should not have leading underscore');
79+
break;
80+
case 'internal':
81+
this.require(
82+
node.name.startsWith('_') !== (node.parent.kind === 'library'),
83+
node,
84+
node.parent.kind === 'library'
85+
? 'Library internal functions should not have leading underscore'
86+
: 'Non-library internal functions must have leading underscore',
87+
);
88+
break;
89+
case 'private':
90+
this.require(node.name.startsWith('_'), node, 'Private functions must have leading underscore');
91+
break;
8192
}
8293
}
8394
},
@@ -86,8 +97,12 @@ module.exports = [
8697
static ruleId = 'no-external-virtual';
8798

8899
FunctionDefinition(node) {
89-
if (node.visibility == 'external' && node.isVirtual) {
90-
this.error(node, 'Functions should not be external and virtual');
100+
if (node.visibility == 'external') {
101+
this.require(
102+
node.isReceiveEther || node.isFallback || !node.isVirtual,
103+
node,
104+
'Functions should not be external and virtual',
105+
);
91106
}
92107
}
93108
},

0 commit comments

Comments
 (0)