treesitter-types-typescript 0.1.1

Pre-generated strongly-typed AST types for TypeScript (tree-sitter-typescript)
Documentation

Strongly-typed AST types for TypeScript, auto-generated from tree-sitter-typescript's node-types.json.

This crate is generated by treesitter-types and is automatically kept up to date when a new version of the grammar crate is released.

These types have been tested by parsing the TypeScript source code.

See the Tree-sitter project for more information about the underlying parser framework.

Example

use treesitter_types_typescript::*;

// A minimal TypeScript hello-world program.
let src = b"\
function greet(name: string): void {
    console.log(\"Hello, \" + name + \"!\");
}

greet(\"World\");
";

// Parse the source with tree-sitter and convert into typed AST.
let mut parser = tree_sitter::Parser::new();
parser
    .set_language(&tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into())
    .unwrap();
let tree = parser.parse(src, None).unwrap();
let program = Program::from_node(tree.root_node(), src).unwrap();

// The program has two top-level children.
assert_eq!(program.children.len(), 2);

// 1) The function declaration — `function greet(name: string): void { ... }`.
let ProgramChildren::Statement(stmt) = &program.children[0] else {
    panic!("expected a statement");
};
let Statement::Declaration(decl) = stmt.as_ref() else {
    panic!("expected a declaration");
};
let Declaration::FunctionDeclaration(func) = decl.as_ref() else {
    panic!("expected a function declaration");
};
assert_eq!(func.name.text(), "greet");
assert_eq!(func.parameters.children.len(), 1); // one parameter: `name: string`
assert!(func.return_type.is_some());            // has return type `: void`

// 2) The call expression — `greet("World");`.
let ProgramChildren::Statement(call_stmt) = &program.children[1] else {
    panic!("expected a statement");
};
let Statement::ExpressionStatement(expr) = call_stmt.as_ref() else {
    panic!("expected an expression statement");
};
assert_eq!(expr.span.start.row, 4);