From 47c18e57961cf32e5c2ede9137395f85123a9dc8 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Mon, 19 Feb 2024 18:43:47 +0100 Subject: [PATCH] refactor(path): prepare for noUncheckedIndexedAccess (#4040) --- path/_common/assert_path.ts | 2 +- path/_common/glob_to_reg_exp.ts | 12 +++++++----- path/posix/join.ts | 2 +- path/posix/resolve.ts | 2 +- path/windows/join.ts | 2 +- path/windows/resolve.ts | 2 +- path/windows/to_file_url.ts | 4 +++- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/path/_common/assert_path.ts b/path/_common/assert_path.ts index abd021fd472a5..7033edcd1a79b 100644 --- a/path/_common/assert_path.ts +++ b/path/_common/assert_path.ts @@ -1,7 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright the Browserify authors. MIT License. -export function assertPath(path: string) { +export function assertPath(path?: string) { if (typeof path !== "string") { throw new TypeError( `Path must be a string. Received ${JSON.stringify(path)}`, diff --git a/path/_common/glob_to_reg_exp.ts b/path/_common/glob_to_reg_exp.ts index 0a3dfbb4f7d54..26e8c82a054fa 100644 --- a/path/_common/glob_to_reg_exp.ts +++ b/path/_common/glob_to_reg_exp.ts @@ -65,7 +65,7 @@ export function _globToRegExp( // Remove trailing separators. let newLength = glob.length; - for (; newLength > 1 && c.seps.includes(glob[newLength - 1]); newLength--); + for (; newLength > 1 && c.seps.includes(glob[newLength - 1]!); newLength--); glob = glob.slice(0, newLength); let regExpString = ""; @@ -80,11 +80,11 @@ export function _globToRegExp( let i = j; // Terminates with `i` at the non-inclusive end of the current segment. - for (; i < glob.length && !c.seps.includes(glob[i]); i++) { + for (; i < glob.length && !c.seps.includes(glob[i]!); i++) { if (inEscape) { inEscape = false; const escapeChars = inRange ? rangeEscapeChars : regExpEscapeChars; - segment += escapeChars.includes(glob[i]) ? `\\${glob[i]}` : glob[i]; + segment += escapeChars.includes(glob[i]!) ? `\\${glob[i]}` : glob[i]; continue; } @@ -247,7 +247,9 @@ export function _globToRegExp( continue; } - segment += regExpEscapeChars.includes(glob[i]) ? `\\${glob[i]}` : glob[i]; + segment += regExpEscapeChars.includes(glob[i]!) + ? `\\${glob[i]}` + : glob[i]; } // Check for unclosed groups or a dangling backslash. @@ -267,7 +269,7 @@ export function _globToRegExp( } // Terminates with `i` at the start of the next segment. - while (c.seps.includes(glob[i])) i++; + while (c.seps.includes(glob[i]!)) i++; // Check that the next value of `j` is indeed higher than the current value. if (!(i > j)) { diff --git a/path/posix/join.ts b/path/posix/join.ts index 0f7b152f60442..ad4bbae55d8e6 100644 --- a/path/posix/join.ts +++ b/path/posix/join.ts @@ -13,7 +13,7 @@ export function join(...paths: string[]): string { let joined: string | undefined; for (let i = 0, len = paths.length; i < len; ++i) { - const path = paths[i]; + const path = paths[i]!; assertPath(path); if (path.length > 0) { if (!joined) joined = path; diff --git a/path/posix/resolve.ts b/path/posix/resolve.ts index 50022c0531ffd..196a08a568baf 100644 --- a/path/posix/resolve.ts +++ b/path/posix/resolve.ts @@ -16,7 +16,7 @@ export function resolve(...pathSegments: string[]): string { for (let i = pathSegments.length - 1; i >= -1 && !resolvedAbsolute; i--) { let path: string; - if (i >= 0) path = pathSegments[i]; + if (i >= 0) path = pathSegments[i]!; else { // deno-lint-ignore no-explicit-any const { Deno } = globalThis as any; diff --git a/path/windows/join.ts b/path/windows/join.ts index 59f6777c50189..b17493dee81a3 100644 --- a/path/windows/join.ts +++ b/path/windows/join.ts @@ -16,7 +16,7 @@ export function join(...paths: string[]): string { let joined: string | undefined; let firstPart: string | null = null; for (let i = 0; i < paths.length; ++i) { - const path = paths[i]; + const path = paths[i]!; assertPath(path); if (path.length > 0) { if (joined === undefined) joined = firstPart = path; diff --git a/path/windows/resolve.ts b/path/windows/resolve.ts index 1732ff6cb293a..c34f1e8c668fa 100644 --- a/path/windows/resolve.ts +++ b/path/windows/resolve.ts @@ -20,7 +20,7 @@ export function resolve(...pathSegments: string[]): string { // deno-lint-ignore no-explicit-any const { Deno } = globalThis as any; if (i >= 0) { - path = pathSegments[i]; + path = pathSegments[i]!; } else if (!resolvedDevice) { if (typeof Deno?.cwd !== "function") { throw new TypeError("Resolved a drive-letter-less path without a CWD."); diff --git a/path/windows/to_file_url.ts b/path/windows/to_file_url.ts index a3d52c5857cff..f7f5ac3d4692e 100644 --- a/path/windows/to_file_url.ts +++ b/path/windows/to_file_url.ts @@ -20,10 +20,12 @@ export function toFileUrl(path: string): URL { if (!isAbsolute(path)) { throw new TypeError("Must be an absolute path."); } - const [, hostname, pathname] = path.match( /^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/, )!; + if (!pathname) { + throw new TypeError("Invalid pathname."); + } const url = new URL("file:///"); url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25")); if (hostname !== undefined && hostname !== "localhost") {