Node `path.posix.normalize` for the require_relative path join (emitRubyRequireRefs): resolve `.`/`..` lexically, collapse `//`, keep unresolvable leading `..`s, preserve a trailing slash.
(p: &str)
| 58 | /// (emitRubyRequireRefs): resolve `.`/`..` lexically, collapse `//`, keep |
| 59 | /// unresolvable leading `..`s, preserve a trailing slash. |
| 60 | fn posix_normalize(p: &str) -> String { |
| 61 | let is_abs = p.starts_with('/'); |
| 62 | let had_trailing = p.len() > 1 && p.ends_with('/'); |
| 63 | let mut out: Vec<&str> = Vec::new(); |
| 64 | for seg in p.split('/') { |
| 65 | match seg { |
| 66 | "" | "." => {} |
| 67 | ".." => { |
| 68 | if matches!(out.last(), Some(&last) if last != "..") { |
| 69 | out.pop(); |
| 70 | } else if !is_abs { |
| 71 | out.push(".."); |
| 72 | } |
| 73 | } |
| 74 | s => out.push(s), |
| 75 | } |
| 76 | } |
| 77 | let mut joined = out.join("/"); |
| 78 | if is_abs { |
| 79 | joined = format!("/{joined}"); |
| 80 | } |
| 81 | if joined.is_empty() || joined == "/" { |
| 82 | return if is_abs { "/".to_string() } else { ".".to_string() }; |
| 83 | } |
| 84 | if had_trailing { |
| 85 | joined.push('/'); |
| 86 | } |
| 87 | joined |
| 88 | } |
| 89 | |
| 90 | struct Scope { |
| 91 | row: u32, |
no test coverage detected