tree-sitter-svelte-ng
An incremental Tree-sitter grammar for Svelte component syntax.
Repository Health
Technical Analysis
tree-sitter-svelte-ng is a Tree-sitter grammar for Svelte, the component framework’s single-file component syntax. It parses .svelte files, blending HTML-like markup, script, style, and Svelte template directives, into a fast, incrementally-updated concrete syntax tree that editors and tooling can walk for highlighting, navigation, and analysis.
Maintained under the tree-sitter-grammars organization, it ships bindings for Rust, Node, and Python from a single grammar definition and is the maintained successor (“ng”) to earlier Svelte grammars.
What You Get
- A Tree-sitter grammar for Svelte single-file components, compiled to a portable C parser.
- A Rust binding exposing the parser as a
tree_sitter_language::LanguageFnfor the Tree-sitter runtime. - Highlight and injection query files that delegate script and style regions to their own grammars.
- Multi-language bindings (Rust, Node, Python) generated from one grammar definition.
Common Use Cases
- Syntax highlighting for .svelte files in editors like Neovim.
- Structural navigation, folding, and text objects over Svelte components.
- Language tooling that needs a reliable parse tree for Svelte markup and directives.
Under The Hood
Architecture - The grammar is authored in grammar.js using Tree-sitter’s DSL and compiled to a portable src/parser.c plus src/node-types.json; injection queries in queries/ delegate <script> and <style> blocks to the JavaScript/TypeScript and CSS grammars, so the Svelte grammar focuses on markup and template directives.
Tech Stack - The Rust binding (built via build.rs with the cc crate) depends on tree-sitter-language at runtime and tree-sitter for testing; the repo also publishes an npm scoped package and a PyPI package from the same grammar, with GitHub Actions CI validating changes.
Code Quality - Correctness is anchored by a Tree-sitter test corpus under test/, exercised through the standard tree-sitter test harness; the codebase is small and consists mostly of generated parser output plus the hand-written grammar and queries.
API Design - The public surface follows Tree-sitter conventions: the crate exports a LANGUAGE constant (LanguageFn) and query strings, so anyone familiar with the ecosystem can wire it into a parser in a few lines.