feat(resolution): tsconfig path aliases + re-export chain following (#130)
* feat(resolution): tsconfig path aliases + re-export chain following
Two related correctness improvements that unlock accurate import
resolution on modern JS/TS codebases.
1) tsconfig/jsconfig path aliases.
The resolver previously had a hard-coded list of common aliases
(@/, ~/, src/, app/) and ignored any project-defined paths from
tsconfig.json compilerOptions.paths — which means every import
through @components/Foo, @lib/utils, etc. on Vite/Next/Nuxt/Nest
projects silently failed to resolve. Adds src/resolution/path-
aliases.ts that reads tsconfig.json (and falls back to jsconfig.json),
honours baseUrl, supports the * wildcard, and respects the priority
order of multiple replacement targets per alias. JSONC tolerant
(strips comments + trailing commas, common in the wild). The new
ResolutionContext.getProjectAliases() lazily loads + caches the
result; resolveAliasedImport consults it before the legacy fallback
list.
Verified live on a synthetic project with @utils/* and @lib custom
aliases: both resolved to the correct files and produced edges,
unresolved_refs empty.
2) Re-export chain following.
`import { Foo } from './barrel'` where barrel.ts only re-exports
(`export { Foo } from './real'` or `export * from './real'`) used
to fail because the resolver only looked for declarations IN the
resolved file — it never followed the export chain to the actual
definition. Adds extractReExports() (named + wildcard + as-rename
forms), a per-file getReExports() context method, and a recursive
findExportedSymbol() helper with depth cap (8) and visited-set
cycle protection. resolveViaImport now uses it whenever the symbol
isn't directly declared in the imported file.
Verified live on a synthetic 3-hop chain (main → all.ts wildcard →
index.ts named → auth.ts declaration): signIn resolved correctly,
unresolved_refs empty.
Full test suite: 380 passed, 0 failed.
* fix(resolution): address reviewer findings — isExternalImport bypass, JSONC strings, comment stripping, optional context method
Five fixes from independent semantic review:
- isExternalImport now consults context.getProjectAliases() before
the bare-specifier heuristic. Without this, custom prefixes like
'@components/*' from tsconfig.paths were classified as npm and
resolveAliasedImport never even ran. Adds a context parameter
(optional, for backward compat with mock contexts).
- stripJsonc rewritten as a string-aware state machine. The previous
regex-only version corrupted any URL embedded in a JSON string
value ('https://cdn.example.com' lost everything after '//').
- extractReExports now strips JS line+block comments from content
before applying the regex, so a commented-out 'export { x } from
...' no longer creates a phantom re-export edge. New
stripJsComments helper preserves string literals (single, double,
template) so '//' inside a string stays intact.
- ResolutionContext.getProjectAliases() made optional so existing
mock contexts in __tests__/resolution.test.ts (which TypeScript
doesn't type-check because tsconfig excludes __tests__) don't
throw at runtime when resolveAliasedImport hits them. Caller
uses ?.
- Two new integration tests in __tests__/resolution.test.ts:
* Path-alias resolution with name-collision: two pickMe() in
different dirs, only the @utils-aliased one should be the
call target. Asserts via getCallers on each candidate node.
* No-tsconfig fallback: relative import still produces the call
edge.
Full test suite: 832 passed (was 380; the increase is from the
biomarkers + LLM hooks that ship via parent branches).
* fix(resolution): allow re-export rename chains past the pre-filter
The fast pre-filter in resolveOne() bails when no symbol with the
reference name exists project-wide, which is incompatible with the
new chain-following code: a renamed re-export (`import { login }
from './barrel'` where the barrel does `export { signIn as login }
from './auth'`) intentionally calls a name that has no project-wide
declaration. The chain finds the renamed upstream symbol — but only
if resolution is allowed to run.
Add an import-mapping escape so the pre-filter only bails when the
ref also doesn't match any local import. Adds two tests covering the
3-hop wildcard chain and the named-rename branch.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Colby McHenry <me@colbymchenry.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
Colby McHenry
parent
56f6b3b485
commit
d151c0f922
@@ -711,4 +711,139 @@ def bootstrap():
|
||||
expect(result?.targetNodeId).toBe('func:di.ts:Inject:10');
|
||||
});
|
||||
});
|
||||
|
||||
describe('tsconfig path aliases', () => {
|
||||
it('resolves an aliased import to the alias-mapped file (not a same-named file elsewhere)', async () => {
|
||||
// Two same-named exports in different directories. Without alias
|
||||
// resolution, name-matcher would pick whichever it finds first;
|
||||
// with alias resolution, the import path uniquely picks one.
|
||||
fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
|
||||
fs.mkdirSync(path.join(tempDir, 'src/legacy'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/utils/format.ts'),
|
||||
`export function pickMe(): number { return 1; }\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/legacy/format.ts'),
|
||||
`export function pickMe(): number { return 99; }\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/main.ts'),
|
||||
`import { pickMe } from '@utils/format';\nexport function go(): number { return pickMe(); }\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'tsconfig.json'),
|
||||
JSON.stringify({
|
||||
compilerOptions: {
|
||||
baseUrl: './src',
|
||||
paths: { '@utils/*': ['utils/*'] },
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
cg = await CodeGraph.init(tempDir, { index: true });
|
||||
cg.resolveReferences();
|
||||
|
||||
// The two pickMe nodes live in different files. The aliased
|
||||
// import should attach the call edge to the @utils-mapped one,
|
||||
// not the legacy duplicate.
|
||||
const all = cg.getNodesByKind('function').filter((n) => n.name === 'pickMe');
|
||||
const utilsNode = all.find((n) => n.filePath === 'src/utils/format.ts');
|
||||
const legacyNode = all.find((n) => n.filePath === 'src/legacy/format.ts');
|
||||
expect(utilsNode).toBeDefined();
|
||||
expect(legacyNode).toBeDefined();
|
||||
|
||||
const utilsCallers = cg.getCallers(utilsNode!.id);
|
||||
const legacyCallers = cg.getCallers(legacyNode!.id);
|
||||
expect(utilsCallers.length).toBeGreaterThan(0);
|
||||
expect(utilsCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
|
||||
// The legacy node should NOT have a caller from src/main.ts —
|
||||
// the alias correctly picked the utils version.
|
||||
expect(legacyCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(false);
|
||||
});
|
||||
|
||||
it('falls back gracefully when tsconfig is absent', async () => {
|
||||
fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/a.ts'),
|
||||
`export function aFn(): void {}\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/b.ts'),
|
||||
`import { aFn } from './a';\nexport function bFn(): void { aFn(); }\n`
|
||||
);
|
||||
|
||||
cg = await CodeGraph.init(tempDir, { index: true });
|
||||
// No tsconfig present — index should still complete and the
|
||||
// relative-import-based call edge should be created.
|
||||
const aFn = cg.getNodesByKind('function').find((n) => n.name === 'aFn');
|
||||
expect(aFn).toBeDefined();
|
||||
const callers = cg.getCallers(aFn!.id);
|
||||
expect(callers.some((c) => c.node.filePath === 'src/b.ts')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('re-export chain following', () => {
|
||||
it('chases a 3-hop barrel chain (wildcard → named → declaration)', async () => {
|
||||
// main.ts → all.ts (wildcard) → index.ts (named) → auth.ts (declaration).
|
||||
// Without chain following, `signIn` resolves to nothing because
|
||||
// none of the barrel files declare it directly.
|
||||
fs.mkdirSync(path.join(tempDir, 'src/services'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/services/auth.ts'),
|
||||
`export function signIn(): void {}\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/services/index.ts'),
|
||||
`export { signIn } from './auth';\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/all.ts'),
|
||||
`export * from './services/index';\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/main.ts'),
|
||||
`import { signIn } from './all';\nexport function go(): void { signIn(); }\n`
|
||||
);
|
||||
|
||||
cg = await CodeGraph.init(tempDir, { index: true });
|
||||
cg.resolveReferences();
|
||||
|
||||
const signInNode = cg
|
||||
.getNodesByKind('function')
|
||||
.find((n) => n.name === 'signIn' && n.filePath === 'src/services/auth.ts');
|
||||
expect(signInNode).toBeDefined();
|
||||
const callers = cg.getCallers(signInNode!.id);
|
||||
expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
|
||||
});
|
||||
|
||||
it('follows a renamed named re-export (export { foo as bar } from ...)', async () => {
|
||||
// The chase has to look up `foo` in the upstream module even
|
||||
// though the importer asked for `bar` — exercises the rename
|
||||
// branch of findExportedSymbol.
|
||||
fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/auth.ts'),
|
||||
`export function signIn(): void {}\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/index.ts'),
|
||||
`export { signIn as login } from './auth';\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/main.ts'),
|
||||
`import { login } from './index';\nexport function go(): void { login(); }\n`
|
||||
);
|
||||
|
||||
cg = await CodeGraph.init(tempDir, { index: true });
|
||||
cg.resolveReferences();
|
||||
|
||||
const signInNode = cg
|
||||
.getNodesByKind('function')
|
||||
.find((n) => n.name === 'signIn' && n.filePath === 'src/auth.ts');
|
||||
expect(signInNode).toBeDefined();
|
||||
const callers = cg.getCallers(signInNode!.id);
|
||||
expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user