Skip to content

Commit

Permalink
refactor(path): prepare for noUncheckedIndexedAccess (denoland#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
javihernant committed Feb 20, 2024
1 parent a828744 commit 90d28a9
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion path/_common/assert_path.ts
Original file line number Diff line number Diff line change
@@ -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)}`,
Expand Down
12 changes: 7 additions & 5 deletions path/_common/glob_to_reg_exp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion path/posix/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion path/posix/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion path/windows/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion path/windows/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
3 changes: 1 addition & 2 deletions path/windows/to_file_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 90d28a9

Please sign in to comment.