fix(fs): accept exts without leading period in walk[Sync]() (#5345)

This commit is contained in:
Asher Gomez 2024-07-08 10:41:32 +10:00 committed by GitHub
parent b6906a97bf
commit f222d807b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 6 deletions

View File

@ -136,7 +136,9 @@ export interface WalkOptions {
* If specified, entries without the file extension specified by this option
* are excluded.
*
* @default {undefined}
* File extensions with or without a leading period are accepted.
*
* @default {[]}
*/
exts?: string[];
/**
@ -433,8 +435,8 @@ export type { WalkEntry };
*
* @example Filter by file extensions
*
* Setting the `exts` option to `[".ts"]` will only include entries with the
* `.ts` file extension.
* Setting the `exts` option to `[".ts"]` or `["ts"]` will only include entries
* with the `.ts` file extension.
*
* File structure:
* ```
@ -516,7 +518,7 @@ export async function* walk(
root: string | URL,
options: WalkOptions = {},
): AsyncIterableIterator<WalkEntry> {
const {
let {
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
@ -532,6 +534,9 @@ export async function* walk(
return;
}
root = toPathString(root);
if (exts) {
exts = exts.map((ext) => ext.startsWith(".") ? ext : `.${ext}`);
}
if (includeDirs && include(root, exts, match, skip)) {
yield await createWalkEntry(root);
}
@ -856,8 +861,8 @@ export async function* walk(
*
* @example Filter by file extensions
*
* Setting the `exts` option to `[".ts"]` will only include entries with the
* `.ts` file extension.
* Setting the `exts` option to `[".ts"]` or `["ts"]` will only include entries
* with the `.ts` file extension.
*
* File structure:
* ```
@ -950,6 +955,9 @@ export function* walkSync(
}: WalkOptions = {},
): IterableIterator<WalkEntry> {
root = toPathString(root);
if (exts) {
exts = exts.map((ext) => ext.startsWith(".") ? ext : `.${ext}`);
}
if (maxDepth < 0) {
return;
}

View File

@ -89,11 +89,21 @@ Deno.test("walk() accepts ext option as strings", async () =>
exts: [".rs", ".ts"],
}));
Deno.test("walk() accepts ext option as strings (excluding period prefix)", async () =>
await assertWalkPaths("ext", ["y.rs", "x.ts"], {
exts: ["rs", "ts"],
}));
Deno.test("walkSync() accepts ext option as strings", () =>
assertWalkSyncPaths("ext", ["y.rs", "x.ts"], {
exts: [".rs", ".ts"],
}));
Deno.test("walkSync() accepts ext option as strings (excluding period prefix)", () =>
assertWalkSyncPaths("ext", ["y.rs", "x.ts"], {
exts: [".rs", ".ts"],
}));
Deno.test("walk() accepts ext option as regExps", async () =>
await assertWalkPaths("match", ["x", "y"], {
match: [/x/, /y/],