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