TSClass
Inherits from
Class, TSHasBlock, TSSymbol, HasBlock, Callable, Symbol, Expression, Exportable, GraphSitterBase, Importable, HasName, Editable
Properties
all_attributes
Returns a list of attributes defined in this class and its superclasses
all_methods
All methods that exist on this Class
attributes
Returns the list of Attributes
defined in this Class
call_sites
All invocations of this function elsewhere in the codebase
comment
Returns the comment group associated with the symbol, if any.
constructor
Returns the constructor method for this class
decorators
Returns list of all Decorators on this Symbol
dependencies
Symbols that this symbol depends on. Opposite of usages
docstring
Returns the docstring of the function. The docstring is represented as a CommentGroup.
export
Returns the export object that exports this symbol. Returns none if this symbol is not exported.
exported_name
Returns the name the symbol is exported as. If the symbol is not exported, returns None.
extended
Returns a SymbolGroup of all extended nodes. This allows a common edit interface for all of extended nodes.
extended_nodes
List of Editables with extended symbols like export
, public
or decorator
extended_source
Text representation of all its extended nodes
file
The file object that this Editable instance belongs to
filepath
The file path of the file that this Editable instance belongs to
full_name
The full name including the object lookup (if applicable).
Examples: For a.b, the full name is a.b
function_calls
Returns a list of all function calls in the code block.
has_semicolon
Returns true if the symbol has a semicolon at the end and false otherwise.
inline_comment
Returns the inline comment group associated with the symbol, if any.
is_decorated
Returns True if the symbol is decorated with a decorator.
is_exported
Returns True iff the symbol is exported from the file it’s defined in
is_jsx
Check if the class represents a JSX component.
is_reexported
Returns True if the symbol is re-exported from a file it is not defined in (if applicable)
is_subclass
Returns True if the class is a subclass of another class
jsx_elements
Returns list of all JSXElements contained in this symbol
methods
All methods that exist on this Class, not counting superclasses or interfaces
name
The name of the object excluding whichever namespace precedes it, as a string.
Examples: For a.b, the name is b
nested_classes
Nested classes defined in this class
parameters
All parameters of this callable
parent_class_names
Returns a list of the args passed to the class (the parent classes)
parent_statement
Returns the parent statement that contains this expression
resolved_value
If the expression is a resolvable value, returns the last assigned expression value. Else, returns itself.
Example: a = 1 b = a foo(b)
If we call resolve_value on b, it returns 1.
semicolon_node
Returns the semicolon node of the symbol if it exists.
source
Returns the source code of the function
superclasses
Returns a list of all classes that this class extends
variable_usages
Returns Editables for all TreeSitter node instances of variable usages. (Excludes: property identifiers and argument keywords) This is useful for renaming variables locally and analyzing variable usages.
Methods
add_attribute
Adds an Attribute
to a Class
. Useful for copying attributes from one Class
to another.
add_attribute_from_source
Adds an attribute to the class from source code. Places it after the docstring (if exists), at the end of existing attributes, and before methods.
add_comment
Adds a new comment to the symbol.
Args: comment (str): The comment to be added. auto_format (bool, optional): Flag to automatically format the text into a comment. Defaults to True. clean_format (bool, optional): Flag to clean the format of the docstring before inserting. Defaults to True. comment_type (PyCommentType, optional): Type of comment to add. Defaults to TSCommentType.DOUBLE_SLASH.
Returns: None
Notes: If a comment already exists, a new comment will be added to the end of the existing comment group. If no comment exists, a new comment group will be created.
Examples:
If auto_format
is True, the comment “Hello” will be formatted as:
If auto_format
is False, the comment string will be added as is, without any formatting.
add_decorator
Adds a decorator to this function based on the source
skip_if_exists
: Do not add a decorator if it already exists- returns
bool
:True
if the decorator was added,False
otherwise
add_keyword
Insert a keyword in the appropriate place if it is not already there.
add_source
Add a block of source code to a class
call_graph_successors
Returns all Callable
objects that are reachable from this Callable
.
Args: include_classes (bool): If True, includes class definitions in the code paths. include_external (bool): If True, includes external modules in the code paths.
Returns:
list[Function | ExternalModule]: A list of Function
or ExternalModule
objects that are reachable from this Callable
.
commit
Commit just this node
edit
Replace the source of this node with new_src.
When fix_indentation
is True
, the indentation of the new_src will be adjusted to match the current text’s indentation.
find
Returns a list of all substring match in strings_to_match
, similar to python’s str.find(..)
Args: exact: Only match individual nodes which exactly match the query
find_string_literals
Returns a list of all editable substrings within string literals that match strings_to_match
flag
Adds a comment so the developer can see this Editable
get_all_symbol_usages
Returns a list of all symbols that uses this exportable object, via direct or indirect usages. Equivalent to get_symbol_usages(UsageType.DIRECT | UsageType.INDIRECT | UsageType.ALIASED)
get_all_usages
Returns a list of all usages of this exportable object, via direct or indirect usages. Equivalent to get_usages(UsageType.DIRECT | UsageType.INDIRECT | UsageType.ALIASED)
get_attribute
Returns a specific attribute by name (None
if nonexistent)
get_attributes
All attributes that exist on this Class, including superclasses, up to max_depth
Args: max_depth: how many superclasses to go up private: Include private methods
get_component
Returns the JSX Element with the given component name
get_import_string
Returns the import string needed to import this symbol.
- If
alias
is specified, formats the import using the alias. - If
module
is specified, uses the specified module, otherwise defaults to the current module. - If
import_type
is ImportType.WILDCARD, imports the symbol’s module. e.g.from module import symbol as alias
get_method
Returns a specific method by name
get_methods
All methods that exist on this Class, including superclasses, up to max_depth
Args:
- max_depth: Include parent classes up to max_depth (None is no limit)
- private : Include private methods
- magic: Include magic methods
get_name
Returns the Name of the object, as a Name
(editable)
get_nested_class
Returns a specific method by name
get_parameter
Returns the parameter with the given name, or None if it doesn’t exist.
get_parameter_by_index
Returns the parameter with the given index, or None if it doesn’t exist.
get_parameter_by_type
Returns the parameter of specified type, or None if it doesn’t exist.
get_parent_class
Returns the parent class of this class
get_subclasses
Returns all classeses which subclass this class
get_superclasses
Returns a list of all classes that this class extends, up to max_depth
get_symbol_usages
Symbols that uses this exportable object, or imports that imports this exportable object.
By default, only returns direct usages, such as Imports or usages within the same file. In other words, it will tell you where this symbol is imported, but not where it is used.
Inverse of dependencies
.
Arguments: usage_types: What kind of usages to search for
get_usages
Returns a list of usages of the exportable object. By default, only returns direct usages, such as Imports or usages within the same file. In other words, it will tell you where this symbol is imported, but not where it is used. Useful for dead code deletion.
Arguments: usage_types: What kind of usages to search for
get_variable_usages
Returns Editables for all TreeSitter nodes corresponding to instances of variable usage that matches the given variable name.
(Excludes: property identifiers and argument keywords)
fuzzy_match
allows for partial matching of variable names and effectively does var_name in usage.name
insert_after
Inserts new_src after this node
insert_before
Inserts new_src
before this node.
Arguments:
- extended (bool): If True, the source is inserted before the extended nodes (e.g. comments, docstrings, access identifiers).(Default: True)
is_subclass_of
Returns True if the class is a subclass of the given parent class
move_to_file
Moves a symbol to the specified file and updates all imports. Import update strategy can be specified. Strategies:
add_back_edge
: just moves the symbol to the new file and adds an import to the old fileupdate_all_imports
: updates all imports and usages of the symbol to the new file
remove
Deletes this Node, and optionally its related extended nodes (e.g. decorators, comments, etc.)
rename
Renames the symbol and all its references in the codebase (imports/callsites/etc.)
replace
Search and replace an instance of substring
within this node’s source. Similar to python’s string.replace(..)
Throws ValueError if there are more than one occurrence of substring in this node’s source.
Arguments:
old: the occurrences of this string to replace
new: the string to replace with
count: the max number of occurrences to replace. Default value: -1 (replace all)
is_regex: whether old
is a regex pattern. Default value: False
Returns:
The total count of old
occurrences replaced.
search
Returns a list of all regex match of regex_pattern
, similar to python’s re.search(…)
When include_strings
is False, the search will exclude the contents of string literals from the search.
When include_comments
is False, the search will exclude the contents of comments from the search.
set_comment
Sets a comment to the symbol.
Args: comment (str): The comment to be added. auto_format (bool, optional): Flag to automatically format the text into a comment. Defaults to True. clean_format (bool, optional): Flag to clean the format of the docstring before inserting. Defaults to True. comment_type (PyCommentType, optional): Type of comment to add. Defaults to TSCommentType.DOUBLE_SLASH.
Returns: None
Notes: If a comment already exists, the existing comment will be edited. If no comment exists, a new comment group will be created.
Examples:
If auto_format
is True, the comment “Hello” will be formatted as:
If auto_format
is False, the comment string will be added as is, without any formatting.
set_docstring
Sets a docstring to the function.
Args: docstring (str): The docstring to be added. auto_format (bool, optional): Flag to automatically format the text into a docstring. Defaults to True. clean_format (bool, optional): Flag to clean the format of the docstring before inserting. Defaults to True. leading_star (bool, optional): Flag to add leading ”*” to each line of the comment block. Defaults to True. force_multiline (bool, optional): Flag to force single line comments to be multi-line. Defaults to False.
Returns: None
Notes: If a docstring already exists, the existing docstring will be edited. If no docstring exists, a new docstring will be created.
Examples:
If auto_format
is True, the comment “Hello” will be formatted as:
If auto_format
is False, the comment string will be added as is, without any formatting.
set_inline_comment
Sets an inline comment to the symbol.
Args: comment (str): The inline comment to be added. auto_format (bool, optional): Flag to automatically format the text into a comment. Defaults to True. clean_format (bool, optional): Flag to clean the format of the docstring before inserting. Defaults to True.
Returns: None
Notes: If an inline comment already exists, it will be replaced with the new comment. If no inline comment exists, a new inline comment will be created.
Examples:
If auto_format
is True, the comment “Hello” will be formatted as:
If auto_format
is False, the comment string will be added as is, without any formatting.
set_name
Sets the name of the object
Was this page helpful?