Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- In JavaScript and TypeScript, a default import now links to what the module's `export default` statement exports, wherever the statement sits in the file. Before, a module that declares its default, like `export default function Vans() {…}` or `export default class Store {…}`, had its default import linked to the first function it exports instead: in a React Router data-router app, where a page exports its `loader` or `action` above the page component, `import Vans, { loader as vansLoader } from './pages/Vans'` made the route `/vans` and every use of `Vans` lead to the loader. A component file that also exports a styled or memoized component no longer hands its default import to that component, and an anonymous default, like `export default function () {…}` or `export default () => …`, is no longer linked to a function exported beside it or nested inside it. A module loaded with `require('./x').default` is read the same way. Re-index JavaScript and TypeScript projects after upgrading.
- In React and Vue, a tag for a component the file imports from a package, like `<Button>` from antd, `<Typography>` from `@mui/material/Typography`, `<Link>` from react-router-dom or `<Card>` from ant-design-vue, no longer links to a component, function or class in your repository that only shares its name. Before, the tag linked to whatever the repository had by that name, so `codegraph_explore`, callers and impact followed renders that never happen: in SigNoz, antd's `<Button>` tags showed up as callers of a styled link on its 404 page, and in Material UI dashboards like mantis and berry, `<Typography>` and `<Button>` tags showed up as callers of the theme's style overrides. A package here is one that a `package.json` above the file lists and that no file in the repository answers to. A tag imported through a path alias like `components/Modal`, from a workspace package of the repository, or under a name no `package.json` lists still links as before. Re-index React and Vue projects after upgrading.
- In React with TypeScript, a component that renders a generic component and passes it type arguments, like outline's `<PaginatedList<Document> items={…} />` or excalidraw's `<DropdownMenuItemContentRadio<"contain" | "overlap"> …>`, now links to the component it renders. Before, a tag followed by type arguments was skipped, so the generic component looked unused by the screens that render it, and `codegraph_explore`'s call paths, callers and impact stopped short of it. Re-index React projects after upgrading.
- In TypeScript and JavaScript, a method called on a freshly constructed object, such as `new RegExp(p).exec(s)` or `new URL(u).toString()`, now links only to that class's own (or inherited) method and no longer to an unrelated project method of the same name. (#1945)

## [1.6.2] - 2026-10-03

