karatelabs/karate · error · ParserException

invalid : comma expression

Error message

invalid ${siteName}: comma expression

What it means

Karate's parser rejects a parenthesized comma expression used where a destructuring pattern or assignment target is required, e.g. `(a, b) = arr`. Parentheses do not turn a comma-separated list into a valid target; only a single expression may appear. The parser detects an EXPR_LIST with more than one element inside the PAREN_EXPR and fails with the site name included.

Solutions

  1. Remove the parentheses and use plain array destructuring: `[a, b] = [1, 2]`
  2. Split into separate assignment statements, one per variable
  3. Remove the comma if only one of the targets was intended

Example fix

// before
(a, b) = pair; // comma expression not a target
// after
[a, b] = pair;
Defensive patterns

Strategy: validation

Validate before calling

// detect comma-separated parenthesized assignment targets
if (/^\(\s*[A-Za-z_$][\w$]*\s*,\s*[A-Za-z_$][\w$]*\s*\)\s*=[^=]/.test(src.trim())) {
  throw new Error('parenthesized comma expression is not a valid assignment target');
}

Try / catch

try {
  runScript(src);
} catch (e) {
  if (String(e).includes('comma expression')) {
    // convert to array destructuring
  }
  throw e;
}

Prevention

When it happens

Trigger: Parsing `(a, b) = [1, 2];` or similar where the parenthesized body's EXPR_LIST has size != 1 — the check fires before the target is refined further.

Common situations: Expecting parentheses to enable tuple-style assignment like in Python; converting array-destructuring code by wrapping it in parens; hand-written code confusing sequence expressions with destructuring.

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/7c7cc8bbd75e02a5. Report an issue: GitHub.

Appendix: source

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

        }
        // 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 -> {
                // REF_EXPR is single-arg arrow `x => ...` when its first child is an
                // FN_ARROW_EXPR rather than the IDENT token; that form is invalid.
                if (n.size() >= 1 && !n.getFirst().isToken()

View on GitHub (pinned to a22eb90246)