TSWhileStatement
A TypeScript while statement class that represents while loops and manages their condition and code block. This class provides functionality for handling while statements in TypeScript code, including managing the loop’s condition and associated code block. It extends the base WhileStatement class with TypeScript-specific behavior. Attributes: condition (str | None): The condition expression of the while loop.
Inherits from
TSHasBlock, WhileStatement, HasBlock, Statement, Expression, Editable
Properties
decorators
Returns a list of decorators associated with this symbol.
Retrieves all decorators applied to this symbol by looking at both previous named siblings and decorator fields. This includes both inline decorators and standalone decorator statements.
Returns: list[TSDecorator]: A list of TSDecorator objects representing all decorators applied to this symbol. Returns an empty list if no decorators are found.
docstring
Retrieves the docstring of a function or class.
Returns any comments immediately preceding this node as a docstring. For nodes that are children of a HasBlock, it returns consecutive comments that end on the line before the node starts. For other nodes, it returns formatted docstring comments.
Returns: TSCommentGroup | None: A CommentGroup representing the docstring if one exists, None otherwise.
extended
Returns a SymbolGroup of all extended nodes associated with this element.
Creates a SymbolGroup that provides a common interface for editing all extended nodes, such as decorators, modifiers, and comments associated with the element.
Args: None
Returns: SymbolGroup: A group containing this node and its extended nodes that allows batch modification through a common interface.
extended_source
Returns the source text representation of all extended nodes.
Gets the source text of all extended nodes combined. This property allows reading the source text of all extended nodes (e.g. decorators, export statements) associated with this node.
Returns: str: The combined source text of all extended nodes.
file
The file object that this Editable instance belongs to.
Retrieves or caches the file object associated with this Editable instance.
Returns: File: The File object containing this Editable instance.
filepath
The file path of the file that this Editable instance belongs to.
Returns a string representing the absolute file path of the File that contains this Editable instance.
Returns: str: The absolute file path.
function_calls
Returns all function calls within the while statement block.
Collects all function calls from both the condition expression and the code block.
Returns: list[FunctionCall]: A list of function calls found in the while statement’s condition and code block.
index
The 0-based index of the statement in the parent code block.
Returns the sequential position of this statement within its containing code block.
Returns: int: The 0-based index of this statement within its parent code block.
is_decorated
Checks if the current symbol has a decorator.
Determines if the symbol has a preceding decorator node.
Returns: bool: True if the symbol has a decorator node as its previous named sibling, False otherwise.
jsx_elements
Returns a list of all JSX elements contained within this symbol.
Searches through the extended nodes of the symbol for any JSX elements or self-closing JSX elements and returns them as a list of JSXElement objects.
Args: None
Returns: list[JSXElement[Self]]: A list of JSXElement objects contained within this symbol.
nested_code_blocks
Returns all nested CodeBlocks within the statement.
Returns all code blocks that are nested within the while statement. For while statements, this will always be a list containing only the single code block associated with the while statement’s body.
Returns: list[TCodeBlock]: A list containing the code blocks associated with this while statement.
nested_statements
Returns a list of statement collections within nested code blocks.
Accesses and retrieves the statements from each code block nested within the current statement, such as the statements within if/else branches or loop bodies.
Returns: A list where each element is a collection of statements from one nested code block. Returns an empty list if there are no nested code blocks.
parent_class
Find the class this node is contained in
parent_function
Find the function this node is contained in
parent_statement
Find the statement this node is contained in
resolved_value
Returns the resolved type of an Expression.
Returns the inferred type of the expression. For example a function call’s resolved value will be it’s definition.
Returns: Union[Expression, list[Expression]]: The resolved expression type(s). Returns a single Expression if there is only one resolved type, or a list of Expressions if there are multiple resolved types. Returns self if the expression is not resolvable or has no resolved types.
source
Text representation of the Editable instance.
Returns the source text of the Editable instance. This is the main property used to access the text content of any code element in GraphSitter.
Returns: str: The text content of this Editable instance.
variable_usages
Returns Editables for all TreeSitter node instances of variable usages within this node’s scope.
This method finds all variable identifier nodes in the TreeSitter AST, excluding:
- Function names in function calls
- Import names in import statements
- Property access identifiers (except the base object)
- Keyword argument names (in Python and TypeScript)
This is useful for variable renaming and usage analysis within a scope.
Returns: list[Editable]: A list of Editable nodes representing variable usages. Each Editable corresponds to a TreeSitter node instance where the variable is referenced.
Attributes
condition
The condition expression of the while loop.
Methods
add_decorator
Adds a decorator to a function or method.
Adds a new decorator to the symbol’s definition. The decorator is inserted before the first non-comment extended node with proper indentation.
Args: new_decorator (str): The decorator to add. Should be a complete decorator string including the ’@’ symbol. skip_if_exists (bool, optional): If True, will not add the decorator if it already exists. Defaults to False.
Returns: bool: True if the decorator was added, False if skipped due to existing decorator.
ancestors
Find all ancestors of the node of the given type. Does not return itself
edit
Replace the source of this Editable
with new_src
.
Replaces the text representation of this Editable instance with new text content. The method handles indentation adjustments and transaction management.
Args:
new_src (str): The new source text to replace the current text with.
fix_indentation (bool): If True, adjusts the indentation of new_src
to match the current text’s indentation level. Defaults to False.
priority (int): The priority of the edit transaction. Higher priority edits are applied first. Defaults to 0.
dedupe (bool): If True, deduplicates identical transactions. Defaults to True.
Returns: None
find
Find and return matching nodes or substrings within an Editable instance.
This method searches through the extended_nodes of the Editable instance and returns all nodes or substrings that match the given search criteria.
Args: strings_to_match (Union[list[str], str]): One or more strings to search for. exact (bool): If True, only return nodes whose source exactly matches one of the strings_to_match. If False, return nodes that contain any of the strings_to_match as substrings. Defaults to False.
Returns: list[Editable]: A list of Editable instances that match the search criteria.
find_string_literals
Returns a list of string literals within this node’s source that match any of the given strings.
Args: strings_to_match (list[str]): A list of strings to search for in string literals. fuzzy_match (bool): If True, matches substrings within string literals. If False, only matches exact strings. Defaults to False.
Returns: list[Editable]: A list of Editable objects representing the matching string literals.
flag
Adds a visual flag comment to the end of this Editable’s source text.
Flags this Editable by appending a comment with emoji flags at the end of its source text. This is useful for visually highlighting specific nodes in the source code during development and debugging.
Returns: None
get_component
Returns a specific JSX element from within this symbol’s JSX elements.
Searches through all JSX elements in this symbol’s code block and returns the first one that matches the given component name.
Args: component_name (str): The name of the JSX component to find.
Returns: JSXElement[Self] | None: The matching JSX element if found, None otherwise.
get_variable_usages
Returns Editables for all TreeSitter nodes corresponding to instances of variable usage that matches the given variable name.
Retrieves a list of variable usages that match a specified name, with an option for fuzzy matching. By default, excludes property identifiers and argument keywords.
Args: var_name (str): The variable name to search for. fuzzy_match (bool): If True, matches variables where var_name is a substring. If False, requires exact match. Defaults to False.
Returns: list[Editable]: List of Editable objects representing variable usage nodes matching the given name.
insert_after
Inserts code after this node.
Args: new_src (str): The source code to insert after this node. fix_indentation (bool, optional): Whether to adjust the indentation of new_src to match the current node. Defaults to False. newline (bool, optional): Whether to add a newline before the new_src. Defaults to True. priority (int, optional): Priority of the insertion transaction. Defaults to 0. dedupe (bool, optional): Whether to deduplicate identical transactions. Defaults to True.
Returns: None
insert_before
Inserts text before this node’s source with optional indentation and newline handling.
This method inserts the provided text before the current node’s source code. It can automatically handle indentation and newline placement.
Args: new_src (str): The text to insert before this node. fix_indentation (bool): Whether to fix the indentation of new_src to match the current node. Defaults to False. newline (bool): Whether to add a newline after new_src. Defaults to True. priority (int): Transaction priority for managing multiple edits. Defaults to 0. dedupe (bool): Whether to deduplicate identical transactions. Defaults to True.
Returns: None
is_wrapped_in
Check if this node is contained another node of the given class
parent_of_type
Find the first ancestor of the node of the given type. Does not return itself
reduce_condition
Reduces an editable to the following condition
remove
Deletes this Node and its related extended nodes (e.g. decorators, comments).
Removes the current node and its extended nodes (e.g. decorators, comments) from the codebase. After removing the node, it handles cleanup of any surrounding formatting based on the context.
Args: delete_formatting (bool): Whether to delete surrounding whitespace and formatting. Defaults to True. priority (int): Priority of the removal transaction. Higher priority transactions are executed first. Defaults to 0. dedupe (bool): Whether to deduplicate removal transactions at the same location. Defaults to True.
Returns: None
replace
Search and replace occurrences of text within this node’s source and its extended nodes.
This method performs string replacement similar to Python’s string.replace(), with support for regex patterns. It operates on both the main node and any extended nodes (e.g. decorators, exports).
Args: old (str): The text or pattern to search for. new (str): The text to replace matches with. count (int, optional): Maximum number of replacements to make. Defaults to -1 (replace all). is_regex (bool, optional): Whether to treat ‘old’ as a regex pattern. Defaults to False. priority (int, optional): Priority of the replacement operation. Defaults to 0.
Returns: int: The total number of replacements made.
Raises: ValueError: If there are multiple occurrences of the substring in a node’s source.
search
Returns a list of all regex match of regex_pattern
, similar to python’s re.search().
Searches for matches of a regular expression pattern within the text of this node and its extended nodes.
Args: regex_pattern (str): The regular expression pattern to search for. include_strings (bool): When False, excludes the contents of string literals from the search. Defaults to True. include_comments (bool): When False, excludes the contents of comments from the search. Defaults to True.
Returns: list[Editable]: A list of Editable objects corresponding to the matches found.
set_docstring
Sets or updates a docstring for a code element.
Adds a new docstring if none exists, or updates the existing docstring. Handles formatting and placement of the docstring according to the specified parameters.
Args: docstring (str): The docstring text to be added or updated. auto_format (bool, optional): Whether to automatically format the text into a docstring format. Defaults to True. clean_format (bool, optional): Whether to clean existing formatting from the docstring before inserting. Defaults to True. leading_star (bool, optional): Whether to add leading ”*” to each line of the comment block. Defaults to True. force_multiline (bool, optional): Whether to force single line comments to be multi-line. Defaults to False.
Returns: None
Was this page helpful?