1
1
'use strict' ;
2
2
3
+ const util = require ( 'util' ) ;
3
4
const isWindows = process . platform === 'win32' ;
4
5
6
+ function assertPath ( path ) {
7
+ if ( typeof path !== 'string' )
8
+ throw new TypeError ( 'Path must be a string. Received ' +
9
+ util . inspect ( path ) ) ;
10
+ }
11
+
5
12
// resolves . and .. elements in a path array with directory names there
6
13
// must be no slashes or device names (c:\) in the array
7
14
// (so also no leading and trailing slashes - it does not distinguish
@@ -84,10 +91,10 @@ win32.resolve = function() {
84
91
}
85
92
}
86
93
87
- // Skip empty and invalid entries
88
- if ( typeof path !== 'string' ) {
89
- throw new TypeError ( 'Arguments to path.resolve must be strings' ) ;
90
- } else if ( ! path ) {
94
+ assertPath ( path ) ;
95
+
96
+ // Skip empty entries
97
+ if ( path === '' ) {
91
98
continue ;
92
99
}
93
100
@@ -137,6 +144,8 @@ win32.resolve = function() {
137
144
138
145
139
146
win32 . normalize = function ( path ) {
147
+ assertPath ( path ) ;
148
+
140
149
var result = splitDeviceRe . exec ( path ) ,
141
150
device = result [ 1 ] || '' ,
142
151
isUnc = device && device . charAt ( 1 ) !== ':' ,
@@ -165,6 +174,8 @@ win32.normalize = function(path) {
165
174
166
175
167
176
win32 . isAbsolute = function ( path ) {
177
+ assertPath ( path ) ;
178
+
168
179
var result = splitDeviceRe . exec ( path ) ,
169
180
device = result [ 1 ] || '' ,
170
181
isUnc = ! ! device && device . charAt ( 1 ) !== ':' ;
@@ -210,6 +221,9 @@ win32.join = function() {
210
221
// to = 'C:\\orandea\\impl\\bbb'
211
222
// The output of the function should be: '..\\..\\impl\\bbb'
212
223
win32 . relative = function ( from , to ) {
224
+ assertPath ( from ) ;
225
+ assertPath ( to ) ;
226
+
213
227
from = win32 . resolve ( from ) ;
214
228
to = win32 . resolve ( to ) ;
215
229
@@ -287,6 +301,8 @@ win32._makeLong = function(path) {
287
301
288
302
289
303
win32 . dirname = function ( path ) {
304
+ assertPath ( path ) ;
305
+
290
306
var result = win32SplitPath ( path ) ,
291
307
root = result [ 0 ] ,
292
308
dir = result [ 1 ] ;
@@ -306,6 +322,11 @@ win32.dirname = function(path) {
306
322
307
323
308
324
win32 . basename = function ( path , ext ) {
325
+ assertPath ( path ) ;
326
+
327
+ if ( ext !== undefined && typeof ext !== 'string' )
328
+ throw new TypeError ( 'ext must be a string' ) ;
329
+
309
330
var f = win32SplitPath ( path ) [ 2 ] ;
310
331
// TODO: make this comparison case-insensitive on windows?
311
332
if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
@@ -316,6 +337,7 @@ win32.basename = function(path, ext) {
316
337
317
338
318
339
win32 . extname = function ( path ) {
340
+ assertPath ( path ) ;
319
341
return win32SplitPath ( path ) [ 3 ] ;
320
342
} ;
321
343
@@ -351,11 +373,8 @@ win32.format = function(pathObject) {
351
373
352
374
353
375
win32 . parse = function ( pathString ) {
354
- if ( typeof pathString !== 'string' ) {
355
- throw new TypeError (
356
- "Parameter 'pathString' must be a string, not " + typeof pathString
357
- ) ;
358
- }
376
+ assertPath ( pathString ) ;
377
+
359
378
var allParts = win32SplitPath ( pathString ) ;
360
379
if ( ! allParts || allParts . length !== 4 ) {
361
380
throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
@@ -395,10 +414,10 @@ posix.resolve = function() {
395
414
for ( var i = arguments . length - 1 ; i >= - 1 && ! resolvedAbsolute ; i -- ) {
396
415
var path = ( i >= 0 ) ? arguments [ i ] : process . cwd ( ) ;
397
416
398
- // Skip empty and invalid entries
399
- if ( typeof path !== 'string' ) {
400
- throw new TypeError ( 'Arguments to path.resolve must be strings' ) ;
401
- } else if ( ! path ) {
417
+ assertPath ( path ) ;
418
+
419
+ // Skip empty entries
420
+ if ( path === '' ) {
402
421
continue ;
403
422
}
404
423
@@ -419,6 +438,8 @@ posix.resolve = function() {
419
438
// path.normalize(path)
420
439
// posix version
421
440
posix . normalize = function ( path ) {
441
+ assertPath ( path ) ;
442
+
422
443
var isAbsolute = posix . isAbsolute ( path ) ,
423
444
trailingSlash = path . substr ( - 1 ) === '/' ;
424
445
@@ -437,6 +458,7 @@ posix.normalize = function(path) {
437
458
438
459
// posix version
439
460
posix . isAbsolute = function ( path ) {
461
+ assertPath ( path ) ;
440
462
return path . charAt ( 0 ) === '/' ;
441
463
} ;
442
464
@@ -463,6 +485,9 @@ posix.join = function() {
463
485
// path.relative(from, to)
464
486
// posix version
465
487
posix . relative = function ( from , to ) {
488
+ assertPath ( from ) ;
489
+ assertPath ( to ) ;
490
+
466
491
from = posix . resolve ( from ) . substr ( 1 ) ;
467
492
to = posix . resolve ( to ) . substr ( 1 ) ;
468
493
@@ -510,6 +535,8 @@ posix._makeLong = function(path) {
510
535
511
536
512
537
posix . dirname = function ( path ) {
538
+ assertPath ( path ) ;
539
+
513
540
var result = posixSplitPath ( path ) ,
514
541
root = result [ 0 ] ,
515
542
dir = result [ 1 ] ;
@@ -529,8 +556,13 @@ posix.dirname = function(path) {
529
556
530
557
531
558
posix . basename = function ( path , ext ) {
559
+ assertPath ( path ) ;
560
+
561
+ if ( ext !== undefined && typeof ext !== 'string' )
562
+ throw new TypeError ( 'ext must be a string' ) ;
563
+
532
564
var f = posixSplitPath ( path ) [ 2 ] ;
533
- // TODO: make this comparison case-insensitive on windows?
565
+
534
566
if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
535
567
f = f . substr ( 0 , f . length - ext . length ) ;
536
568
}
@@ -539,6 +571,7 @@ posix.basename = function(path, ext) {
539
571
540
572
541
573
posix . extname = function ( path ) {
574
+ assertPath ( path ) ;
542
575
return posixSplitPath ( path ) [ 3 ] ;
543
576
} ;
544
577
@@ -566,11 +599,8 @@ posix.format = function(pathObject) {
566
599
567
600
568
601
posix . parse = function ( pathString ) {
569
- if ( typeof pathString !== 'string' ) {
570
- throw new TypeError (
571
- "Parameter 'pathString' must be a string, not " + typeof pathString
572
- ) ;
573
- }
602
+ assertPath ( pathString ) ;
603
+
574
604
var allParts = posixSplitPath ( pathString ) ;
575
605
if ( ! allParts || allParts . length !== 4 ) {
576
606
throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
0 commit comments