karatelabs/karate · error · ParserException

invalid

Error message

invalid ${siteName}

What it means

A generic invalidity error emitted during Karate's destructuring-target validation, with `${siteName}` substituted (e.g. 'invalid assignment target' or a declaration site name). While unwrapping parentheses around a would-be destructuring/assignment target, the parser found a PAREN_EXPR node with no body — a malformed parenthesized expression — so the target cannot be valid. Parsing aborts.

Solutions

  1. Complete the expression inside the parentheses with a valid binding or target
  2. Remove the empty parentheses if they are leftover syntax
  3. Re-check the statement for missing identifiers on the left side of `=` or in a declaration list

Example fix

// before
var () = arr; // empty target
// after
var [a, b] = arr;
Defensive patterns

Strategy: validation

Validate before calling

// detect empty parentheses in binding/assignment position
if (/\b(?:var|let|const)\s*\(\s*\)/.test(src) || /^\(\s*\)\s*=[^=]/.test(src.trim())) {
  throw new Error('empty () used as a destructuring/assignment target');
}

Try / catch

try {
  runScript(src);
} catch (e) {
  if (/^invalid /.test(e.message)) {
    // inspect the left-hand side for malformed parens
  }
  throw e;
}

Prevention

When it happens

Trigger: Parsing a syntactically broken parenthesized expression used as a destructuring or assignment target, e.g. `() = 5;` or `var () = arr;` — the PAREN_EXPR shape check `n.size() >= 2 ? n.get(1) : null` yields null and throws 'invalid ' + siteName.

Common situations: Typos leaving an empty `()` where a binding was expected; incomplete edits saving partially written destructuring code; macro/template generation producing malformed targets.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/2ee7e635b10b692c. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/parser/JsParser.java:1694

        if (n == null) {
            // Defensive — empty LHS only happens in error-recovery edge cases.
            return;
        }
        // Top-level Object/Array literal refines into a destructuring pattern.
        if (n.type == NodeType.LIT_EXPR && n.size() >= 1) {
            NodeType lit = n.getFirst().type;
            if (lit == NodeType.LIT_ARRAY || lit == NodeType.LIT_OBJECT) {
                return;
            }
        }
        // Peel any layers of parens. Per spec, a ParenthesizedExpression around an
        // ObjectLiteral or ArrayLiteral does NOT refine to a destructuring pattern,
        // so it becomes invalid; everything else falls through to the simple check.
        while (n.type == NodeType.PAREN_EXPR) {
            // PAREN_EXPR shape: [(, body, )]
            Node body = n.size() >= 2 ? n.get(1) : null;
            if (body == null) {
                throw new ParserException("invalid " + siteName);
            }
            if (body.type == NodeType.EXPR_LIST && body.size() != 1) {
                throw new ParserException("invalid " + siteName + ": comma expression");
            }
            Node inner = stripExprWrappers(body);
            if (inner == null) {
                throw new ParserException("invalid " + siteName);
            }
            if (inner.type == NodeType.LIT_EXPR && inner.size() >= 1) {
                NodeType lit = inner.getFirst().type;
                if (lit == NodeType.LIT_ARRAY || lit == NodeType.LIT_OBJECT) {
                    throw new ParserException("invalid " + siteName + ": parenthesized destructuring pattern");
                }
            }
            n = inner;
        }
        switch (n.type) {
            case REF_EXPR -> {

View on GitHub (pinned to a22eb90246)