Skip to content

JS/TS AST: function symbols assigned via this.X=, exports.X=, prototype, class arrow fields, and function expressions are never extracted #1322

Closed

Description

@siddarthc

Summary

The JS/TS extractor captures top-level function declarations, top-level const x = () => … arrows, classes, and class-method shorthand — but silently drops several extremely common ways of defining a named function. On expression-style and CommonJS codebases this means the majority of callable symbols never become nodes (and so can never be call-edge endpoints).

Forms that are missed

Form Example Today
constructor-assigned method function UserDAO(db){ this.addUser = () => {} } ❌
this.x = function(){} inside any function body ❌
CommonJS export exports.foo = () => {} / module.exports.bar = fn ❌
prototype method Foo.prototype.baz = function(){} ❌
class arrow field class C { onClick = (e) => {} } ❌
const f = function(){} function expression (non-arrow) ❌

The first one is the big one: the constructor-function-with-this.method = () => {} pattern is pervasive in pre-class JS (DAOs, route handlers, services). For such a file the extractor emits only the outer function node and none of its methods.

Minimal repro

// probe.js
function declTop(a){ return a; }              // ✅ captured
const arrowTop = (b) => b;                     // ✅ captured
const fnExprTop = function(c){ return c; };    // ❌ missed
exports.exportedArrow = (d) => d;              // ❌ missed
module.exports.modArrow = (e) => e;            // ❌ missed
Foo.prototype.protoFn = function(){};          // ❌ missed
class Foo { methodInClass(f){ return f; } arrowField = (g) => g; }  // method ✅, field ❌
function Outer(db){ this.thisMethod = (h) => h; this.thisFnExpr = function(i){ return i; }; }  // ❌ both missed
from graphify.extract import extract_js
from pathlib import Path
print(sorted(n["label"] for n in extract_js(Path("probe.js"))["nodes"]))
# ['.methodInClass()', 'Foo', 'Outer()', 'arrowTop()', 'declTop()', 'probe.js']
# missing: fnExprTop, exportedArrow, modArrow, protoFn, arrowField, thisMethod, thisFnExpr

Root cause

  • _js_extra_walk only emits arrow declarations from module-level lexical_declaration, and only for arrow_function values (not function_expression).
  • assignment_expression (the this.X = / exports.X = / Foo.prototype.X = forms) is not handled at all.
  • Class field_definition / public_field_definition with a function value is not emitted as a method.
  • The function-emission branch appends the body to function_bodies for the call pass and returns, so this.X = … assignments inside the body are never visited by the node-emitting walk.

Proposed fix

Extend the JS/TS walk to emit these forms, scoped to avoid the phantom-god-node class (#1077):

  • exports.X / module.exports.X → file-contained function
  • Foo.prototype.X → method owned by Foo
  • this.X = fn in a function/constructor body → method owned by the enclosing class (if any) else the function
  • class arrow/function fields → method owned by the class
  • const f = function(){} → file-contained function

Arbitrary obj.x = fn (object not this/exports/module.exports/<Name>.prototype) is deliberately not captured, to preserve the #1077 guard.

PR with tests to follow.

Activity

  1. added a commit that references this issue on Aug 13, 2026
    15b95f5
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