Skip to main content

Formatter

Struct Formatter 

Source
pub struct Formatter { /* private fields */ }
Expand description

Formats many TypeScript sources on one thread, reusing its allocations from one source to the next.

format() allocates a fresh AST arena and a fresh doc arena for every call and drops both before returning. A Formatter owns the pair and rewinds them instead: each rewind keeps the largest block it has grown, so after the first few sources the work allocates almost nothing new — the shape of a driver that formats a directory, or an editor that formats on every save. The output is exactly format()’s (and format_with_goal’s) for the same source.

The arenas rewind at the start of every call, so nothing one call leaves behind reaches the next — including a call that never returned because it panicked and the panic was caught: the next call starts from a clean arena all the same. The AST arena is rewound once more as each call returns, which releases every block but the largest while the Formatter sits idle.

Catching that panic takes std::panic::AssertUnwindSafe around the call — a closure borrowing the Formatter mutably is not UnwindSafe — and the rewind is what makes the assertion sound.

A Formatter is Send, so it can move to (or be created on) a worker thread, but not Sync: give each thread its own.

use tsv_ts::{Formatter, Goal};

let mut formatter = Formatter::new();
for source in ["const x=42;", "let y = [1,2]"] {
    let formatted = formatter.format(source)?;
    assert_eq!(formatted, tsv_ts::format(source)?);
}
assert!(formatter.format_with_goal("with (a) b;", Goal::Module).is_err());
assert_eq!(formatter.format("with (a) b;")?, "with (a) b;\n");

Implementations§

Source§

impl Formatter

Source

pub fn new() -> Self

A Formatter with empty arenas; they grow to the sources it is given.

Source

pub fn format(&mut self, source: &str) -> Result<String>

format() on this formatter’s arenas: no source type named, so Module, retried at Script only if that fails (Goals).

§Errors

A parse error under both goals (the one Goals picks).

Source

pub fn format_with_goal(&mut self, source: &str, goal: Goal) -> Result<String>

format_with_goal on this formatter’s arenas: exactly goal, with no fallback.

§Errors

A parse error at goal.

Trait Implementations§

Source§

impl Debug for Formatter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Formatter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.