Repository navigation
fix(js): a constructor's prototype and static functions are its methods - #3380
Merged
Merged
Conversation
colbymchenry
force-pushed
the
claude/issue-2966-prototype-methods
branch
from
November 18, 2026 06:24
cd7659c to
1aca81c
Compare
colbymchenry
force-pushed
the
claude/issue-2966-prototype-methods
branch
2 times, most recently
from
November 18, 2026 08:24
7837d33 to
0d0a47f
Compare
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
force-pushed
the
claude/issue-2966-prototype-methods
branch
from
November 18, 2026 08:26
0d0a47f to
b47c4b3
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Methods written the classic JavaScript way get nodes now, in both extraction engines:
and
this.validate()inside them reachesHooks::validate, not an unrelatedvalidatein another file or an interface method in a.d.ts.Model.init()from a file that requires the constructor reaches the staticModel::init, andrequire('./context').toJSON()(or a destructuredinspect) reaches the module object's member.Fixes #2966
Root cause
objectOwnerPathinsrc/extraction/tree-sitter.ts(andobject_owner_pathincodegraph-kernel/src/tsjs/extractors.rs) deliberately skipprototype,moduleandexportspaths, andextractFunctionnames an anonymous function expression only from avariable_declaratoror a CommonJSexports.x =target. SoCtor.prototype.m = function () {}had no node (its calls were credited to the file),Ctor.m = function name () {}was a free functionname, andmodule.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):
scopeDeclarations/scope_declarations).Ctor.prototype.morCtor.m(function,async, arrow, generator), whereCtoris a function or class declared in the scope the assignment is written in, is extracted as methodmofCtor(constructorMember/extractConstructorMember): contained byCtor, qualifiedCtor::m,isStaticforCtor.m, its JSDoc read from the statement, its body walked under it. The body walker does the same for assignments in a module wrapper or a factory.module.exports = {…}owns its function members likewindow.App = {…}(JS/TS: object-literal members get no node unless the container is anexport const— plain consts,window.X = {…}and IIFE-scoped objects are invisible #2300): ownermodule.exports, membersmodule.exports::m, marked exported likeexports.x = function(CommonJS exports.X = async () => {} route handlers aren't indexed as symbols #1675).window.X), computed keys,Ctor.prototype = {…}literals,app.use = function use ()on a plain object, generated or minified bundles.Resolution:
matchEnclosingScopeMember(name-matcher.ts): a constructor function that owns methods, or an object literal's owner, is its members'thisscope, like a class:this.m()in a member, or in the constructor itself, means the owner's ownm.resolveStaticMember(import-resolver.ts):Ctor.m()through an import of a constructor function reaches its static methodCtor::m(onlymethodnodes marked static; what a function nests stays out).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'sUser.all = functionbecame everyapp.all()'s target in express (24 edges), and a test's localInstance.prototype.testeveryt.test()'s in fastify (258).Naming choices (flagged for review)
methodnamed by the property (Model.from = function build ()isModel::from), qualified and contained like an ES class's method. The constructor itself stays afunctionnode; turning it into aclasswould change every constructor's kind incodegraph_explore, the viewer and the search, so it is left for a decision of its own.module.exports = {…}readmodule.exports::inspect, following JS/TS: object-literal members get no node unless the container is anexport const— plain consts,window.X = {…}and IIFE-scoped objects are invisible #2300's path-owner convention (window.App::init), with an owner node namedexports(qualifiedmodule.exports), rather than a name invented from the file.Validation
Tests.
__tests__/js-prototype-methods.test.ts, 5 cases × both engines: the issue's files and every call in its table, every function shape plus constructors in a wrapper IIFE and a factory, the shapes left alone, statics / module-object members reached from another file, andthis.validate()in a constructor's method staying on its ownvalidatewhere the file also imports one (with fix(js,ts): this.m() and a local that shadows an import no longer link to the import #3345 on main, athis.call no longer goes through the file's imports; a barevalidate()there is still the import's). The wasm half fails with main'stree-sitter.ts,name-matcher.tsandimport-resolver.ts(4 of 5; the fifth is the "left alone" guard) and passes now; the native half passes with the rebuilt kernel.__tests__/kernel-tsjs-parity.test.ts: a new parity case over TS/TSX/JS/JSX, LF and CRLF, and the JS/TS: object-literal members get no node unless the container is anexport const— plain consts,window.X = {…}and IIFE-scoped objects are invisible #2300 case now expectsmodule.exports::cjs.__tests__/js-object-literal-members.test.ts(JS/TS: object-literal members get no node unless the container is anexport const— plain consts,window.X = {…}and IIFE-scoped objects are invisible #2300):module.exports = { cjs () {} }now ownscjs.__tests__/js-prototype-member-guess.test.ts(fix(js): a file that gives an object a method itself takes no lone guess from elsewhere #3171): its bundle's constructor is one the file doesn't declare (var Sha256 = window.Sha256), the case that guard still covers; a declared one's method is a node now.scripts/kernel-parity.mjs(TS/JS): fastify 303/304 files at parity (one file 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. 89 JS-related and kernel test files (807 tests, the rebuilt kernel expected) pass on the commit. The full suite is in the READY note.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):
reply.send()/reply.code()/reply.header()reachReply's own methods; 57 retargeted, 20/20 right (this.getHeader(),reply.serializeInput()leavetypes/reply.d.ts,reply.type()leavesContentType::type); 30 calls now credited to their method; 47 removed: 37fastify.getSchema()/getSchemas()lone guesses ontoSchemaControllerthat a newSchemas::getSchemamade ambiguous (the receiver is a bound copy), 10 wrong (a localserializecall, adonecallback parameter).User.all()/User.count()reach the example's statics instead of theUserconstructor,View'sthis.lookup()/this.resolve()its own methods, and a bareresolve()the module'spath.resolveinstead ofView.prototype.resolve; 1 added, right (view.render()); 9 credited to their method.module.exportsmembers)this.toJSON()in context/request/response stays in its own module object instead ofApplication::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.types/*.d.tsdeclarations for the implementation,SchemaType._isRef()reaches its static instead of the constructor,this.set()recursion insideQuery::set), 3 one guess for another, 2 sampling artifacts; 1705 added, 29/30 right (onedb.model()ontoMongoose::modelwheredbis a Connection); 497 removed, 29/30 were wrong lone guesses (db.close()→ChangeStream::close,doc.toObject()→MongooseMap::toObject); 1318 credited to their method.this._types.setTypeParser()→TypeOverrides,utils.prepareValue()); 4 right lone guesses (client.setTypeParser()) lost to the newTypeOverrides::setTypeParsernamesake.Two resolver guards came out of this triage: without them an example's
User.all = functionbecame everyapp.all()'s target in express (24 wrong edges), a test's localInstance.prototype.testeveryt.test()'s in fastify (258), and a test'sServer.prototype.closeevery other test'sserver.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:
this.call skips the import resolver, so on mainthis.cast(),this.clone()orthis.discriminator()in a prototype method now links to the file'sconst cast = require(…)constant. This branch links each to the constructor's own method (Query::cast,SchemaArray::cast,Model::discriminator), 6 edges.document.$__delta(),mongoose.createConnection()) reach methods main has no node for..d.tsdeclaration.this.get(p)calls in a virtual getter go toSchema::getinstead of thegetconstant. Both targets are wrong:thisis 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 inmatchMethodCallOn; a helper besidesharesReceiverWord),src/resolution/import-resolver.ts(resolveStaticMemberonly). Nothing inresolveRelativeImport,localBindingPatterns/jsBindingSites,isImportedModuleReceiver,resolution/index.tsor 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.resolveStaticMemberanswers before #3345's new rule for a member of an imported function (a static method is that member), and constructor methods, beingmethodnodes, stay out of #3352's bare-call candidates.Left out
this.m()naming no member of its owner still falls back by name, as an ES class's does (this.missing()→ a freemissingelsewhere).Ctor.prototype = { m () {} }, a constructor declared after its prototype assignments, andutil.inherits/Object.createinheritance between constructors.thisonly in class methods; giving constructor methods the same identity is a follow-up once both land.No contributor PR covers this issue.
🤖 Generated with Claude Code