Graph-sitter provides first-class support for both Python and TypeScript codebases. The language is automatically inferred when you initialize a codebase.
When you create a new Codebase instance, Graph-sitter automatically detects the programming language:
Copy
Ask AI
from graph_sitter import Codebase# Automatically detects Python or TypeScriptcodebase = Codebase("./")# View language with `codebase.language`print(codebase.language) # "python" or "typescript"
Learn more about codebase initialization options in Parsing
Codebases.
# Base class (core/function.py)class Function: """Abstract representation of a Function.""" pass# Python implementation (python/function.py)class PyFunction(Function): """Extends Function for Python codebases.""" pass# TypeScript implementation (typescript/function.py)class TSFunction(Function): """Extends Function for TypeScript codebases.""" pass
This inheritance pattern means that most Graph-sitter programs can work with either Python or TypeScript without modification, since they share the same API structure.
Copy
Ask AI
# Works for both Python and TypeScriptfor function in codebase.functions: print(f"Function: {function.name}") print(f"Parameters: {[p.name for p in function.parameters]}") print(f"Return type: {function.return_type}")
JSX/TSX: React component handling (see React and JSX)
Example of TypeScript-specific features:
Copy
Ask AI
# Only works with TypeScript codebasesif isinstance(codebase, TSCodebaseType): # Work with TypeScript interfaces for interface in codebase.interfaces: print(f"Interface: {interface.name}") print(f"Extends: {[i.name for i in interface.parent_interfaces]}") # Work with type aliases for type_alias in codebase.type_aliases: print(f"Type alias: {type_alias.name}")