{"record":{"id":"ad7390de5925f5b1","repo":"karatelabs/karate","slug":"super-keyword-is-only-valid-inside-a-class","errorCode":null,"errorMessage":"'super' keyword is only valid inside a class","messagePattern":"'super' keyword is only valid inside a class","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":1344,"sourceCode":"            // with them, so a callee ran on the strength of an argument expression that had\n            // already failed. The throw did surface afterwards, which is what made this invisible:\n            // the exception was right and the side effects along the way were not.\n            if (context.isError()) {\n                break;\n            }\n        }\n        if (spill != null) {\n            return spill.toArray();\n        }\n        return n == args.length ? args : java.util.Arrays.copyOf(args, n);\n    }\n\n    // `super(...)` in a derived constructor — runs the parent constructor against\n    // the instance currently under construction (context.thisObject).\n    private static Object evalSuperCall(Node fnArgsNode, CoreContext context) {\n        JsFunctionNode active = context.activeFunction;\n        if (active == null) {\n            throw JsErrorException.syntaxError(\"'super' keyword is only valid inside a class\");\n        }\n        Object[] args = evalCallArgs(fnArgsNode, context);\n        if (context.isError()) {\n            return Terms.UNDEFINED;\n        }\n        runSuperConstructor(active, context.thisObject, args, context);\n        // A derived class's own instance fields init right after super() returns.\n        runInstanceFieldInitializers(active, context.thisObject, context);\n        return Terms.UNDEFINED; // the value of a super() call is unused as a statement\n    }\n\n    // The built-in constructors whose instances carry internal slots a plain\n    // JsObject lacks, and whose state the super() shim can initialize in\n    // place. `extends` on one of these makes `new` allocate the matching\n    // exotic type (see allocateInstance). Error is deliberately absent — the\n    // enumerable-prop copy-shim (message + stack) already covers it on a\n    // plain JsObject. RegExp is absent because its compiled state is\n    // immutable — `class X extends RegExp` stays on the copy-shim (exec/test","sourceCodeStart":1326,"sourceCodeEnd":1362,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L1326-L1362","documentation":"Thrown as a syntax error when `super(...)` is evaluated outside any active class function. `evalSuperCall` requires context.activeFunction (a JsFunctionNode from a class); if there is no active function, `super()` cannot have a parent constructor to run.","triggerScenarios":"Calling `super(...)` in a top-level script, inside a plain (non-class) function, inside an arrow function at script top level, or in a feature-file JS block not nested in a derived class constructor.","commonSituations":"Copying a derived-class constructor body out of its class wrapper into utility code; calling a class method containing `super()` as a standalone function.","solutions":["Ensure `super(...)` only appears in the constructor of a class with an `extends` clause.","Remove `super()` from extracted/refactored code that is no longer inside a derived class.","Replace `super(...)` calls in plain functions with an explicit `ParentClass.call(this, ...)`-style construction."],"exampleFix":"// before\nfunction makeChild() { super(1); }\n// after\nclass Child extends Parent {\n  constructor() { super(1); }\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { runSnippet(); } catch (e) { if (String(e).includes(\"'super' keyword\")) { /* snippet needs class context */ } }","preventionTips":["Keep `super(...)` only in derived class constructors","When extracting code, strip or replace super() calls","Always pair class constructors with an `extends` clause when using super()"],"tags":["javascript","super","syntax-error"],"backgroundTag":"invalid-super-usage","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}