Skip to main content

Crate tsv_svelte

Crate tsv_svelte 

Source
Expand description

Svelte formatting, and parsing to the wire JSON of Svelte’s modern parse() — part of tsv.

format() formats one component in one call; a Formatter formats many on one thread, keeping its arenas warm between calls. Each <script>, <style> and template expression is formatted by tsv_ts or tsv_css as part of one document. The formatter is non-configurable: Prettier’s style with a print width of 100, tabs, single quotes and no trailing commas. Formatting output is not frozen — it changes between releases as formatting bugs are fixed.

§Usage

use tsv_svelte::{Formatter, format};

assert_eq!(
    format("<script>let x=1</script><p>{x}</p>")?,
    "<script>\n\tlet x = 1;\n</script>\n\n<p>{x}</p>\n"
);
let mut formatter = Formatter::new();
assert_eq!(formatter.format("<p  class=\"a\">y</p>")?, "<p class=\"a\">y</p>\n");

// Parse to the wire JSON AST (the `json` feature, on by default)
let json = tsv_svelte::parse_to_json("<p>x</p>")?;
assert!(json.contains(r#""type":"Root""#));

The wire JSON is the AST as JSON in the canonical parser’s shape — Svelte’s modern parse()’s here. Its start / end are UTF-16 offsets, and the default wire is span-only: no loc (the locations feature adds it). Error::span, unlike the wire, is a UTF-8 byte range.

§Errors

A source the format cannot take is an Error — here a refusal of one that parses:

use tsv_svelte::{ErrorKind, format};

// formatting folds this `\r` to `\n`, which would end the in-tag comment early
let err = format("<div // a\rb\n></div>").unwrap_err();
assert_eq!(err.kind(), ErrorKind::Refusal);
assert_eq!(err.span(), Some(9..10));

Every tsv language crate returns the same Error: its Error::kind, its Error::span in the input’s UTF-8 bytes, and Error::to_report — how tsv’s bindings (its npm packages) report it — in the wire’s UTF-16 offsets and lines; one line under {}, the tsv CLI’s frame under {:#}.

§Deep input

The parser refuses a tree past its depth limits with ErrorKind::TooDeep, so every walk of an accepted tree fits a 2 MiB thread in an optimized build and a 16 MiB one in a debug build. The limits count a whole component, its <script>, <style> and template expressions included, against one budget. A stack overflow aborts the process rather than panicking, so a debug build handling input it does not control — cargo test runs each test on a 2 MiB thread — wraps the call in run_with_stack, which runs it on a thread reserving STACK_SIZE.

§Features

  • json (default) — parse_to_json, parse_to_json_with and JsonOptions. Turn default features off for a format-only build.
  • locations — JsonOptions::locations, adding a loc (line and column) to every positioned object of the wire, and a name_loc to elements, attributes and directives.

The _internal_* features are tsv’s own diagnostics, not for consumers (Stability).

§Stability

This release exposes no AST: parsing returns the wire JSON, in Svelte’s modern parse() shape, which @fuzdev/tsv-parse-wasm’s tsv_ast.d.ts types.

Items hidden from these docs (__private, ast and every other #[doc(hidden)] item) and the _internal_* features are tsv’s own plumbing for its sibling crates, which pin each other exactly; they change in any release, outside semver.

Structs§

Error
Why a parse or format produced no output — a syntax error, a depth limit, a refusal of a source that parses, or a source over the size cap (ErrorKind): what went wrong (Error::kind, Error::message), where (Error::span, UTF-8 byte offsets), and the pointed report tsv’s bindings (its npm packages) render (Error::to_report). One pointer wide, so a Result carrying it costs a successful call almost nothing.
ErrorReport
A pointed error as tsv’s bindings (its npm packages) report it: the message and the frame apart, and the span in the document’s wire coordinates (Error::to_report). The error’s full text (its {:#} form) is exactly message, a \n, then frame.
Formatter
Formats many Svelte sources on one thread, reusing its allocations from one source to the next.
JsonOptions
What parse_to_json_with’s wire carries. The default is parse_to_json’s: the span-only wire. Its one option, locations, requires the locations feature.
WirePoint
One position in a language’s wire coordinates, the positions its wire JSON (the parse output: the AST as JSON in the canonical parser’s shape) carries. It holds the offset a wire node starting at that byte would carry as its start, and the loc point tsv parse --locations (and the npm packages’ create_locator(…).position_at(start)) gives it. The coordinates are the language’s canonical parser’s: a leading byte-order mark counted for TypeScript (acorn) and elided for Svelte and CSS, and lines ended by ECMAScript’s terminators for TypeScript and by \n alone for Svelte and CSS.

Enums§

ErrorKind
What kind of failure an Error is (Error::kind).

Constants§

STACK_SIZE
Stack reserved by run_with_stack: 32 MiB, twice the debug build’s contract.

Functions§

format
Parse and format source in one call.
parse_to_json
Parse source and return its wire JSON AST: compact JSON in Svelte’s modern parse() shape, every node’s start / end a UTF-16 offset and no loc.
parse_to_json_with
parse_to_json under options: whether the wire carries loc.
run_with_stack
Run f on a thread reserving STACK_SIZE and return its value.

Type Aliases§

Result
std::result::Result with Error as the default error type, so a glob import leaves the two-parameter form usable.