Assignment
Represents an assignment for a single variable within an assignment statement.
Attributes: left: The left side of the assignment right: The right side of the assignment
Example: typescript var z var z = 5
Inherits from
Symbol, Typeable, HasValue, GraphSitterBase, Exportable, Importable, HasName, Editable, Expression
Properties
comment
Returns the comment group associated with the symbol, if any.
dependencies
Symbols that this symbol depends on. Opposite of usages
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 all contained function calls. Useful for renaming function invocations etc.
index
inline_comment
Returns the inline comment group associated with the symbol, if any.
is_exported
Returns True iff the symbol is exported from the file it’s defined in
is_local_variable
Returns True if the assignment is a local variable in the scope of the code .
is_reexported
Returns True if the symbol is re-exported from a file it is not defined in (if applicable)
is_typed
Returns True if the object is explicitly typed, False otherwise
name
The name of the object excluding whichever namespace precedes it, as a string.
Examples: For a.b, the name is b
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.
source
Returns the source code of the function
value
The value of the object
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.
Attributes
type
The type annotation associated with this node
Methods
add_comment
Adds a comment to the symbol.
If a comment already exists, a new comment will be added to the existing comment group. If no comment exists, a new comment group will be created.
add_keyword
Insert a keyword in the appropriate place if it is not already there.
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_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_local_usages
Returns all instances of its usages in the code block as a list of Editable
.
get_name
Returns the Name of the object, as a Name
(editable)
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)
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 assignment name as well as all its usages in the code block
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.
If a comment already exists, the existing comment will be overridden If no comment exists, a new comment group will be created.
set_inline_comment
Sets an inline comment to the symbol.
If an inline comment already exists, the existing inline comment will be overridden. If no inline comment exists, a new inline comment will be created.
set_name
Sets the name of the object
set_type_annotation
Adds a type annotation to the assignment.
If a type annotation already exists, it will be overwritten to specified type_str
.
foo = Bar()
and will add a type annotation to foo
like foo: Bar = Bar()
.
set_value
Sets the value of the assignment
Was this page helpful?