use of org.sonar.plugins.python.api.tree.StatementList in project sonar-python by SonarSource.
the class PythonTreeMaker method ifStatement.
// Compound statements
public IfStatement ifStatement(AstNode astNode) {
Token ifToken = toPyToken(astNode.getTokens().get(0));
AstNode condition = astNode.getFirstChild(PythonGrammar.NAMED_EXPR_TEST);
Token colon = toPyToken(astNode.getFirstChild(PythonPunctuator.COLON).getToken());
AstNode suite = astNode.getFirstChild(PythonGrammar.SUITE);
StatementList body = getStatementListFromSuite(suite);
AstNode elseSuite = astNode.getLastChild(PythonGrammar.SUITE);
ElseClause elseClause = null;
if (elseSuite.getPreviousSibling().getPreviousSibling().is(PythonKeyword.ELSE)) {
elseClause = elseClause(elseSuite);
}
List<IfStatement> elifBranches = astNode.getChildren(PythonKeyword.ELIF).stream().map(this::elifStatement).collect(Collectors.toList());
return new IfStatementImpl(ifToken, expression(condition), colon, suiteNewLine(suite), suiteIndent(suite), body, suiteDedent(suite), elifBranches, elseClause);
}
use of org.sonar.plugins.python.api.tree.StatementList in project sonar-python by SonarSource.
the class PythonTreeMaker method forStatement.
public ForStatement forStatement(AstNode astNode) {
AstNode forStatementNode = astNode;
Token asyncToken = null;
if (astNode.is(PythonGrammar.ASYNC_STMT)) {
asyncToken = toPyToken(astNode.getFirstChild().getToken());
forStatementNode = astNode.getFirstChild(PythonGrammar.FOR_STMT);
}
Token forKeyword = toPyToken(forStatementNode.getFirstChild(PythonKeyword.FOR).getToken());
Token inKeyword = toPyToken(forStatementNode.getFirstChild(PythonKeyword.IN).getToken());
Token colon = toPyToken(forStatementNode.getFirstChild(PythonPunctuator.COLON).getToken());
List<Expression> expressions = expressionsFromExprList(forStatementNode.getFirstChild(PythonGrammar.EXPRLIST));
List<Expression> testExpressions = expressionsFromTest(forStatementNode.getFirstChild(PythonGrammar.TESTLIST));
AstNode firstSuite = forStatementNode.getFirstChild(PythonGrammar.SUITE);
StatementList body = getStatementListFromSuite(firstSuite);
AstNode lastSuite = forStatementNode.getLastChild(PythonGrammar.SUITE);
ElseClause elseClause = firstSuite == lastSuite ? null : elseClause(lastSuite);
return new ForStatementImpl(forKeyword, expressions, inKeyword, testExpressions, colon, suiteNewLine(firstSuite), suiteIndent(firstSuite), body, suiteDedent(firstSuite), elseClause, asyncToken);
}
use of org.sonar.plugins.python.api.tree.StatementList in project sonar-python by SonarSource.
the class PythonTreeMaker method elseClause.
private ElseClause elseClause(AstNode astNode) {
Token elseToken = toPyToken(astNode.getPreviousSibling().getPreviousSibling().getToken());
Token colon = toPyToken(astNode.getPreviousSibling().getToken());
StatementList body = getStatementListFromSuite(astNode);
return new ElseClauseImpl(elseToken, colon, suiteNewLine(astNode), suiteIndent(astNode), body, suiteDedent(astNode));
}
use of org.sonar.plugins.python.api.tree.StatementList in project sonar-python by SonarSource.
the class AllBranchesAreIdenticalCheck method handleIfStatement.
private static void handleIfStatement(IfStatement ifStmt, SubscriptionContext ctx) {
if (ifStmt.elseBranch() == null) {
return;
}
StatementList body = ifStmt.body();
for (IfStatement elifBranch : ifStmt.elifBranches()) {
StatementList elifBody = elifBranch.body();
if (!CheckUtils.areEquivalent(body, elifBody)) {
return;
}
}
if (!CheckUtils.areEquivalent(body, ifStmt.elseBranch().body())) {
return;
}
PreciseIssue issue = ctx.addIssue(ifStmt.keyword(), "Remove this if statement or edit its code blocks so that they're not all the same.");
issue.secondary(issueLocation(ifStmt.body()));
ifStmt.elifBranches().forEach(e -> issue.secondary(issueLocation(e.body())));
issue.secondary(issueLocation(ifStmt.elseBranch().body()));
}
use of org.sonar.plugins.python.api.tree.StatementList in project sonar-python by SonarSource.
the class WildcardImportCheck method initialize.
@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, ctx -> {
if (ctx.pythonFile().fileName().equals("__init__.py")) {
// Ignore __init__.py files, as wildcard imports are commonly used to populate those.
return;
}
FileInput fileInput = (FileInput) ctx.syntaxNode();
StatementList statements = fileInput.statements();
if (statements == null) {
return;
}
WildcardImportVisitor visitor = new WildcardImportVisitor();
statements.accept(visitor);
if (visitor.shouldRaiseIssues) {
visitor.wildcardImports.forEach(importFrom -> ctx.addIssue(importFrom, MESSAGE));
}
});
}
Aggregations