Skip to content

Commit

Permalink
typings: add JSDoc types to lib/path
Browse files Browse the repository at this point in the history
PR-URL: #38186
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
Skn0tt authored and targos committed Apr 14, 2021
1 parent 05df701 commit f1a21e5
Showing 1 changed file with 105 additions and 6 deletions.
111 changes: 105 additions & 6 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
return res;
}

/**
* @param {string} sep
* @param {{
* dir?: string;
* root?: string;
* base?: string;
* name?: string;
* ext?: string;
* }} pathObject
* @returns {string}
*/
function _format(sep, pathObject) {
validateObject(pathObject, 'pathObject');
const dir = pathObject.dir || pathObject.root;
Expand All @@ -139,7 +150,11 @@ function _format(sep, pathObject) {
}

const win32 = {
// path.resolve([from ...], to)
/**
* path.resolve([from ...], to)
* @param {...string} args
* @returns {string}
*/
resolve(...args) {
let resolvedDevice = '';
let resolvedTail = '';
Expand Down Expand Up @@ -282,6 +297,10 @@ const win32 = {
`${resolvedDevice}${resolvedTail}` || '.';
},

/**
* @param {string} path
* @returns {string}
*/
normalize(path) {
validateString(path, 'path');
const len = path.length;
Expand Down Expand Up @@ -376,6 +395,10 @@ const win32 = {
return isAbsolute ? `${device}\\${tail}` : `${device}${tail}`;
},

/**
* @param {string} path
* @returns {boolean}
*/
isAbsolute(path) {
validateString(path, 'path');
const len = path.length;
Expand All @@ -391,6 +414,10 @@ const win32 = {
isPathSeparator(StringPrototypeCharCodeAt(path, 2)));
},

/**
* @param {...string} args
* @returns {string}
*/
join(...args) {
if (args.length === 0)
return '.';
Expand Down Expand Up @@ -457,10 +484,15 @@ const win32 = {
return win32.normalize(joined);
},

// It will solve the relative path from `from` to `to`, for instance:
// from = 'C:\\orandea\\test\\aaa'
// to = 'C:\\orandea\\impl\\bbb'
// The output of the function should be: '..\\..\\impl\\bbb'
/**
* It will solve the relative path from `from` to `to`, for instancee
* from = 'C:\\orandea\\test\\aaa'
* to = 'C:\\orandea\\impl\\bbb'
* The output of the function should be: '..\\..\\impl\\bbb'
* @param {string} from
* @param {string} to
* @returns {string}
*/
relative(from, to) {
validateString(from, 'from');
validateString(to, 'to');
Expand Down Expand Up @@ -618,6 +650,10 @@ const win32 = {
return path;
},

/**
* @param {string} path
* @returns {string}
*/
dirname(path) {
validateString(path, 'path');
const len = path.length;
Expand Down Expand Up @@ -709,6 +745,11 @@ const win32 = {
return StringPrototypeSlice(path, 0, end);
},

/**
* @param {string} path
* @param {string} [ext]
* @returns {string}
*/
basename(path, ext) {
if (ext !== undefined)
validateString(ext, 'ext');
Expand Down Expand Up @@ -792,6 +833,10 @@ const win32 = {
return StringPrototypeSlice(path, start, end);
},

/**
* @param {string} path
* @returns {string}
*/
extname(path) {
validateString(path, 'path');
let start = 0;
Expand Down Expand Up @@ -858,6 +903,16 @@ const win32 = {

format: FunctionPrototypeBind(_format, null, '\\'),

/**
* @param {string} path
* @returns {{
* dir: string;
* root: string;
* base: string;
* name: string;
* ext: string;
* }}
*/
parse(path) {
validateString(path, 'path');

Expand Down Expand Up @@ -1032,7 +1087,11 @@ const posixCwd = (() => {
})();

const posix = {
// path.resolve([from ...], to)
/**
* path.resolve([from ...], to)
* @param {...string} args
* @returns {string}
*/
resolve(...args) {
let resolvedPath = '';
let resolvedAbsolute = false;
Expand Down Expand Up @@ -1065,6 +1124,10 @@ const posix = {
return resolvedPath.length > 0 ? resolvedPath : '.';
},

/**
* @param {string} path
* @returns {string}
*/
normalize(path) {
validateString(path, 'path');

Expand All @@ -1090,12 +1153,20 @@ const posix = {
return isAbsolute ? `/${path}` : path;
},

/**
* @param {string} path
* @returns {boolean}
*/
isAbsolute(path) {
validateString(path, 'path');
return path.length > 0 &&
StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH;
},

/**
* @param {...string} args
* @returns {string}
*/
join(...args) {
if (args.length === 0)
return '.';
Expand All @@ -1115,6 +1186,11 @@ const posix = {
return posix.normalize(joined);
},

/**
* @param {string} from
* @param {string} to
* @returns {string}
*/
relative(from, to) {
validateString(from, 'from');
validateString(to, 'to');
Expand Down Expand Up @@ -1196,6 +1272,10 @@ const posix = {
return path;
},

/**
* @param {string} path
* @returns {string}
*/
dirname(path) {
validateString(path, 'path');
if (path.length === 0)
Expand All @@ -1222,6 +1302,11 @@ const posix = {
return StringPrototypeSlice(path, 0, end);
},

/**
* @param {string} path
* @param {string} [ext]
* @returns {string}
*/
basename(path, ext) {
if (ext !== undefined)
validateString(ext, 'ext');
Expand Down Expand Up @@ -1297,6 +1382,10 @@ const posix = {
return StringPrototypeSlice(path, start, end);
},

/**
* @param {string} path
* @returns {string}
*/
extname(path) {
validateString(path, 'path');
let startDot = -1;
Expand Down Expand Up @@ -1351,6 +1440,16 @@ const posix = {

format: FunctionPrototypeBind(_format, null, '/'),

/**
* @param {string} path
* @returns {{
* dir: string;
* root: string;
* base: string;
* name: string;
* ext: string;
* }}
*/
parse(path) {
validateString(path, 'path');

Expand Down

0 comments on commit f1a21e5

Please sign in to comment.