Skip to content
Merged
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 @@ -78,6 +78,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- In JavaScript and TypeScript, a name imported from a package your `package.json` doesn't list but your project has installed, like `import { useForm } from 'react-hook-form'` hoisted to a monorepo's root `node_modules`, no longer links to an unrelated function of the same name elsewhere in your project (#3271). Re-index JavaScript and TypeScript projects after upgrading.
- A file whose parse never ends, like a `.tsx` file with a half-typed JSX attribute, no longer keeps `codegraph init` from exiting or leaves `codegraph sync` and the file watcher spinning at full CPU: its parse now stops at the file's parse time limit, and the file is listed as a parse that timed out. Thanks @inth3shadows for the report. (#3274)
- In PHP, an include built from the file's own directory, like `require_once __DIR__ . '/inc/a.php'` or `dirname(__FILE__) . '/inc/b.php'`, now links to that file the way a plain `require_once 'lib.php'` does, and a path starting with `./` or `../` links only to the file named exactly, as PHP loads it, never one with `.php` added. (#3298)
- TypeScript and TSX files now parse the way the TypeScript compiler reads them in many more places, such as an operator before a generic call, `as` and `!` precedence, import types and `using` declarations, so a function containing `for (using x of items)` is no longer missing from the index, and a type written `import("./m").T` links that module's `T` rather than any type of the same name; re-index TypeScript projects after upgrading.

## [1.6.3] - 2026-11-17

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Known gap: `await using` declaration in a for-await-of head (TS 5.2).
// Was a gap until fork v0.25.0: `await using` declaration in a for-await-of head (TS 5.2).
declare const asyncHandles: AsyncIterable<AsyncDisposable>;
export async function drain() { for await (await using h of asyncHandles) { void h; } }
2 changes: 2 additions & 0 deletions __tests__/fixtures/ts-syntax/export-named-from-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Was a gap until fork v0.25.0: import attributes on a named re-export.
export { default as data } from "./data.json" with { type: "json" };
2 changes: 2 additions & 0 deletions __tests__/fixtures/ts-syntax/export-star-from-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Was a gap until fork v0.25.0: import attributes on `export * from`.
export * from "./more.json" with { type: "json" };

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions __tests__/fixtures/ts-syntax/gap-using-in-for.ts

This file was deleted.

3 changes: 3 additions & 0 deletions __tests__/fixtures/ts-syntax/using-in-for.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Was a gap until fork v0.25.0: `using` declaration in a for-of head (TS 5.2).
declare const handles: Disposable[];
for (using h of handles) { void h; }
198 changes: 198 additions & 0 deletions __tests__/ts-import-type-reference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/**
* A TypeScript import type, `import("./m").Shape`, names the export of that
* module, the way `import { Shape } from "./m"` does. The grammar parses it as
* an `import_type` node since tree-sitter-typescript fork v0.25.0 (before, a
* call to a function named `import`). Read by its last name alone, the
* reference reached whatever project declaration shared the name: another
* copy of a package's types, or the property the type annotates. Now the name
* is followed through the module, and a module the project doesn't hold links
* nothing.
*/
import { describe, it, expect, afterEach } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { CodeGraph } from '../src';

const roots: string[] = [];
afterEach(() => {
for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true });
});

async function indexed(files: Record<string, string>): Promise<CodeGraph> {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-ts-import-type-'));
roots.push(root);
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
return CodeGraph.init(root, { index: true });
}

/** `references` targets of the declarations named `name` in `file`, as `file:name`. */
function refs(cg: CodeGraph, file: string, name: string): string[] {
const from = cg.searchNodes(name, { limit: 50 }).map((r) => r.node).filter((n) => n.name === name && n.filePath === file);
if (from.length === 0) throw new Error(`no ${name} in ${file}`);
return from
.flatMap((n) => cg.getCallees(n.id))
.filter((r) => r.edge.kind === 'references')
.map((r) => `${r.node.filePath}:${r.node.name}`)
.sort();
}

describe('TypeScript import types', () => {
it('reach the export of the module they name, not a namesake elsewhere', async () => {
const cg = await indexed({
'src/shapes.ts': 'export interface Shape { a: number }\n',
'src/other/shape.ts': 'export interface Shape { b: string }\n',
'src/use.ts': 'export interface Holder {\n shape: import("./shapes").Shape;\n list: (import("./shapes").Shape)[];\n}\n',
});
try {
expect(refs(cg, 'src/use.ts', 'shape')).toEqual(['src/shapes.ts:Shape']);
expect(refs(cg, 'src/use.ts', 'list')).toEqual(['src/shapes.ts:Shape']);
} finally {
cg.close();
}
});

it('reach the class a typeof import names, never the property it annotates', async () => {
const cg = await indexed({
'src/proxy.ts': 'export class ProxyZoneSpec { run(): void {} }\n',
'src/global.ts': 'export interface Globals {\n ProxyZoneSpec?: typeof import("./proxy").ProxyZoneSpec;\n}\n',
});
try {
expect(refs(cg, 'src/global.ts', 'ProxyZoneSpec')).toEqual(['src/proxy.ts:ProxyZoneSpec']);
} finally {
cg.close();
}
});

it('follow a namespace path inside the module', async () => {
const cg = await indexed({
'src/ns.ts': 'export namespace Outer {\n export interface Inner { x: number }\n}\n',
'src/elsewhere.ts': 'export interface Inner { y: number }\n',
'src/use.ts': [
'export const v: import("./ns").Outer.Inner = { x: 1 };',
'export const w: import("./ns")',
' .Outer.Inner = { x: 1 };',
'export const u: import("./ns")',
' // a note',
' .Outer',
' .Inner = { x: 1 };',
'',
].join('\n'),
});
try {
expect(refs(cg, 'src/use.ts', 'v')).toEqual(['src/ns.ts:Inner']);
expect(refs(cg, 'src/use.ts', 'w')).toEqual(['src/ns.ts:Inner']);
expect(refs(cg, 'src/use.ts', 'u')).toEqual(['src/ns.ts:Inner']);
} finally {
cg.close();
}
});

it('link a package path the tsconfig maps to its source, not a copy of its types', async () => {
const cg = await indexed({
'tsconfig.json': JSON.stringify({ compilerOptions: { baseUrl: '.', paths: { '@app/core': ['packages/core/index.ts'] } } }),
'packages/core/index.ts': 'export { ModelSignal } from "./model";\n',
'packages/core/model.ts': 'export interface ModelSignal<T> { value: T }\n',
'tests/workspace/node_modules_mock/@app/core/index.d.ts': 'export declare interface ModelSignal<T> { value: T }\n',
'tests/golden/out.d.ts': 'export declare class Cmp {\n counter: import("@app/core").ModelSignal<number>;\n}\n',
});
try {
expect(refs(cg, 'tests/golden/out.d.ts', 'counter')).toEqual(['packages/core/model.ts:ModelSignal']);
} finally {
cg.close();
}
});

it('read the module across line breaks, comments and import attributes', async () => {
const cg = await indexed({
'src/shapes.ts': 'export interface Shape { a: number }\n',
'src/other/shape.ts': 'export interface Shape { b: string }\n',
'src/use.ts': [
'export interface Holder {',
' split: import(',
' "./shapes"',
' ).Shape;',
' dotted: import("./shapes")',
' .Shape;',
' commented: import("./shapes" /* the shapes */).Shape;',
' attrs: import("./shapes", { with: { "resolution-mode": "import" } }).Shape;',
' noted: import("./shapes")',
' // the shapes module',
' .Shape;',
' inline: import("./shapes") /* the shapes */ .Shape;',
'}',
'',
].join('\n'),
});
try {
for (const prop of ['split', 'dotted', 'commented', 'attrs', 'noted', 'inline']) {
expect(refs(cg, 'src/use.ts', prop), prop).toEqual(['src/shapes.ts:Shape']);
}
} finally {
cg.close();
}
});

it('follow a namespace re-export the module makes', async () => {
const cg = await indexed({
'src/inner.ts': 'export interface T { x: number }\n',
'src/other.ts': 'export interface T { y: number }\n',
'src/m.ts': 'export * as ns from "./inner";\n',
'src/use.ts': 'export interface Holder {\n b: import("./m").ns.T;\n}\n',
});
try {
expect(refs(cg, 'src/use.ts', 'b')).toEqual(['src/inner.ts:T']);
} finally {
cg.close();
}
});

it('link nothing for a package outside the project, even when a project type shares the name', async () => {
const cg = await indexed({
'package.json': JSON.stringify({ name: 'app', dependencies: { lodash: '4.17.21' } }),
'src/dict.ts': 'export interface Dictionary<T> { [k: string]: T }\n',
'src/use.ts': [
'export interface Holder {',
' d: import("lodash").Dictionary<number>;',
' split: import(',
' "lodash"',
' ).Dictionary<number>;',
' attrs: import("lodash", { with: { "resolution-mode": "import" } }).Dictionary<number>;',
' deep: import("lodash")',
' .Outer.Dictionary<number>;',
' noted: import("lodash")',
' // a note',
' .Dictionary<number>;',
' inline: import("lodash") /* c */ .Dictionary<number>;',
'}',
'',
].join('\n'),
});
try {
expect(refs(cg, 'src/use.ts', 'd')).toEqual([]);
expect(refs(cg, 'src/use.ts', 'split')).toEqual([]);
expect(refs(cg, 'src/use.ts', 'attrs')).toEqual([]);
for (const prop of ['deep', 'noted', 'inline']) expect(refs(cg, 'src/use.ts', prop), prop).toEqual([]);
} finally {
cg.close();
}
});

it('index a function whose for-of head declares with using, and its calls', async () => {
const cg = await indexed({
'src/res.ts': 'export function open(): Disposable { return { [Symbol.dispose]() {} }; }\n',
'src/use.ts': 'import { open } from "./res";\nexport function drain(rs: Disposable[]) {\n for (using r of rs) { void r; }\n using h = open();\n return open();\n}\n',
});
try {
const drain = cg.getNodesByKind('function').find((n) => n.name === 'drain' && n.filePath === 'src/use.ts');
expect(drain).toBeDefined();
const callees = cg.getCallees(drain!.id).filter((r) => r.edge.kind === 'calls').map((r) => `${r.node.filePath}:${r.node.name}`);
expect([...new Set(callees)]).toEqual(['src/res.ts:open']);
} finally {
cg.close();
}
});
});
13 changes: 8 additions & 5 deletions codegraph-kernel/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ fn main() {
// ABI 14 and lacks the patch, so it can't be reused. The
// vendored wasm is built from these same generated sources. Layout mirrors
// the upstream repo so scanner.c's `../../common/scanner.h` resolves
// unedited. See docs/grammars/tree-sitter-typescript.md for what the base
// and the fork each fix.
// common/scanner.h 23fd656d7e82f80bf66e0b9b8ef96aa6102ef5bfd9f32c561a4caabe08b9fd34
// typescript/src/parser.c db8c411258ff82520df32ad7213be1c9a60c0cec77fdb6d28858014ae92ea0aa
// tsx/src/parser.c 34f37cd33244724c9d5dc4863123f3a5f6a919f4f472c31981610f6442077394
// as written. The fork patches tsx/src/scanner.c (a JSX string marker
// outside error recovery returns early); typescript/src/scanner.c is
// upstream's. See docs/grammars/tree-sitter-typescript.md for what the
// base and the fork each fix.
// common/scanner.h ce8180d07ed12e5235c8875842a751da7d13cdcd8cdd0bfe398ec044a8e6aa9f
// tsx/src/scanner.c d66248c9a043c34b74342c8aa6ca69904faafdd39e28bdac6c6d58217e17d4ec
// typescript/src/parser.c 83c7c1dc70b5062809c35d1aafc737218ab501fa5cc08f32a275a660721c3721
// tsx/src/parser.c 1094e54097329927ec9b24827fa40d863fd352c1be2b96c48aa2ae6885cff97f
// Flags crib the crate's bindings/rust/build.rs (c11). Each parser.c is
// ~9 MB.
for dialect in ["typescript", "tsx"] {
Expand Down
Loading
Loading