Search in sources :

Example 1 with AssertStatement

use of org.sonar.plugins.python.api.tree.AssertStatement in project sonar-python by SonarSource.

the class UselessParenthesisAfterKeywordCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.ASSERT_STMT, ctx -> checkExpr(((AssertStatement) ctx.syntaxNode()).condition(), ctx, "assert"));
    context.registerSyntaxNodeConsumer(Tree.Kind.DEL_STMT, ctx -> checkExpr(((DelStatement) ctx.syntaxNode()).expressions().get(0), ctx, "del"));
    context.registerSyntaxNodeConsumer(Tree.Kind.IF_STMT, ctx -> {
        IfStatement ifStmt = (IfStatement) ctx.syntaxNode();
        checkExpr(ifStmt.condition(), ctx, ifStmt.keyword().value());
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.WHILE_STMT, ctx -> {
        WhileStatement whileStmt = (WhileStatement) ctx.syntaxNode();
        checkExpr(whileStmt.condition(), ctx, whileStmt.whileKeyword().value());
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, ctx -> handleForStatement(ctx, (ForStatement) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.RAISE_STMT, ctx -> handleRaiseStatement(ctx, (RaiseStatement) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.RETURN_STMT, ctx -> handleReturnStatement(ctx, (ReturnStatement) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.YIELD_EXPR, ctx -> handleYieldExpression(ctx, (YieldExpression) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.EXCEPT_CLAUSE, ctx -> {
        Expression exception = ((ExceptClause) ctx.syntaxNode()).exception();
        if (exception != null) {
            checkExprExcludeTuples(exception, ctx, "except");
        }
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.NOT, ctx -> handleNotOperator(ctx, (UnaryExpression) ctx.syntaxNode()));
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) Expression(org.sonar.plugins.python.api.tree.Expression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) AssertStatement(org.sonar.plugins.python.api.tree.AssertStatement) ReturnStatement(org.sonar.plugins.python.api.tree.ReturnStatement) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) WhileStatement(org.sonar.plugins.python.api.tree.WhileStatement) ForStatement(org.sonar.plugins.python.api.tree.ForStatement) ExceptClause(org.sonar.plugins.python.api.tree.ExceptClause) RaiseStatement(org.sonar.plugins.python.api.tree.RaiseStatement)

Example 2 with AssertStatement

use of org.sonar.plugins.python.api.tree.AssertStatement in project sonar-python by SonarSource.

the class PythonTreeMakerTest method assertStatement.

@Test
public void assertStatement() {
    setRootRule(PythonGrammar.ASSERT_STMT);
    AstNode astNode = p.parse("assert x");
    StatementWithSeparator statementWithSeparator = new StatementWithSeparator(astNode, null);
    AssertStatement assertStatement = treeMaker.assertStatement(statementWithSeparator);
    assertThat(assertStatement).isNotNull();
    assertThat(assertStatement.assertKeyword().value()).isEqualTo("assert");
    assertThat(assertStatement.condition()).isNotNull();
    assertThat(assertStatement.message()).isNull();
    assertThat(assertStatement.children()).hasSize(2);
    astNode = p.parse("assert x, y");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    assertStatement = treeMaker.assertStatement(statementWithSeparator);
    assertThat(assertStatement).isNotNull();
    assertThat(assertStatement.assertKeyword().value()).isEqualTo("assert");
    assertThat(assertStatement.condition()).isNotNull();
    assertThat(assertStatement.message()).isNotNull();
    assertThat(assertStatement.children()).hasSize(3);
}
Also used : AssertStatement(org.sonar.plugins.python.api.tree.AssertStatement) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Aggregations

AssertStatement (org.sonar.plugins.python.api.tree.AssertStatement)2 AstNode (com.sonar.sslr.api.AstNode)1 Test (org.junit.Test)1 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)1 ExceptClause (org.sonar.plugins.python.api.tree.ExceptClause)1 Expression (org.sonar.plugins.python.api.tree.Expression)1 ForStatement (org.sonar.plugins.python.api.tree.ForStatement)1 IfStatement (org.sonar.plugins.python.api.tree.IfStatement)1 ParenthesizedExpression (org.sonar.plugins.python.api.tree.ParenthesizedExpression)1 RaiseStatement (org.sonar.plugins.python.api.tree.RaiseStatement)1 ReturnStatement (org.sonar.plugins.python.api.tree.ReturnStatement)1 UnaryExpression (org.sonar.plugins.python.api.tree.UnaryExpression)1 WhileStatement (org.sonar.plugins.python.api.tree.WhileStatement)1 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)1 RuleTest (org.sonar.python.parser.RuleTest)1