{"record":{"id":"da33b7178e1c5f99","repo":"karatelabs/karate","slug":"private-name-name-is-not-declared-in-an-enclosing-class","errorCode":null,"errorMessage":"private name ${name} is not declared in an enclosing class","messagePattern":"private name (.+?) is not declared in an enclosing class","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsParser.java","lineNumber":315,"sourceCode":"                    }\n                    if (target.type == NodeType.PAREN_EXPR) {\n                        target = target.size() > 1 ? target.get(1) : null;\n                    } else if (target.type == NodeType.EXPR || target.type == NodeType.EXPR_LIST) {\n                        target = target.size() == 1 ? target.getFirst() : null;\n                    } else {\n                        break;\n                    }\n                }\n            }\n            default -> {\n            }\n        }\n        return privates;\n    }\n\n    private static void requirePrivateDeclared(String name, PrivateScope privates) {\n        if (privates == null || !privates.declares(name)) {\n            throw new ParserException(\"private name \" + name + \" is not declared in an enclosing class\");\n        }\n    }\n\n    /** Collects the {@code #name} keys declared directly by a class body. Each\n     *  CLASS_METHOD holds its key as a direct token child, so at most one\n     *  PRIVATE_NAME can appear per member (an initializer lives in a nested EXPR). */\n    private static PrivateScope classPrivateScope(Node classNode, PrivateScope parent) {\n        Set<String> names = null;\n        Map<String, Integer> kinds = null;\n        for (int i = 0, n = classNode.size(); i < n; i++) {\n            Node member = classNode.get(i);\n            if (member.type != NodeType.CLASS_METHOD) {\n                continue;\n            }\n            Node key = null;\n            int kind = 4; // 1 = get, 2 = set, 4 = anything else; 8 = static\n            for (int j = 0, m = member.size(); j < m; j++) {\n                Node c = member.get(j);","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L297-L333","documentation":"A private name may only be used inside the class (or nested class) whose body declares it. Karate tracks PrivateScope objects per class body during parsing; when a private name is referenced (or a member key resolves) but the enclosing scope chain has no class declaring that name, requirePrivateDeclared throws this early error.","triggerScenarios":"Referencing `#x` (in an `in` check, member access, or declaration-dependent position) when privates is null (not inside any class body) or the enclosing PrivateScope does not declare the name — e.g. using a private name of an outer class from unrelated code, or a typo in the private name.","commonSituations":"Typos in private names (`#lenght` vs `#length`), using private members outside the class body, copy-pasting methods between classes where the target class lacks the private declaration.","solutions":["Declare the private name in the enclosing class body (e.g. `class C { #x; ... }`)","Fix the spelling of the private name to match its declaration","Move the usage inside the class that declares the private name"],"exampleFix":"// before\nclass C { m() { return #x in this; } }\n// after\nclass C { #x; m() { return #x in this; } }","handlingStrategy":"validation","validationCode":"// Verify every referenced #name is declared in the class body:\nconst used = new Set(js.match(/#\\w+/g) || []);\nconst declared = new Set((js.match(/\\s#\\w+\\s*[=;(]/g) || []).map(s => s.trim().split(/\\s|\\b/)[0]));","typeGuard":null,"tryCatchPattern":"try { eval(js); } catch (e) { if (String(e).includes('is not declared in an enclosing class')) { /* declare the private name in the class */ } throw e; }","preventionTips":["Declare private fields at the top of the class body","Check spelling of private names against declarations","Keep private-member usage inside the declaring class"],"tags":["javascript","parser","private-fields","scope"],"backgroundTag":"invalid-private-field-usage","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"}