swc-project/swc · error · TypeError

attempted to use private field on non-instance

Error message

attempted to use private field on non-instance

What it means

After the root element is closed the parser enters the End phase. Comments and processing instructions are legal there, but a CDATA section is not, so this arm both reports ErrorKind::UnexpectedTokenInEndPhase and still appends the CDATA node to the document (best-effort recovery). The error carries the CDATA span.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_class_private_field_loose_base.rs:12

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

use super::{HelperDef, HelperName};

pub const DEF: HelperDef = HelperDef {
    name: HelperName::class_private_field_loose_base,
    local: "_class_private_field_loose_base",
    import_path: "@swc/helpers/_/_class_private_field_loose_base",
    #[cfg(feature = "inline-helpers")]
    source: r#"function _class_private_field_loose_base(receiver, privateKey) {
    if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
        throw new TypeError("attempted to use private field on non-instance");
    }

    return receiver;
}
"#,
    #[cfg(feature = "inline-helpers")]
    deps: super::HelperBitmap::from_bits(0x00000000000000000000000008000000),
};

#[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 CDATA section inside the root element where character data is permitted.
  2. If the trailing CDATA came from concatenation, stop concatenating complete documents and merge at the node/tree level instead.
  3. Delete the trailing block if it is accidental boilerplate.
  4. Use the error span to locate which of several trailing blocks is the offender.

Example fix

<!-- before: CDATA after root -->
<doc/>
<![CDATA[trailing data]]>

<!-- after -->
<doc>
  <![CDATA[trailing data]]>
</doc>
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)?;
let bad = errors.iter().any(|e| {
    matches!(e.kind(), ErrorKind::UnexpectedTokenInEndPhase)
});
if bad {
    return Err("content found after the root element".into());
}

Prevention

When it happens

Trigger: A `<![CDATA[...]]>` block appearing after the closing tag of the root element, e.g. `<root/><![CDATA[trailing]]>`, typically from concatenating a fragment that starts with CDATA onto a complete document.

Common situations: Appending raw payload chunks after a serialized document; templating that emits CDATA-wrapped scripts after the document body; split files where the second part begins with CDATA and is glued to the first part's completed root.

Related errors


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