A Vue Single-File Component and template parser in Rust: lossless, never-failing,
span-based, and built to interoperate with the oxc toolchain.
Sibling to svelte_markup_parser,
under the same contract.
parse_sfcsplits a.vuefile into its top-level blocks (<template>,<script>,<style>, custom blocks) without touching their contents. Raw-text blocks (script/style) never nest;<template>does. The whole file is parsed in one pass, so every span it returns is already file-relative.parse_templateparses template source into anast::Nodetree: elements with fully decomposed attributes (directives,v-bind/:,v-on/@,v-slot/#, modifiers),{{ ... }}interpolations, comments, and text runs. It takes abase_offsetadded to every span in the result, so spans are file-relative by construction: passblock.content_span.startwhen re-parsing anSfcBlock's extracted content, or0for standalone template text.
The parser never fails. Malformed markup degrades to ast::Node::Raw
pass-through spans rather than errors, so consumers (formatters, linters)
always receive a tree that covers the whole input. Unterminated comments,
interpolations, and attribute values are flagged, not fatal.
Embedded JavaScript/TypeScript is intentionally not parsed here. Directive
values and interpolation expressions are exposed as spans into the source;
consumers hand them to a JS/TS parser (e.g. oxc_parser) in whatever mode
they need. This keeps the crate small, dependency-light, and parser-agnostic.
Spans are oxc_span::Span. The one dependency. Byte-offset spans that
plug directly into oxc's diagnostics, semantic analysis, and fixers with zero
conversion.
use vue_sfc_parser::{parse_sfc, parse_template, ast::Node};
let source = std::fs::read_to_string("component.vue")?;
let sfc = parse_sfc(&source);
for block in &sfc.blocks {
if block.tag == "template" {
let content = &source[block.content_span];
let nodes = parse_template(content, block.content_span.start);
for node in &nodes {
if let Node::Element(element) = node {
println!("<{}> at {:?}", element.tag, element.span);
}
}
}
}There is also a corpus checker that parses every .vue file under a directory
and verifies structural invariants (no panics, contiguous sibling spans,
blocks extracted):
cargo run --example parse_corpus -- path/to/vue/project
This crate was developed inside pitis/oxc, a
fork of oxc, where it is the foundation for 56 native vue/* template lint
rules running in oxlint. It is extracted here as a standalone crate with its
original commit history.
The API is young (0.1.x) and may move; the never-failing span contract will
not.