* Resolve a Rust module path (segments WITHOUT the leaf symbol) to the file of * the last module segment — `crate::a::b` → ` /a/b.rs` (or `.../b/mod.rs`). * Anchors on `crate` / `self` / `super`; a bare path is tried crate-relative.
( segments: string[], fromFile: string, context: ResolutionContext )
| 1625 | * Anchors on `crate` / `self` / `super`; a bare path is tried crate-relative. |
| 1626 | */ |
| 1627 | function resolveRustModuleFile( |
| 1628 | segments: string[], |
| 1629 | fromFile: string, |
| 1630 | context: ResolutionContext |
| 1631 | ): string | null { |
| 1632 | if (segments.length === 0) return null; |
| 1633 | const projectRoot = context.getProjectRoot(); |
| 1634 | const fromAbs = path.join(projectRoot, fromFile); |
| 1635 | const toRel = (p: string) => path.relative(projectRoot, p).replace(/\\/g, '/'); |
| 1636 | |
| 1637 | // Walk a sequence of module segments down from `startDir`, mapping each to a |
| 1638 | // `<seg>.rs` or `<seg>/mod.rs` file. Returns the leaf module's file, or null |
| 1639 | // if `startDir` is null or any segment has no file on disk. |
| 1640 | const resolveUnder = (startDir: string | null, rest: string[]): string | null => { |
| 1641 | if (!startDir) return null; |
| 1642 | let dir = startDir; |
| 1643 | let targetFile: string | null = null; |
| 1644 | for (const seg of rest) { |
| 1645 | if (seg === 'self' || seg === 'crate' || seg === 'super') continue; |
| 1646 | const asFile = toRel(path.join(dir, seg + '.rs')); |
| 1647 | const asMod = toRel(path.join(dir, seg, 'mod.rs')); |
| 1648 | if (context.fileExists(asFile)) targetFile = asFile; |
| 1649 | else if (context.fileExists(asMod)) targetFile = asMod; |
| 1650 | else return null; |
| 1651 | dir = path.join(dir, seg); |
| 1652 | } |
| 1653 | return targetFile; |
| 1654 | }; |
| 1655 | |
| 1656 | const first = segments[0]!; |
| 1657 | if (first === 'crate') { |
| 1658 | return resolveUnder(rustCrateRootDir(fromAbs, context), segments.slice(1)); |
| 1659 | } |
| 1660 | if (first === 'self') { |
| 1661 | return resolveUnder(rustSelfModuleDir(fromAbs), segments.slice(1)); |
| 1662 | } |
| 1663 | if (first === 'super') { |
| 1664 | let supers = 0; |
| 1665 | while (segments[supers] === 'super') supers++; |
| 1666 | let dir: string | null = rustSelfModuleDir(fromAbs); |
| 1667 | for (let s = 0; s < supers && dir; s++) dir = path.dirname(dir); |
| 1668 | return resolveUnder(dir, segments.slice(supers)); |
| 1669 | } |
| 1670 | // Bare path. In expression position (`submodule::item()` — the router-assembly |
| 1671 | // and general cross-module-call pattern) the prefix is a SUBMODULE of the |
| 1672 | // current module, i.e. 2018 `self::`-relative — so try self-relative FIRST. |
| 1673 | // Fall back to crate-relative for 2015-edition / crate-root items. External |
| 1674 | // crate paths (`serde::de::Error`) miss both and fall through to name-matching. |
| 1675 | return ( |
| 1676 | resolveUnder(rustSelfModuleDir(fromAbs), segments) ?? |
| 1677 | resolveUnder(rustCrateRootDir(fromAbs, context), segments) |
| 1678 | ); |
| 1679 | } |
| 1680 | |
| 1681 | /** |
| 1682 | * Resolve a Java/Kotlin reference whose receiver is the simple name of |
no test coverage detected