{"record":{"id":"a8e8497d6282205f","repo":"karatelabs/karate","slug":"constructor-set-requires-new","errorCode":null,"errorMessage":"Constructor Set requires 'new'","messagePattern":"Constructor Set requires 'new'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsSetConstructor.java","lineNumber":48,"sourceCode":" * {@code Set.prototype.add} is honored.\n */\nclass JsSetConstructor extends JsFunction {\n    JsSetConstructor() {\n        this.name = \"Set\";\n        // length=0 — Set() takes optional iterable; spec arity is 0.\n        installIntrinsics();\n    }\n\n    private void installIntrinsics() {\n        defineOwn(\"prototype\", JsSetPrototype.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 Set requires 'new'\");\n        }\n        JsSet set = new JsSet();\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(\"Set.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":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsSetConstructor.java#L30-L66","documentation":"The Set constructor was invoked as a plain function without `new`. The engine implements Set as a constructor-only callable, so a missing `new` is a TypeError rather than silently returning a set (spec-compliant engines may allow it, this one deliberately does not).","triggerScenarios":"Calling Set([1,2,3]) directly instead of new Set([1,2,3]); destructuring Set from a namespace and calling it; using Set as a callback, e.g. arr.map(Set).","commonSituations":"Refactors that dropped `new`; code written for engines/DSLs where Set is callable without new; minified or transpiled output that lost the constructor call.","solutions":["Add `new`: new Set(iterable).","Wrap in a factory if you need call-style construction: const makeSet = (it) => new Set(it).","Do not pass Set directly as a callback; use an arrow wrapper: arr.map(x => new Set(x))."],"exampleFix":"// before\nconst s = Set([1, 2, 3]);\n// after\nconst s = new Set([1, 2, 3]);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isSet(v) { return v instanceof Set; }","tryCatchPattern":"try { return Set(items); } catch (e) { if (String(e).includes(\"requires 'new'\")) return new Set(items); throw e; }","preventionTips":["Always use new Set(...).","Never pass Set as a bare callback; wrap in an arrow function.","Grep codebase for `= Set(` patterns after refactors."],"tags":["javascript","set","constructor"],"backgroundTag":"invalid-constructor-argument","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"}