Inherits from

SourceFile, TSHasBlock, File, HasBlock, Exportable, Importable, GraphSitterBase, Expression, Editable

Properties


classes

Returns all Classes in the file

def classes(self) -> list[TClass]:
    ...

content

Returns contents of the file as a string.

def content(self) -> str:
    ...

decorators

Returns list of all Decorators on this Symbol

def decorators(self) -> list[TSDecorator]:
    ...

default_exports

Returns all default export symbols. All returned export objects should belong to one export statement.

def default_exports(self) -> list[TSExport]:
    ...

dependencies

Symbols that this symbol depends on. Opposite of usages

def dependencies(self) -> list[Union["Symbol", "Import"]]:
    ...

directory

Returns the directory that contains this file

def directory(self) -> Directory | None:
    ...

docstring

Returns the docstring of the function. The docstring is represented as a CommentGroup.

def docstring(self) -> TSCommentGroup | None:
    ...

export

Returns the export object that exports this symbol. Returns none if this symbol is not exported.

def export(self) -> Export | None:
    ...

export_statements

Returns all ExportStatements in the file, where each export statement can contain multiple exports

def export_statements(self) -> list[ExportStatement[TSExport]]:
    ...

exported_name

Returns the name the symbol is exported as. If the symbol is not exported, returns None.

def exported_name(self) -> str | None:
    ...

exports

Returns all Export symbols in the file.

def exports(self) -> list[TSExport]:
    ...

extended

Returns a SymbolGroup of all extended nodes. This allows a common edit interface for all of extended nodes.

def extended(self) -> SymbolGroup:
    ...

extended_source

Text representation of all its extended nodes

def extended_source(self) -> str:
    ...

extension

Returns the file extension

def extension(self) -> str:
    ...

file

The file object that this Editable instance belongs to

def file(self) -> Self:
    ...

filepath

The file path of the file that this Editable instance belongs to

def filepath(self) -> str:
    ...

function_calls

Returns a list of all function calls in the code block.

def function_calls(self) -> list[FunctionCall]:
    ...

functions

Returns all Functions in the file

def functions(self) -> list[TFunction]:
    ...

global_vars

Returns all GlobalVars in the file

def global_vars(self) -> list[TGlobalVar]:
    ...

import_statements

Returns all ImportStatements in the file, where each import statement can contain multiple imports

def import_statements(self) -> list[ImportStatement]:
    ...

importers

All imports that directly imports this file as a module eg. from a import <this file>

does NOT include imports that import symbols from this file eg. from <this file> import a does NOT count as an importer

def importers(self) -> list[TImport]:
    ...

imports

List of all Imports in this file

def imports(self) -> list[TImport]:
    ...

inbound_imports

Returns all imports that are importing symbols contained in this file

def inbound_imports(self) -> list[TImport]:
    ...

interfaces

Returns all Interfaces in the file

def interfaces(self) -> list[TSInterface]:
    ...

is_binary

def is_binary(self) -> bool:
    ...

is_decorated

Returns True if the symbol is decorated with a decorator.

def is_decorated(self) -> bool:
    ...

is_exported

Returns True iff the symbol is exported from the file it’s defined in

def is_exported(self) -> bool:
    ...

is_reexported

Returns True if the symbol is re-exported from a file it is not defined in (if applicable)

def is_reexported(self) -> bool:
    ...

jsx_elements

Returns list of all JSXElements contained in this file

def jsx_elements(self) -> list[JSXElement]:
    ...

named_exports

Returns all named export symbols. All returned export objects should belong to one export statement.

def named_exports(self) -> list[TSExport]:
    ...

owners

Returns the CODEOWNERS of the file.

def owners(self) -> set[str]:
    ...

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.

def resolved_value(self) -> Expression | list[Expression]:
    ...

source

Text representation of the Editable instance

def source(self) -> str:
    ...

start_byte

The start byte of the Editable instance that appears in file

def start_byte(self) -> int:
    ...

symbols

Returns all Symbols in the file, sorted by position in the file

def symbols(self) -> list[Symbol | TClass | TFunction | TGlobalVar | TInterface]:
    ...

symbols_sorted_topologically

Returns all Symbols in the file, sorted topologically (parents first). Robust to dependency loops.

def symbols_sorted_topologically(self) -> list[Symbol]:
    ...

types

Returns all Types in the file

def types(self) -> list[TSTypeAlias]:
    ...

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.

def variable_usages(self) -> list[Editable]:
    ...

Methods


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
def add_decorator(self, new_decorator: str, skip_if_exists: bool = False) -> bool:
    ...

add_export_to_symbol

Adds an export to a symbol in a file

def add_export_to_symbol(self, symbol: TSSymbol) -> None:
    ...

add_import_from_import_string

Adds import to the file from a string representation of an import statement

def add_import_from_import_string(self, import_string: str) -> None:
    ...

add_symbol

Adds symbol to the file.

