Class TreeUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic RegularArgumentargumentByKeyword(String keyword, List<Argument> arguments) static StringdecoratorNameFromExpression(Expression expression) dottedNameToPartFqn(DottedName dottedName) static intfindIndentationSize(Tree tree) static TreefirstAncestor(Tree tree, Predicate<Tree> predicate) static <T extends Tree>
TfirstAncestorOfClass(Tree tree, Class<T> clazz) static TreefirstAncestorOfKind(Tree tree, Tree.Kind... kinds) firstChild(Tree tree, Predicate<Tree> filter) static Stream<Expression> flattenTuples(Expression expression) fullyQualifiedNameFromExpression(Expression expression) fullyQualifiedNameFromQualifiedExpression(QualifiedExpression qualifiedExpression) static ClassSymbolgetClassSymbolFromDef(ClassDef classDef) static ClassDefgetEnclosingClassDef(Tree tree) static FunctionSymbolgetFunctionSymbolFromDef(FunctionDef functionDef) getLocalVariableSymbols(FunctionDef functionDef) getParentClassesFQN(ClassDef classDef) getSymbolFromTree(Tree tree) static Comparator<Tree> static TokenStatements can have a separator like semicolon.static booleanhasDescendant(Tree tree, Predicate<Tree> predicate) static PythonTypeThis method returns the type of an expression.static booleanisBooleanLiteral(Tree tree) static booleanisConstantName(String name) static booleanisDecoratorWithFQN(Decorator decorator, String fullyQualifiedName) static booleanisDoubleStarExpression(Tree tree) Checks if a tree node represents a double-star (**) expression.static booleanisFunctionWithGivenDecoratorFQN(Tree tree, String decoratorFQN) static LocationInFilelocationInFile(Tree tree, String fileId) static StringnameFromExpression(Expression expression) nameFromExpressionOrQualifiedExpression(Expression expression) static StringnameFromQualifiedExpression(QualifiedExpression qualifiedExpression) nameFromQualifiedOrCallExpression(Expression expression) nonTupleParameters(FunctionDef functionDef) nonWhitespaceTokens(Tree tree) static RegularArgumentnthArgumentOrKeyword(int argPosition, String keyword, List<Argument> arguments) static Optional<RegularArgument> nthArgumentOrKeywordOptional(int argPosition, String keyword, List<Argument> arguments) positionalParameters(FunctionDef functionDef) stringValueFromNameOrQualifiedExpression(Expression expression) Extracts the string value from a name or qualified expression.toInstanceOfMapper(Class<T> castToClass) toOptionalInstanceOf(Class<T> castToClass, Tree tree) toOptionalInstanceOfMapper(Class<T> castToClass) static List<FunctionDef> topLevelFunctionDefs(ClassDef classDef) Collects all top-level function definitions within a class def.toStreamInstanceOfMapper(Class<T> castToClass) static StringtreeToString(Tree tree, boolean renderMultiline)
-
Method Details
-
firstAncestor
-
firstAncestorOfKind
-
firstAncestorOfClass
-
groupAssignmentByParentStatementList
-
getTreeByPositionComparator
-
tokens
-
nonWhitespaceTokens
-
hasDescendant
-
flattenTuples
-
getSymbolFromTree
-
getClassSymbolFromDef
-
getEnclosingClassDef
-
getParentClassesFQN
-
getFunctionSymbolFromDef
@CheckForNull public static FunctionSymbol getFunctionSymbolFromDef(@Nullable FunctionDef functionDef) -
nonTupleParameters
-
positionalParameters
-
topLevelFunctionDefs
Collects all top-level function definitions within a class def. It is used to discover methods defined within "strange" constructs, such asclass A: if p: def f(self): ... -
findIndentationSize
-
argumentByKeyword
@CheckForNull public static RegularArgument argumentByKeyword(String keyword, List<Argument> arguments) -
nthArgumentOrKeyword
@CheckForNull public static RegularArgument nthArgumentOrKeyword(int argPosition, String keyword, List<Argument> arguments) -
nthArgumentOrKeywordOptional
public static Optional<RegularArgument> nthArgumentOrKeywordOptional(int argPosition, String keyword, List<Argument> arguments) -
isBooleanLiteral
-
nameFromExpression
-
nameFromQualifiedOrCallExpression
-
nameFromExpressionOrQualifiedExpression
-
nameFromQualifiedExpression
-
decoratorNameFromExpression
-
asyncTokenOfEnclosingFunction
-
isFunctionWithGivenDecoratorFQN
-
isDecoratorWithFQN
-
fullyQualifiedNameFromQualifiedExpression
public static Optional<String> fullyQualifiedNameFromQualifiedExpression(QualifiedExpression qualifiedExpression) -
fullyQualifiedNameFromExpression
-
locationInFile
-
getTreeSeparatorOrLastToken
Statements can have a separator like semicolon. When handling ranges we want to take them into account. -
toInstanceOfMapper
-
toOptionalInstanceOfMapper
-
toOptionalInstanceOf
-
toStreamInstanceOfMapper
-
firstChild
-
treeToString
-
dottedNameToPartFqn
-
stringValueFromNameOrQualifiedExpression
Extracts the string value from a name or qualified expression. Returns empty Optional for expressions that are not simple names or chains of qualified expressions. -
inferSingleAssignedExpressionType
This method returns the type of an expression.
If the expression is a name, the function will find where the name was assigned and return the type of the assigned expression. If the expression is a qualified expression, it will resolve the type of the qualifier and then resolve the member type on that type. The function will do that recursively until it finds a name or resolves something to an
PythonType.UNKNOWNtype. If the expression is neither a name nor a qualified expression, it will returnExpression.typeV2().This function is intended to used with variables that are defined in the global scope, and then used in a function. Since type inference does not propagate types from global scope to function scope, this would leave the name of the variable in the function without a type.
See SONARPY-3218 and SONARPY-3219 for more details.
- Parameters:
name- the name of a variable that is assigned in the global scope- Returns:
- the type of the name, or `UNKNOWN` if it cannot be determined
-
getLocalVariableSymbols
-
isDoubleStarExpression
Checks if a tree node represents a double-star (**) expression. Supports bothParameter(e.g.**kwargsin function definitions) andUnpackingExpression(e.g.**kwargsin call arguments). -
isConstantName
-