swc-project/swc · error · napi::Error

InvalidArg

InvalidArg

Error message

invalid `syntax`: {err}

What it means

parse_syntax_option deserializes the caller-supplied `syntax` option from JSON bytes into the parser's Syntax config; malformed JSON or an unknown shape makes serde_json fail and lint/lint_sync wrap the error before any parsing starts. The binding was called with an invalid option payload.

Source

Thrown at bindings/binding_react_compiler_node/src/lint.rs:38

/// tests call constructs a `napi::Error` anywhere in its body — even in a
/// branch that specific test never executes — the whole function's compiled
/// object code references those unresolved symbols, and a standalone
/// `cargo test` binary (never loaded by Node) fails to link. Keep this
/// function's signature free of `napi::Error`/`napi::Result` so tests can
/// call it safely.
fn decode_syntax(bytes: &[u8]) -> std::result::Result<Syntax, serde_json::Error> {
    serde_json::from_slice(bytes)
}

/// Thin napi-facing wrapper — not unit tested (see `decode_syntax`'s doc
/// comment), matching this crate's existing convention of only testing the
/// pure logic behind a `#[napi]` function, never the function itself (see
/// `support.rs`'s `is_react_compiler_required(_sync)`, which have no tests
/// at all).
fn parse_syntax_option(syntax: Option<Buffer>) -> napi::Result<Syntax> {
    match syntax {
        Some(buf) => decode_syntax(buf.as_ref()).map_err(|err| {
            napi::Error::new(napi::Status::InvalidArg, format!("invalid `syntax`: {err}"))
        }),
        None => Ok(default_syntax()),
    }
}

fn lint_inner(code: &str, syntax: Syntax) -> Vec<Diagnostic> {
    let result = swc_ecma_react_compiler::lint_source(
        code,
        syntax,
        swc_ecma_react_compiler::default_plugin_options(),
    );

    result
        .diagnostics
        .iter()
        .map(|d| diagnostic_from_message(code, d))
        .collect()
}

View on GitHub (pinned to 5176682b65)

Solutions

  1. Send a JSON value matching the expected Syntax option shape
  2. Log the wrapped serde_json error to find the offending field
  3. Validate the option payload on the JS side before calling the binding
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at bindings/binding_react_compiler_node/src/lint.rs:38 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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