* 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 )
| 1873 | * Anchors on `crate` / `self` / `super`; a bare path is tried crate-relative. |
| 1874 | */ |
| 1875 | function resolveRustModuleFile( |
| 1876 | segments: string[], |
| 1877 | fromFile: string, |
| 1878 | context: ResolutionContext |
| 1879 | ): string | null { |
| 1880 | if (segments.length === 0) return null; |
| 1881 | const projectRoot = context.getProjectRoot(); |
| 1882 | const fromAbs = path.join(projectRoot, fromFile); |
| 1883 | const toRel = (p: string) => path.relative(projectRoot, p).replace(/\\/g, '/'); |
| 1884 | |
| 1885 | // Walk a sequence of module segments down from `startDir`, mapping each to a |
| 1886 | // `<seg>.rs` or `<seg>/mod.rs` file. Returns the leaf module's file, or null |
| 1887 | // if `startDir` is null or any segment has no file on disk. |
| 1888 | const resolveUnder = (startDir: string | null, rest: string[]): string | null => { |
| 1889 | if (!startDir) return null; |
| 1890 | let dir = startDir; |
| 1891 | let targetFile: string | null = null; |
| 1892 | for (const seg of rest) { |
| 1893 | if (seg === 'self' || seg === 'crate' || seg === 'super') continue; |
| 1894 | const asFile = toRel(path.join(dir, seg + '.rs')); |
| 1895 | const asMod = toRel(path.join(dir, seg, 'mod.rs')); |
| 1896 | if (context.fileExists(asFile)) targetFile = asFile; |
| 1897 | else if (context.fileExists(asMod)) targetFile = asMod; |
| 1898 | else return null; |
| 1899 | dir = path.join(dir, seg); |
| 1900 | } |
| 1901 | return targetFile; |
| 1902 | }; |
| 1903 | |
| 1904 | const first = segments[0]!; |
| 1905 | if (first === 'crate') { |
| 1906 | return resolveUnder(rustCrateRootDir(fromAbs, context), segments.slice(1)); |
| 1907 | } |
| 1908 | if (first === 'self') { |
| 1909 | return resolveUnder(rustSelfModuleDir(fromAbs), segments.slice(1)); |
| 1910 | } |
| 1911 | if (first === 'super') { |
| 1912 | let supers = 0; |
| 1913 | while (segments[supers] === 'super') supers++; |
| 1914 | let dir: string | null = rustSelfModuleDir(fromAbs); |
| 1915 | for (let s = 0; s < supers && dir; s++) dir = path.dirname(dir); |
| 1916 | return resolveUnder(dir, segments.slice(supers)); |
| 1917 | } |
| 1918 | // Bare path. In expression position (`submodule::item()` — the router-assembly |
| 1919 | // and general cross-module-call pattern) the prefix is a SUBMODULE of the |
| 1920 | // current module, i.e. 2018 `self::`-relative — so try self-relative FIRST. |
| 1921 | // Fall back to crate-relative for 2015-edition / crate-root items. External |
| 1922 | // crate paths (`serde::de::Error`) miss both and fall through to name-matching. |
| 1923 | return ( |
| 1924 | resolveUnder(rustSelfModuleDir(fromAbs), segments) ?? |
| 1925 | resolveUnder(rustCrateRootDir(fromAbs, context), segments) |
| 1926 | ); |
| 1927 | } |
| 1928 | |
| 1929 | /** |
| 1930 | * Resolve a Java/Kotlin reference whose receiver is the simple name of |
no test coverage detected