@@ -4,7 +4,7 @@ use crate::rules::types::*;
4
4
use crate :: types:: * ;
5
5
use solc_wrapper:: { ContractDefinitionChildNodes , ContractKind , decode_location, NodeType , SourceLocation , SourceUnit , SourceUnitChildNodes } ;
6
6
7
- pub struct Ordering {
7
+ pub struct OrderingContract {
8
8
data : RuleEntry
9
9
}
10
10
@@ -14,93 +14,45 @@ pub struct Eval {
14
14
src : SourceLocation
15
15
}
16
16
17
- fn eval_file ( source_unit_childs : & Vec < SourceUnitChildNodes > ) -> Vec < Eval > {
18
-
19
- let mut eval = Vec :: new ( ) ;
20
-
21
- for node in source_unit_childs {
22
- match node {
23
- SourceUnitChildNodes :: ErrorDefinition ( error) => {
24
- if error. node_type == NodeType :: EnumDefinition {
25
- eval. push ( Eval { weight : 1 , src : error. src . clone ( ) } ) ;
26
- } else if error. node_type == NodeType :: StructDefinition {
27
- eval. push ( Eval { weight : 2 , src : error. src . clone ( ) } ) ;
28
- }
29
- //TODO: Remove this when Error definition fixed
30
- }
31
- SourceUnitChildNodes :: EnumDefinition ( tmp) => eval. push ( Eval { weight : 2 , src : tmp. src . clone ( ) } ) ,
32
- SourceUnitChildNodes :: StructDefinition ( tmp) => eval. push ( Eval { weight : 3 , src : tmp. src . clone ( ) } ) ,
33
- SourceUnitChildNodes :: ContractDefinition ( contract) => {
34
- if contract. contract_kind == ContractKind :: Interface {
35
- eval. push ( Eval { weight : 3 , src : contract. src . clone ( ) } ) ;
36
- } else if contract. contract_kind == ContractKind :: Library {
37
- eval. push ( Eval { weight : 4 , src : contract. src . clone ( ) } ) ;
38
- } else {
39
- eval. push ( Eval { weight : 5 , src : contract. src . clone ( ) } ) ;
40
- }
41
- }
42
- _ => { continue ; }
43
- }
44
- }
45
-
46
- eval
47
- }
48
-
49
17
fn eval_contract ( contract_child_node : & Vec < ContractDefinitionChildNodes > ) -> Vec < Eval > {
50
18
51
19
let mut eval = Vec :: new ( ) ;
52
20
53
21
for node in contract_child_node {
54
22
match node {
23
+
24
+ ContractDefinitionChildNodes :: UsingForDirective ( tmp) => eval. push ( Eval { weight : 1 , src : tmp. src . clone ( ) } ) ,
25
+
55
26
ContractDefinitionChildNodes :: ErrorDefinition ( tmp) => {
56
27
//TODO: Remove this when Error definition fixed
57
28
if tmp. node_type == NodeType :: EnumDefinition {
58
29
eval. push ( Eval { weight : 3 , src : tmp. src . clone ( ) } ) ;
59
30
} else if tmp. node_type == NodeType :: StructDefinition {
60
31
eval. push ( Eval { weight : 2 , src : tmp. src . clone ( ) } ) ;
32
+ } else if tmp. node_type == NodeType :: EventDefinition {
33
+ eval. push ( Eval { weight : 5 , src : tmp. src . clone ( ) } ) ;
61
34
}
62
35
} ,
63
- ContractDefinitionChildNodes :: UsingForDirective ( tmp) => eval. push ( Eval { weight : 1 , src : tmp. src . clone ( ) } ) ,
64
36
ContractDefinitionChildNodes :: StructDefinition ( tmp) => eval. push ( Eval { weight : 2 , src : tmp. src . clone ( ) } ) ,
65
37
ContractDefinitionChildNodes :: EnumDefinition ( tmp) => eval. push ( Eval { weight : 3 , src : tmp. src . clone ( ) } ) ,
38
+
66
39
ContractDefinitionChildNodes :: VariableDeclaration ( tmp) => eval. push ( Eval { weight : 4 , src : tmp. src . clone ( ) } ) ,
67
40
ContractDefinitionChildNodes :: EventDefinition ( tmp) => eval. push ( Eval { weight : 5 , src : tmp. src . clone ( ) } ) ,
68
41
ContractDefinitionChildNodes :: ModifierDefinition ( tmp) => eval. push ( Eval { weight : 6 , src : tmp. src . clone ( ) } ) ,
69
42
ContractDefinitionChildNodes :: FunctionDefinition ( tmp) => eval. push ( Eval { weight : 7 , src : tmp. src . clone ( ) } ) ,
43
+
70
44
_ => { continue ; }
71
45
}
72
46
}
73
47
74
48
eval
75
49
}
76
50
77
- impl RuleType for Ordering {
51
+ impl RuleType for OrderingContract {
78
52
79
53
fn diagnose ( & self , file : & SolidFile , files : & Vec < SolidFile > ) -> Vec < LintDiag > {
80
54
81
55
let mut res = Vec :: new ( ) ;
82
- let eval = eval_file ( & file. data . nodes ) ;
83
-
84
- if eval. len ( ) > 1 {
85
- for i in 0 ..eval. len ( ) - 1 {
86
- if eval[ i] . weight > eval[ i + 1 ] . weight {
87
- let location = decode_location ( & eval[ i] . src , & file. content ) ;
88
- res. push ( LintDiag {
89
- range : Range {
90
- start : Position { line : location. 0 . line as u64 , character : location. 0 . column as u64 } ,
91
- end : Position { line : location. 1 . line as u64 , character : location. 1 . column as u64 } ,
92
- length : location. 0 . length as u64 ,
93
- } ,
94
- message : format ! ( "File need to be ordered: Using for -> Struct -> Enum -> Variable -> Event -> Modifier -> Function" ) ,
95
- severity : Some ( self . data . severity ) ,
96
- code : None ,
97
- source : None ,
98
- uri : file. path . clone ( ) ,
99
- source_file_content : file. content . clone ( ) ,
100
- } ) ;
101
- }
102
- }
103
- }
104
56
105
57
for node in & file. data . nodes {
106
58
match node {
@@ -117,7 +69,7 @@ impl RuleType for Ordering {
117
69
end : Position { line : location. 1 . line as u64 , character : location. 1 . column as u64 } ,
118
70
length : location. 0 . length as u64 ,
119
71
} ,
120
- message : format ! ( "Contract need to be ordered: Enum -> Struct -> Interface -> Library -> Contract " ) ,
72
+ message : format ! ( "Contract need to be ordered: Using for -> Struct -> Enum -> Variable -> Event -> Modifier -> Function " ) ,
121
73
severity : Some ( self . data . severity ) ,
122
74
code : None ,
123
75
source : None ,
@@ -135,17 +87,17 @@ impl RuleType for Ordering {
135
87
}
136
88
}
137
89
138
- impl Ordering {
90
+ impl OrderingContract {
139
91
pub ( crate ) fn create ( data : RuleEntry ) -> Box < dyn RuleType > {
140
- let mut rule = Ordering {
92
+ let mut rule = OrderingContract {
141
93
data
142
94
} ;
143
95
Box :: new ( rule)
144
96
}
145
97
146
98
pub ( crate ) fn create_default ( ) -> RuleEntry {
147
99
RuleEntry {
148
- id : "ordering" . to_string ( ) ,
100
+ id : "ordering-contract " . to_string ( ) ,
149
101
severity : Severity :: WARNING ,
150
102
data : vec ! [ ]
151
103
}
0 commit comments