Arguments: should_export: If true, the symbol will be exported (if applicable).

def add_symbol(self, symbol: Symbol, should_export: bool = True):
    ...

add_symbol_from_source

Adds a symbol to a file from a string representation.

def add_symbol_from_source(self, source: str) -> None:
    ...

add_symbol_import

Adds an import to a file for a given symbol

def add_symbol_import(
        self,
        symbol: Symbol,
        alias: str | None = None,
        import_type: ImportType = ImportType.UNKNOWN,
        is_type_import: bool = False,
    ):
    ...

commit

Commit just this node

def commit(self) -> None:
    ...

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.

def edit(self, new_src: str, fix_indentation: bool = False, priority: int = 0, dedupe: bool = True) -> 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

def find(self, strings_to_match: list[str] | str, *, exact: bool = False) -> list[Editable]:
    ...

find_string_literals

Returns a list of all editable substrings within string literals that match strings_to_match

def find_string_literals(self, strings_to_match: list[str], fuzzy_match: bool = False) -> list[Editable]:
    ...

flag

Adds a comment so the developer can see this Editable

def flag(self) -> None:
    ...

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)

def get_all_symbol_usages(self) -> list[Import | Symbol | Export]:
    ...

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)

def get_all_usages(self) -> list[Usage]:
    ...

get_class

Returns a specific Class by full name. Returns None if not found.

def get_class(self, name: str) -> TClass | None:
    ...

get_component

Returns the JSX Element with the given component name

def get_component(self, component_name: str) -> JSXElement[Self] | None:
    ...

get_export

Returns the export with the specified name.

def get_export(self, export_name: str) -> TSExport | None:
    ...

get_extensions

Returns file name extensions this class can parse

def get_extensions() -> list[str]:
    ...

get_function

Returns a specific Function by name. Returns None if not found.

def get_function(self, name: str) -> TFunction | None:
    ...

get_global_var

Returns a specific global var by name. Returns None if not found.

def get_global_var(self, name: str) -> TGlobalVar | None:
    ...

get_import

Returns the import with matching alias. Returns None if not found.

For example:

from a import b # matches file.get_import('b')
from a import c as d  # matches file.get_import('d')
def get_import(self, symbol_alias: str) -> TImport | None:
    ...

get_import_module_name

Returns the module name that this file gets imported as

def get_import_module_name(self) -> str:
    ...

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
def get_import_string(self, alias: str | None = None, module: str | None = None, import_type: ImportType = ImportType.UNKNOWN, is_type_import: bool = False) -> str:
    ...

get_interface

Returns a specific Interface by name. Returns None if not found.

def get_interface(self, name: str) -> TSInterface | None:
    ...

get_symbol

Returns a specific symbol by name. Returns None if not found.

def get_symbol(self, name: str) -> Symbol | None:
    ...

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

def get_symbol_usages(self, usage_types: UsageType = UsageType.DIRECT | UsageType.CHAINED) -> list[Import | Symbol | Export]:
    ...

get_type

Returns a specific Type by name. Returns None if not found.

def get_type(self, name: str) -> TSTypeAlias | None:
    ...

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

def get_usages(self, usage_types: UsageType = UsageType.DIRECT | UsageType.CHAINED) -> list[Usage]:
    ...

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

def get_variable_usages(self, var_name: str, fuzzy_match: bool = False) -> list[Editable]:
    ...

has_import

Returns True if the file has an import with the given alias

def has_import(self, symbol_alias: str) -> bool:
    ...

insert_after

Inserts new_src after this node

def insert_after(self, new_src: str, fix_indentation: bool = False, newline: bool = True, priority: int = 0, dedupe: bool = True) -> None:
    ...

insert_before

Inserts new_src before this node

def insert_before(self, new_src: str, fix_indentation: bool = False, newline: bool = True, priority: int = 0, dedupe: bool = True) -> None:
    ...

remove

Removes the file from file directory

def remove(self) -> None:
    ...

remove_unused_exports

Removes unused exports from the file.

def remove_unused_exports(self) -> None:
    ...

rename

Renames the file

def rename(self, new_name: str) -> None:
    ...

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.

def replace(self, old: str, new: str, count: int = -1, is_regex: bool = False, priority: int = 0) -> int:
    ...

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.

def search(self, regex_pattern: str, include_strings: bool = True, include_comments: bool = True) -> list[Editable]:
    ...

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:

/* Hello */

If auto_format is False, the comment string will be added as is, without any formatting.

def set_docstring(self, docstring: str, auto_format: bool = True, clean_format: bool = True, leading_star: bool = True, force_multiline: bool = False) -> None:
    ...

symbol_can_be_added

Checks if the file type supports the symbol being added.

def symbol_can_be_added(self, symbol: TSSymbol) -> bool:
    ...

update_filepath

Renames the file and updates all imports to the file

def update_filepath(self, new_filepath: str):
    ...