Search in sources :

Example 21 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