@@ -32,14 +32,14 @@ func NPathComplexity(n *uast.Node) []*NPathData {
32
32
var funcs []* uast.Node
33
33
var names []string
34
34
35
- if containsRole (n , uast .FunctionDeclarationBody ) {
35
+ if containsRoles (n , [] uast.Role { uast . Function , uast . Body }, nil ) {
36
36
funcs = append (funcs , n )
37
37
names = append (names , "NoName" )
38
38
} else {
39
- funcDecs := deepChildrenOfRole (n , uast .FunctionDeclaration )
39
+ funcDecs := deepChildrenOfRoles (n , [] uast.Role { uast . Function , uast . Declaration }, []uast. Role { uast . Argument } )
40
40
for _ , funcDec := range funcDecs {
41
- names = append (names , childrenOfRole (funcDec , uast .FunctionDeclarationName )[0 ].Token )
42
- funcs = append (funcs , childrenOfRole (funcDec , uast .FunctionDeclarationBody )[0 ])
41
+ names = append (names , childrenOfRoles (funcDec , [] uast.Role { uast . Function , uast . Name }, nil )[0 ].Token )
42
+ funcs = append (funcs , childrenOfRoles (funcDec , [] uast.Role { uast . Function , uast . Body }, nil )[0 ])
43
43
}
44
44
}
45
45
for i , function := range funcs {
@@ -51,28 +51,26 @@ func NPathComplexity(n *uast.Node) []*NPathData {
51
51
}
52
52
53
53
func visitorSelector (n * uast.Node ) int {
54
- // I need to add a error when the node dont have any rol
55
- // when I got 2 or more roles that are inside the switch this doesn't work
56
- for _ , role := range n .Roles {
57
- switch role {
58
- case uast .If :
59
- return visitIf (n )
60
- case uast .While :
61
- return visitWhile (n )
62
- case uast .Switch :
63
- return visitSwitch (n )
64
- case uast .DoWhile :
65
- return visitDoWhile (n )
66
- case uast .For :
67
- return visitFor (n )
68
- case uast .ForEach :
69
- return visitForEach (n )
70
- case uast .Return :
71
- return visitReturn (n )
72
- case uast .Try :
73
- return visitTry (n )
74
- default :
75
- }
54
+ if containsRoles (n , []uast.Role {uast .Statement , uast .If }, []uast.Role {uast .Then , uast .Else }) {
55
+ return visitIf (n )
56
+ }
57
+ if containsRoles (n , []uast.Role {uast .Statement , uast .While }, nil ) {
58
+ return visitWhile (n )
59
+ }
60
+ if containsRoles (n , []uast.Role {uast .Statement , uast .Switch }, nil ) {
61
+ return visitSwitch (n )
62
+ }
63
+ if containsRoles (n , []uast.Role {uast .Statement , uast .DoWhile }, nil ) {
64
+ return visitDoWhile (n )
65
+ }
66
+ if containsRoles (n , []uast.Role {uast .Statement , uast .For }, nil ) {
67
+ return visitFor (n )
68
+ }
69
+ if containsRoles (n , []uast.Role {uast .Statement , uast .Return }, nil ) {
70
+ return visitReturn (n )
71
+ }
72
+ if containsRoles (n , []uast.Role {uast .Statement , uast .Try }, nil ) {
73
+ return visitTry (n )
76
74
}
77
75
return visitNotCompNode (n )
78
76
}
@@ -96,21 +94,16 @@ func visitNotCompNode(n *uast.Node) int {
96
94
func visitIf (n * uast.Node ) int {
97
95
// (npath of if + npath of else (or 1) + bool_comp of if) * npath of next
98
96
npath := 0
99
- ifBody := childrenOfRole (n , uast .IfBody )
100
- ifCondition := childrenOfRole (n , uast .IfCondition )
101
- ifElse := childrenOfRole (n , uast .IfElse )
97
+ ifThen := childrenOfRoles (n , [] uast.Role { uast . If , uast . Then }, nil )
98
+ ifCondition := childrenOfRoles (n , [] uast.Role { uast . If , uast . Condition }, nil )
99
+ ifElse := childrenOfRoles (n , [] uast.Role { uast . If , uast . Else }, nil )
102
100
103
101
if len (ifElse ) == 0 {
104
102
npath ++
105
103
} else {
106
- // This if is a short circuit to avoid the two roles in the switch problem
107
- if containsRole (ifElse [0 ], uast .If ) {
108
- npath += visitIf (ifElse [0 ])
109
- } else {
110
- npath += complexityMultOf (ifElse [0 ])
111
- }
104
+ npath += complexityMultOf (ifElse [0 ])
112
105
}
113
- npath *= complexityMultOf (ifBody [0 ])
106
+ npath *= complexityMultOf (ifThen [0 ])
114
107
npath += expressionComp (ifCondition [0 ])
115
108
116
109
return npath
@@ -119,9 +112,9 @@ func visitIf(n *uast.Node) int {
119
112
func visitWhile (n * uast.Node ) int {
120
113
// (npath of while + bool_comp of while + npath of else (or 1)) * npath of next
121
114
npath := 0
122
- whileCondition := childrenOfRole (n , uast .WhileCondition )
123
- whileBody := childrenOfRole (n , uast .WhileBody )
124
- whileElse := childrenOfRole (n , uast .IfElse )
115
+ whileCondition := childrenOfRoles (n , [] uast.Role { uast . While , uast . Condition }, nil )
116
+ whileBody := childrenOfRoles (n , [] uast.Role { uast . While , uast . Body }, nil )
117
+ whileElse := childrenOfRoles (n , [] uast.Role { uast . While , uast . Else }, nil )
125
118
// Some languages like python can have an else in a while loop
126
119
if len (whileElse ) == 0 {
127
120
npath ++
@@ -137,8 +130,8 @@ func visitWhile(n *uast.Node) int {
137
130
func visitDoWhile (n * uast.Node ) int {
138
131
// (npath of do + bool_comp of do + 1) * npath of next
139
132
npath := 1
140
- doWhileCondition := childrenOfRole (n , uast .DoWhileCondition )
141
- doWhileBody := childrenOfRole (n , uast .DoWhileBody )
133
+ doWhileCondition := childrenOfRoles (n , [] uast.Role { uast . DoWhile , uast . Condition }, nil )
134
+ doWhileBody := childrenOfRoles (n , [] uast.Role { uast . DoWhile , uast . Body }, nil )
142
135
143
136
npath *= complexityMultOf (doWhileBody [0 ])
144
137
npath += expressionComp (doWhileCondition [0 ])
@@ -149,7 +142,7 @@ func visitDoWhile(n *uast.Node) int {
149
142
func visitFor (n * uast.Node ) int {
150
143
// (npath of for + bool_comp of for + 1) * npath of next
151
144
npath := 1
152
- forBody := childrenOfRole (n , uast .ForBody )
145
+ forBody := childrenOfRoles (n , [] uast.Role { uast . For , uast . Body }, nil )
153
146
154
147
npath *= complexityMultOf (forBody [0 ])
155
148
@@ -165,8 +158,8 @@ func visitReturn(n *uast.Node) int {
165
158
}
166
159
167
160
func visitSwitch (n * uast.Node ) int {
168
- caseDefault := childrenOfRole (n , uast .SwitchDefault )
169
- switchCases := childrenOfRole (n , uast .SwitchCase )
161
+ caseDefault := childrenOfRoles (n , [] uast.Role { uast . Switch , uast . Default }, nil )
162
+ switchCases := childrenOfRoles (n , [] uast.Role { uast . Statement , uast . Switch , uast . Case }, []uast. Role { uast . Body } )
170
163
npath := 0
171
164
172
165
if len (caseDefault ) != 0 {
@@ -180,25 +173,16 @@ func visitSwitch(n *uast.Node) int {
180
173
return npath
181
174
}
182
175
183
- func visitForEach (n * uast.Node ) int {
184
- forBody := childrenOfRole (n , uast .ForBody )
185
- npath := 1
186
-
187
- npath *= complexityMultOf (forBody [0 ])
188
- npath ++
189
- return npath
190
- }
191
-
192
176
func visitTry (n * uast.Node ) int {
193
177
/*
194
178
In pmd they decided the complexity of a try is the summatory of the complexity
195
- of the tryBody,cathBody and finallyBody .I don't think this is the most acurate way
179
+ of the try body, catch body and finally body .I don't think this is the most acurate way
196
180
of doing this.
197
181
*/
198
182
199
- tryBody := childrenOfRole (n , uast .TryBody )
200
- tryCatch := childrenOfRole (n , uast .TryCatch )
201
- tryFinaly := childrenOfRole (n , uast .TryFinally )
183
+ tryBody := childrenOfRoles (n , [] uast.Role { uast . Try , uast . Body }, nil )
184
+ tryCatch := childrenOfRoles (n , [] uast.Role { uast . Try , uast . Catch }, nil )
185
+ tryFinaly := childrenOfRoles (n , [] uast.Role { uast . Try , uast . Finally }, nil )
202
186
203
187
catchComp := 0
204
188
if len (tryCatch ) != 0 {
@@ -220,59 +204,70 @@ func visitConditionalExpr(n *uast.Node) {
220
204
}
221
205
222
206
func expressionComp (n * uast.Node ) int {
223
- orCount := deepCountChildrenOfRole (n , uast .OpBooleanAnd )
224
- andCount := deepCountChildrenOfRole (n , uast .OpBooleanOr )
207
+ orCount := deepCountChildrenOfRoles (n , [] uast.Role { uast . Operator , uast . Boolean , uast . And }, nil )
208
+ andCount := deepCountChildrenOfRoles (n , [] uast.Role { uast . Operator , uast . Boolean , uast . Or }, nil )
225
209
226
210
return orCount + andCount + 1
227
211
}
228
212
229
- func containsRole (n * uast.Node , role uast.Role ) bool {
213
+ func containsRoles (n * uast.Node , andRoles []uast.Role , notRoles []uast.Role ) bool {
214
+ roleMap := make (map [uast.Role ]bool )
230
215
for _ , r := range n .Roles {
231
- if role == r {
232
- return true
216
+ roleMap [r ] = true
217
+ }
218
+ for _ , r := range andRoles {
219
+ if ! roleMap [r ] {
220
+ return false
221
+ }
222
+ }
223
+ if notRoles != nil {
224
+ for _ , r := range notRoles {
225
+ if roleMap [r ] {
226
+ return false
227
+ }
233
228
}
234
229
}
235
- return false
230
+ return true
236
231
}
237
232
238
- func childrenOfRole (n * uast.Node , role uast.Role ) []* uast.Node {
233
+ func childrenOfRoles (n * uast.Node , andRoles []uast. Role , notRoles [] uast.Role ) []* uast.Node {
239
234
var children []* uast.Node
240
235
for _ , child := range n .Children {
241
- if containsRole (child , role ) {
236
+ if containsRoles (child , andRoles , notRoles ) {
242
237
children = append (children , child )
243
238
}
244
239
}
245
240
return children
246
241
}
247
242
248
- func deepChildrenOfRole (n * uast.Node , role uast.Role ) []* uast.Node {
243
+ func deepChildrenOfRoles (n * uast.Node , andRoles []uast. Role , notRoles [] uast.Role ) []* uast.Node {
249
244
var childList []* uast.Node
250
245
for _ , child := range n .Children {
251
- if containsRole (child , role ) {
246
+ if containsRoles (child , andRoles , notRoles ) {
252
247
childList = append (childList , child )
253
248
}
254
- childList = append (childList , deepChildrenOfRole (child , role )... )
249
+ childList = append (childList , deepChildrenOfRoles (child , andRoles , notRoles )... )
255
250
}
256
251
return childList
257
252
}
258
253
259
- func countChildrenOfRole (n * uast.Node , role uast.Role ) int {
254
+ func countChildrenOfRoles (n * uast.Node , andRoles []uast. Role , notRoles [] uast.Role ) int {
260
255
count := 0
261
256
for _ , child := range n .Children {
262
- if containsRole (child , role ) {
257
+ if containsRoles (child , andRoles , notRoles ) {
263
258
count ++
264
259
}
265
260
}
266
261
return count
267
262
}
268
263
269
- func deepCountChildrenOfRole (n * uast.Node , role uast.Role ) int {
264
+ func deepCountChildrenOfRoles (n * uast.Node , andRoles []uast. Role , notRoles [] uast.Role ) int {
270
265
count := 0
271
266
for _ , child := range n .Children {
272
- if containsRole (child , role ) {
267
+ if containsRoles (child , andRoles , notRoles ) {
273
268
count ++
274
269
}
275
- count += deepCountChildrenOfRole (child , role )
270
+ count += deepCountChildrenOfRoles (child , andRoles , notRoles )
276
271
}
277
272
return count
278
273
}
0 commit comments