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()));
}
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);
}
Aggregations