( filePath: string, base: string | null, resolveRoot?: () => string | string[], )
| 98 | * Returns the absolute path, or null when it escapes every allowed root. |
| 99 | */ |
| 100 | export function resolveOpenInTarget( |
| 101 | filePath: string, |
| 102 | base: string | null, |
| 103 | resolveRoot?: () => string | string[], |
| 104 | ): string | null { |
| 105 | const provided = resolveRoot?.(); |
| 106 | const roots = ( |
| 107 | provided == null |
| 108 | ? [ |
| 109 | base |
| 110 | ? resolvePath(base) |
| 111 | : isAbsolute(filePath) |
| 112 | ? dirname(resolvePath(filePath)) |
| 113 | : resolvePath(process.cwd()), |
| 114 | ] |
| 115 | : Array.isArray(provided) |
| 116 | ? provided |
| 117 | : [provided] |
| 118 | ) |
| 119 | .filter((r): r is string => !!r) |
| 120 | .map((r) => resolvePath(r)); |
| 121 | // Resolve against each root and return the first that contains its own |
| 122 | // resolution. For an absolute filePath the resolution is identical for every |
| 123 | // root (a pure containment check); for a relative filePath this avoids the |
| 124 | // duplicate-basename trap of resolving only against roots[0]. |
| 125 | for (const root of roots) { |
| 126 | const abs = resolvePath(root, filePath); |
| 127 | if (isWithinDirectory(abs, root)) return abs; |
| 128 | } |
| 129 | return null; |
| 130 | } |
no test coverage detected