Skip to content

Commit 90f93e9

Browse files
author
Juanjo Alvarez
committed
Fix out of bounds error. Consistency fixes
Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent c4f9c6b commit 90f93e9

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

npath.go

+30-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,16 @@ 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+
// 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++
105120
}
106121
npath *= complexityMultOf(ifThen[0])
107122
npath += expressionComp(ifCondition[0])
@@ -116,11 +131,12 @@ func visitWhile(n *uast.Node) int {
116131
whileBody := childrenOfRoles(n, []uast.Role{uast.While, uast.Body}, nil)
117132
whileElse := childrenOfRoles(n, []uast.Role{uast.While, uast.Else}, nil)
118133
// 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 {
122135
npath += complexityMultOf(whileElse[0])
136+
} else {
137+
npath++
123138
}
139+
124140
npath *= complexityMultOf(whileBody[0])
125141
npath += expressionComp(whileCondition[0])
126142

@@ -143,9 +159,9 @@ func visitFor(n *uast.Node) int {
143159
// (npath of for + bool_comp of for + 1) * npath of next
144160
npath := 1
145161
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+
}
149165
npath++
150166
return npath
151167
}
@@ -162,7 +178,7 @@ func visitSwitch(n *uast.Node) int {
162178
switchCases := childrenOfRoles(n, []uast.Role{uast.Statement, uast.Switch, uast.Case}, []uast.Role{uast.Body})
163179
npath := 0
164180

165-
if len(caseDefault) != 0 {
181+
if len(caseDefault) > 0 {
166182
npath += complexityMultOf(caseDefault[0])
167183
} else {
168184
npath++
@@ -185,13 +201,13 @@ func visitTry(n *uast.Node) int {
185201
tryFinaly := childrenOfRoles(n, []uast.Role{uast.Try, uast.Finally}, nil)
186202

187203
catchComp := 0
188-
if len(tryCatch) != 0 {
204+
if len(tryCatch) > 0 {
189205
for _, catch := range tryCatch {
190206
catchComp += complexityMultOf(catch)
191207
}
192208
}
193209
finallyComp := 0
194-
if len(tryFinaly) != 0 {
210+
if len(tryFinaly) > 0 {
195211
finallyComp = complexityMultOf(tryFinaly[0])
196212
}
197213
npath := complexityMultOf(tryBody[0]) + catchComp + finallyComp

0 commit comments

Comments
 (0)