Skip to content

GSiesto/tree-sitter-typescript

 
 

Repository files navigation

tree-sitter-typescript

CI discord matrix crates npm pypi

TypeScript and TSX grammars for tree-sitter.

Because TSX and TypeScript are actually two different dialects, this module defines two grammars. Require them as follows:

require("tree-sitter-typescript").typescript; // TypeScript grammar
require("tree-sitter-typescript").tsx; // TSX grammar

For Javascript files with flow type annotations you can use the tsx parser.

Python usage

The tree-sitter library is required for this package. Ensure you have pip installed.

pip install tree-sitter

First, install the package using pip:

pip install .

This package allows you to load both TypeScript and TSX grammars as a Language object in Python. Here's how you can do it:

import tree_sitter_typescript as tstypescript
from tree_sitter import Language, Parser

# Load TypeScript grammar
TYPESCRIPT_LANGUAGE = Language(tstypescript.language_typescript())

# Load TSX grammar
TSX_LANGUAGE = Language(tstypescript.language_tsx())

Practical Example

For a practical example of how to use these grammars with tree-sitter python library, please refer to the test file located at bindings/python/tree_sitter_typescript/test.py.

You can test a successful installation by running the following command:

python bindings/python/tree_sitter_typescript/test.py

This will execute the test script, which demonstrates how to parse TypeScript and TSX code using the tree-sitter library.

References

About

TypeScript grammar for tree-sitter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 67.5%
  • C 22.5%
  • Makefile 7.4%
  • Scheme 2.6%