tree-sitter-typescript
tree-sitter-typescript provides pre-compiled TypeScript and TSX grammars for the `tree-sitter` parsing library in Python. It allows developers to parse TypeScript and TSX code into concrete syntax trees for advanced code analysis and manipulation. The library is actively and regularly updated, with the current stable version being 0.23.2.
Warnings
- breaking The `Language.build_library` method, previously used to compile grammars in older versions of `py-tree-sitter` (prior to approximately 0.22.x), has been removed. Grammars like `tree-sitter-typescript` are now expected to be installed as pre-compiled Python packages.
- breaking The underlying `tree-sitter` core library occasionally introduces Application Binary Interface (ABI) changes (e.g., ABI 15 in v0.25.0). These changes can cause compatibility issues with older `tree-sitter` Python bindings or grammar packages if they are not updated to match the new ABI.
- gotcha While `tree-sitter-typescript` provides both TypeScript and TSX grammars, complex or less common syntax constructs (e.g., specific styled components patterns, LaTeX strings within templates, or certain advanced `import`/`export` declarations) may result in `ERROR` or `MISSING` nodes in the parsed syntax tree. This indicates the grammar could not fully parse that section.
- gotcha The `tree-sitter-typescript` grammar internally relies on the `tree-sitter-javascript` grammar for JavaScript-specific constructs. However, installing `tree-sitter-typescript` via pip does not automatically install `tree-sitter-javascript` as a separate Python package. This might lead to unexpected parsing behavior or errors if the JavaScript grammar is implicitly expected for certain parts of the code.
Install
-
pip install tree-sitter tree-sitter-typescript
Imports
- language
from tree_sitter import Language, Parser import tree_sitter_typescript TS_LANGUAGE = Language(tree_sitter_typescript.language(), 'typescript')
- tsx_language
from tree_sitter import Language, Parser import tree_sitter_typescript TSX_LANGUAGE = Language(tree_sitter_typescript.tsx_language(), 'tsx')
Quickstart
from tree_sitter import Language, Parser
import tree_sitter_typescript
# Load the TypeScript grammar
TS_LANGUAGE = Language(tree_sitter_typescript.language(), 'typescript')
# Create a parser and set its language
parser = Parser()
parser.set_language(TS_LANGUAGE)
# Code to parse
ts_code = b"""interface MyInterface { name: string; age?: number; }\nconst x: MyInterface = { name: 'Alice' };"""
# Parse the code
tree = parser.parse(ts_code)
# Access the root node
root_node = tree.root_node
print(f"Parsed TypeScript root node type: {root_node.type}")
# Load the TSX grammar
TSX_LANGUAGE = Language(tree_sitter_typescript.tsx_language(), 'tsx')
parser.set_language(TSX_LANGUAGE)
# TSX code to parse
tsx_code = b"""function App() { return <div>Hello, {name}</div>; }"""
# Parse the TSX code
tree_tsx = parser.parse(tsx_code)
root_node_tsx = tree_tsx.root_node
print(f"Parsed TSX root node type: {root_node_tsx.type}")
# Example of finding a node
interface_node = root_node.children[0] # Assuming interface declaration is the first child
print(f"First node in TS: {interface_node.type}, text: {interface_node.text.decode('utf8')}")