11'use strict' ;
22
3+ const util = require ( 'util' ) ;
34const isWindows = process . platform === 'win32' ;
45
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+ }
12+
513// resolves . and .. elements in a path array with directory names there
614// must be no slashes or device names (c:\) in the array
715// (so also no leading and trailing slashes - it does not distinguish
@@ -84,10 +92,10 @@ win32.resolve = function() {
8492 }
8593 }
8694
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 ) {
95+ assertPath ( path ) ;
96+
97+ // Skip empty entries
98+ if ( path === '' ) {
9199 continue ;
92100 }
93101
@@ -137,6 +145,8 @@ win32.resolve = function() {
137145
138146
139147win32 . normalize = function ( path ) {
148+ assertPath ( path ) ;
149+
140150 var result = splitDeviceRe . exec ( path ) ,
141151 device = result [ 1 ] || '' ,
142152 isUnc = device && device . charAt ( 1 ) !== ':' ,
@@ -165,6 +175,8 @@ win32.normalize = function(path) {
165175
166176
167177win32 . isAbsolute = function ( path ) {
178+ assertPath ( path ) ;
179+
168180 var result = splitDeviceRe . exec ( path ) ,
169181 device = result [ 1 ] || '' ,
170182 isUnc = ! ! device && device . charAt ( 1 ) !== ':' ;
@@ -210,6 +222,9 @@ win32.join = function() {
210222// to = 'C:\\orandea\\impl\\bbb'
211223// The output of the function should be: '..\\..\\impl\\bbb'
212224win32 . relative = function ( from , to ) {
225+ assertPath ( from ) ;
226+ assertPath ( to ) ;
227+
213228 from = win32 . resolve ( from ) ;
214229 to = win32 . resolve ( to ) ;
215230
@@ -287,6 +302,8 @@ win32._makeLong = function(path) {
287302
288303
289304win32 . dirname = function ( path ) {
305+ assertPath ( path ) ;
306+
290307 var result = win32SplitPath ( path ) ,
291308 root = result [ 0 ] ,
292309 dir = result [ 1 ] ;
@@ -306,6 +323,11 @@ win32.dirname = function(path) {
306323
307324
308325win32 . basename = function ( path , ext ) {
326+ assertPath ( path ) ;
327+
328+ if ( ext !== undefined && typeof ext !== 'string' )
329+ throw new TypeError ( 'ext must be a string' ) ;
330+
309331 var f = win32SplitPath ( path ) [ 2 ] ;
310332 // TODO: make this comparison case-insensitive on windows?
311333 if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
@@ -316,6 +338,7 @@ win32.basename = function(path, ext) {
316338
317339
318340win32 . extname = function ( path ) {
341+ assertPath ( path ) ;
319342 return win32SplitPath ( path ) [ 3 ] ;
320343} ;
321344
@@ -351,11 +374,8 @@ win32.format = function(pathObject) {
351374
352375
353376win32 . parse = function ( pathString ) {
354- if ( typeof pathString !== 'string' ) {
355- throw new TypeError (
356- "Parameter 'pathString' must be a string, not " + typeof pathString
357- ) ;
358- }
377+ assertPath ( pathString ) ;
378+
359379 var allParts = win32SplitPath ( pathString ) ;
360380 if ( ! allParts || allParts . length !== 4 ) {
361381 throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
@@ -395,10 +415,10 @@ posix.resolve = function() {
395415 for ( var i = arguments . length - 1 ; i >= - 1 && ! resolvedAbsolute ; i -- ) {
396416 var path = ( i >= 0 ) ? arguments [ i ] : process . cwd ( ) ;
397417
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 ) {
418+ assertPath ( path ) ;
419+
420+ // Skip empty entries
421+ if ( path === '' ) {
402422 continue ;
403423 }
404424
@@ -419,6 +439,8 @@ posix.resolve = function() {
419439// path.normalize(path)
420440// posix version
421441posix . normalize = function ( path ) {
442+ assertPath ( path ) ;
443+
422444 var isAbsolute = posix . isAbsolute ( path ) ,
423445 trailingSlash = path . substr ( - 1 ) === '/' ;
424446
@@ -437,6 +459,7 @@ posix.normalize = function(path) {
437459
438460// posix version
439461posix . isAbsolute = function ( path ) {
462+ assertPath ( path ) ;
440463 return path . charAt ( 0 ) === '/' ;
441464} ;
442465
@@ -463,6 +486,9 @@ posix.join = function() {
463486// path.relative(from, to)
464487// posix version
465488posix . relative = function ( from , to ) {
489+ assertPath ( from ) ;
490+ assertPath ( to ) ;
491+
466492 from = posix . resolve ( from ) . substr ( 1 ) ;
467493 to = posix . resolve ( to ) . substr ( 1 ) ;
468494
@@ -510,6 +536,8 @@ posix._makeLong = function(path) {
510536
511537
512538posix . dirname = function ( path ) {
539+ assertPath ( path ) ;
540+
513541 var result = posixSplitPath ( path ) ,
514542 root = result [ 0 ] ,
515543 dir = result [ 1 ] ;
@@ -529,8 +557,13 @@ posix.dirname = function(path) {
529557
530558
531559posix . basename = function ( path , ext ) {
560+ assertPath ( path ) ;
561+
562+ if ( ext !== undefined && typeof ext !== 'string' )
563+ throw new TypeError ( 'ext must be a string' ) ;
564+
532565 var f = posixSplitPath ( path ) [ 2 ] ;
533- // TODO: make this comparison case-insensitive on windows?
566+
534567 if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
535568 f = f . substr ( 0 , f . length - ext . length ) ;
536569 }
@@ -539,6 +572,7 @@ posix.basename = function(path, ext) {
539572
540573
541574posix . extname = function ( path ) {
575+ assertPath ( path ) ;
542576 return posixSplitPath ( path ) [ 3 ] ;
543577} ;
544578
@@ -566,11 +600,8 @@ posix.format = function(pathObject) {
566600
567601
568602posix . parse = function ( pathString ) {
569- if ( typeof pathString !== 'string' ) {
570- throw new TypeError (
571- "Parameter 'pathString' must be a string, not " + typeof pathString
572- ) ;
573- }
603+ assertPath ( pathString ) ;
604+
574605 var allParts = posixSplitPath ( pathString ) ;
575606 if ( ! allParts || allParts . length !== 4 ) {
576607 throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
0 commit comments