Skip to content

Commit f1cbb69

Browse files
vilchik-elenapynicolas
authored andcommitted
SONARPY-145 Raise issues with precise location for NestedControlFlowDepthCheck
1 parent 47d2916 commit f1cbb69

3 files changed

Lines changed: 34 additions & 27 deletions

File tree

python-checks/src/main/java/org/sonar/python/checks/NestedControlFlowDepthCheck.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
package org.sonar.python.checks;
2121

2222
import com.sonar.sslr.api.AstNode;
23-
import com.sonar.sslr.api.Grammar;
23+
import java.util.ArrayDeque;
24+
import java.util.Deque;
25+
import java.util.Iterator;
2426
import org.sonar.check.Priority;
2527
import org.sonar.check.Rule;
2628
import org.sonar.check.RuleProperty;
29+
import org.sonar.python.PythonCheck;
2730
import org.sonar.python.api.PythonGrammar;
2831
import org.sonar.squidbridge.annotations.ActivatedByDefault;
2932
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
30-
import org.sonar.squidbridge.checks.SquidCheck;
3133

3234
@Rule(
3335
key = NestedControlFlowDepthCheck.CHECK_KEY,
@@ -37,17 +39,18 @@
3739
)
3840
@SqaleConstantRemediation("10min")
3941
@ActivatedByDefault
40-
public class NestedControlFlowDepthCheck extends SquidCheck<Grammar> {
42+
public class NestedControlFlowDepthCheck extends PythonCheck {
4143

4244
public static final String CHECK_KEY = "S134";
4345
private static final int DEFAULT_MAX = 4;
46+
private static final String MESSAGE = "Refactor this code to not nest more than %s \"if\", \"for\", \"while\", \"try\" and \"with\" statements.";
4447

4548
@RuleProperty(
4649
key = "max",
4750
defaultValue = "" + DEFAULT_MAX)
4851
public int max = DEFAULT_MAX;
4952

50-
private int depth;
53+
private Deque<AstNode> depthNodes;
5154

5255
@Override
5356
public void init() {
@@ -61,21 +64,30 @@ public void init() {
6164

6265
@Override
6366
public void visitFile(AstNode astNode) {
64-
depth = 0;
67+
depthNodes = new ArrayDeque<>();
6568
}
6669

6770
@Override
6871
public void visitNode(AstNode node) {
69-
depth++;
70-
if (depth == max + 1) {
71-
String message = "Refactor this code to not nest more than {0} \"if\", \"for\", \"while\", \"try\" and \"with\" statements.";
72-
getContext().createLineViolation(this, message, node, max);
72+
AstNode stmtKeywordNode = node.getFirstChild();
73+
depthNodes.push(stmtKeywordNode);
74+
if (depthNodes.size() == max + 1) {
75+
PreciseIssue issue = addIssue(stmtKeywordNode, String.format(MESSAGE, max));
76+
77+
Iterator<AstNode> depthNodesIterator = depthNodes.iterator();
78+
79+
// skip current node
80+
depthNodesIterator.next();
81+
82+
while (depthNodesIterator.hasNext()) {
83+
issue.secondary(depthNodesIterator.next(), "Nesting +1");
84+
}
7385
}
7486
}
7587

7688
@Override
7789
public void leaveNode(AstNode astNode) {
78-
depth--;
90+
depthNodes.pop();
7991
}
8092
}
8193

python-checks/src/test/java/org/sonar/python/checks/NestedControlFlowDepthCheckTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,14 @@
2121

2222
import java.io.File;
2323
import org.junit.Test;
24-
import org.sonar.python.PythonAstScanner;
25-
import org.sonar.squidbridge.api.SourceFile;
26-
import org.sonar.squidbridge.checks.CheckMessagesVerifier;
24+
import org.sonar.python.checks.utils.PythonCheckVerifier;
2725

2826
public class NestedControlFlowDepthCheckTest {
2927

3028
@Test
3129
public void test() {
3230
NestedControlFlowDepthCheck check = new NestedControlFlowDepthCheck();
33-
SourceFile file = PythonAstScanner.scanSingleFile(new File("src/test/resources/checks/nestedControlFlowDepth.py"), check);
34-
CheckMessagesVerifier.verify(file.getCheckMessages())
35-
.next().atLine(10).withMessage(
36-
"Refactor this code to not nest more than 4 \"if\", \"for\", \"while\", \"try\" and \"with\" statements.")
37-
.noMore();
31+
PythonCheckVerifier.verify(new File("src/test/resources/checks/nestedControlFlowDepth.py"), check);
3832
}
3933

4034
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
for y in range(10):
2-
if x > y:
3-
if x > 1:
4-
pass
2+
if x > y:
3+
if x > 1:
4+
pass
55

66
for x in range(10):
7-
while x:
8-
for y in range(10):
9-
if x > y:
10-
if x > 1:
11-
if y > 10:
12-
pass
7+
while x:
8+
for y in range(10):
9+
if x > y:
10+
if x > 1: # Noncompliant [[secondary=-4,-3,-2,-1]] {{Refactor this code to not nest more than 4 "if", "for", "while", "try" and "with" statements.}}
11+
# ^^
12+
if y > 10:
13+
pass

0 commit comments

Comments
 (0)