{"record":{"id":"761c339445f9b15a","repo":"karatelabs/karate","slug":"invalid-destructuring-rest-element-cannot-have-an","errorCode":null,"errorMessage":"invalid destructuring: rest element cannot have an initializer","messagePattern":"invalid destructuring: rest element cannot have an initializer","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsParser.java","lineNumber":1539,"sourceCode":"     * a spread {@code [...arr, b]} in a regular array literal stays valid.\n     */\n    private static void validateRestElementRules(Node litArray) {\n        int lastArrayElemIdx = -1;\n        for (int i = 0, n = litArray.size(); i < n; i++) {\n            Node ch = litArray.get(i);\n            if (!ch.isToken() && ch.type == NodeType.ARRAY_ELEM) {\n                lastArrayElemIdx = i;\n            }\n        }\n        for (int i = 0, n = litArray.size(); i < n; i++) {\n            Node ch = litArray.get(i);\n            if (ch.isToken() || ch.type != NodeType.ARRAY_ELEM) continue;\n            if (!hasRestPrefix(ch)) continue;\n            if (i != lastArrayElemIdx) {\n                throw new ParserException(\"invalid destructuring: rest element must be the last binding element\");\n            }\n            if (restHasInitializer(ch)) {\n                throw new ParserException(\"invalid destructuring: rest element cannot have an initializer\");\n            }\n        }\n    }\n\n    private static boolean hasRestPrefix(Node arrayElem) {\n        if (arrayElem.size() == 0) return false;\n        Node first = arrayElem.getFirst();\n        return first.isToken() && first.token.type == DOT_DOT_DOT;\n    }\n\n    /**\n     * For an ARRAY_ELEM that starts with {@code ...}, returns true iff the target\n     * expression after the DOT_DOT_DOT carries a top-level assignment — i.e. the\n     * rest target is being given a default value, which is invalid.\n     */\n    private static boolean restHasInitializer(Node arrayElem) {\n        boolean sawDot = false;\n        for (int i = 0, n = arrayElem.size(); i < n; i++) {","sourceCodeStart":1521,"sourceCodeEnd":1557,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L1521-L1557","documentation":"The Karate JS parser rejects a rest element in array destructuring that carries a default value, e.g. `[...rest = []] = arr`. The spec forbids initializers on rest elements because the rest binding always receives an array (possibly empty) and can never be undefined. Detected statically via restHasInitializer on the last ARRAY_ELEM.","triggerScenarios":"Parsing patterns like `[a, ...rest = []] = arr;` — hasRestPrefix identifies the rest element and restHasInitializer finds an `=` default after it.","commonSituations":"Copy-paste where a normal element's default was left on the rest element; defensive coding habits of adding defaults everywhere applied to `...rest` where it is meaningless.","solutions":["Remove the `= default` from the rest element: `[a, ...rest]`","Assign the fallback after destructuring: `rest = rest || []` (though rest is always an array)","If a default is genuinely needed, destructure into a plain binding instead of a rest element"],"exampleFix":"// before\nconst [a, ...rest = []] = arr;\n// after\nconst [a, ...rest] = arr;","handlingStrategy":"validation","validationCode":"// flag rest elements with initializers\nif (/\\.\\.\\.\\s*[A-Za-z_$][\\w$]*\\s*=/.test(src)) {\n  throw new Error('script has a rest element with an initializer');\n}","typeGuard":null,"tryCatchPattern":"try {\n  runScript(src);\n} catch (e) {\n  if (String(e).includes('rest element cannot have an initializer')) {\n    // strip the = default from the rest element\n  }\n  throw e;\n}","preventionTips":["Never write defaults on ...rest elements","Remember rest is always an array, never undefined","Lint destructuring patterns before embedding them in scripts"],"tags":["javascript","parser","destructuring","syntax"],"backgroundTag":"invalid-argument-format","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}