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