@@ -190,6 +190,7 @@ const win32 = {
190
190
let resolvedDevice = '' ;
191
191
let resolvedTail = '' ;
192
192
let resolvedAbsolute = false ;
193
+ let slashCheck = false ;
193
194
194
195
for ( let i = args . length - 1 ; i >= - 1 ; i -- ) {
195
196
let path ;
@@ -221,6 +222,10 @@ const win32 = {
221
222
}
222
223
}
223
224
225
+ if ( i === args . length - 1 &&
226
+ isPathSeparator ( StringPrototypeCharCodeAt ( path , path . length - 1 ) ) ) {
227
+ slashCheck = true ;
228
+ }
224
229
const len = path . length ;
225
230
let rootEnd = 0 ;
226
231
let device = '' ;
@@ -268,10 +273,16 @@ const win32 = {
268
273
j ++ ;
269
274
}
270
275
if ( j === len || j !== last ) {
271
- // We matched a UNC root
272
- device =
273
- `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
274
- rootEnd = j ;
276
+ if ( firstPart !== '.' && firstPart !== '?' ) {
277
+ // We matched a UNC root
278
+ device =
279
+ `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
280
+ rootEnd = j ;
281
+ } else {
282
+ // We matched a device root (e.g. \\\\.\\PHYSICALDRIVE0)
283
+ device = `\\\\${ firstPart } ` ;
284
+ rootEnd = 4 ;
285
+ }
275
286
}
276
287
}
277
288
}
@@ -323,9 +334,21 @@ const win32 = {
323
334
resolvedTail = normalizeString ( resolvedTail , ! resolvedAbsolute , '\\' ,
324
335
isPathSeparator ) ;
325
336
326
- return resolvedAbsolute ?
327
- `${ resolvedDevice } \\${ resolvedTail } ` :
328
- `${ resolvedDevice } ${ resolvedTail } ` || '.' ;
337
+ if ( ! resolvedAbsolute ) {
338
+ return `${ resolvedDevice } ${ resolvedTail } ` || '.' ;
339
+ }
340
+
341
+ if ( resolvedTail . length === 0 ) {
342
+ return slashCheck ? `${ resolvedDevice } \\` : resolvedDevice ;
343
+ }
344
+
345
+ if ( slashCheck ) {
346
+ return resolvedTail === '\\' ?
347
+ `${ resolvedDevice } \\` :
348
+ `${ resolvedDevice } \\${ resolvedTail } \\` ;
349
+ }
350
+
351
+ return `${ resolvedDevice } \\${ resolvedTail } ` ;
329
352
} ,
330
353
331
354
/**
@@ -381,17 +404,22 @@ const win32 = {
381
404
! isPathSeparator ( StringPrototypeCharCodeAt ( path , j ) ) ) {
382
405
j ++ ;
383
406
}
384
- if ( j === len ) {
385
- // We matched a UNC root only
386
- // Return the normalized version of the UNC root since there
387
- // is nothing left to process
388
- return `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last ) } \\` ;
389
- }
390
- if ( j !== last ) {
391
- // We matched a UNC root with leftovers
392
- device =
393
- `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
394
- rootEnd = j ;
407
+ if ( j === len || j !== last ) {
408
+ if ( firstPart === '.' || firstPart === '?' ) {
409
+ // We matched a device root (e.g. \\\\.\\PHYSICALDRIVE0)
410
+ device = `\\\\${ firstPart } ` ;
411
+ rootEnd = 4 ;
412
+ } else if ( j === len ) {
413
+ // We matched a UNC root only
414
+ // Return the normalized version of the UNC root since there
415
+ // is nothing left to process
416
+ return `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last ) } \\` ;
417
+ } else {
418
+ // We matched a UNC root with leftovers
419
+ device =
420
+ `\\\\${ firstPart } \\${ StringPrototypeSlice ( path , last , j ) } ` ;
421
+ rootEnd = j ;
422
+ }
395
423
}
396
424
}
397
425
}
@@ -1162,6 +1190,7 @@ const posix = {
1162
1190
resolve ( ...args ) {
1163
1191
let resolvedPath = '' ;
1164
1192
let resolvedAbsolute = false ;
1193
+ let slashCheck = false ;
1165
1194
1166
1195
for ( let i = args . length - 1 ; i >= - 1 && ! resolvedAbsolute ; i -- ) {
1167
1196
const path = i >= 0 ? args [ i ] : posixCwd ( ) ;
@@ -1171,8 +1200,17 @@ const posix = {
1171
1200
if ( path . length === 0 ) {
1172
1201
continue ;
1173
1202
}
1203
+ if ( i === args . length - 1 &&
1204
+ isPosixPathSeparator ( StringPrototypeCharCodeAt ( path ,
1205
+ path . length - 1 ) ) ) {
1206
+ slashCheck = true ;
1207
+ }
1174
1208
1175
- resolvedPath = `${ path } /${ resolvedPath } ` ;
1209
+ if ( resolvedPath . length !== 0 ) {
1210
+ resolvedPath = `${ path } /${ resolvedPath } ` ;
1211
+ } else {
1212
+ resolvedPath = path ;
1213
+ }
1176
1214
resolvedAbsolute =
1177
1215
StringPrototypeCharCodeAt ( path , 0 ) === CHAR_FORWARD_SLASH ;
1178
1216
}
@@ -1184,10 +1222,20 @@ const posix = {
1184
1222
resolvedPath = normalizeString ( resolvedPath , ! resolvedAbsolute , '/' ,
1185
1223
isPosixPathSeparator ) ;
1186
1224
1187
- if ( resolvedAbsolute ) {
1188
- return `/${ resolvedPath } ` ;
1225
+ if ( ! resolvedAbsolute ) {
1226
+ if ( resolvedPath . length === 0 ) {
1227
+ return '.' ;
1228
+ }
1229
+ if ( slashCheck ) {
1230
+ return `${ resolvedPath } /` ;
1231
+ }
1232
+ return resolvedPath ;
1233
+ }
1234
+
1235
+ if ( resolvedPath . length === 0 || resolvedPath === '/' ) {
1236
+ return '/' ;
1189
1237
}
1190
- return resolvedPath . length > 0 ? resolvedPath : '.' ;
1238
+ return slashCheck ? `/ ${ resolvedPath } /` : `/ ${ resolvedPath } ` ;
1191
1239
} ,
1192
1240
1193
1241
/**
@@ -1271,11 +1319,35 @@ const posix = {
1271
1319
if ( from === to )
1272
1320
return '' ;
1273
1321
1274
- const fromStart = 1 ;
1275
- const fromEnd = from . length ;
1322
+ // Trim any leading slashes
1323
+ let fromStart = 0 ;
1324
+ while ( fromStart < from . length &&
1325
+ StringPrototypeCharCodeAt ( from , fromStart ) === CHAR_FORWARD_SLASH ) {
1326
+ fromStart ++ ;
1327
+ }
1328
+ // Trim trailing slashes
1329
+ let fromEnd = from . length ;
1330
+ while (
1331
+ fromEnd - 1 > fromStart &&
1332
+ StringPrototypeCharCodeAt ( from , fromEnd - 1 ) === CHAR_FORWARD_SLASH
1333
+ ) {
1334
+ fromEnd -- ;
1335
+ }
1276
1336
const fromLen = fromEnd - fromStart ;
1277
- const toStart = 1 ;
1278
- const toLen = to . length - toStart ;
1337
+
1338
+ // Trim any leading slashes
1339
+ let toStart = 0 ;
1340
+ while ( toStart < to . length &&
1341
+ StringPrototypeCharCodeAt ( to , toStart ) === CHAR_FORWARD_SLASH ) {
1342
+ toStart ++ ;
1343
+ }
1344
+ // Trim trailing slashes
1345
+ let toEnd = to . length ;
1346
+ while ( toEnd - 1 > toStart &&
1347
+ StringPrototypeCharCodeAt ( to , toEnd - 1 ) === CHAR_FORWARD_SLASH ) {
1348
+ toEnd -- ;
1349
+ }
1350
+ const toLen = toEnd - toStart ;
1279
1351
1280
1352
// Compare paths to find the longest common path from root
1281
1353
const length = ( fromLen < toLen ? fromLen : toLen ) ;
0 commit comments