Skip to content

Commit ed52bbe

Browse files
authored
Merge pull request #17 from juanjux/fix/bounds
Fix out of bounds error. Consistency fixes
2 parents c4f9c6b + 1f5d615 commit ed52bbe

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

npath.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@ func NPathComplexity(n *uast.Node) []*NPathData {
3838
} else {
3939
funcDecs := deepChildrenOfRoles(n, []uast.Role{uast.Function, uast.Declaration}, []uast.Role{uast.Argument})
4040
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+
}
4352
}
4453
}
4554
for i, function := range funcs {
@@ -98,10 +107,10 @@ func visitIf(n *uast.Node) int {
98107
ifCondition := childrenOfRoles(n, []uast.Role{uast.If, uast.Condition}, nil)
99108
ifElse := childrenOfRoles(n, []uast.Role{uast.If, uast.Else}, nil)
100109

101-
if len(ifElse) == 0 {
102-
npath++
103-
} else {
110+
if len(ifElse) > 0 {
104111
npath += complexityMultOf(ifElse[0])
112+
} else {
113+
npath++
105114
}
106115
npath *= complexityMultOf(ifThen[0])
107116
npath += expressionComp(ifCondition[0])
@@ -116,11 +125,12 @@ func visitWhile(n *uast.Node) int {
116125
whileBody := childrenOfRoles(n, []uast.Role{uast.While, uast.Body}, nil)
117126
whileElse := childrenOfRoles(n, []uast.Role{uast.While, uast.Else}, nil)
118127
// 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 {
122129
npath += complexityMultOf(whileElse[0])
130+
} else {
131+
npath++
123132
}
133+
124134
npath *= complexityMultOf(whileBody[0])
125135
npath += expressionComp(whileCondition[0])
126136

@@ -143,9 +153,9 @@ func visitFor(n *uast.Node) int {
143153
// (npath of for + bool_comp of for + 1) * npath of next
144154
npath := 1
145155
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+
}
149159
npath++
150160
return npath
151161
}
@@ -162,7 +172,7 @@ func visitSwitch(n *uast.Node) int {
162172
switchCases := childrenOfRoles(n, []uast.Role{uast.Statement, uast.Switch, uast.Case}, []uast.Role{uast.Body})
163173
npath := 0
164174

165-
if len(caseDefault) != 0 {
175+
if len(caseDefault) > 0 {
166176
npath += complexityMultOf(caseDefault[0])
167177
} else {
168178
npath++
@@ -185,13 +195,13 @@ func visitTry(n *uast.Node) int {
185195
tryFinaly := childrenOfRoles(n, []uast.Role{uast.Try, uast.Finally}, nil)
186196

187197
catchComp := 0
188-
if len(tryCatch) != 0 {
198+
if len(tryCatch) > 0 {
189199
for _, catch := range tryCatch {
190200
catchComp += complexityMultOf(catch)
191201
}
192202
}
193203
finallyComp := 0
194-
if len(tryFinaly) != 0 {
204+
if len(tryFinaly) > 0 {
195205
finallyComp = complexityMultOf(tryFinaly[0])
196206
}
197207
npath := complexityMultOf(tryBody[0]) + catchComp + finallyComp

0 commit comments

Comments
 (0)