TSCodeBlock
Extends the CodeBlock class to provide TypeScript-specific functionality.
Inherits from
CodeBlock, Expression, Editable
Properties
assignment_statements
Returns list of top level assignment statements in the code block.
assignments
Returns all assignments in the code block, for all nest levels.
attributes
Returns top level attribute statements in the code block.
comments
Gets list of top level comments in the code block.
extended
Returns a SymbolGroup of all extended nodes. This allows a common edit interface for all of extended nodes.
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
function_calls
Returns a list of all function calls in the code block.
if_blocks
Returns list of top level if statements in the code block.
local_var_assignments
Returns all local variable assignment in the code block, for all nest levels.
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.
return_statements
Returns list of top level return statements in the code block.
source
Text representation of the Editable instance
statements
Returns a list of top level statements in the code block The statements are ordered by appearance in the code block.
symbol_statements
Returns list of top level symbol statements in the code block.
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
commit
Commit just this node
edit
Replace the source of this Editable
with new_src
.
When fix_indentation
is True, the indentation of 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_assignments
Returns the first code statement that assigns a local variable with specified name var_name
.
get_attributes
Returns top level attribute statements in the code block that are private.
get_comment
Returns the first comment statement that matches the specified comment. Searches all nested statements in the code block.
get_local_var_assignment
Returns the first code statement that assigns a local variable with specified name var_name
.
get_local_var_assignments
Returns all instances of code statements that assigns to the local variable with specified name var_name
.
When fuzzy_match
is True, it matches on local variables with names that contain var_name
,
otherwise defaults on exact match of local var name.
get_statements
Returns all statements up to the specified block level.
Arguments: max_level (int): specifies the maximum block level of statements to return. Default: None (returns all)
get_variable_usages
Returns all instances of variable usages in the code block as a list of Editable
.
When fuzzy_match
is True, it matches on usages that contains var_name
, otherwise defaults on exact match.
indent
Adjusts the indentation level on the whole code block by the specified level
.
If the level is < 0, the block is unindented to the left.
If the level is > 0, the block is indented to the right.
insert_after
Inserts new_src at the bottom of the code block
insert_before
Inserts new_src at the top of the code block
remove
Deletes this Node, and optionally its related extended nodes (e.g. decorators, comments, etc.)
rename_variable_usages
Renames all instances of variable usages in the code block.
When fuzzy_match
is True, it matches on usages that contains old_var_name
, otherwise defaults on exact match
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.
unwrap
Unwraps the code block by removing the opening and closing braces.
wrap
Wraps the block with a statement.
Ex: wrap a block with an if, else, or for loop, etc.
For example: return b =>
if b: return b