Skip to content
Closed
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 @@ -53,6 +53,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixes

- In Rust, a call through a module that `use super::*` brings in, like `gzip::write()` in a submodule whose parent declares `pub mod gzip;`, now links to that module's function. Thanks @Egrn for the report. (#3253)
- In TypeScript, an awaited call with type arguments like `await fetchApi<User>("/me")` or `await api.get<T>(url)` now links to the function it calls. Before, the call was dropped, so typed fetch wrappers looked like they had no callers. (#2586)
- In JavaScript and TypeScript, a call or `new` in a parameter's default value, like `function renderWithNote(note = buildNote())`, a destructured `{ size = defaultSize() }` or `constructor(private svc = new Service())`, now counts as the function's own call. Before, it was dropped, so `buildNote` was missing that caller and impact on it came back incomplete. Re-index JavaScript and TypeScript projects after upgrading. Thanks @inth3shadows for the report and the pull request. (#2588)
- In React, a component rendered at module scope, in a top-level constant or inside a test's `it(…)` callback, like `createRoot(el).render(<App />)` or `render(<Card />)`, now counts as rendered there, so an app's entry reaches its root component and a component's tests show among its callers. (#2587)
Expand Down
89 changes: 89 additions & 0 deletions __tests__/rust-super-glob-module-call.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, it, expect, afterAll, beforeAll } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { CodeGraph } from '../src';

let root = '';
let cg: CodeGraph;

beforeAll(async () => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-rust-super-glob-'));
const files: Record<string, string> = {
'Cargo.toml': '[package]\nname = "app"\nversion = "0.1.0"\n',
'src/lib.rs': `pub mod archive;
pub mod inline_parent;
`,
'src/archive/mod.rs': `pub mod gzip;

#[cfg(test)]
mod tests {
use super::*;

fn test_write() -> usize {
gzip::write()
}
}

#[cfg(test)]
mod file_tests;
`,
'src/archive/gzip.rs': `pub fn write() -> usize {
42
}
`,
'src/archive/file_tests.rs': `use super::*;

pub fn test_from_file_submod() -> usize {
gzip::write()
}
`,
'src/inline_parent.rs': `pub mod helper {
pub fn run() -> usize {
100
}
}

#[cfg(test)]
mod tests {
use super::*;

fn test_inline() -> usize {
helper::run()
}
}
`,
};
for (const [filePath, content] of Object.entries(files)) {
const full = path.join(root, filePath);
fs.mkdirSync(path.dirname(full), { recursive: true });
fs.writeFileSync(full, content);
}
cg = await CodeGraph.init(root, { index: true });
});

afterAll(() => {
cg?.close();
if (root) fs.rmSync(root, { recursive: true, force: true });
});

function callsFrom(file: string, fnName: string): string[] {
const node = cg.getNodesInFile(file).find((n) => n.name === fnName);
expect(node, `${file}:${fnName}`).toBeDefined();
const callees = cg.getCallees(node!.id);
return callees.map((c) => `${c.node.kind} ${c.node.name} ${c.node.filePath}:${c.node.startLine}`);
}

describe('Rust use super::* glob brings in declared modules (#3253)', () => {
it('resolves a call through a file module brought in by use super::* in an inline submodule', () => {
expect(callsFrom('src/archive/mod.rs', 'test_write')).toEqual(['function write src/archive/gzip.rs:1']);
});

it('resolves a call through a file module brought in by use super::* in a file submodule', () => {
expect(callsFrom('src/archive/file_tests.rs', 'test_from_file_submod')).toEqual(['function write src/archive/gzip.rs:1']);
});

it('resolves a call through an inline module brought in by use super::*', () => {
expect(callsFrom('src/inline_parent.rs', 'test_inline')).toEqual(['function run src/inline_parent.rs:2']);
});
});
21 changes: 19 additions & 2 deletions src/resolution/name-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10372,8 +10372,8 @@ function rustLibModule(crateDir: string, context: ResolutionContext): RustModule
* `unix/mod.rs` or `windows.rs`) are all of them (rustCfgChoiceItems); null
* when a `cfg_attr` sets the path, as for one with no file.
*/
function rustSubmodule(m: RustModule, name: string, context: ResolutionContext, depth = 0): RustModule | null {
if (m.choices) return rustCfgChoice(m.choices.map((c) => rustSubmodule(c, name, context, depth)).filter((c) => c !== null));
function rustSubmodule(m: RustModule, name: string, context: ResolutionContext, depth = 0, seen?: Set<string>): RustModule | null {
if (m.choices) return rustCfgChoice(m.choices.map((c) => rustSubmodule(c, name, context, depth, seen)).filter((c) => c !== null));
const dir = rustJoin(m.dir, name);
const layout = rustUsesOf(m.file, context);
if (layout.pathMods.has(name)) {
Expand All @@ -10396,6 +10396,23 @@ function rustSubmodule(m: RustModule, name: string, context: ResolutionContext,
const passed = rustWalkModules(p.segs, m, context, depth + 1);
if (passed !== null && passed !== 'outside') return passed;
}
const key = `${m.file}\0${m.block?.open ?? -1}\0${name}`;
if (seen?.has(key)) return null;
const nextSeen = seen ?? new Set<string>();
nextSeen.add(key);
try {
for (const g of layout.globPaths) {
if (g.scope || rustBlockAt(layout.blocks, g.at) !== m.block) continue;
const passed = rustWalkModules(g.segs, m, context, depth + 1);
if (passed !== null && passed !== 'outside') {
const sub = rustSubmodule(passed, name, context, depth + 1, nextSeen);
if (sub !== null) return sub;
}
}
} finally {
if (!seen) nextSeen.clear();
else nextSeen.delete(key);
}
return null;
}

Expand Down