swc-project/swc · error · ReferenceError

Class "${name}" cannot be referenced in computed property ke

Error message

Class "${name}" cannot be referenced in computed property keys.

What it means

In the Main phase (inside the root element) every token kind has a handler except the doctype token, which lands in the `_` catch-all and yields ErrorKind::UnexpectedTokenInMainPhase. XML allows exactly one doctype, and only in the prolog before the root element, so a `<!DOCTYPE ...>` seen after content has started is reported here. It is a recoverable error: the doctype token is dropped and parsing continues.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_class_name_tdz_error.rs:11

// This file is generated by `cargo codegen helpers`. DO NOT MODIFY.

use super::{HelperDef, HelperName};

pub const DEF: HelperDef = HelperDef {
    name: HelperName::class_name_tdz_error,
    local: "_class_name_tdz_error",
    import_path: "@swc/helpers/_/_class_name_tdz_error",
    #[cfg(feature = "inline-helpers")]
    source: r#"function _class_name_tdz_error(name) {
    throw new ReferenceError("Class \"" + name + "\" cannot be referenced in computed property keys.");
}
"#,
    #[cfg(feature = "inline-helpers")]
    deps: super::HelperBitmap::from_bits(0x00000000000000000000000000800000),
};

#[cfg(feature = "inline-helpers")]
pub fn stmts() -> &'static [swc_ecma_ast::Stmt] {
    static STMTS: once_cell::sync::Lazy<Vec<swc_ecma_ast::Stmt>> =
        once_cell::sync::Lazy::new(|| super::super::parse(DEF.source, DEF.import_path));
    &STMTS
}

View on GitHub (pinned to 5176682b65)

Solutions

  1. Move the `<!DOCTYPE ...>` declaration to the very top of the document, before the root element and after nothing but the optional XML declaration.
  2. If generating fragments, emit the doctype once for the outer document and strip it from inner fragments.
  3. Check the error span to find which doctype is misplaced when several exist.
  4. Validate with xmllint --noout to get a second opinion on the document structure.

Example fix

<!-- before: doctype inside the root element -->
<items>
  <!DOCTYPE item>
  <item/>
</items>

<!-- after -->
<!DOCTYPE items>
<items>
  <item/>
</items>
Defensive patterns

Strategy: validation

Validate before calling

use swc_xml_parser::error::ErrorKind;

let mut errors = Vec::new();
swc_xml_parser::parse_file_as_document(&fm, config, &mut errors)?;
if errors
    .iter()
    .any(|e| matches!(e.kind(), ErrorKind::UnexpectedTokenInMainPhase))
{
    return Err("misplaced doctype (must precede the root element)".into());
}

Prevention

When it happens

Trigger: A `<!DOCTYPE ...>` declaration that appears inside the root element or after it has been opened, e.g. `<root><!DOCTYPE item></root>`, or doctype text embedded in generated fragments that were concatenated after the root started.

Common situations: XML fragments generated per-item each containing their own doctype and then concatenated inside a wrapper element; templating that injects a doctype partial in the wrong slot; SVG/HTML-style documents (which commonly carry doctypes) fed to the XML parser with the doctype nested in the body.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/f1c4673b4f0f19b4. Report an issue: GitHub.