@@ -186,6 +186,7 @@ const win32 = {
186186 let resolvedDevice = '' ;
187187 let resolvedTail = '' ;
188188 let resolvedAbsolute = false ;
189+ let slashCheck = false ;
189190
190191 for ( let i = args . length - 1 ; i >= - 1 ; i -- ) {
191192 let path ;
@@ -217,6 +218,10 @@ const win32 = {
217218 }
218219 }
219220
221+ if ( i === args . length - 1 &&
222+ isPathSeparator ( StringPrototypeCharCodeAt ( path , path . length - 1 ) ) ) {
223+ slashCheck = true ;
224+ }
220225 const len = path . length ;
221226 let rootEnd = 0 ;
222227 let device = '' ;
@@ -263,11 +268,17 @@ const win32 = {
263268 ! isPathSeparator ( StringPrototypeCharCodeAt ( path , j ) ) ) {
264269 j ++ ;
265270 }
266- if ( j === len || j !== last ) {
267- // We matched a UNC root
268- device =
269- `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
270- rootEnd = j ;
271+ if ( ( j === len || j !== last ) ) {
272+ if ( firstPart !== '.' && firstPart !== '?' ) {
273+ // We matched a UNC root
274+ device =
275+ `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
276+ rootEnd = j ;
277+ } else {
278+ // We matched a device root (e.g. \\\\.\\PHYSICALDRIVE0)
279+ device = `\\\\${ firstPart } ` ;
280+ rootEnd = 4 ;
281+ }
271282 }
272283 }
273284 }
@@ -319,9 +330,21 @@ const win32 = {
319330 resolvedTail = normalizeString ( resolvedTail , ! resolvedAbsolute , '\\' ,
320331 isPathSeparator ) ;
321332
322- return resolvedAbsolute ?
323- `${ resolvedDevice } \\${ resolvedTail } ` :
324- `${ resolvedDevice } ${ resolvedTail } ` || '.' ;
333+ if ( ! resolvedAbsolute ) {
334+ return `${ resolvedDevice } ${ resolvedTail } ` || '.' ;
335+ }
336+
337+ if ( resolvedTail . length === 0 ) {
338+ return slashCheck ? `${ resolvedDevice } \\` : resolvedDevice ;
339+ }
340+
341+ if ( slashCheck ) {
342+ return resolvedTail === '\\' ?
343+ `${ resolvedDevice } \\` :
344+ `${ resolvedDevice } \\${ resolvedTail } \\` ;
345+ }
346+
347+ return `${ resolvedDevice } \\${ resolvedTail } ` ;
325348 } ,
326349
327350 /**
@@ -377,17 +400,22 @@ const win32 = {
377400 ! isPathSeparator ( StringPrototypeCharCodeAt ( path , j ) ) ) {
378401 j ++ ;
379402 }
380- if ( j === len ) {
381- // We matched a UNC root only
382- // Return the normalized version of the UNC root since there
383- // is nothing left to process
384- return `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last ) } \\` ;
385- }
386- if ( j !== last ) {
387- // We matched a UNC root with leftovers
388- device =
389- `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
390- rootEnd = j ;
403+ if ( j === len || j !== last ) {
404+ if ( firstPart === '.' || firstPart === '?' ) {
405+ // We matched a device root (e.g. \\\\.\\PHYSICALDRIVE0)
406+ device = `\\\\${ firstPart } ` ;
407+ rootEnd = 4 ;
408+ } else if ( j === len ) {
409+ // We matched a UNC root only
410+ // Return the normalized version of the UNC root since there
411+ // is nothing left to process
412+ return `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last ) } \\` ;
413+ } else {
414+ // We matched a UNC root with leftovers
415+ device =
416+ `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
417+ rootEnd = j ;
418+ }
391419 }
392420 }
393421 }
@@ -1122,6 +1150,7 @@ const posix = {
11221150 resolve ( ...args ) {
11231151 let resolvedPath = '' ;
11241152 let resolvedAbsolute = false ;
1153+ let slashCheck = false ;
11251154
11261155 for ( let i = args . length - 1 ; i >= - 1 && ! resolvedAbsolute ; i -- ) {
11271156 const path = i >= 0 ? args [ i ] : posixCwd ( ) ;
@@ -1131,8 +1160,17 @@ const posix = {
11311160 if ( path . length === 0 ) {
11321161 continue ;
11331162 }
1163+ if ( i === args . length - 1 &&
1164+ isPosixPathSeparator ( StringPrototypeCharCodeAt ( path ,
1165+ path . length - 1 ) ) ) {
1166+ slashCheck = true ;
1167+ }
11341168
1135- resolvedPath = `${ path } /${ resolvedPath } ` ;
1169+ if ( resolvedPath . length !== 0 ) {
1170+ resolvedPath = `${ path } /${ resolvedPath } ` ;
1171+ } else {
1172+ resolvedPath = path ;
1173+ }
11361174 resolvedAbsolute =
11371175 StringPrototypeCharCodeAt ( path , 0 ) === CHAR_FORWARD_SLASH ;
11381176 }
@@ -1144,10 +1182,20 @@ const posix = {
11441182 resolvedPath = normalizeString ( resolvedPath , ! resolvedAbsolute , '/' ,
11451183 isPosixPathSeparator ) ;
11461184
1147- if ( resolvedAbsolute ) {
1148- return `/${ resolvedPath } ` ;
1185+ if ( ! resolvedAbsolute ) {
1186+ if ( resolvedPath . length === 0 ) {
1187+ return '.' ;
1188+ }
1189+ if ( slashCheck ) {
1190+ return `${ resolvedPath } /` ;
1191+ }
1192+ return resolvedPath ;
1193+ }
1194+
1195+ if ( resolvedPath . length === 0 || resolvedPath === '/' ) {
1196+ return '/' ;
11491197 }
1150- return resolvedPath . length > 0 ? resolvedPath : '.' ;
1198+ return slashCheck ? `/ ${ resolvedPath } /` : `/ ${ resolvedPath } ` ;
11511199 } ,
11521200
11531201 /**
@@ -1231,11 +1279,35 @@ const posix = {
12311279 if ( from === to )
12321280 return '' ;
12331281
1234- const fromStart = 1 ;
1235- const fromEnd = from . length ;
1282+ // Trim any leading slashes
1283+ let fromStart = 0 ;
1284+ while ( fromStart < from . length &&
1285+ StringPrototypeCharCodeAt ( from , fromStart ) === CHAR_FORWARD_SLASH ) {
1286+ fromStart ++ ;
1287+ }
1288+ // Trim trailing slashes
1289+ let fromEnd = from . length ;
1290+ while (
1291+ fromEnd - 1 > fromStart &&
1292+ StringPrototypeCharCodeAt ( from , fromEnd - 1 ) === CHAR_FORWARD_SLASH
1293+ ) {
1294+ fromEnd -- ;
1295+ }
12361296 const fromLen = fromEnd - fromStart ;
1237- const toStart = 1 ;
1238- const toLen = to . length - toStart ;
1297+
1298+ // Trim any leading slashes
1299+ let toStart = 0 ;
1300+ while ( toStart < to . length &&
1301+ StringPrototypeCharCodeAt ( to , toStart ) === CHAR_FORWARD_SLASH ) {
1302+ toStart ++ ;
1303+ }
1304+ // Trim trailing slashes
1305+ let toEnd = to . length ;
1306+ while ( toEnd - 1 > toStart &&
1307+ StringPrototypeCharCodeAt ( to , toEnd - 1 ) === CHAR_FORWARD_SLASH ) {
1308+ toEnd -- ;
1309+ }
1310+ const toLen = toEnd - toStart ;
12391311
12401312 // Compare paths to find the longest common path from root
12411313 const length = ( fromLen < toLen ? fromLen : toLen ) ;
0 commit comments