swc-project/swc · error

unable to access unknown nodes

Error message

unable to access unknown nodes

What it means

transform_fn_params iterates function parameters in reverse, matching each Pat (Ident/Array/Object carry optionality; Assign/Invalid/Expr/Rest do not) to compute whether later params must stay required in .d.ts output. Under the swc_ast_unknown cfg (`--cfg=swc_ast_unknown`, set for wasm plugin builds) Pat has an Unknown(tag, payload) variant; this arm panics because optionality of an unknown pattern kind cannot be determined.

Source

Thrown at crates/swc_typescript/src/fast_dts/function.rs:47

    }

    pub(crate) fn transform_fn_return_type(&mut self, func: &mut Function) {
        if func.return_type.is_none() && !func.is_async && !func.is_generator {
            func.return_type = self.infer_function_return_type(func);
        }
    }

    pub(crate) fn transform_fn_params(&mut self, params: &mut [Param]) {
        // If there is required param after current param.
        let mut is_required = false;
        for param in params.iter_mut().rev() {
            is_required |= match &param.pat {
                Pat::Ident(binding_ident) => !binding_ident.optional,
                Pat::Array(array_pat) => !array_pat.optional,
                Pat::Object(object_pat) => !object_pat.optional,
                Pat::Assign(_) | Pat::Invalid(_) | Pat::Expr(_) | Pat::Rest(_) => false,
                #[cfg(swc_ast_unknown)]
                _ => panic!("unable to access unknown nodes"),
            };
            self.transform_fn_param(param, is_required);
        }
    }

    pub(crate) fn transform_function_params(&mut self, function: &mut Function) {
        self.check_this_param(function.this_param.as_deref());
        self.transform_fn_params(&mut function.params);
    }

    pub(crate) fn check_this_param(&mut self, this_param: Option<&TsThisParam>) {
        let Some(this_param) = this_param else {
            return;
        };
        if this_param.type_ann.is_none() {
            self.parameter_must_have_explicit_type(this_param.span);
        }
    }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Rebuild the plugin with the swc_core version matching the host @swc/core
  2. Update swc_core to the latest release so the pattern decodes as a known Pat variant
  3. Pre-check params for Unknown patterns and skip fast_dts for those functions
  4. Automate plugin rebuilds as part of host SWC upgrades

Example fix

// before
transform.run(&mut program); // fast_dts hits Pat::Unknown in fn params -> panic

// after (guard under the plugin cfg)
#[cfg(swc_ast_unknown)]
fn fn_params_have_unknown_pat(f: &swc_ecma_ast::Function) -> bool {
    f.params.iter().any(|p| matches!(p.pat, swc_ecma_ast::Pat::Unknown(..)))
}
if !fn_params_have_unknown_pat(&function) { transform.run(&mut program); }
Defensive patterns

Strategy: type-guard

Validate before calling

#[cfg(swc_ast_unknown)]
fn program_fns_contain_unknown_param_pat(m: &swc_ecma_ast::Module) -> bool {
    use swc_ecma_ast::*;
    fn any_unknown(params: &[Param]) -> bool {
        params.iter().any(|p| matches!(p.pat, Pat::Unknown(..)))
    }
    m.body.iter().any(|i| matches!(i, ModuleItem::Stmt(Stmt::Decl(Decl::Fn(f)))
        if any_unknown(&f.function.params)))
}

Type guard

#[cfg(swc_ast_unknown)]
fn pat_is_unknown(p: &swc_ecma_ast::Pat) -> bool {
    matches!(p, swc_ecma_ast::Pat::Unknown(..))
}

Try / catch

let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    run_fast_dts(&mut program)
}));
if out.is_err() {
    return Err("unknown param pattern; plugin swc_core is older than host".into());
}

Prevention

When it happens

Trigger: Running fast_dts on any function or method whose parameter binding pattern deserialized as Unknown — a plugin compiled with --cfg=swc_ast_unknown receiving a Program from a newer host @swc/core that emitted a binding-pattern kind absent from the plugin's swc_ecma_ast.

Common situations: Upgrading @swc/core without rebuilding plugins; plugins built against old lockfiles; new Pat variants introduced upstream.

Related errors


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