Skip to main content

TemplateElement

Struct TemplateElement 

Source
#[repr(C)]
pub struct TemplateElement<'gc> { pub metadata: NodeMetadata<'gc>, pub tail: Cell<bool>, pub cooked: Cell<NodeString>, pub raw: Cell<NodeLabel>, }
Expand description

The TemplateElement AST node.

Fields§

§metadata: NodeMetadata<'gc>

Source range, debug location, paren count, and node id.

§tail: Cell<bool>

ESTree tail property.

§cooked: Cell<NodeString>

ESTree cooked property.

§raw: Cell<NodeLabel>

ESTree raw property.

Implementations§

Source§

impl<'gc> TemplateElement<'gc>

Source

pub fn new( metadata: NodeMetadata<'gc>, tail: bool, cooked: NodeString, raw: NodeLabel, ) -> Self

Build TemplateElement from its metadata and ESTree.def fields.

Source

pub fn try_cooked_str<'a>(&self, gc: &'a GCLock<'_, '_>) -> Option<&'a str>

The cooked string value as UTF-8, or None if it has no UTF-8 form.

A JS string value is a sequence of UTF-16 code units, so it may legally contain an unpaired surrogate ("\uD800" parses, and is not an error). That, and only that, is what None reports: the value is intact, it simply cannot be spelled in UTF-8. Read it losslessly with GCLock::bytes.

Only an unpaired surrogate is unrepresentable. A WTF-8 surrogate pair is not: the lexer interns an astral character in surrogate-pair form, and it is folded back into the character it encodes, so a "😀" literal yields Some("😀"), not None.

There is deliberately no plain cooked_str: an unrepresentable identifier means something is broken, but an unrepresentable string literal is legal JS, and a codegen or refactoring tool that let U+FFFD be substituted here would silently rewrite the user’s program. Reach for Self::cooked_str_lossy only when a best-effort rendering is what you want.

Valid UTF-8 is borrowed from the atom’s own bytes and allocates nothing; folding a surrogate pair allocates once per atom, cached in the context. The returned &str borrows from gc, not from self.

Source

pub fn cooked_str_lossy<'a>(&self, gc: &'a GCLock<'_, '_>) -> &'a str

The cooked string value as UTF-8, substituting U+FFFD for anything unrepresentable.

This is lossy. A JS string value may legally contain an unpaired surrogate ("\uD800" parses), which has no UTF-8 form; each one becomes exactly one U+FFFD here. Do not use this to re-emit source: a codegen or refactoring tool would silently rewrite the user’s program.

Only an unpaired surrogate is unrepresentable. A WTF-8 surrogate pair is not: the lexer interns an astral character in surrogate-pair form, and it is folded back into the character it encodes, so a "😀" literal comes out intact.

Use Self::try_cooked_str or GCLock::bytes when the exact value matters. The returned &str borrows from gc, not from self.

Source

pub fn raw_str<'a>(&self, gc: &'a GCLock<'_, '_>) -> &'a str

The raw label as UTF-8 text.

Label fields hold identifier names, operators, keyword-like kinds and raw source spans. An identifier cannot contain an unpaired surrogate — the lexer rejects one with a dedicated diagnostic — and the remaining labels are literal source text, so in practice the bytes are already valid UTF-8 and this borrows them directly, allocating nothing.

A label that is nevertheless unrepresentable, which realistically means a hand-built AST rather than a parsed one, has each unpaired surrogate rendered as a single U+FFFD instead of being reported. Only an unpaired surrogate is affected: a WTF-8 surrogate pair is folded back into the character it encodes, so a "😀" label comes out intact.

Use GCLock::bytes for the exact bytes, or GCLock::try_bytes_str to detect the substitution. The returned &str borrows from gc, not from self.

Trait Implementations§

Source§

impl<'gc> Debug for TemplateElement<'gc>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'gc> !Freeze for TemplateElement<'gc>

§

impl<'gc> !RefUnwindSafe for TemplateElement<'gc>

§

impl<'gc> !Send for TemplateElement<'gc>

§

impl<'gc> !Sync for TemplateElement<'gc>

§

impl<'gc> !UnwindSafe for TemplateElement<'gc>

§

impl<'gc> Unpin for TemplateElement<'gc>

§

impl<'gc> UnsafeUnpin for TemplateElement<'gc>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.