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