swc-project/swc · error
multiple constructor?
Error message
multiple constructor?
What it means
The decorator transform (swc_ecma_transforms_proposal::decorators) builds property descriptors from a class body; by the time this filter_map runs, the constructor has already been extracted and removed from the member list, so encountering another ClassMember::Constructor is treated as `multiple constructor?`. The parser itself rejects duplicate constructors, so this unreachable! signals a mutated/hand-built class AST rather than parsed source.
Source
Thrown at crates/swc_ecma_transforms_proposal/src/decorators/mod.rs:439
}
.into(),
),
},
)))))
.collect(),
}
.as_arg(),
)
}};
}
let descriptors = class
.body
.into_iter()
.filter_map(|member| {
//
match member {
ClassMember::Constructor(_) => unreachable!("multiple constructor?"),
ClassMember::Empty(_) | ClassMember::TsIndexSignature(_) => None,
ClassMember::Method(method) => {
let fn_name = match method.key {
PropName::Ident(ref i) => Some(i.clone()),
PropName::Str(ref s) => s
.value
.as_str()
.map(|sym| IdentName::new(Atom::from(sym), s.span)),
_ => None,
};
let key_prop_value = Box::new(prop_name_to_expr_value(method.key.clone()));
fold_method!(method, fn_name, key_prop_value)
}
ClassMember::PrivateMethod(method) => {
let fn_name = Ident::new_no_ctxt(
format!("_{}", method.key.name).into(),
method.key.span,View on GitHub (pinned to 5176682b65)
Solutions
- Validate the class body before the decorator pass: exactly zero-or-one Constructor member.
- Fix the upstream pass that injects or duplicates constructor members into the class body.
- If the class comes from parsed source, report an swc bug with the repro (the parser should have rejected it).
Example fix
// before: synthesized AST accidentally carries two constructors
class.body.push(ClassMember::Constructor(second_ctor));
// after: mutate in place, keep a single constructor
if let Some(ClassMember::Constructor(existing)) = class.body.iter_mut().find(|m| matches!(m, ClassMember::Constructor(_))) {
*existing = new_ctor;
} Defensive patterns
Strategy: validation
Validate before calling
use swc_ecma_ast::{ClassMember, Class};
fn single_constructor(c: &Class) -> bool {
c.body.iter().filter(|m| matches!(m, ClassMember::Constructor(_))).count() <= 1
}
// assert!(single_constructor(&class)) before the decorators pass Type guard
fn has_valid_ctor_count(c: &Class) -> bool { c.body.iter().filter(|m| matches!(m, ClassMember::Constructor(_))).count() <= 1 } Prevention
- Never inject ClassMember::Constructor into bodies mid-transform; mutate the existing one.
- Validate class bodies after foreign AST conversion and before decorator passes.
- Serialize/deserialize ASTs with the same swc version on both ends.
When it happens
Trigger: Programmatically constructed or transform-modified Class AST where two ClassMember::Constructor nodes exist (custom decorators, class-mutation passes, AST round-trips through FFI/serde that duplicate members), compiled with the 2022-era decorators option.
Common situations: Decorator plugin ecosystems that rewrite class bodies before the decorator pass; codemods synthesizing classes; swc AST serialized by one version and deserialized by another re-adding members.
Related errors
- unknown compound assignment operator
- getter/setter property be compiled as CompiledProp::Accessor
- Unknown bit operator {:?}
- unknown binary operator: {:?}
- Invalid attempt to iterate non-iterable instance. In order t
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/9914c4f291cc07d6.
Report an issue: GitHub.