Expand Down
2 changes: 1 addition & 1 deletion __tests__/expression-receiver-calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('expression receivers', () => {
.map((r) => r.referenceName);
expect(refs).toEqual([
'list().map', 'list', 'x.run', 'y.run', 'x.stop', 'getTarget().install', 'getTarget',
'f', 'run', 'go', 'start',
'f', 'run', 'new Runner().go', 'start',
]);
});
});
3 changes: 3 additions & 0 deletions __tests__/fixtures/kernel-parity/torture.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,7 @@ async function exprReceivers(x) {
(() => 1).call(null);
this.a.b.run();
new Runner().go();
new RegExp(p).exec(s);
(new ns.Widget(1)).draw();
new (pick())().go();
}
5 changes: 4 additions & 1 deletion __tests__/kernel-tsjs-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ async function exprReceivers(x, y) {
this.a.b.run();
super.stop();
new Runner().go();
(new ns.Widget(1)).draw();
new (pick())().go();
new Runner().a.run();
window.Api.start();
}
`, language);
Expand All @@ -286,7 +289,7 @@ async function exprReceivers(x, y) {
.map((r) => r.referenceName)).toEqual([
'list().map', 'list', 'x.run',
...(typed ? ['x.run', 'y.run', 'x.stop', 'getTarget().install', 'getTarget', 'has'] : []),
'f', 'run', 'stop', 'go', 'start',
'f', 'run', 'stop', 'new Runner().go', 'new ns.Widget().draw', 'pick', 'start',
]);
});

Expand Down
106 changes: 106 additions & 0 deletions __tests__/new-expression-receiver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* A method called on a `new C(...)` receiver resolves on `C` or nowhere.
* `new RegExp(p).exec(s)` used to reach the resolver as the bare `exec` and
* exact-match a project database wrapper's `exec`; the extractor now keeps the
* constructed class (`new RegExp().exec`), so a built-in or external class
* binds to nothing while a project class resolves its own (or inherited)
* method.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { CodeGraph } from '../src';
import { extractFromSource } from '../src/extraction';
import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';

let dir: string;
let cg: CodeGraph;

const files: Record<string, string> = {
'src/db.ts':
'export class SqliteDatabase { exec(sql: string) { return sql; } get(k: string) { return k; } }\n' +
'export class Printer { toString() { return "p"; } }\n',
'src/runner.ts':
'export class Base { baseMethod() { return 1; } }\n' +
'export class Runner { run() { return 1; } }\n' +
'export class Sub extends Base { own() { return 2; } }\n',
'src/use.ts':
"import { Runner, Sub, Runner as R } from './runner';\n" +
"import * as rr from './runner';\n" +
'export function builtins(p: string, u: string, s: string) {\n' +
' const a = new RegExp(p).exec(s);\n' +
' const b = new URL(u).toString();\n' +
' const c = new Map([[1, 2]]).get(1);\n' +
' const d = new Date().getTime();\n' +
' return [a, b, c, d];\n' +
'}\n' +
'export function projectClasses() {\n' +
' return new Runner().run() + new Sub().baseMethod() + new Sub().own() + new R().run() + new rr.Runner().run();\n' +
'}\n',
'src/plain.js':
"const { Runner } = require('./runner');\n" +
'function jsCalls(p, s) {\n' +
' new RegExp(p).exec(s);\n' +
' new Set([1]).get(1);\n' +
' return new Runner().run();\n' +
'}\n' +
'module.exports = { jsCalls };\n',
};

beforeAll(async () => {
await initGrammars();
await loadAllGrammars();
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-newrecv-'));
for (const [rel, text] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true });
fs.writeFileSync(path.join(dir, rel), text);
}
cg = CodeGraph.initSync(dir);
await cg.indexAll();
});

afterAll(() => {
cg.destroy();
fs.rmSync(dir, { recursive: true, force: true });
});

const node = (name: string, file: string) =>
cg.getNodesByName(name).find((n) => n.filePath.endsWith(file) && (n.kind === 'function' || n.kind === 'method'))!;
const calleesOf = (name: string, file: string) =>
cg.getCallees(node(name, file).id)
.filter(({ edge }) => edge.kind === 'calls')
.map(({ node: n }) => n.qualifiedName)
.sort();

describe('new-expression receivers', () => {
it('TS: a built-in or external constructed class binds to no project method', () => {
expect(calleesOf('builtins', 'use.ts')).toEqual([]);
expect(cg.getCallers(node('exec', 'db.ts').id)).toEqual([]);
expect(cg.getCallers(node('get', 'db.ts').id)).toEqual([]);
expect(cg.getCallers(node('toString', 'db.ts').id)).toEqual([]);
});

it('TS: a project class resolves its own and inherited methods, via alias and namespace too', () => {
expect([...new Set(calleesOf('projectClasses', 'use.ts'))]).toEqual(['Base::baseMethod', 'Runner::run', 'Sub::own']);
expect(cg.getCallers(node('run', 'runner.ts').id).map(({ node: n }) => n.name)).toContain('projectClasses');
});

it('JS: the same rules apply', () => {
expect(calleesOf('jsCalls', 'plain.js')).toEqual(['Runner::run']);
});

it('extraction keeps the constructed class in the ref name', () => {
const src =
'function f(p, s) {\n' +
' new RegExp(p).exec(s);\n' +
' (new ns.Widget<T>(1)).draw();\n' +
' new (pick())().go();\n' +
' new Runner().a.run();\n' +
'}\n';
const refs = extractFromSource('t.ts', src, 'typescript').unresolvedReferences
.filter((r) => r.referenceKind === 'calls')
.map((r) => r.referenceName);
expect(refs).toEqual(['new RegExp().exec', 'new ns.Widget().draw', 'pick']);
});
});
19 changes: 15 additions & 4 deletions codegraph-kernel/src/tsjs/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,19 @@ impl<'t> Walker<'t> {
// TreeSitterExtractor.extractCall.
let Some(inner) = self.plain_inner_callee(r) else { return };
callee_name = format!("{inner}().{method_name}");
} else if let Some(r) = receiver.filter(|r| r.kind() == "new_expression") {
// Constructor receiver — `new Runner().run()`: keep the
// class as `new <Class>().<method>` so the resolver
// resolves the method on it or nowhere; emit nothing for
// a constructor that is not a plain name / member chain.
// Mirrors TreeSitterExtractor.extractCall.
let Some(ctor) = r
.child_by_field_name("constructor")
.and_then(|c| Self::plain_member_name(self.text(c)))
else {
return;
};
callee_name = format!("new {ctor}().{method_name}");
} else if receiver.is_some_and(|r| !keeps_bare_receiver(r, self.src)) {
// An expression receiver with no static type
// (`(a ?? b).map()`, `f().list.map()`): emit nothing
Expand Down Expand Up @@ -1791,9 +1804,8 @@ fn peel_receiver(node: Node<'_>) -> Node<'_> {
cur
}

/// `this` / `super`, a member chain rooted at either or at `window`, or
/// `new C()` still collapse to the bare method name (mirrors
/// keepsBareTsJsReceiver).
/// `this` / `super`, or a member chain rooted at either or at `window`, still
/// collapse to the bare method name (mirrors keepsBareTsJsReceiver).
fn keeps_bare_receiver(node: Node<'_>, src: &str) -> bool {
let mut cur = node;
while matches!(cur.kind(), "member_expression" | "subscript_expression") {
Expand All @@ -1805,7 +1817,6 @@ fn keeps_bare_receiver(node: Node<'_>, src: &str) -> bool {
match cur.kind() {
"this" | "super" => true,
"identifier" => &src[cur.byte_range()] == "window",
"new_expression" => cur.id() == node.id(),
_ => false,
}
}
23 changes: 18 additions & 5 deletions src/extraction/tree-sitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,9 @@ function peelTsJsReceiver(node: SyntaxNode): SyntaxNode {

/**
* Whether a TS/JS receiver still collapses to the bare method name: `this` /
* `super` (the resolver reads the owner off the enclosing class), a member
* `super` (the resolver reads the owner off the enclosing class) and a member
* chain rooted at either or at `window` (the project-global escape of
* {@link isUnresolvedTsJsChain}), and `new C()` (its class is written at the
* call).
* {@link isUnresolvedTsJsChain}). `new C().m()` has its own encoding.
*/
function keepsBareTsJsReceiver(node: SyntaxNode, source: string): boolean {
let cur: SyntaxNode | null = node;
Expand All @@ -513,8 +512,7 @@ function keepsBareTsJsReceiver(node: SyntaxNode, source: string): boolean {
}
if (!cur) return false;
if (cur.type === 'this' || cur.type === 'super') return true;
if (cur.type === 'identifier') return getNodeText(cur, source) === 'window';
return cur === node && cur.type === 'new_expression';
return cur.type === 'identifier' && getNodeText(cur, source) === 'window';
}

/**
Expand Down Expand Up @@ -5581,6 +5579,21 @@ export class TreeSitterExtractor {
const chain = getNodeText(func, this.source).replace(/\s+/g, '').replace(/\?\./g, '.');
if (!/^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*){2,}$/.test(chain)) return;
calleeName = chain;
} else if (TS_JS_CHAIN_LANGUAGES.has(this.language) && receiver?.type === 'new_expression') {
// Constructor receiver — `new Runner().run()`, `new RegExp(p).exec(s)`.
// The class is written at the call, so keep it: `new Runner().run`
// tells the resolver the receiver is an INSTANCE of `Runner`, and
// the method resolves on that class (or a supertype) or nowhere —
// a built-in or external class has no project method to bind to.
// The bare method name this used to emit exact-matched any
// same-named project method (`new RegExp(p).exec()` onto a
// database wrapper's `exec`). A constructor that is not a plain
// name or member chain (`new (f())().m()`) emits nothing.
// Mirrored in the kernel's extract_call (tsjs/extractors.rs).
const ctor = getChildByField(receiver, 'constructor');
const ctorName = ctor ? getNodeText(ctor, this.source).replace(/\s+/g, '') : '';
if (!/^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(ctorName)) return;
calleeName = `new ${ctorName}().${methodName}`;
} else if (
TS_JS_CHAIN_LANGUAGES.has(this.language) &&
receiver &&
Expand Down
16 changes: 14 additions & 2 deletions src/resolution/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
isImportableKind,
CPP_DEFINE_SIGNATURE,
} from './types';
import { isPythonSelfCall, matchJsStoreBindingCall, isUnresolvedJsMemberCall, matchObjectPathCall, thisScopeCaller, isVisibleAcrossFiles, matchReference, matchFunctionRef, matchDottedCallChain, matchScopedCallChain, matchMethodCall, sameLanguageFamily, crossesCodeBoundary, gateLanguageMatch, dumpNameMatcherProfile, clearNameMatcherMemos, isRustNameInScope, CASE_INSENSITIVE_LANGUAGES, isDartMemberRead, matchDartMemberRead, isDartChainLink, matchDartChainLink, isDartAnnotation, matchDartAnnotation, isStdMethodName, isGoUnknownQualified, isGoBareName, goTypePositionTarget, GO_TYPE_KINDS, matchGoAssertedCall, isGoAssertedLocal } from './name-matcher';
import { isPythonSelfCall, matchJsStoreBindingCall, isUnresolvedJsMemberCall, matchObjectPathCall, thisScopeCaller, isVisibleAcrossFiles, matchReference, matchFunctionRef, matchDottedCallChain, matchScopedCallChain, matchMethodCall, sameLanguageFamily, crossesCodeBoundary, gateLanguageMatch, dumpNameMatcherProfile, clearNameMatcherMemos, isRustNameInScope, CASE_INSENSITIVE_LANGUAGES, isDartMemberRead, matchDartMemberRead, isDartChainLink, matchDartChainLink, isDartAnnotation, matchDartAnnotation, isStdMethodName, isGoUnknownQualified, isGoBareName, goTypePositionTarget, GO_TYPE_KINDS, matchGoAssertedCall, isGoAssertedLocal, matchNewReceiverCall, newReceiverClass, NEW_RECEIVER_SHAPE } from './name-matcher';
import { isVisibleCppMacro, clearCppMacroVisibility } from './cpp-macro-visibility';
import { isCppConstructorRef, matchCppConstructor } from './cpp-constructor';
import { isCppSupertypeRef, matchCppSupertype, clearCppSupertypeMemos } from './cpp-supertypes';
Expand Down Expand Up @@ -1493,7 +1493,17 @@ export class ReferenceResolver {
CHAIN_SHAPE.test(ref.referenceName) &&
(ref.language === 'typescript' || ref.language === 'javascript' || ref.language === 'tsx' || ref.language === 'jsx' || ref.language === 'python')
) {
return this.gateLanguage(matchReference(ref, this.context), ref);
const chainResult = this.gateLanguage(matchReference(ref, this.context), ref);
// `new Sub().inherited()`: the method may live on a supertype of a
// project class, resolvable once extends edges exist — defer to the
// conformance pass like the other chained calls.
if (!chainResult && NEW_RECEIVER_SHAPE.test(ref.referenceName)) {
const cls = newReceiverClass(ref, this.context);
if (cls && this.context.getNodesByName(cls).some((n) => n.kind === 'class' && sameLanguageFamily(n.language, ref.language))) {
this.deferReference(ref, this.deferredChainRefs);
}
}
return chainResult;
}

const tImp = this.profileStages ? process.hrtime.bigint() : 0n;
Expand Down Expand Up @@ -1920,6 +1930,8 @@ export class ReferenceResolver {
// dotted-receiver languages on `.` (matchDottedCallChain).
const chainMatch = (ref.language === 'php' && PHP_PROP_SHAPE.test(ref.referenceName))
? matchMethodCall(ref, this.context)
: NEW_RECEIVER_SHAPE.test(ref.referenceName)
? matchNewReceiverCall(ref, this.context)
: SCOPED_CHAIN_LANGUAGES.has(ref.language)
? matchScopedCallChain(ref, this.context)
: matchDottedCallChain(ref, this.context);
Expand Down
Loading