Search in sources :

Example 6 with Decorator

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

the class CorsCheck method checkFlaskDecorator.

private static void checkFlaskDecorator(SubscriptionContext ctx) {
    Decorator decorator = (Decorator) ctx.syntaxNode();
    Expression expression = decorator.expression();
    if (expression.is(CALL_EXPR)) {
        expression = ((CallExpression) expression).callee();
    }
    TreeUtils.getSymbolFromTree(expression).ifPresent(symbol -> {
        if (isSymbol(symbol, "flask_cors.cross_origin")) {
            ArgList arguments = decorator.arguments();
            if (arguments == null) {
                ctx.addIssue(decorator, MESSAGE);
                return;
            }
            getArgument(arguments.arguments(), ORIGINS).ifPresent(argument -> {
                if (originsToReport(argument)) {
                    ctx.addIssue(decorator, MESSAGE);
                }
            });
        }
    });
}
Also used : Decorator(org.sonar.plugins.python.api.tree.Decorator) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Expression(org.sonar.plugins.python.api.tree.Expression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) ArgList(org.sonar.plugins.python.api.tree.ArgList)

Example 7 with Decorator

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

the class PythonTreeMaker method classDefStatement.

public ClassDef classDefStatement(AstNode astNode) {
    AstNode decoratorsNode = astNode.getFirstChild(PythonGrammar.DECORATORS);
    List<Decorator> decorators = Collections.emptyList();
    if (decoratorsNode != null) {
        decorators = decoratorsNode.getChildren(PythonGrammar.DECORATOR).stream().map(this::decorator).collect(Collectors.toList());
    }
    Name name = name(astNode.getFirstChild(PythonGrammar.CLASSNAME).getFirstChild(PythonGrammar.NAME));
    ArgList args = null;
    AstNode leftPar = astNode.getFirstChild(PythonPunctuator.LPARENTHESIS);
    if (leftPar != null) {
        args = argList(astNode.getFirstChild(PythonGrammar.ARGLIST));
    }
    AstNode suite = astNode.getFirstChild(PythonGrammar.SUITE);
    StatementList body = getStatementListFromSuite(suite);
    Token classToken = toPyToken(astNode.getFirstChild(PythonKeyword.CLASS).getToken());
    AstNode rightPar = astNode.getFirstChild(PythonPunctuator.RPARENTHESIS);
    Token colon = toPyToken(astNode.getFirstChild(PythonPunctuator.COLON).getToken());
    return new ClassDefImpl(decorators, classToken, name, leftPar != null ? toPyToken(leftPar.getToken()) : null, args, rightPar != null ? toPyToken(rightPar.getToken()) : null, colon, suiteNewLine(suite), suiteIndent(suite), body, suiteDedent(suite), DocstringExtractor.extractDocstring(body));
}
Also used : Decorator(org.sonar.plugins.python.api.tree.Decorator) StatementList(org.sonar.plugins.python.api.tree.StatementList) ArgList(org.sonar.plugins.python.api.tree.ArgList) Token(org.sonar.plugins.python.api.tree.Token) AstNode(com.sonar.sslr.api.AstNode) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) DottedName(org.sonar.plugins.python.api.tree.DottedName) ImportName(org.sonar.plugins.python.api.tree.ImportName)

Example 8 with Decorator

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

the class PythonTreeMaker method funcDefStatement.

public FunctionDef funcDefStatement(AstNode astNode) {
    AstNode decoratorsNode = astNode.getFirstChild(PythonGrammar.DECORATORS);
    List<Decorator> decorators = Collections.emptyList();
    if (decoratorsNode != null) {
        decorators = decoratorsNode.getChildren(PythonGrammar.DECORATOR).stream().map(this::decorator).collect(Collectors.toList());
    }
    Name name = name(astNode.getFirstChild(PythonGrammar.FUNCNAME).getFirstChild(PythonGrammar.NAME));
    ParameterList parameterList = null;
    AstNode typedArgListNode = astNode.getFirstChild(PythonGrammar.TYPEDARGSLIST);
    if (typedArgListNode != null) {
        List<AnyParameter> arguments = typedArgListNode.getChildren(PythonGrammar.TFPDEF, PythonPunctuator.MUL, PythonPunctuator.DIV).stream().map(this::parameter).filter(Objects::nonNull).collect(Collectors.toList());
        List<Token> commas = punctuators(typedArgListNode, PythonPunctuator.COMMA);
        parameterList = new ParameterListImpl(arguments, commas);
    }
    AstNode suite = astNode.getFirstChild(PythonGrammar.SUITE);
    StatementList body = getStatementListFromSuite(suite);
    AstNode defNode = astNode.getFirstChild(PythonKeyword.DEF);
    Token asyncToken = null;
    AstNode defPreviousSibling = defNode.getPreviousSibling();
    if (defPreviousSibling != null && defPreviousSibling.getToken().getValue().equals("async")) {
        asyncToken = toPyToken(defPreviousSibling.getToken());
    }
    Token lPar = toPyToken(astNode.getFirstChild(PythonPunctuator.LPARENTHESIS).getToken());
    Token rPar = toPyToken(astNode.getFirstChild(PythonPunctuator.RPARENTHESIS).getToken());
    TypeAnnotation returnType = null;
    AstNode returnTypeNode = astNode.getFirstChild(PythonGrammar.FUN_RETURN_ANNOTATION);
    if (returnTypeNode != null) {
        List<AstNode> children = returnTypeNode.getChildren();
        returnType = new TypeAnnotationImpl(toPyToken(children.get(0).getToken()), toPyToken(children.get(1).getToken()), expression(children.get(2)));
    }
    Token colon = toPyToken(astNode.getFirstChild(PythonPunctuator.COLON).getToken());
    return new FunctionDefImpl(decorators, asyncToken, toPyToken(defNode.getToken()), name, lPar, parameterList, rPar, returnType, colon, suiteNewLine(suite), suiteIndent(suite), body, suiteDedent(suite), isMethodDefinition(astNode), DocstringExtractor.extractDocstring(body));
}
Also used : TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) Token(org.sonar.plugins.python.api.tree.Token) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) DottedName(org.sonar.plugins.python.api.tree.DottedName) ImportName(org.sonar.plugins.python.api.tree.ImportName) Decorator(org.sonar.plugins.python.api.tree.Decorator) StatementList(org.sonar.plugins.python.api.tree.StatementList) ParameterList(org.sonar.plugins.python.api.tree.ParameterList) AnyParameter(org.sonar.plugins.python.api.tree.AnyParameter) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

Decorator (org.sonar.plugins.python.api.tree.Decorator)8 AstNode (com.sonar.sslr.api.AstNode)4 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)4 Expression (org.sonar.plugins.python.api.tree.Expression)4 FunctionDef (org.sonar.plugins.python.api.tree.FunctionDef)4 Name (org.sonar.plugins.python.api.tree.Name)4 Arrays (java.util.Arrays)3 List (java.util.List)3 Test (org.junit.Test)3 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)3 ArgList (org.sonar.plugins.python.api.tree.ArgList)3 ClassDef (org.sonar.plugins.python.api.tree.ClassDef)3 ImportName (org.sonar.plugins.python.api.tree.ImportName)3 ListLiteral (org.sonar.plugins.python.api.tree.ListLiteral)3 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)3 RegularArgument (org.sonar.plugins.python.api.tree.RegularArgument)3 StatementList (org.sonar.plugins.python.api.tree.StatementList)3 StringLiteral (org.sonar.plugins.python.api.tree.StringLiteral)3 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)3 Token (org.sonar.plugins.python.api.tree.Token)3