Search in sources :

Example 1 with ParameterList

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

the class TooManyParametersCheck method checkFunctionDef.

private void checkFunctionDef(SubscriptionContext ctx) {
    FunctionDef functionDef = (FunctionDef) ctx.syntaxNode();
    ParameterList parameters = functionDef.parameters();
    FunctionSymbol functionSymbol = ((FunctionDefImpl) functionDef).functionSymbol();
    if (parameters != null && functionSymbol != null) {
        long nbParameters = functionSymbol.parameters().size();
        boolean isMethod = functionDef.isMethodDefinition();
        if (isMethod && functionSymbol.decorators().stream().noneMatch(d -> d.contains("staticmethod"))) {
            // First parameter is implicitly passed: either "self" or "cls"
            nbParameters -= 1;
        }
        if (nbParameters > max) {
            if (isMethod && isAlreadyReportedInParent(functionSymbol)) {
                return;
            }
            String typeName = isMethod ? "Method" : "Function";
            String name = String.format("%s \"%s\"", typeName, functionDef.name().name());
            String message = String.format(MESSAGE, name, nbParameters, max);
            ctx.addIssue(parameters, message);
        }
    }
}
Also used : SymbolUtils(org.sonar.python.semantic.SymbolUtils) ParameterList(org.sonar.plugins.python.api.tree.ParameterList) Kind(org.sonar.plugins.python.api.tree.Tree.Kind) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) RuleProperty(org.sonar.check.RuleProperty) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) FunctionDefImpl(org.sonar.python.tree.FunctionDefImpl) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) Rule(org.sonar.check.Rule) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) FunctionDefImpl(org.sonar.python.tree.FunctionDefImpl) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) ParameterList(org.sonar.plugins.python.api.tree.ParameterList)

Example 2 with ParameterList

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

the class ControlFlowGraphBuilder method addParametersToStartBlock.

private void addParametersToStartBlock(StatementList statementList) {
    if (statementList.parent().is(Tree.Kind.FUNCDEF)) {
        ParameterList parameterList = ((FunctionDef) statementList.parent()).parameters();
        if (parameterList != null) {
            PythonCfgSimpleBlock parametersBlock = createSimpleBlock(start);
            addParameters(parameterList.all(), parametersBlock);
            start = parametersBlock;
        }
    }
}
Also used : ParameterList(org.sonar.plugins.python.api.tree.ParameterList) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef)

Example 3 with ParameterList

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

the class PythonTreeMaker method lambdaExpression.

public LambdaExpression lambdaExpression(AstNode astNode) {
    Token lambdaKeyword = toPyToken(astNode.getFirstChild(PythonKeyword.LAMBDA).getToken());
    Token colonToken = toPyToken(astNode.getFirstChild(PythonPunctuator.COLON).getToken());
    Expression body = expression(astNode.getFirstChild(PythonGrammar.TEST, PythonGrammar.TEST_NOCOND));
    AstNode varArgsListNode = astNode.getFirstChild(PythonGrammar.VARARGSLIST);
    ParameterList argListTree = null;
    if (varArgsListNode != null) {
        List<AnyParameter> parameters = varArgsListNode.getChildren(PythonGrammar.FPDEF, PythonGrammar.NAME, PythonPunctuator.MUL, PythonPunctuator.DIV).stream().map(this::parameter).filter(Objects::nonNull).collect(Collectors.toList());
        List<Token> commas = punctuators(varArgsListNode, PythonPunctuator.COMMA);
        argListTree = new ParameterListImpl(parameters, commas);
    }
    return new LambdaExpressionImpl(lambdaKeyword, colonToken, body, argListTree);
}
Also used : 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) ParameterList(org.sonar.plugins.python.api.tree.ParameterList) Token(org.sonar.plugins.python.api.tree.Token) AnyParameter(org.sonar.plugins.python.api.tree.AnyParameter) AstNode(com.sonar.sslr.api.AstNode)

Example 4 with ParameterList

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

the class MethodShouldBeStaticCheck method isUsingSelfArg.

private static boolean isUsingSelfArg(FunctionDef funcDef) {
    ParameterList parameters = funcDef.parameters();
    if (parameters == null) {
        // if a method has no parameters then it can't be a instance method.
        return true;
    }
    List<AnyParameter> params = parameters.all();
    if (params.isEmpty()) {
        return false;
    }
    if (params.get(0).is(Tree.Kind.TUPLE_PARAMETER)) {
        return false;
    }
    Parameter first = (Parameter) params.get(0);
    Name paramName = first.name();
    if (paramName == null) {
        // star argument should not raise issue
        return true;
    }
    SelfVisitor visitor = new SelfVisitor(paramName.name());
    funcDef.body().accept(visitor);
    return visitor.isUsingSelfArg;
}
Also used : ParameterList(org.sonar.plugins.python.api.tree.ParameterList) AnyParameter(org.sonar.plugins.python.api.tree.AnyParameter) Parameter(org.sonar.plugins.python.api.tree.Parameter) AnyParameter(org.sonar.plugins.python.api.tree.AnyParameter) Name(org.sonar.plugins.python.api.tree.Name)

Example 5 with ParameterList

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

the class TooManyParametersCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Kind.FUNCDEF, this::checkFunctionDef);
    context.registerSyntaxNodeConsumer(Kind.LAMBDA, ctx -> {
        LambdaExpression tree = (LambdaExpression) ctx.syntaxNode();
        ParameterList parameters = tree.parameters();
        if (parameters != null) {
            int nbParameters = parameters.all().size();
            if (nbParameters > max) {
                String name = "Lambda";
                String message = String.format(MESSAGE, name, nbParameters, max);
                ctx.addIssue(parameters, message);
            }
        }
    });
}
Also used : ParameterList(org.sonar.plugins.python.api.tree.ParameterList) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression)

Aggregations

ParameterList (org.sonar.plugins.python.api.tree.ParameterList)11 AnyParameter (org.sonar.plugins.python.api.tree.AnyParameter)5 FunctionDef (org.sonar.plugins.python.api.tree.FunctionDef)5 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)3 Name (org.sonar.plugins.python.api.tree.Name)3 Parameter (org.sonar.plugins.python.api.tree.Parameter)3 Token (org.sonar.plugins.python.api.tree.Token)3 AstNode (com.sonar.sslr.api.AstNode)2 ArrayList (java.util.ArrayList)1 Rule (org.sonar.check.Rule)1 RuleProperty (org.sonar.check.RuleProperty)1 PythonSubscriptionCheck (org.sonar.plugins.python.api.PythonSubscriptionCheck)1 SubscriptionContext (org.sonar.plugins.python.api.SubscriptionContext)1 FunctionSymbol (org.sonar.plugins.python.api.symbols.FunctionSymbol)1 Symbol (org.sonar.plugins.python.api.symbols.Symbol)1 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)1 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)1 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)1 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)1 Decorator (org.sonar.plugins.python.api.tree.Decorator)1