Commit b77bb1a
authored
fix: cognitive complexity nesting with if-else chains (#1268)
* test: prove bug in cognitive complexity rule
* fix: cognitive complexity nesting with if-else chains
Currently, an if-else chain will increase the nesting level and add the
nesting increment for every addition `else if` statement in an if-else
chain. This is incorrect behaviour; an `else if` statement should
increment complexity by 1 (regardless of current nesting level) and
leave the nesting level as-is.
For example, the following should yield a total complexity of 5:
```
for { // +1
if a { // +2 (nesting = 1)
foo()
} else if b { // +1
bar()
} else if c { // +1
baz()
}
}
```
but the current implementation incorrectly increments the nesting level
with each `else if` and adds the nesting increment where it shouldn't:
```
for { // +1
if a { // +2 (nesting = 1)
foo()
} else if b { // +3 (nesting = 2)
bar()
} else if c { // +4 (nesting = 3)
baz()
}
}
```1 parent 6d0498c commit b77bb1a
File tree
2 files changed
+37
-2
lines changed- rule
- testdata
2 files changed
+37
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
102 | | - | |
| 101 | + | |
103 | 102 | | |
104 | 103 | | |
105 | 104 | | |
| |||
156 | 155 | | |
157 | 156 | | |
158 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
159 | 181 | | |
160 | 182 | | |
161 | 183 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
0 commit comments