tree-sitter-typescript

repository·master·Indexed 19 days ago

https://github.com/tree-sitter/tree-sitter-typescript

Tree-sitter grammars for parsing TypeScript and TSX, including support for Flow-annotated JavaScript. The package provides two distinct grammars accessible via the .typescript and .tsx properties, along with nodeTypeInfo metadata for advanced tree traversal and debugging.

Tokens
578
Snippets
4
Records
4
Agent score
18%

What's inside tree-sitter-typescript

  1. Use TypeScript and TSX grammars from tree-sitter-typescript

    master

    The tree-sitter-typescript module provides two distinct grammars because TypeScript and TSX are treated as different dialects. To use them, require the specific grammar property from the module:

    • Use .typescript for standard TypeScript files.
    • Use .tsx for TSX files or JavaScript files containing [Flow] type annotations.
    require("tree-sitter-typescript").typescript; // TypeScript grammar
    require("tree-sitter-typescript").tsx; // TSX grammar
  2. Use eslint-config-treesitter in ESLint configuration

    master

    To apply the recommended ESLint rules for tree-sitter projects, import eslint-config-treesitter and spread its exported configuration array into your eslint.config.mjs file. This provides a pre-configured set of rules tailored for the repository's development environment.

    import treesitter from 'eslint-config-treesitter';
    
    export default [
      ...treesitter,
    ];
  3. Import the tree-sitter-typescript bindings

    master

    You can access the TypeScript and TSX grammars by requiring tree-sitter-typescript. The package automatically selects the correct prebuilt binary based on your platform and architecture. It supports both Node.js (via node-gyp-build) and Bun (via direct prebuild loading).

    const treeSitterTypeScript = require('tree-sitter-typescript');
  4. Access nodeTypeInfo for TypeScript and TSX

    master

    The bindings export nodeTypeInfo for both the typescript and tsx grammars. This object contains metadata about the node types defined in the grammar, which can be useful for advanced tree traversal or debugging.

    Available properties:

    • treeSitterTypeScript.typescript.nodeTypeInfo: Metadata for the TypeScript grammar.
    • treeSitterTypeScript.tsx.nodeTypeInfo: Metadata for the TSX grammar.
    const treeSitterTypeScript = require('tree-sitter-typescript');
    
    const tsNodeTypes = treeSitterTypeScript.typescript.nodeTypeInfo;
    const tsxNodeTypes = treeSitterTypeScript.tsx.nodeTypeInfo;