@@ -38,8 +38,17 @@ func NPathComplexity(n *uast.Node) []*NPathData {
38
38
} else {
39
39
funcDecs := deepChildrenOfRoles (n , []uast.Role {uast .Function , uast .Declaration }, []uast.Role {uast .Argument })
40
40
for _ , funcDec := range funcDecs {
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 ])
41
+ if containsRoles (funcDec , []uast.Role {uast .Function , uast .Name }, nil ) {
42
+ names = append (names , funcDec .Token )
43
+ }
44
+ childNames := childrenOfRoles (funcDec , []uast.Role {uast .Function , uast .Name }, nil )
45
+ if len (childNames ) > 0 {
46
+ names = append (names , childNames [0 ].Token )
47
+ }
48
+ childFuncs := childrenOfRoles (funcDec , []uast.Role {uast .Function , uast .Body }, nil )
49
+ if len (childFuncs ) > 0 {
50
+ funcs = append (funcs , childFuncs [0 ])
51
+ }
43
52
}
44
53
}
45
54
for i , function := range funcs {
@@ -98,10 +107,16 @@ func visitIf(n *uast.Node) int {
98
107
ifCondition := childrenOfRoles (n , []uast.Role {uast .If , uast .Condition }, nil )
99
108
ifElse := childrenOfRoles (n , []uast.Role {uast .If , uast .Else }, nil )
100
109
101
- if len (ifElse ) == 0 {
102
- npath ++
103
- } else {
110
+ if len (ifElse ) > 0 {
104
111
npath += complexityMultOf (ifElse [0 ])
112
+ } else {
113
+ // This if is a short circuit to avoid the two roles in the switch problem
114
+ if containsRole (ifElse [0 ], uast .If ) {
115
+ npath += visitIf (ifElse [0 ])
116
+ } else {
117
+ npath += complexityMultOf (ifElse [0 ])
118
+ }
119
+ npath ++
105
120
}
106
121
npath *= complexityMultOf (ifThen [0 ])
107
122
npath += expressionComp (ifCondition [0 ])
@@ -116,11 +131,12 @@ func visitWhile(n *uast.Node) int {
116
131
whileBody := childrenOfRoles (n , []uast.Role {uast .While , uast .Body }, nil )
117
132
whileElse := childrenOfRoles (n , []uast.Role {uast .While , uast .Else }, nil )
118
133
// Some languages like python can have an else in a while loop
119
- if len (whileElse ) == 0 {
120
- npath ++
121
- } else {
134
+ if len (whileElse ) > 0 {
122
135
npath += complexityMultOf (whileElse [0 ])
136
+ } else {
137
+ npath ++
123
138
}
139
+
124
140
npath *= complexityMultOf (whileBody [0 ])
125
141
npath += expressionComp (whileCondition [0 ])
126
142
@@ -143,9 +159,9 @@ func visitFor(n *uast.Node) int {
143
159
// (npath of for + bool_comp of for + 1) * npath of next
144
160
npath := 1
145
161
forBody := childrenOfRoles (n , []uast.Role {uast .For , uast .Body }, nil )
146
-
147
- npath *= complexityMultOf (forBody [0 ])
148
-
162
+ if len ( forBody ) > 0 {
163
+ npath *= complexityMultOf (forBody [0 ])
164
+ }
149
165
npath ++
150
166
return npath
151
167
}
@@ -162,7 +178,7 @@ func visitSwitch(n *uast.Node) int {
162
178
switchCases := childrenOfRoles (n , []uast.Role {uast .Statement , uast .Switch , uast .Case }, []uast.Role {uast .Body })
163
179
npath := 0
164
180
165
- if len (caseDefault ) != 0 {
181
+ if len (caseDefault ) > 0 {
166
182
npath += complexityMultOf (caseDefault [0 ])
167
183
} else {
168
184
npath ++
@@ -185,13 +201,13 @@ func visitTry(n *uast.Node) int {
185
201
tryFinaly := childrenOfRoles (n , []uast.Role {uast .Try , uast .Finally }, nil )
186
202
187
203
catchComp := 0
188
- if len (tryCatch ) != 0 {
204
+ if len (tryCatch ) > 0 {
189
205
for _ , catch := range tryCatch {
190
206
catchComp += complexityMultOf (catch )
191
207
}
192
208
}
193
209
finallyComp := 0
194
- if len (tryFinaly ) != 0 {
210
+ if len (tryFinaly ) > 0 {
195
211
finallyComp = complexityMultOf (tryFinaly [0 ])
196
212
}
197
213
npath := complexityMultOf (tryBody [0 ]) + catchComp + finallyComp
0 commit comments