swc-project/swc · error · TypeError

attempted to reassign private method

Error message

attempted to reassign private method

What it means

In the End phase only comments, processing instructions, whitespace characters, and EOF are legal; everything else (a second start/empty tag, an end tag, or a doctype) falls into the `_` catch-all and is reported as ErrorKind::UnexpectedTokenInEndPhase. The token is then discarded - notably this is how a second root element is rejected, because XML permits exactly one.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_class_private_method_set.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_private_method_set,
    local: "_class_private_method_set",
    import_path: "@swc/helpers/_/_class_private_method_set",
    #[cfg(feature = "inline-helpers")]
    source: r#"function _class_private_method_set() {
    throw new TypeError("attempted to reassign private method");
}
"#,
    #[cfg(feature = "inline-helpers")]
    deps: super::HelperBitmap::from_bits(0x00000000000000000000000200000000),
};

#[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. Wrap all top-level content in a single root element so there is exactly one root.
  2. Remove duplicate end tags or stray declarations after the root's closing tag.
  3. If you must store many documents in one file, use a container element or a format that supports multiple roots (e.g. XML fragments with an external parser policy).
  4. Use the error span to find the offset of the second root/tag and split the file there if it truly contains two documents.

Example fix

<!-- before: two root elements -->
<record id="1"/>
<record id="2"/>

<!-- after -->
<records>
  <record id="1"/>
  <record id="2"/>
</records>
Defensive patterns

Strategy: validation

Validate before calling

use swc_xml_parser::error::ErrorKind;

let mut errors = Vec::new();
let doc = swc_xml_parser::parse_file_as_document(&fm, config, &mut errors)?;
if errors
    .iter()
    .any(|e| matches!(e.kind(), ErrorKind::UnexpectedTokenInEndPhase))
{
    return Err("extra root element or stray tags at document end".into());
}
// belt-and-braces: count element children of the document
let roots = doc
    .children
    .iter()
    .filter(|c| matches!(c, swc_xml_ast::Child::Element(_)))
    .count();
assert_eq!(roots, 1, "XML requires exactly one root element");

Prevention

When it happens

Trigger: Two sibling root elements (`<a/><b/>`), an end tag with no matching open element (`</a>` twice), or a doctype after the root element. Any tag token arriving after the open-elements stack emptied and the phase switched to EndPhase.

Common situations: Concatenating multiple XML documents into one file (very common with logs or batch exports); templating loops that repeat the root element; copy-pasting a second document into an existing file; forgetting to wrap repeated items in a container element.

Related errors


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