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+
512// resolves . and .. elements in a path array with directory names there
613// must be no slashes or device names (c:\) in the array
714// (so also no leading and trailing slashes - it does not distinguish
@@ -84,10 +91,10 @@ win32.resolve = function() {
8491 }
8592 }
8693
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 === '' ) {
9198 continue ;
9299 }
93100
@@ -137,6 +144,8 @@ win32.resolve = function() {
137144
138145
139146win32 . normalize = function ( path ) {
147+ assertPath ( path ) ;
148+
140149 var result = splitDeviceRe . exec ( path ) ,
141150 device = result [ 1 ] || '' ,
142151 isUnc = device && device . charAt ( 1 ) !== ':' ,
@@ -165,6 +174,8 @@ win32.normalize = function(path) {
165174
166175
167176win32 . isAbsolute = function ( path ) {
177+ assertPath ( path ) ;
178+
168179 var result = splitDeviceRe . exec ( path ) ,
169180 device = result [ 1 ] || '' ,
170181 isUnc = ! ! device && device . charAt ( 1 ) !== ':' ;
@@ -210,6 +221,9 @@ win32.join = function() {
210221// to = 'C:\\orandea\\impl\\bbb'
211222// The output of the function should be: '..\\..\\impl\\bbb'
212223win32 . relative = function ( from , to ) {
224+ assertPath ( from ) ;
225+ assertPath ( to ) ;
226+
213227 from = win32 . resolve ( from ) ;
214228 to = win32 . resolve ( to ) ;
215229
@@ -287,6 +301,8 @@ win32._makeLong = function(path) {
287301
288302
289303win32 . dirname = function ( path ) {
304+ assertPath ( path ) ;
305+
290306 var result = win32SplitPath ( path ) ,
291307 root = result [ 0 ] ,
292308 dir = result [ 1 ] ;
@@ -306,6 +322,11 @@ win32.dirname = function(path) {
306322
307323
308324win32 . 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+
309330 var f = win32SplitPath ( path ) [ 2 ] ;
310331 // TODO: make this comparison case-insensitive on windows?
311332 if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
@@ -316,6 +337,7 @@ win32.basename = function(path, ext) {
316337
317338
318339win32 . extname = function ( path ) {
340+ assertPath ( path ) ;
319341 return win32SplitPath ( path ) [ 3 ] ;
320342} ;
321343
@@ -351,11 +373,8 @@ win32.format = function(pathObject) {
351373
352374
353375win32 . 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+
359378 var allParts = win32SplitPath ( pathString ) ;
360379 if ( ! allParts || allParts . length !== 4 ) {
361380 throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
@@ -395,10 +414,10 @@ posix.resolve = function() {
395414 for ( var i = arguments . length - 1 ; i >= - 1 && ! resolvedAbsolute ; i -- ) {
396415 var path = ( i >= 0 ) ? arguments [ i ] : process . cwd ( ) ;
397416
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 === '' ) {
402421 continue ;
403422 }
404423
@@ -419,6 +438,8 @@ posix.resolve = function() {
419438// path.normalize(path)
420439// posix version
421440posix . normalize = function ( path ) {
441+ assertPath ( path ) ;
442+
422443 var isAbsolute = posix . isAbsolute ( path ) ,
423444 trailingSlash = path . substr ( - 1 ) === '/' ;
424445
@@ -437,6 +458,7 @@ posix.normalize = function(path) {
437458
438459// posix version
439460posix . isAbsolute = function ( path ) {
461+ assertPath ( path ) ;
440462 return path . charAt ( 0 ) === '/' ;
441463} ;
442464
@@ -463,6 +485,9 @@ posix.join = function() {
463485// path.relative(from, to)
464486// posix version
465487posix . relative = function ( from , to ) {
488+ assertPath ( from ) ;
489+ assertPath ( to ) ;
490+
466491 from = posix . resolve ( from ) . substr ( 1 ) ;
467492 to = posix . resolve ( to ) . substr ( 1 ) ;
468493
@@ -510,6 +535,8 @@ posix._makeLong = function(path) {
510535
511536
512537posix . dirname = function ( path ) {
538+ assertPath ( path ) ;
539+
513540 var result = posixSplitPath ( path ) ,
514541 root = result [ 0 ] ,
515542 dir = result [ 1 ] ;
@@ -529,8 +556,13 @@ posix.dirname = function(path) {
529556
530557
531558posix . 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+
532564 var f = posixSplitPath ( path ) [ 2 ] ;
533- // TODO: make this comparison case-insensitive on windows?
565+
534566 if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
535567 f = f . substr ( 0 , f . length - ext . length ) ;
536568 }
@@ -539,6 +571,7 @@ posix.basename = function(path, ext) {
539571
540572
541573posix . extname = function ( path ) {
574+ assertPath ( path ) ;
542575 return posixSplitPath ( path ) [ 3 ] ;
543576} ;
544577
@@ -566,11 +599,8 @@ posix.format = function(pathObject) {
566599
567600
568601posix . 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+
574604 var allParts = posixSplitPath ( pathString ) ;
575605 if ( ! allParts || allParts . length !== 4 ) {
576606 throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
0 commit comments