refactor(path): prepare for noUncheckedIndexedAccess (#4356)

refactor(path): prepare for noUncheckedIndexedAccess (#4040)
This commit is contained in:
Javier Hernández 2024-02-20 22:01:07 +01:00 committed by GitHub
parent fe84d285f7
commit 88cbc0f7b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 13 additions and 12 deletions

View File

@ -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)}`,

View File

@ -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)) {

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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.");

View File

@ -20,12 +20,11 @@ export function toFileUrl(path: string): URL {
if (!isAbsolute(path)) {
throw new TypeError("Must be an absolute path.");
}
const [, hostname, pathname] = path.match(
/^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/,
)!;
const url = new URL("file:///");
url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25"));
url.pathname = encodeWhitespace(pathname!.replace(/%/g, "%25"));
if (hostname !== undefined && hostname !== "localhost") {
url.hostname = hostname;
if (!url.hostname) {