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
- Move the CDATA section inside the root element where character data is permitted.
- If the trailing CDATA came from concatenation, stop concatenating complete documents and merge at the node/tree level instead.
- Delete the trailing block if it is accidental boilerplate.
- 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
- Wrap multi-part payloads in a container element instead of appending CDATA after the root.
- Run generated output through xmllint in CI to catch trailing junk.
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
- attempted to reassign private method
- The requested module '{specifier}' does not provide an expor
- Cannot use import.meta outside a module
- Assignment to constant variable.
- attempted to ${action} private field on non-instance
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/ccabae308e8421a6.
Report an issue: GitHub.