TSComment
Abstract representation of typescript comments
Inherits from
Comment, Statement, Expression, Editable
Properties
comment_type
Parse the comment type. Determines if the comment is a single line comment or a multi-line comment.
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 all contained function calls. Useful for renaming function invocations etc.
index
The 0-based index of the statement in the parent code block.
nested_code_blocks
Returns all nested CodeBlocks within the statement.
nested_statements
Returns a list of collections of nested statements within nested blocks in the given statement.
Example:
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.
source
Text representation of the Editable instance
text
Returns the text content of the comment, without the comment delimiters like #
or /* */
.
To get the full comment block (including delimiters), use source
instead.
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
clean_comment
r"""Cleans the comment block by removing leading and trailing whitespace.
Args: comment (str): The comment block to be cleaned.
Returns: str: The cleaned comment block.
Examples: For Typescript, the comment ”/* Hello */ ” will be cleaned to “Hello”.
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.
edit_text
Replace the text of the comment with new_text. Autoformats the comment delimiters like #
or /* */
.
Args: new_src (str): The new content to replace the existing comment.
Returns: None
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
generate_comment
Unparses cleaned text content into a comment block.
Args: new_src (str): The text content to be converted into a comment. comment_type (TSCommentType): The type of comment to generate. 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: str: The generated comment block.
Examples:
For TypeScript, if comment_type
is TSCommentType.DOUBLE_SLASH
:
If comment_type
is TSCommentType.SLASH_STAR
:
Notes:
The leading_star
flag is on by default.
The force_multiline
flag is off by default.
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
remove
Deletes this Node, and optionally its related extended nodes (e.g. decorators, comments, 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.
Was this page helpful?