MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / expandPath

Function expandPath

source code/utils/path.ts:34–87  ·  view source on GitHub ↗
(path: string, baseDir?: string)

Source from the content-addressed store, hash-verified

32 * expandPath('/absolute/path') // '/absolute/path'
33 */
34export function expandPath(path: string, baseDir?: string): string {
35 // Set default baseDir to getCwd() if not provided
36 const actualBaseDir = baseDir ?? getCwd() ?? getFsImplementation().cwd()
37
38 // Input validation
39 if (typeof path !== 'string') {
40 throw new TypeError(`Path must be a string, received ${typeof path}`)
41 }
42
43 if (typeof actualBaseDir !== 'string') {
44 throw new TypeError(
45 `Base directory must be a string, received ${typeof actualBaseDir}`,
46 )
47 }
48
49 // Security: Check for null bytes
50 if (path.includes('\0') || actualBaseDir.includes('\0')) {
51 throw new Error('Path contains null bytes')
52 }
53
54 // Handle empty or whitespace-only paths
55 const trimmedPath = path.trim()
56 if (!trimmedPath) {
57 return normalize(actualBaseDir).normalize('NFC')
58 }
59
60 // Handle home directory notation
61 if (trimmedPath === '~') {
62 return homedir().normalize('NFC')
63 }
64
65 if (trimmedPath.startsWith('~/')) {
66 return join(homedir(), trimmedPath.slice(2)).normalize('NFC')
67 }
68
69 // On Windows, convert POSIX-style paths (e.g., /c/Users/...) to Windows format
70 let processedPath = trimmedPath
71 if (getPlatform() === 'windows' && trimmedPath.match(/^\/[a-z]\//i)) {
72 try {
73 processedPath = posixPathToWindowsPath(trimmedPath)
74 } catch {
75 // If conversion fails, use original path
76 processedPath = trimmedPath
77 }
78 }
79
80 // Handle absolute paths
81 if (isAbsolute(processedPath)) {
82 return normalize(processedPath).normalize('NFC')
83 }
84
85 // Handle relative paths
86 return resolve(actualBaseDir, processedPath).normalize('NFC')
87}
88
89/**
90 * Converts an absolute path to a relative path from cwd, to save tokens in

Callers 15

getPathFunction · 0.85
validateInputFunction · 0.85
backfillObservableInputFunction · 0.85
validateInputFunction · 0.85
callFunction · 0.85
validateAttachmentPathsFunction · 0.85
resolveAttachmentsFunction · 0.85
normalizeFileEditInputFunction · 0.85
backfillObservableInputFunction · 0.85
validateInputFunction · 0.85
callFunction · 0.85
backfillObservableInputFunction · 0.85

Calls 5

getCwdFunction · 0.85
getFsImplementationFunction · 0.85
getPlatformFunction · 0.85
resolveFunction · 0.70
normalizeFunction · 0.50

Tested by

no test coverage detected