From abb6cbad16ac7803175ee07dd55591d515516620 Mon Sep 17 00:00:00 2001 From: Olaf Tomalka Date: Wed, 3 Jan 2018 23:44:18 +0100 Subject: [PATCH] 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. --- types/node/index.d.ts | 5 ++--- types/node/node-tests.ts | 40 ++++++++++++++++++++++++++++++++++++ types/node/v4/index.d.ts | 28 ++++++++++++++++++++++--- types/node/v4/node-tests.ts | 40 ++++++++++++++++++++++++++++++++++++ types/node/v6/index.d.ts | 28 ++++++++++++++++++++++--- types/node/v6/node-tests.ts | 41 +++++++++++++++++++++++++++++++++++++ types/node/v7/index.d.ts | 28 ++++++++++++++++++++++--- types/node/v7/node-tests.ts | 40 ++++++++++++++++++++++++++++++++++++ 8 files changed, 238 insertions(+), 12 deletions(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index fbd0a4c211..d474a1045a 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -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; } } diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index 1ed9332e94..e0c2b22b10 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -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' } //////////////////////////////////////////////////// diff --git a/types/node/v4/index.d.ts b/types/node/v4/index.d.ts index eefbf46bef..259259afd0 100644 --- a/types/node/v4/index.d.ts +++ b/types/node/v4/index.d.ts @@ -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; } } diff --git a/types/node/v4/node-tests.ts b/types/node/v4/node-tests.ts index 431dfb0520..2bb28ce779 100644 --- a/types/node/v4/node-tests.ts +++ b/types/node/v4/node-tests.ts @@ -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' } //////////////////////////////////////////////////// diff --git a/types/node/v6/index.d.ts b/types/node/v6/index.d.ts index 1a697551a4..0c84770e93 100644 --- a/types/node/v6/index.d.ts +++ b/types/node/v6/index.d.ts @@ -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; } } diff --git a/types/node/v6/node-tests.ts b/types/node/v6/node-tests.ts index 171b92ba1a..34a62bf88d 100644 --- a/types/node/v6/node-tests.ts +++ b/types/node/v6/node-tests.ts @@ -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' } //////////////////////////////////////////////////// diff --git a/types/node/v7/index.d.ts b/types/node/v7/index.d.ts index 25c120ad3c..f0766a2da2 100644 --- a/types/node/v7/index.d.ts +++ b/types/node/v7/index.d.ts @@ -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; } } diff --git a/types/node/v7/node-tests.ts b/types/node/v7/node-tests.ts index f0878cc6fc..a2f25db8ec 100644 --- a/types/node/v7/node-tests.ts +++ b/types/node/v7/node-tests.ts @@ -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' } ////////////////////////////////////////////////////