@@ -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,10 @@ 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
+ npath ++
105
114
}
106
115
npath *= complexityMultOf (ifThen [0 ])
107
116
npath += expressionComp (ifCondition [0 ])
@@ -116,11 +125,12 @@ func visitWhile(n *uast.Node) int {
116
125
whileBody := childrenOfRoles (n , []uast.Role {uast .While , uast .Body }, nil )
117
126
whileElse := childrenOfRoles (n , []uast.Role {uast .While , uast .Else }, nil )
118
127
// Some languages like python can have an else in a while loop
119
- if len (whileElse ) == 0 {
120
- npath ++
121
- } else {
128
+ if len (whileElse ) > 0 {
122
129
npath += complexityMultOf (whileElse [0 ])
130
+ } else {
131
+ npath ++
123
132
}
133
+
124
134
npath *= complexityMultOf (whileBody [0 ])
125
135
npath += expressionComp (whileCondition [0 ])
126
136
@@ -143,9 +153,9 @@ func visitFor(n *uast.Node) int {
143
153
// (npath of for + bool_comp of for + 1) * npath of next
144
154
npath := 1
145
155
forBody := childrenOfRoles (n , []uast.Role {uast .For , uast .Body }, nil )
146
-
147
- npath *= complexityMultOf (forBody [0 ])
148
-
156
+ if len ( forBody ) > 0 {
157
+ npath *= complexityMultOf (forBody [0 ])
158
+ }
149
159
npath ++
150
160
return npath
151
161
}
@@ -162,7 +172,7 @@ func visitSwitch(n *uast.Node) int {
162
172
switchCases := childrenOfRoles (n , []uast.Role {uast .Statement , uast .Switch , uast .Case }, []uast.Role {uast .Body })
163
173
npath := 0
164
174
165
- if len (caseDefault ) != 0 {
175
+ if len (caseDefault ) > 0 {
166
176
npath += complexityMultOf (caseDefault [0 ])
167
177
} else {
168
178
npath ++
@@ -185,13 +195,13 @@ func visitTry(n *uast.Node) int {
185
195
tryFinaly := childrenOfRoles (n , []uast.Role {uast .Try , uast .Finally }, nil )
186
196
187
197
catchComp := 0
188
- if len (tryCatch ) != 0 {
198
+ if len (tryCatch ) > 0 {
189
199
for _ , catch := range tryCatch {
190
200
catchComp += complexityMultOf (catch )
191
201
}
192
202
}
193
203
finallyComp := 0
194
- if len (tryFinaly ) != 0 {
204
+ if len (tryFinaly ) > 0 {
195
205
finallyComp = complexityMultOf (tryFinaly [0 ])
196
206
}
197
207
npath := complexityMultOf (tryBody [0 ]) + catchComp + finallyComp
0 commit comments