Skip to main content

treesitter_types_javascript/
lib.rs

1//! Strongly-typed AST types for JavaScript, auto-generated from
2//! [`tree-sitter-javascript`](https://docs.rs/tree-sitter-javascript)'s `node-types.json`.
3//!
4//! This crate is generated by [`treesitter-types`](https://docs.rs/treesitter-types) and is
5//! automatically kept up to date when a new version of the grammar crate is released.
6//!
7//! These types have been tested by parsing the
8//! [Node.js](https://github.com/nodejs/node) source code.
9//!
10//! See the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) project for more
11//! information about the underlying parser framework.
12//!
13//! # Example
14//!
15//! ```
16//! use treesitter_types_javascript::*;
17//!
18//! // A minimal JavaScript hello-world program.
19//! let src = b"\
20//! function greet(name) {
21//!     console.log(\"Hello, \" + name + \"!\");
22//! }
23//!
24//! greet(\"World\");
25//! ";
26//!
27//! // Parse the source with tree-sitter and convert into typed AST.
28//! let mut parser = tree_sitter::Parser::new();
29//! parser.set_language(&tree_sitter_javascript::LANGUAGE.into()).unwrap();
30//! let tree = parser.parse(src, None).unwrap();
31//! let program = Program::from_node(tree.root_node(), src).unwrap();
32//!
33//! // The program has two top-level children.
34//! assert_eq!(program.children.len(), 2);
35//!
36//! // 1) The function declaration — `function greet(name) { ... }`.
37//! let ProgramChildren::Statement(stmt) = &program.children[0] else {
38//!     panic!("expected a statement");
39//! };
40//! let Statement::Declaration(decl) = stmt.as_ref() else {
41//!     panic!("expected a declaration");
42//! };
43//! let Declaration::FunctionDeclaration(func) = decl.as_ref() else {
44//!     panic!("expected a function declaration");
45//! };
46//! assert_eq!(func.name.text(), "greet");
47//! assert_eq!(func.parameters.children.len(), 1); // one parameter: `name`
48//!
49//! // 2) The call expression — `greet("World");`.
50//! let ProgramChildren::Statement(call_stmt) = &program.children[1] else {
51//!     panic!("expected a statement");
52//! };
53//! let Statement::ExpressionStatement(expr) = call_stmt.as_ref() else {
54//!     panic!("expected an expression statement");
55//! };
56//! assert_eq!(expr.span.start.row, 4);
57//! ```
58
59pub use tree_sitter_javascript;
60pub use treesitter_types::tree_sitter;
61pub use treesitter_types::{FromNode, LeafNode, ParseError, Span, Spanned};
62
63mod generated;
64pub use generated::*;