swc-project/swc · error · TypeError

attempted to get private field on non-instance

Error message

attempted to get private field on non-instance

What it means

In the Start and End phases (outside the root element) only whitespace character data is legal. When a Token::Character carries a non-whitespace value in the End phase, the parser records ErrorKind::UnexpectedCharacter (message: 'Unexpected character, only whitespace character allowed') with that character's span and otherwise ignores the text. The identical check exists in the Start phase arm just above.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_class_private_method_get.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_get,
    local: "_class_private_method_get",
    import_path: "@swc/helpers/_/_class_private_method_get",
    #[cfg(feature = "inline-helpers")]
    source: r#"function _class_private_method_get(receiver, privateSet, fn) {
    if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance");

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

#[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. Go to the reported span and delete or relocate the non-whitespace text that sits outside the root element.
  2. Wrap stray trailing content inside the root element if it is meaningful data.
  3. Trim the document after serialization and ensure templates have no literal footer text.
  4. Remember only U+0020, U+0009, U+000D, U+000A count as whitespace - replace fancy Unicode spaces with plain spaces.

Example fix

<!-- before: text after root element -->
<doc>ok</doc>
trailing junk

<!-- after -->
<doc>ok</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)?;
if errors
    .iter()
    .any(|e| matches!(e.kind(), ErrorKind::UnexpectedCharacter))
{
    return Err("non-whitespace text outside the root element".into());
}

Prevention

When it happens

Trigger: Any visible text after the root element's closing tag (or, via the Start-phase arm at line 281, before it): `<doc/>trailing`, stray punctuation, or formatting artifacts like a BOM-adjacent character or leftover markup fragments that the lexer tokenized as characters.

Common situations: Trailing debug prints or log lines appended after serialized XML; template footer text placed after the closing tag; concatenated documents; data whose final newline was followed by a stray character during manual editing; non-breaking space or other Unicode whitespace-classified-as-non-whitespace characters (the check uses the XML whitespace set: space, tab, CR, LF only).

Related errors


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