Search in sources :

Example 1 with FunctionLike

use of org.sonar.uast.helpers.FunctionLike in project sonar-go by SonarSource.

the class TooManyParametersCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    FunctionLike function = FunctionLike.from(node);
    if (function == null) {
        return;
    }
    List<UastNode> parameters = function.parameters();
    int parameterCount = 0;
    for (UastNode parameter : parameters) {
        List<UastNode> identifiers = new ArrayList<>();
        parameter.getDescendants(UastNode.Kind.IDENTIFIER, identifiers::add, UastNode.Kind.TYPE);
        parameterCount += identifiers.size();
    }
    if (parameterCount > maximum) {
        reportIssue(function.name(), String.format("Function has %d parameters, which is more than %d authorized.", parameterCount, maximum));
    }
}
Also used : ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) FunctionLike(org.sonar.uast.helpers.FunctionLike)

Example 2 with FunctionLike

use of org.sonar.uast.helpers.FunctionLike in project sonar-go by SonarSource.

the class FunctionCognitiveComplexityCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    nestedFunctionLevel++;
    if (nestedFunctionLevel != 1) {
        return;
    }
    FunctionLike functionNode = FunctionLike.from(node);
    if (functionNode == null) {
        return;
    }
    CognitiveComplexity complexity = CognitiveComplexity.calculateFunctionComplexity(functionNode.node());
    if (complexity.value() > maxComplexity) {
        String message = "Refactor this function to reduce its Cognitive Complexity from " + complexity.value() + " to the " + maxComplexity + " allowed.";
        int effortToFix = complexity.value() - maxComplexity;
        reportIssue(functionNode.name(), functionNode.name(), message, effortToFix, complexity.secondaryLocations());
    }
}
Also used : CognitiveComplexity(org.sonar.commonruleengine.CognitiveComplexity) FunctionLike(org.sonar.uast.helpers.FunctionLike)

Example 3 with FunctionLike

use of org.sonar.uast.helpers.FunctionLike in project sonar-go by SonarSource.

the class NoIdenticalFunctionsCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    if (node.kinds.contains(UastNode.Kind.CLASS)) {
        functions.clear();
    }
    if (node.kinds.contains(FunctionLike.KIND)) {
        FunctionLike thisFunction = FunctionLike.from(node);
        if (thisFunction == null) {
            return;
        }
        if (thisFunction.body().getChildren(UastNode.Kind.STATEMENT).size() < 2) {
            return;
        }
        for (FunctionLike function : functions) {
            if (syntacticallyEquivalent(thisFunction.body(), function.body()) && syntacticallyEquivalent(thisFunction.parameters(), function.parameters()) && syntacticallyEquivalent(thisFunction.resultList(), function.resultList())) {
                reportIssue(thisFunction.name(), "Function is identical with function on line " + function.node().firstToken().line + ".", new Issue.Message(function.name(), "Original implementation"));
                break;
            }
        }
        functions.add(thisFunction);
    }
}
Also used : Issue(org.sonar.commonruleengine.Issue) FunctionLike(org.sonar.uast.helpers.FunctionLike)

Aggregations

FunctionLike (org.sonar.uast.helpers.FunctionLike)3 ArrayList (java.util.ArrayList)1 CognitiveComplexity (org.sonar.commonruleengine.CognitiveComplexity)1 Issue (org.sonar.commonruleengine.Issue)1 UastNode (org.sonar.uast.UastNode)1