biomejs/biome · error

token kind {kind:?} cannot be transformed to text

Error message

token kind {kind:?} cannot be transformed to text

What it means

biome_html_factory::make::token(kind) builds a detached HTML token using the fixed source text that HtmlSyntaxKind::to_string() maps the kind to (make.rs:30-35). Only kinds with static text - punctuation, keywords, fixed tokens - have such a mapping; kinds whose text depends on the source (IDENT, HTML_STRING_LITERAL, error/bogus kinds) return None and hit this panic. It is a factory misuse guard: the token text must be provided explicitly for dynamic kinds.

Source

Thrown at crates/biome_html_factory/src/make.rs:34

    )
}

/// Create a new single-quoted string literal token with no attached trivia
pub fn html_string_literal_single_quotes(text: &str) -> HtmlSyntaxToken {
    HtmlSyntaxToken::new_detached(
        HtmlSyntaxKind::HTML_STRING_LITERAL,
        &format!("'{text}'"),
        [],
        [],
    )
}

/// Create a new token with the specified syntax kind and no attached trivia
pub fn token(kind: HtmlSyntaxKind) -> HtmlSyntaxToken {
    if let Some(text) = kind.to_string() {
        HtmlSyntaxToken::new_detached(kind, text, [], [])
    } else {
        panic!("token kind {kind:?} cannot be transformed to text")
    }
}

View on GitHub (pinned to 405dedb0ff)

Solutions

  1. Use the dedicated constructors that take the text: make::ident(text) for IDENT, make::html_string_literal(text) / make::html_string_literal_single_quotes(text) for HTML_STRING_LITERAL (make.rs:5-27)
  2. Build the token directly with HtmlSyntaxToken::new_detached(kind, text, [], []) for any other dynamic kind
  3. Check kind.to_string().is_some() before calling make::token when the kind comes from user input or a match over many kinds
  4. If you believe the kind should have static text (keyword/punct), verify it is declared with text in the generated HtmlSyntaxKind list; re-run just gen-grammar html if the grammar was changed

Example fix

// before
let tok = biome_html_factory::make::token(HtmlSyntaxKind::HTML_STRING_LITERAL); // panics

// after
let tok = biome_html_factory::make::html_string_literal("color: red");
Defensive patterns

Strategy: validation

Validate before calling

// Rust - only call make::token for kinds with fixed source text
if kind.to_string().is_some() {
    let tok = biome_html_factory::make::token(kind);
} else {
    // dynamic kind: supply the text explicitly
    let tok = HtmlSyntaxToken::new_detached(kind, "actual text", [], []);
}

Type guard

fn has_static_text(kind: HtmlSyntaxKind) -> bool {
    kind.to_string().is_some()
}

Prevention

When it happens

Trigger: Calling make::token(HtmlSyntaxKind::IDENT), make::token(HtmlSyntaxKind::HTML_STRING_LITERAL), or passing any variable-content or error kind. Typically hit when porting code from another language factory (e.g. biome_js_factory::make::token) where the same kind names do carry static text, or when constructing nodes in tests via make:: token.

Common situations: Writing or updating HTML parser/formatter code or snapshot tests in crates/biome_html_parser, biome_html_formatter, biome_html_factory after a grammar regeneration added new kinds; copying factory idioms from the JS factory where token(JSX_TEXT-like kinds) worked.

Related errors


AI-assisted analysis of biomejs/biome@405dedb0ff (2026-08-20). Data as JSON: /api/errors/46288d84dc4f6509. Report an issue: GitHub.