Skip to content

About

Vue SFC and template parser: lossless, never-failing, span-based, oxc-interoperable

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

vue_sfc_parser

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_sfc splits a .vue file 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_template parses template source into an ast::Node tree: elements with fully decomposed attributes (directives, v-bind/:, v-on/@, v-slot/#, modifiers), {{ ... }} interpolations, comments, and text runs. It takes a base_offset added to every span in the result, so spans are file-relative by construction: pass block.content_span.start when re-parsing an SfcBlock's extracted content, or 0 for standalone template text.

Design contract

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.

Example

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

Provenance and status

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.

License

MIT

About

Vue SFC and template parser: lossless, never-failing, span-based, oxc-interoperable

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages