mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-10-22 01:07:18 +08:00
fix(go): an explicitly named import owns its name over an unaliased import's last path element (#2660)
fix(go): an explicitly named import owns its name over an unaliased import's last path element
Go binds a name to one import per file. etcd's benchmark tool imports
"github.com/cheggaaa/pb/v3" (package pb) beside
v3 "go.etcd.io/etcd/client/v3", so `v3.Op` is clientv3's. The import
mappings also listed the unaliased import under its last path element,
`v3`, and the first one listed, the progress-bar library outside the
project, took every `v3.` qualifier: `op v3.Op`, `c *v3.Client` and
`[]v3.OpOption` linked to nothing.
extractGoImports no longer lists an unaliased import under a name another
import is explicitly given; it keeps the name goimports assumes for it
(`pb`), as before.
A/B on main 383c2cd9: etcd +8 edges (tools/benchmark and etcdctl), all to
client/v3's types; gin, prometheus and kubernetes unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1382,10 +1382,17 @@ function extractGoImports(content: string): ImportMapping[] {
|
||||
|
||||
const mapping = (localName: string, source: string): ImportMapping =>
|
||||
({ localName, exportedName: '*', source, isDefault: false, isNamespace: true });
|
||||
// Go binds a name to one import of a file, so a name another import is
|
||||
// given explicitly is not an unaliased import's: etcd's benchmark tool has
|
||||
// `"github.com/cheggaaa/pb/v3"` (package pb) beside
|
||||
// `v3 "go.etcd.io/etcd/client/v3"`, and listed first, pb took every `v3.`.
|
||||
const explicit = new Set(specs.flatMap(({ name }) => (name !== undefined && name !== '.' && name !== '_' ? [name] : [])));
|
||||
// A dot import (`. "pkg"`) binds no name of its own; it stays listed under
|
||||
// its last path element.
|
||||
const mappings = specs.map(({ name, source }) =>
|
||||
mapping(name === undefined || name === '.' ? source.split('/').pop()! : name, source));
|
||||
const mappings = specs
|
||||
.map(({ name, source }) => ({ explicitly: name !== undefined && name !== '.', localName: name === undefined || name === '.' ? source.split('/').pop()! : name, source }))
|
||||
.filter(({ explicitly, localName }) => explicitly || !explicit.has(localName))
|
||||
.map(({ localName, source }) => mapping(localName, source));
|
||||
const bound = new Set(mappings.map((m) => m.localName));
|
||||
const assumed = new Map<string, string[]>();
|
||||
for (const { name, source } of specs) {
|
||||
|
||||
Reference in New Issue
Block a user