fix(kotlin-extractor): collect members declared in enum class bodies#446
fix(kotlin-extractor): collect members declared in enum class bodies#446tirth8205 wants to merge 2 commits into
Conversation
An `enum class` body is parsed by tree-sitter as an `enum_class_body` node, not `class_body`. extractClassDeclaration only looked up `class_body`, which returns null for an enum class, so collectClassBody was never invoked. Every method/property declared inside an enum class body was silently dropped from the class's methods[], the top-level functions[], and exports[]. Fix: look up either body variant before collecting members (`findChild(declNode, "class_body") ?? findChild(declNode, "enum_class_body")`). collectClassBody already only reacts to function_declaration / property_declaration / object_declaration children, so the enum_entry children are harmlessly ignored. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
thejesh23
left a comment
There was a problem hiding this comment.
1. Enum constants themselves are not surfaced as members.
collectClassBody only reacts to function_declaration / property_declaration / object_declaration, so enum_entry children (NORTH, SOUTH) are silently dropped. For a fix targeted at enum bodies, omitting the entries themselves is the larger gap — they're the canonical "members" of an enum and would be the natural thing to land in properties[] (parallels the val/var primary-constructor handling). My in-flight Dart PR #435 will hit the same modeling question for enum_constant_declaration.
2. Companion object inside an enum body is not exercised.
object_declaration is handled, but companion_object is a distinct node type in tree-sitter-kotlin and won't match. An enum class with companion object { fun create(): Direction = NORTH } likely still loses create. Worth a test, and a separate findChild(body, "companion_object") branch if confirmed.
3. Test gap: properties and export are not asserted.
The new test only checks classes[0].methods and functions. It does not assert result.exports contains opposite (the default-public path through isExported is the actual regression surface), nor that the enum entries appear anywhere. Consider adding both, plus a private fun case to lock down visibility.
Nit: the comment "enum class body is parsed as enum_class_body" is the load-bearing fact — worth linking the tree-sitter-kotlin grammar node name in the comment so the next maintainer doesn't have to re-derive it.
…odies collectClassBody now handles `companion_object` (its own tree-sitter-kotlin node, not a subtype of object_declaration) by recursing into its class_body so companion functions/properties surface in methods[]/functions[]/exports[], and surfaces `enum_entry` constants as exported properties. Also names the grammar source in the enum_class_body comment. Adds tests for companion methods (class + enum bodies), enum-constant properties/exports, body-level default-public exports, and the private-fun negative export path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
All four points addressed in 78e5da4. Node shapes were confirmed against the real tree-sitter-kotlin parse tree before implementing. 1. Enum constants now surfaced. 2. Companion object handled. Confirmed 3. Export/visibility gap closed. The enum test now asserts Nit. The Full core suite green (697/697), |
Problem
An
enum classbody is parsed by tree-sitter as anenum_class_bodynode, notclass_body.extractClassDeclarationinkotlin-extractor.tsonly looked upfindChild(declNode, "class_body"), which returnsnullfor an enum class, socollectClassBodywas never invoked.As a result, every method and property declared inside an enum class body was silently dropped:
methods[]functions[]exports[]For example,
enum class Direction { NORTH, SOUTH; fun opposite(): Direction = NORTH }yieldedclasses: [{ name: "Direction", methods: [], properties: [] }]andfunctions: []— theoppositemethod vanished entirely. (Enum primary-constructorval/varproperties still worked, since those come fromprimary_constructor, not the body.)Fix
Look up either body variant before collecting members:
collectClassBodyalready only reacts tofunction_declaration/property_declaration/object_declarationchildren, so theenum_entrychildren of an enum body are harmlessly ignored — no other change is needed.Testing
collects methods declared inside an enum class bodytokotlin-extractor.test.ts. It fails before the fix (expected [] to include 'opposite') and passes after.tsc --noEmit) exits 0 and ESLint on both changed files exits 0.🤖 Generated with Claude Code