Search in sources :

Example 11 with StatementList

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);
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) ElseClause(org.sonar.plugins.python.api.tree.ElseClause) StatementList(org.sonar.plugins.python.api.tree.StatementList) Token(org.sonar.plugins.python.api.tree.Token) AstNode(com.sonar.sslr.api.AstNode)

Example 12 with StatementList

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);
}
Also used : ElseClause(org.sonar.plugins.python.api.tree.ElseClause) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Expression(org.sonar.plugins.python.api.tree.Expression) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) StatementList(org.sonar.plugins.python.api.tree.StatementList) Token(org.sonar.plugins.python.api.tree.Token) AstNode(com.sonar.sslr.api.AstNode)

Example 13 with StatementList

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));
}
Also used : StatementList(org.sonar.plugins.python.api.tree.StatementList) Token(org.sonar.plugins.python.api.tree.Token)

Example 14 with StatementList

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()));
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) StatementList(org.sonar.plugins.python.api.tree.StatementList)

Example 15 with StatementList

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));
        }
    });
}
Also used : StatementList(org.sonar.plugins.python.api.tree.StatementList) FileInput(org.sonar.plugins.python.api.tree.FileInput)

Aggregations

StatementList (org.sonar.plugins.python.api.tree.StatementList)21 Token (org.sonar.plugins.python.api.tree.Token)12 AstNode (com.sonar.sslr.api.AstNode)9 IfStatement (org.sonar.plugins.python.api.tree.IfStatement)6 Tree (org.sonar.plugins.python.api.tree.Tree)6 ElseClause (org.sonar.plugins.python.api.tree.ElseClause)5 FileInput (org.sonar.plugins.python.api.tree.FileInput)5 Name (org.sonar.plugins.python.api.tree.Name)5 AssignmentStatement (org.sonar.plugins.python.api.tree.AssignmentStatement)4 Expression (org.sonar.plugins.python.api.tree.Expression)4 ArrayList (java.util.ArrayList)3 Rule (org.sonar.check.Rule)3 PythonSubscriptionCheck (org.sonar.plugins.python.api.PythonSubscriptionCheck)3 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)3 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)3 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)3 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)3 Decorator (org.sonar.plugins.python.api.tree.Decorator)3 DottedName (org.sonar.plugins.python.api.tree.DottedName)3 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)3