Fix Node's path.format() requiring all options (#22525)

* Fix Node's path.format() requiring all options

The bug manifested during use of path.win32 and path.posix submodules.
Both functions required all of the options to exist which is not
the case as some override others.
Further research indicated that previous Node versions were also
susceptible, up to v4.

I've used the Typescript's Partial<> type which makes all properties
optional, allowing for easier maintenance and readabillity at the cost
of now requiring Typescript 2.1, which I believe is a good trade-off.

* Revert the decision to use TS 2.1 in @types/node

Updating Typescript version would require to update
all other types that rely on Node, which may be quite a lot.
Fixing a bug is not a big enough reason to add new version to all
of those packages.
This commit is contained in:
Olaf Tomalka
2018-01-03 14:44:18 -08:00
committed by Mohamed Hegazy
parent 34f8553e46
commit abb6cbad16
8 changed files with 238 additions and 12 deletions
+2 -3
View File
@@ -4580,7 +4580,6 @@ declare module "path" {
*/
name: string;
}
export interface FormatInputPathObject {
/**
* The root of the path such as '/' or 'c:\'
@@ -4693,7 +4692,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
export module win32 {
@@ -4708,7 +4707,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
}
+40
View File
@@ -1759,6 +1759,46 @@ namespace path_tests {
});
// returns
// '/home/user/dir/file.txt'
path.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
root: "/",
dir: "/home/user/dir",
base: "file.txt",
ext: ".txt",
name: "file"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.win32.format({
root: "C:\\",
dir: "C:\\home\\user\\dir",
ext: ".txt",
name: "file"
});
// returns
// 'C:\home\user\dir\file.txt'
path.win32.format({
dir: "C:\\home\\user\\dir",
base: "file.txt"
});
// returns
// 'C:\home\user\dir\file.txt'
}
////////////////////////////////////////////////////
+25 -3
View File
@@ -1993,6 +1993,28 @@ declare module "path" {
*/
name: string;
}
export interface FormatInputPathObject {
/**
* The root of the path such as '/' or 'c:\'
*/
root?: string;
/**
* The full directory path such as '/home/user/dir' or 'c:\path\dir'
*/
dir?: string;
/**
* The file name including extension (if any) such as 'index.html'
*/
base?: string;
/**
* The file extension (if any) such as '.html'
*/
ext?: string;
/**
* The file name without extension (if any) such as 'index'
*/
name?: string;
}
/**
* Normalize a string path, reducing '..' and '.' parts.
@@ -2076,7 +2098,7 @@ declare module "path" {
*
* @param pathString path to evaluate.
*/
export function format(pathObject: ParsedPath): string;
export function format(pathObject: FormatInputPathObject): string;
export module posix {
export function normalize(p: string): string;
@@ -2090,7 +2112,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
export module win32 {
@@ -2105,7 +2127,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
}
+40
View File
@@ -772,6 +772,46 @@ namespace path_tests {
});
// returns
// '/home/user/dir/file.txt'
path.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
root: "/",
dir: "/home/user/dir",
base: "file.txt",
ext: ".txt",
name: "file"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.win32.format({
root: "C:\\",
dir: "C:\\home\\user\\dir",
ext: ".txt",
name: "file"
});
// returns
// 'C:\home\user\dir\file.txt'
path.win32.format({
dir: "C:\\home\\user\\dir",
base: "file.txt"
});
// returns
// 'C:\home\user\dir\file.txt'
}
////////////////////////////////////////////////////
+25 -3
View File
@@ -2875,6 +2875,28 @@ declare module "path" {
*/
name: string;
}
export interface FormatInputPathObject {
/**
* The root of the path such as '/' or 'c:\'
*/
root?: string;
/**
* The full directory path such as '/home/user/dir' or 'c:\path\dir'
*/
dir?: string;
/**
* The file name including extension (if any) such as 'index.html'
*/
base?: string;
/**
* The file extension (if any) such as '.html'
*/
ext?: string;
/**
* The file name without extension (if any) such as 'index'
*/
name?: string;
}
/**
* Normalize a string path, reducing '..' and '.' parts.
@@ -2951,7 +2973,7 @@ declare module "path" {
*
* @param pathString path to evaluate.
*/
export function format(pathObject: ParsedPath): string;
export function format(pathObject: FormatInputPathObject): string;
export module posix {
export function normalize(p: string): string;
@@ -2965,7 +2987,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
export module win32 {
@@ -2980,7 +3002,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
}
+41
View File
@@ -1282,6 +1282,47 @@ namespace path_tests {
});
// returns
// '/home/user/dir/file.txt'
path.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
root: "/",
dir: "/home/user/dir",
base: "file.txt",
ext: ".txt",
name: "file"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.win32.format({
root: "C:\\",
dir: "C:\\home\\user\\dir",
ext: ".txt",
name: "file"
});
// returns
// 'C:\home\user\dir\file.txt'
path.win32.format({
dir: "C:\\home\\user\\dir",
base: "file.txt"
});
// returns
// 'C:\home\user\dir\file.txt'
}
////////////////////////////////////////////////////
+25 -3
View File
@@ -3005,6 +3005,28 @@ declare module "path" {
*/
name: string;
}
export interface FormatInputPathObject {
/**
* The root of the path such as '/' or 'c:\'
*/
root?: string;
/**
* The full directory path such as '/home/user/dir' or 'c:\path\dir'
*/
dir?: string;
/**
* The file name including extension (if any) such as 'index.html'
*/
base?: string;
/**
* The file extension (if any) such as '.html'
*/
ext?: string;
/**
* The file name without extension (if any) such as 'index'
*/
name?: string;
}
/**
* Normalize a string path, reducing '..' and '.' parts.
@@ -3081,7 +3103,7 @@ declare module "path" {
*
* @param pathString path to evaluate.
*/
export function format(pathObject: ParsedPath): string;
export function format(pathObject: FormatInputPathObject): string;
export module posix {
export function normalize(p: string): string;
@@ -3095,7 +3117,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
export module win32 {
@@ -3110,7 +3132,7 @@ declare module "path" {
export var sep: string;
export var delimiter: string;
export function parse(p: string): ParsedPath;
export function format(pP: ParsedPath): string;
export function format(pP: FormatInputPathObject): string;
}
}
+40
View File
@@ -1379,6 +1379,46 @@ namespace path_tests {
});
// returns
// '/home/user/dir/file.txt'
path.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
root: "/",
dir: "/home/user/dir",
base: "file.txt",
ext: ".txt",
name: "file"
});
// returns
// '/home/user/dir/file.txt'
path.posix.format({
dir: "/home/user/dir",
base: "file.txt"
});
// returns
// '/home/user/dir/file.txt'
path.win32.format({
root: "C:\\",
dir: "C:\\home\\user\\dir",
ext: ".txt",
name: "file"
});
// returns
// 'C:\home\user\dir\file.txt'
path.win32.format({
dir: "C:\\home\\user\\dir",
base: "file.txt"
});
// returns
// 'C:\home\user\dir\file.txt'
}
////////////////////////////////////////////////////