Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static void createAndActivateRuleFromTemplate() {
.setParam("template_key", "python:XPath")
.setParam("custom_key", RULE_KEY)
.setParam("prevent_reactivation", "true")
.setParam("params", "message=\"Do something fantastic!\";xpathQuery=\"//FILE_INPUT\"")).failIfNotSuccessful();
.setParam("params", "message=\"Do something fantastic!\";xpathQuery=\"//CLASSDEF\"")).failIfNotSuccessful();

Qualityprofiles.SearchWsResponse.QualityProfile qualityProfile = newWsClient().qualityprofiles().search(new SearchRequest()).getProfilesList().stream()
.filter(qp -> qp.getLanguage().equals(language))
Expand Down
25 changes: 13 additions & 12 deletions python-checks/src/main/java/org/sonar/python/checks/XPathCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.xpath.api.AstNodeXPathQuery;
import java.util.List;
import javax.annotation.CheckForNull;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.python.PythonCheck;
Expand All @@ -44,7 +44,8 @@ public class XPathCheck extends PythonCheck {

private AstNodeXPathQuery<Object> query = null;

public AstNodeXPathQuery<Object> query() {
@CheckForNull
private AstNodeXPathQuery<Object> query() {
if (query == null && xpathQuery != null && !xpathQuery.isEmpty()) {
try {
query = AstNodeXPathQuery.create(xpathQuery);
Expand All @@ -57,17 +58,17 @@ public AstNodeXPathQuery<Object> query() {

@Override
public void visitFile(AstNode fileNode) {
if (query() != null) {
List<Object> objects = query().selectNodes(fileNode);
AstNodeXPathQuery<Object> compiledQuery = query();
if (compiledQuery != null) {
compiledQuery.selectNodes(fileNode).forEach(this::reportIssue);
}
}

for (Object object : objects) {
if (object instanceof AstNode) {
AstNode astNode = (AstNode) object;
addLineIssue(message, astNode.getTokenLine());
} else if (object instanceof Boolean && (Boolean) object) {
addFileIssue(message);
}
}
private void reportIssue(Object object) {
if (object instanceof AstNode) {
addIssue((AstNode) object, message);
} else if (object instanceof Boolean && (Boolean) object) {
addFileIssue(message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ public class XPathCheckTest {
public static final String XPATH_PY = "xpath.py";

@Test
public void line_level_issue() {
public void node_level_issue() {
analyze("xpath-statement.py", "//STATEMENT");
}

@Test
public void node_level_issue_print_statement() {
analyze("xpath-print-statement.py", "//PRINT_STMT");
}

@Test
public void boolean_true_result() {
analyze("xpath-count-statement.py", "count(//STATEMENT) > 0");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def fib(n):
a, b = 0, 1
while a < n:
print a, #Noncompliant [[sc=9;ec=17]] {{Avoid statements}}
a, b = b, a+b
print #Noncompliant [[sc=5;ec=10]] {{Avoid statements}}

fib(1000)