Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: replace assertPath() with validator #24840

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ const {
CHAR_COLON,
CHAR_QUESTION_MARK,
} = require('internal/constants');

function assertPath(path) {
if (typeof path !== 'string') {
throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
}
}
const { validateString } = require('internal/validators');

function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
Expand Down Expand Up @@ -163,7 +158,7 @@ const win32 = {
}
}

assertPath(path);
validateString(path, 'path');

// Skip empty entries
if (path.length === 0) {
Expand Down Expand Up @@ -282,7 +277,7 @@ const win32 = {
},

normalize: function normalize(path) {
assertPath(path);
validateString(path, 'path');
const len = path.length;
if (len === 0)
return '.';
Expand Down Expand Up @@ -401,7 +396,7 @@ const win32 = {


isAbsolute: function isAbsolute(path) {
assertPath(path);
validateString(path, 'path');
const len = path.length;
if (len === 0)
return false;
Expand Down Expand Up @@ -429,7 +424,7 @@ const win32 = {
var firstPart;
for (var i = 0; i < arguments.length; ++i) {
var arg = arguments[i];
assertPath(arg);
validateString(arg, 'path');
if (arg.length > 0) {
if (joined === undefined)
joined = firstPart = arg;
Expand Down Expand Up @@ -494,8 +489,8 @@ const win32 = {
// to = 'C:\\orandea\\impl\\bbb'
// The output of the function should be: '..\\..\\impl\\bbb'
relative: function relative(from, to) {
assertPath(from);
assertPath(to);
validateString(from, 'from');
validateString(to, 'to');

if (from === to)
return '';
Expand Down Expand Up @@ -648,7 +643,7 @@ const win32 = {
},

dirname: function dirname(path) {
assertPath(path);
validateString(path, 'path');
const len = path.length;
if (len === 0)
return '.';
Expand Down Expand Up @@ -744,9 +739,9 @@ const win32 = {


basename: function basename(path, ext) {
if (ext !== undefined && typeof ext !== 'string')
throw new ERR_INVALID_ARG_TYPE('ext', 'string', ext);
assertPath(path);
if (ext !== undefined)
validateString(ext, 'ext');
validateString(path, 'path');
var start = 0;
var end = -1;
var matchedSlash = true;
Expand Down Expand Up @@ -832,7 +827,7 @@ const win32 = {


extname: function extname(path) {
assertPath(path);
validateString(path, 'path');
var start = 0;
var startDot = -1;
var startPart = 0;
Expand Down Expand Up @@ -905,7 +900,7 @@ const win32 = {


parse: function parse(path) {
assertPath(path);
validateString(path, 'path');

var ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0)
Expand Down Expand Up @@ -1082,7 +1077,7 @@ const posix = {
path = process.cwd();
}

assertPath(path);
validateString(path, 'path');

// Skip empty entries
if (path.length === 0) {
Expand Down Expand Up @@ -1114,7 +1109,7 @@ const posix = {


normalize: function normalize(path) {
assertPath(path);
validateString(path, 'path');

if (path.length === 0)
return '.';
Expand All @@ -1138,7 +1133,7 @@ const posix = {


isAbsolute: function isAbsolute(path) {
assertPath(path);
validateString(path, 'path');
return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
},

Expand All @@ -1149,7 +1144,7 @@ const posix = {
var joined;
for (var i = 0; i < arguments.length; ++i) {
var arg = arguments[i];
assertPath(arg);
validateString(arg, 'path');
if (arg.length > 0) {
if (joined === undefined)
joined = arg;
Expand All @@ -1164,8 +1159,8 @@ const posix = {


relative: function relative(from, to) {
assertPath(from);
assertPath(to);
validateString(from, 'from');
validateString(to, 'to');

if (from === to)
return '';
Expand Down Expand Up @@ -1262,7 +1257,7 @@ const posix = {
},

dirname: function dirname(path) {
assertPath(path);
validateString(path, 'path');
if (path.length === 0)
return '.';
const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
Expand All @@ -1289,9 +1284,9 @@ const posix = {


basename: function basename(path, ext) {
if (ext !== undefined && typeof ext !== 'string')
throw new ERR_INVALID_ARG_TYPE('ext', 'string', ext);
assertPath(path);
if (ext !== undefined)
validateString(ext, 'ext');
validateString(path, 'path');

var start = 0;
var end = -1;
Expand Down Expand Up @@ -1367,7 +1362,7 @@ const posix = {


extname: function extname(path) {
assertPath(path);
validateString(path, 'path');
var startDot = -1;
var startPart = 0;
var end = -1;
Expand Down Expand Up @@ -1428,7 +1423,7 @@ const posix = {


parse: function parse(path) {
assertPath(path);
validateString(path, 'path');

var ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0)
Expand Down