Skip to content

TS/JS: methods inside a module-level object-literal export get no graph node at all (repository pattern) #2110

Open

Description

@nn-tmg

What's broken

const repo = { findX(...) {...}, y: async () => {...} } at module scope produces exactly ONE graph node — for repo itself — and nothing for its methods. This is distinct from #2023 / #1985, which are about edge resolution to nodes that already exist; here the target node never exists in the first place, so there's nothing to resolve to.

Every call into or out of such a method is invisible to the graph: affected findX, explain findX, and the call graph itself all silently drop this whole pattern.

This hits the very common "repository object" pattern in TypeScript codebases (export const fooRepo = { findById() {...}, create() {...}, ... }), used instead of a class when there's no instance state.

Root cause

In graphify/extractors/engine.py, the module-level-const branch of _js_extra_walk (handling lexical_declaration with a value type of object/array/as_expression/call_expression/new_expression) creates a single node for the const and then unconditionally returns True. That return short-circuits the caller's default recursive descent into the const's children, so the object literal's own pair/method_definition children — the actual methods — are never visited by any code path.

Compare this to the already-correct handling just above it in the same function, for class fields with function values (class C { handler = () => {} }), which walks each member and gives it its own node + a method edge back to the class.

Repro

export const repo = {
  findX(a: string) {
    return a;
  },
  arrowY: async (a: string) => {
    return a;
  },
};

Extracting this file produces a node for repo, but no node for findX or arrowY — even though free-standing function findX() {} at module scope, or the same method inside a class, both get correctly extracted.

Verified against a real production repository-pattern file (28 methods) — all 28 are silently dropped as graph nodes; only the container const shows up.

Fix

I have a working patch + a new test file (tests/test_js_object_literal_repository_methods.py, 7 tests) that:

  • walks the object literal's direct children when the module-level const's value is type == "object"
  • for method_definition children (shorthand methods: findX(...) {...}) and pair children whose value is a function/arrow-function (key: () => {...}), creates a member node scoped under the const's node id, with a method edge from the const — mirroring the existing class-field pattern
  • wires the new nodes into callable_def_nids / local_bound_names / function_bodies the same way every other callable-node path does, so downstream call resolution, indirect-dispatch guards, and body-walking all treat these consistently with any other method

Full JS/TS test suite (675 tests) plus the full suite (3344 tests, excluding pre-existing environment-only gaps unrelated to this — missing openai/tree_sitter_hcl optional deps) pass clean with the patch applied. PR incoming.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions