TypeScript: an import resolves to a same-named method of an exported object literal instead of the exported function

Đã đóng Phù hợp với người mới
#2,945 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@inth3shadows đang làm issue này rồi.

Từ ngày 28/10/2026.

  • #2990 của @inth3shadows — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
75/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
node.js, typescript
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu trong src/resolution/import-resolver.ts tại getFileExportIndex quanh dòng 279, nơi nút được export đầu tiên theo mỗi tên thắng và phương thức object literal hashDriver::symbolHref che khuất export thật. Tái hiện với fixture hai tệp nav.ts/walk.ts (issue cho thấy hình dạng chính xác), sau đó áp dụng và so sánh tie-break được đề xuất, chạy __tests__/ui-package.test.ts và các bộ test import/export/reexport/barrel/commonjs với CODEGRAPH_KERNEL=0. Hoàn tất nghĩa là walkTo liên kết tới nav.ts:6 và các bộ test đó vẫn xanh.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

import { f } from './m' resolves to a method named f inside an exported object literal in m.ts, instead of m.ts's exported function f, when the object comes first in the file.

// nav.ts
export interface Driver { symbolHref(id: string): string; }
export const hashDriver: Driver = {
  symbolHref(id) { return '#' + id; },          // line 3
};
let driver: Driver = hashDriver;
export function symbolHref(id: string): string { return driver.symbolHref(id); }   // line 6

// walk.ts
import { symbolHref } from './nav';
export function walkTo(id: string) { return symbolHref(id); }
expected actual
walkTo calls symbolHref at nav.ts:6 symbolHref at nav.ts:3 (hashDriver::symbolHref, resolvedBy: import)

Reproduced on main at 31ec9082 with CODEGRAPH_KERNEL=0, and on 69af19ac with the native kernel too (Linux/WSL2, Node 22.23.2). The same thing happens in this repo: ui/src/lib/walk.ts calls symbolHref/navigate, and __tests__/ui-package.test.ts calls symbolHref/fileHref/mapHref/flowHref. All of them land on hashNavigation's methods in ui/src/lib/navigation.ts (lines 123–187) rather than the exported functions at lines 240–272.

Cause. The object's method is indexed as function symbolHref, isExported = 1, qualifiedName hashDriver::symbolHref (presumably through the object-literal extraction for an exported const). getFileExportIndex keeps the first exported node per name (import-resolver.ts#L279), so the method shadows the real export.

Possible fix. When two exported nodes share a name, let a top-level declaration replace a member:

const prev = idx.byName.get(n.name);
if (!prev || (prev.qualifiedName !== prev.name && n.qualifiedName === n.name)) idx.byName.set(n.name, n);

With that change walkTo links to nav.ts:6, and the 62 __tests__ files matching import/export/reexport/barrel/store/commonjs/require/default pass (490 tests, CODEGRAPH_KERNEL=0). I haven't run the full suite, and a stricter version might keep members out of byName entirely.

Not fixed by #2754 or #2800: I ran this fixture on both heads and the edge still lands on line 3. I also searched open issues and PRs for object-literal methods and import namesakes, and found nothing else that covers it.

Ngôn ngữ chính
C
Star
73.2k
Fork
4.7k
Merge trung bình
3 giờ 39 phút
Pull request đã merge (30 ngày)
528

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của colbymchenry/codegraph

Tất cả issue của colbymchenry/codegraph

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.