Skip to content

fix(js): a constructor's prototype and static functions are its methods - #3380

Merged
colbymchenry merged 1 commit into
mainfrom
claude/issue-2966-prototype-methods
Nov 18, 2026
Merged

colbymchenry merged 1 commit into
mainfrom
claude/issue-2966-prototype-methods

Conversation

@colbymchenry

@colbymchenry colbymchenry commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

Methods written the classic JavaScript way get nodes now, in both extraction engines:

function Hooks () {}
Hooks.prototype.validate = function (hook, fn) {…}   // method Hooks::validate
Hooks.create = function () {…}                       // static method Hooks::create
module.exports = { inspect () {…}, toJSON () {…} }   // module.exports::inspect, ::toJSON (exported)

and this.validate() inside them reaches Hooks::validate, not an unrelated validate in another file or an interface method in a .d.ts. Model.init() from a file that requires the constructor reaches the static Model::init, and require('./context').toJSON() (or a destructured inspect) reaches the module object's member.

Fixes #2966

Root cause

objectOwnerPath in src/extraction/tree-sitter.ts (and object_owner_path in codegraph-kernel/src/tsjs/extractors.rs) deliberately skip prototype, module and exports paths, and extractFunction names an anonymous function expression only from a variable_declarator or a CommonJS exports.x = target. So Ctor.prototype.m = function () {} had no node (its calls were credited to the file), Ctor.m = function name () {} was a free function name, and module.exports = { m () {} } members had none. With no member to find, this.m() resolved by bare name across the project.

Fix

Extraction (wasm extractor and kernel, mirrored line for line):

Resolution:

  • matchEnclosingScopeMember (name-matcher.ts): a constructor function that owns methods, or an object literal's owner, is its members' this scope, like a class: this.m() in a member, or in the constructor itself, means the owner's own m.
  • resolveStaticMember (import-resolver.ts): Ctor.m() through an import of a constructor function reaches its static method Ctor::m (only method nodes marked static; what a function nests stays out).
  • The lone-name guess on an untyped receiver (matchMethodCallOn) takes a constructor function's method from another file only when the receiver names the constructor (reply.send() → Reply::send), and never takes a test file's own constructor from another file. Without this, an example's User.all = function became every app.all()'s target in express (24 edges), and a test's local Instance.prototype.test every t.test()'s in fastify (258).

Naming choices (flagged for review)

Validation

Tests.

A/B (main 96abf09 vs this branch, both on their own kernel builds, full graph dumped by natural keys, every bucket sampled against the source; the wasm engine gives the same graphs):

