karatelabs/karate · error · ParserException

invalid : arrow function

Error message

invalid ${siteName}: arrow function

What it means

Karate's JS engine validates that the left-hand side of an assignment (or other assignment-target position) is a valid Reference. A single-argument arrow function like `x => ...` appearing in that position has no AssignmentTargetType, so the parser rejects it with this message naming the syntax site.

Solutions

  1. Fix the source so the left side of `=` is an identifier, member access, or destructuring pattern
  2. Replace `=>` with `=` if an assignment was intended
  3. Wrap the arrow function in a call or store it in a variable before using its result

Example fix

// before
x => 42 = y;
// after
let f = x => 42;
y = f(0);
Defensive patterns

Strategy: validation

Validate before calling

// validate before evaluating assignment
const lhs = expr.left;
if (lhs && lhs.type === 'ArrowFunctionExpression') {
  throw new Error('LHS of assignment cannot be an arrow function');
}

Type guard

function isArrowLhs(node) {
  return node != null && node.type === 'ArrowFunctionExpression';
}

Try / catch

try {
  karate.eval(code);
} catch (e) {
  if (String(e.message).includes('arrow function')) { /* fix syntax */ }
  throw e;
}

Prevention

When it happens

Trigger: Parsing code where an assignment/destructuring target position contains a REF_EXPR whose first child is an FN_ARROW_EXPR, e.g. `(x => x) = 1` or `(a, b = () => c) = [1, 2]`.

Common situations: Typos such as using `=>` instead of `=` in an assignment; accidentally swapping the sides of an arrow function and an assignment; generated or transpiled code with malformed output.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

            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()
                        && n.getFirst().type == NodeType.FN_ARROW_EXPR) {
                    throw new ParserException("invalid " + siteName + ": arrow function");
                }
                // `this` lexes as IDENT (see JsLexer.keywordOrIdent) but per spec
                // the ThisExpression has AssignmentTargetType=invalid.
                if (n.size() >= 1 && n.getFirst().isToken()
                        && n.getFirst().token.type == IDENT
                        && "this".equals(n.getFirst().getText())) {
                    throw new ParserException("invalid " + siteName + ": this");
                }
            }
            case REF_DOT_EXPR, REF_BRACKET_EXPR -> {
                // Plain member access; a `?.` would have been caught earlier by
                // the optional-chain branch above with a more specific message.
                // `import.meta` is a meta-property whose AssignmentTargetType is
                // invalid; `import` is not a reserved word in our lexer so it
                // parses as a normal REF_DOT_EXPR — flag the literal shape here.
                if (n.type == NodeType.REF_DOT_EXPR && isImportMeta(n)) {
                    throw new ParserException("invalid " + siteName + ": import.meta");
                }

View on GitHub (pinned to a22eb90246)