{"record":{"id":"e5f973815f6a524f","repo":"karatelabs/karate","slug":"constructor-weakset-requires-new","errorCode":null,"errorMessage":"Constructor WeakSet requires 'new'","messagePattern":"Constructor WeakSet requires 'new'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsWeakSetConstructor.java","lineNumber":44,"sourceCode":"/**\n * Global {@code WeakSet} constructor — mirrors {@link JsSetConstructor}: no\n * {@code new} is a TypeError, and construction from an iterable invokes\n * {@code this.add(v)} through the prototype chain so a user-overridden\n * {@code WeakSet.prototype.add} is honored.\n */\nclass JsWeakSetConstructor extends JsFunction {\n\n    JsWeakSetConstructor() {\n        this.name = \"WeakSet\";\n        defineOwn(\"prototype\", JsWeakSetPrototype.INSTANCE, PropertySlot.INTRINSIC);\n    }\n\n    @Override\n    public Object call(Context context, Object[] args) {\n        CallInfo callInfo = context.getCallInfo();\n        boolean isNew = callInfo != null && callInfo.constructor;\n        if (!isNew) {\n            throw JsErrorException.typeError(\"Constructor WeakSet requires 'new'\");\n        }\n        JsWeakSet set = new JsWeakSet();\n        if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) {\n            return set;\n        }\n        Object addFn = set.getMember(\"add\");\n        if (!(addFn instanceof JsCallable adder)) {\n            throw JsErrorException.typeError(\"WeakSet.prototype.add is not callable\");\n        }\n        JsIterator iter = IterUtils.getIterator(args[0], context);\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        Object savedThis = cc != null ? cc.thisObject : null;\n        try {\n            while (iter.hasNext()) {\n                Object v = iter.next();\n                if (cc != null) cc.thisObject = set;\n                adder.call(context, new Object[]{v});\n                if (cc != null && cc.isError()) {","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsWeakSetConstructor.java#L26-L62","documentation":"WeakSet is constructor-only: calling it without new is a TypeError per spec. Karate's JsWeakSetConstructor.call reads CallInfo.constructor and throws 'Constructor WeakSet requires new' when the call was not a construction.","triggerScenarios":"Evaluating `const ws = WeakSet()` in Karate JS; `const S = WeakSet; S()`; reflection-driven or generated code that omits the new keyword.","commonSituations":"Refactoring that lost `new`; copying call-style from factory functions; dynamic constructor invocation via variable indirection.","solutions":["Always use new WeakSet().","Wrap for dual-style use: function mkWeakSet(){ return new WeakSet(); }.","Grep scripts for bare `WeakSet(` calls and fix them.","If you need a factory, return new WeakSet() from a named helper."],"exampleFix":"// before\nconst ws = WeakSet();\n\n// after\nconst ws = new WeakSet();","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var ws = WeakSet(); } catch (e) { if (String(e).indexOf('requires') !== -1) ws = new WeakSet(); }","preventionTips":["Always write `new WeakSet()`","Do not invoke constructors through bare aliases","Lint for missing `new` before WeakSet"],"tags":["javascript","weakset","constructor","typeerror"],"backgroundTag":"invalid-argument-value","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"}