repo (commit) nodes main → branch what changed (every bucket sampled against the source)
fastify (825d772) 3032 → 3071 (+39 methods, +3 module members) 1451 calls added, 25/25 sampled right: tests' reply.send() / reply.code() / reply.header() reach Reply's own methods; 57 retargeted, 20/20 right (this.getHeader(), reply.serializeInput() leave types/reply.d.ts, reply.type() leaves ContentType::type); 30 calls now credited to their method; 47 removed: 37 fastify.getSchema()/getSchemas() lone guesses onto SchemaController that a new Schemas::getSchema made ambiguous (the receiver is a bound copy), 10 wrong (a local serialize call, a done callback parameter).
express (1c6f1b0) 1143 → 1152 10 retargeted, all right: User.all() / User.count() reach the example's statics instead of the User constructor, View's this.lookup() / this.resolve() its own methods, and a bare resolve() the module's path.resolve instead of View.prototype.resolve; 1 added, right (view.render()); 9 credited to their method.
koa (9dbf012) 415 → 503 (+86 module.exports members) 15 retargeted, all right: this.toJSON() in context/request/response stays in its own module object instead of Application::toJSON (the issue's third case); 96 added, 20/20 right (this.get(), this.set() in the same object, tests' sp.stringify()); 20 credited to their member.
mongoose (7f0f6ff) 8133 → 8493 (+662 methods, +38 members) 3701 retargeted, 30 sampled: 25 right (calls leave types/*.d.ts declarations for the implementation, SchemaType._isRef() reaches its static instead of the constructor, this.set() recursion inside Query::set), 3 one guess for another, 2 sampling artifacts; 1705 added, 29/30 right (one db.model() onto Mongoose::model where db is a Connection); 497 removed, 29/30 were wrong lone guesses (db.close() → ChangeStream::close, doc.toObject() → MongooseMap::toObject); 1318 credited to their method.
node-postgres (2a5850f) 2126 → 2133 31 added, right in the sample (this._types.setTypeParser() → TypeOverrides, utils.prepareValue()); 4 right lone guesses (client.setTypeParser()) lost to the new TypeOverrides::setTypeParser namesake.
gin (Go), flask (Python) byte-identical

Two resolver guards came out of this triage: without them an example's User.all = function became every app.all()'s target in express (24 wrong edges), a test's local Instance.prototype.test every t.test()'s in fastify (258), and a test's Server.prototype.close every other test's server.close() in node-postgres (10). The emitter pass on main still looks handlers up by name, so 4 of its edges shift in koa/mongoose; PR #3362 replaces that lookup.

Re-run on adad315 (the branch now sits on d2f3f07, whose later commits touch PHP, Java, Kotlin, Laravel, Rust and Python only; adad315 had gained the JS/TS resolver fixes #3345, #3352, #3353, #3371 and #3372, plus #3373 and #3332): express, koa, fastify and node-postgres change exactly the same edges as against 96abf09, and gin and flask stay byte-identical. In mongoose 9,482 of 9,525 removed and 10,542 of 10,582 added lines are the same, and the rest follow main's own changes:

  • Since fix(js,ts): this.m() and a local that shadows an import no longer link to the import #3345 a this. call skips the import resolver, so on main this.cast(), this.clone() or this.discriminator() in a prototype method now links to the file's const cast = require(…) constant. This branch links each to the constructor's own method (Query::cast, SchemaArray::cast, Model::discriminator), 6 edges.
  • 9 calls on a document or on the mongoose instance (document.$__delta(), mongoose.createConnection()) reach methods main has no node for.
  • 3 calls in the type tests reach the implementation instead of a .d.ts declaration.
  • 2 this.get(p) calls in a virtual getter go to Schema::get instead of the get constant. Both targets are wrong: this is a document there.

Cost, process CPU time (worker threads included), median of interleaved rounds: fastify 15.08 s → 15.48 s (5 rounds; the branch writes 18% more edges there), mongoose 31.39 s → 30.87 s (3 rounds). Wall time moved within noise on both.

Areas touched

src/extraction/tree-sitter.ts (extractFunction hook, two new methods, createNode's declaration map, objectOwnerPath, owned-member export flag, the body walker's function branch), codegraph-kernel/src/tsjs/{extractors,mod}.rs (the same), src/resolution/name-matcher.ts (matchEnclosingScopeMember's scope walk; two filters in matchMethodCallOn; a helper beside sharesReceiverWord), src/resolution/import-resolver.ts (resolveStaticMember only). Nothing in resolveRelativeImport, localBindingPatterns/jsBindingSites, isImportedModuleReceiver, resolution/index.ts or the parse path. Rebased onto bbaf718, which carries the JS/TS resolver fixes #3345, #3352, #3353, #3371 and #3372 and the sync pass #3373: no textual overlap. resolveStaticMember answers before #3345's new rule for a member of an imported function (a static method is that member), and constructor methods, being method nodes, stay out of #3352's bare-call candidates.

Left out

No contributor PR covers this issue.

🤖 Generated with Claude Code

@colbymchenry
colbymchenry force-pushed the claude/issue-2966-prototype-methods branch from cd7659c to 1aca81c Compare November 18, 2026 06:24
@colbymchenry
colbymchenry force-pushed the claude/issue-2966-prototype-methods branch 2 times, most recently from 7837d33 to 0d0a47f Compare November 18, 2026 08:24
Methods written the classic JavaScript way had no node. In
`Hooks.prototype.validate = function () {}`, `Model.insertMany = async
function () {}` and `module.exports = { inspect () {} }` the function was
anonymous to the extractor, or a free function named by its expression, so
the calls inside were credited to the file, and `this.validate()` between
them linked by bare name to any `validate` in the project: a function in
another file, an interface method in a `.d.ts`, another class's method.
Both engines skip these shapes (objectOwnerPath leaves out prototypes and
`module.exports`; the kernel's object_owner_path mirrors it).

Extraction, in the wasm extractor and the kernel alike:
- A function assigned to `Ctor.prototype.m` or `Ctor.m`, `Ctor` a function
  or class declared in the scope the assignment is written in (module
  scope, a wrapper IIFE, a factory), is method `m` of `Ctor`: contained by
  it and qualified `Ctor::m` as a class's method is, static for `Ctor.m`,
  named by the property callers use. Each scope's declarations are kept in
  a map as they are created; a constructor declared later, an imported one,
  a computed key, `Ctor.prototype = {...}` and minified bundles keep their
  old shape.
- `module.exports = {...}` owns its function members the way `window.App =
  {...}` does (#2300): `module.exports::m`, marked exported like an
  `exports.x = function` (#1675).
Resolution:
- `this.m()` in a member of a constructor function or of an object
  literal's owner means the owner's own `m`, as in a class.
- `Ctor.m()` through an import of a constructor reaches its static method.
- The lone-name guess on an untyped receiver takes a constructor
  function's method from another file only when the receiver names the
  constructor, and never takes a test file's own constructor from another
  file: an example's `User.all` is no `app.all()`, a test's
  `Instance.prototype.test` no `t.test()`.

Validation, main (96abf09) vs this branch:
- New test file (5 cases, each on both engines): the issue's three shapes
  and every call in its table, every function shape, constructors in a
  wrapper and a factory, the shapes left alone, statics and module-object
  members reached from another file, and `this.m()` in a constructor's
  method staying on its own `m` where the file also imports an `m`. Red
  on main's sources (4 of 5 in each engine), green now on both engines.
- Kernel parity: a new parity case (TS/TSX/JS/JSX, LF and CRLF);
  scripts/kernel-parity.mjs: fastify 303/304 files (one with a parse error
  defers to wasm by design), express 142/142, koa 84/84, mongoose 554/554.
  The branch's graphs are byte-identical across the two engines on all
  five A/B repos.
- Updated tests: the object-literal tests now expect `module.exports::cjs`;
  the lone-guess test (issue 3171) uses a constructor its file doesn't
  declare, since a declared one's method is a node now.
- A/B, every bucket sampled against the source (nodes main -> branch):
  fastify 3032 -> 3071: 1451 calls added, 25/25 sampled right (tests'
  `reply.send()` etc. reach Reply's own methods); 57 retargeted, 20/20
  right (`this.getHeader()` leaves `types/reply.d.ts`); 47 removed, 37 of
  them `fastify.getSchema()` guesses a new `Schemas::getSchema` made
  ambiguous, 10 wrong. express 1143 -> 1152: 10 retargeted, all right
  (`User.all()` reaches its static, `View` its methods), 1 added, right.
  koa 415 -> 503: 15 retargeted, all right (`this.toJSON()` no longer
  reaches `Application::toJSON`), 96 added, 20/20 right. mongoose 8133 ->
  8493: 3701 retargeted, 25/30 right and 3 a guess for a guess; 1705
  added, 29/30 right; 497 removed, 29/30 wrong lone guesses (`db.close()`
  -> `ChangeStream::close`). node-postgres 2126 -> 2133: 31 added, right
  in the sample; 4 right guesses lost to a new namesake.
- Controls gin and flask: byte-identical. Index CPU time, median of
  interleaved rounds: fastify 15.08 s -> 15.48 s (5 rounds; the branch
  writes 18% more edges there), mongoose 31.39 s -> 30.87 s (3 rounds).
- Re-run against adad315, which adds the JS/TS resolver fixes of PRs
  3345, 3352, 3353, 3371 and 3372 (the branch now sits on d2f3f07, whose
  later commits touch PHP, Java, Kotlin, Laravel, Rust and Python only):
  express, koa,
  fastify and node-postgres change exactly the same edges as against
  96abf09. In
  mongoose 9,482 of 9,525 removed and 10,542 of 10,582 added lines are the
  same; the rest follow main's own changes. Main now links a `this.cast()`
  in a prototype method to the file's `const cast = require(...)`
  constant, since a `this.` call skips the import resolver there, and this
  branch links it to the constructor's own method (6 edges). 9 calls on a
  document or the mongoose instance reach methods main has no node for,
  and 3 test calls reach the implementation instead of a .d.ts
  declaration. 2 `this.get(p)` calls in a virtual getter go to
  `Schema::get` instead of the `get` constant: both wrong, as `this` is
  a document there. gin and flask: byte-identical.

Fixes #2966

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@colbymchenry
colbymchenry force-pushed the claude/issue-2966-prototype-methods branch from 0d0a47f to b47c4b3 Compare November 18, 2026 08:26
@colbymchenry
colbymchenry merged commit bb0f254 into main Nov 18, 2026
@colbymchenry
colbymchenry deleted the claude/issue-2966-prototype-methods branch November 18, 2026 08:26
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Nov 18, 2026
Upstream landed our colbymchenry#3330 (b286279), colbymchenry#3278 (df20853) and colbymchenry#3249, plus its own
fixes for issues the fork carried: Python typing.overload (colbymchenry#3349, colbymchenry#3216's
approach), Java wildcard visibility (colbymchenry#3347), colbymchenry#3272 (colbymchenry#3364), colbymchenry#2951/colbymchenry#2953.

Conflicts:
- name-matcher.ts: upstream's side for Rust (colbymchenry#3381-colbymchenry#3384) and Java (colbymchenry#3347).
  The fork keeps its module-receiver check (namespace-only defaults, a default
  that is a module's own function) and adds upstream's colbymchenry#2951 clause and
  isImportedFunctionReceiver guard. Upstream's colbymchenry#3272 block (a module default
  made with `new`) runs first, but defers when the module exports the name or
  its exports can't be read for certain, so the fork's alias edges stay.
  A parameter or local shadowing the binding is the receiver (colbymchenry#2953).
- import-resolver.ts: upstream's member-of-imported-function block, then the
  fork's guard for the remaining cases (default imports), excluding
  call/apply/bind.
- python-overloads.ts, index.ts: upstream's (duplicate memo clear dropped).
- module-importers.ts, workspace-packages.ts: both imports.
- CHANGELOG: upstream's [Unreleased] plus the fork's PHP line (colbymchenry#3327).

Tests: member-call-not-receiver's static-property case now expects
User::all (upstream colbymchenry#3380 indexes `User.all = function` as a static method).
parse-deadline's two wasm-hang cases are skipped on the fork: its patched
runtime (tree-sitter#5997) parses the input instead of hanging.

README: colbymchenry#3249, colbymchenry#3278, colbymchenry#3330, colbymchenry#3216 and colbymchenry#3138 moved to "now in upstream".

Checks: tsc 0; suite 11415 passed, 2 failed before the parse-deadline gate
(both from the patched runtime); fork-only and upstream colbymchenry#3272/colbymchenry#3380 tests pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JavaScript: Ctor.prototype.m = function () {} methods get no node; this.m() inside them links to unrelated namesakes

1 participant