-
Notifications
You must be signed in to change notification settings - Fork 15
fix: resolve renamed-import receiver calls in qualified-method lookup #1971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carlos-alm
wants to merge
1
commit into
fix/issue-1824-dynamic-import-destructuring-rename-const-a-b
Choose a base branch
from
fix/issue-1825-renamed-import-used-as-a-receiver-import-x-as
base: fix/issue-1824-dynamic-import-destructuring-rename-const-a-b
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/integration/issue-1825-renamed-import-receiver.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /** | ||
| * Regression test for #1825: a renamed import used as a call *receiver* | ||
| * (`import { X as Y } from '...'; Y.method()`) must resolve — the qualified- | ||
| * name fallback in `resolveByReceiver` (src/domain/graph/resolver/strategy.ts) | ||
| * previously built the lookup key from the local alias (`Y.method`) instead | ||
| * of the declared name (`X.method`), so the call was silently dropped. | ||
| * | ||
| * Setup: two files. | ||
| * - helpers.js: `export const NamespaceObj = { doThing() {...} }` and | ||
| * `export class Greeter { greet() {...} }`. | ||
| * - consumer.js: imports both renamed (`NamespaceObj as NsAlias`, | ||
| * `Greeter as GreeterAlias`) and calls: | ||
| * - `NsAlias.doThing()` — direct-qualified-method path (no typeMap | ||
| * entry for the receiver; #1825's exact reported repro). | ||
| * - `new GreeterAlias().greet()` — typed-method path (typeMap records | ||
| * the constructor's local alias as the receiver's type; the same | ||
| * de-aliasing gap applies to `resolveViaTypedMethod`). | ||
| * | ||
| * Before the fix, neither call produced a `calls` edge on either engine. | ||
| * | ||
| * Verified on both engines — this is resolver logic mirrored in | ||
| * `crates/codegraph-core/`, so WASM and native must agree. | ||
| */ | ||
|
|
||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import Database from 'better-sqlite3'; | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
| import { buildGraph } from '../../src/domain/graph/builder.js'; | ||
|
|
||
| const FILE_HELPERS = ` | ||
| export const NamespaceObj = { | ||
| doThing() { | ||
| return 1; | ||
| }, | ||
| }; | ||
|
|
||
| export class Greeter { | ||
| greet() { | ||
| return 'hi'; | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const FILE_CONSUMER = ` | ||
| import { NamespaceObj as NsAlias, Greeter as GreeterAlias } from './helpers.js'; | ||
|
|
||
| export function useReceiver() { | ||
| return NsAlias.doThing(); | ||
| } | ||
|
|
||
| export function useConstructedReceiver() { | ||
| const g = new GreeterAlias(); | ||
| return g.greet(); | ||
| } | ||
| `; | ||
|
|
||
| let tmpWasm: string; | ||
| let tmpNative: string; | ||
|
|
||
| beforeAll(async () => { | ||
| tmpWasm = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1825-wasm-')); | ||
| fs.writeFileSync(path.join(tmpWasm, 'helpers.js'), FILE_HELPERS); | ||
| fs.writeFileSync(path.join(tmpWasm, 'consumer.js'), FILE_CONSUMER); | ||
|
|
||
| tmpNative = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1825-native-')); | ||
| fs.writeFileSync(path.join(tmpNative, 'helpers.js'), FILE_HELPERS); | ||
| fs.writeFileSync(path.join(tmpNative, 'consumer.js'), FILE_CONSUMER); | ||
|
|
||
| await Promise.all([ | ||
| buildGraph(tmpWasm, { incremental: false, skipRegistry: true, engine: 'wasm' }), | ||
| buildGraph(tmpNative, { incremental: false, skipRegistry: true, engine: 'native' }), | ||
| ]); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| fs.rmSync(tmpWasm, { recursive: true, force: true }); | ||
| fs.rmSync(tmpNative, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| function getCallEdges(dbPath: string) { | ||
| const db = new Database(dbPath, { readonly: true }); | ||
| try { | ||
| return db | ||
| .prepare( | ||
| `SELECT n1.name AS src, n1.file AS src_file, n2.name AS tgt, n2.file AS tgt_file | ||
| FROM edges e | ||
| JOIN nodes n1 ON e.source_id = n1.id | ||
| JOIN nodes n2 ON e.target_id = n2.id | ||
| WHERE e.kind = 'calls' | ||
| ORDER BY n1.name, n2.name`, | ||
| ) | ||
| .all() as Array<{ src: string; src_file: string; tgt: string; tgt_file: string }>; | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| } | ||
|
|
||
| describe('call-edge resolution through a renamed import used as a receiver (#1825)', () => { | ||
| it('WASM: useReceiver -> NamespaceObj.doThing calls edge exists (direct-qualified path)', () => { | ||
| const edges = getCallEdges(path.join(tmpWasm, '.codegraph', 'graph.db')); | ||
| const edge = edges.find((e) => e.src === 'useReceiver' && e.tgt === 'NamespaceObj.doThing'); | ||
| expect(edge).toBeDefined(); | ||
| expect(edge?.tgt_file).toBe('helpers.js'); | ||
| }); | ||
|
|
||
| it('Native: useReceiver -> NamespaceObj.doThing calls edge exists (direct-qualified path)', () => { | ||
| const edges = getCallEdges(path.join(tmpNative, '.codegraph', 'graph.db')); | ||
| const edge = edges.find((e) => e.src === 'useReceiver' && e.tgt === 'NamespaceObj.doThing'); | ||
| expect(edge).toBeDefined(); | ||
| expect(edge?.tgt_file).toBe('helpers.js'); | ||
| }); | ||
|
|
||
| it('WASM: useConstructedReceiver -> Greeter.greet calls edge exists (typed-method path)', () => { | ||
| const edges = getCallEdges(path.join(tmpWasm, '.codegraph', 'graph.db')); | ||
| const edge = edges.find((e) => e.src === 'useConstructedReceiver' && e.tgt === 'Greeter.greet'); | ||
| expect(edge).toBeDefined(); | ||
| expect(edge?.tgt_file).toBe('helpers.js'); | ||
| }); | ||
|
|
||
| it('Native: useConstructedReceiver -> Greeter.greet calls edge exists (typed-method path)', () => { | ||
| const edges = getCallEdges(path.join(tmpNative, '.codegraph', 'graph.db')); | ||
| const edge = edges.find((e) => e.src === 'useConstructedReceiver' && e.tgt === 'Greeter.greet'); | ||
| expect(edge).toBeDefined(); | ||
| expect(edge?.tgt_file).toBe('helpers.js'); | ||
| }); | ||
|
|
||
| it('no spurious edge is created against the local alias names', () => { | ||
| for (const dbPath of [ | ||
| path.join(tmpWasm, '.codegraph', 'graph.db'), | ||
| path.join(tmpNative, '.codegraph', 'graph.db'), | ||
| ]) { | ||
| const edges = getCallEdges(dbPath); | ||
| expect(edges.find((e) => e.tgt === 'NsAlias.doThing')).toBeUndefined(); | ||
| expect(edges.find((e) => e.tgt === 'GreeterAlias.greet')).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If setup throws before both temp directory variables are assigned, this cleanup calls
fs.rmSyncwithundefined. The original setup error is then followed by a teardownTypeError, and any directory created before the failure can be left behind.