Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions fs/expand_glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function comparePath(a: WalkEntry, b: WalkEntry): number {
export async function* expandGlob(
glob: string | URL,
options?: ExpandGlobOptions,
): AsyncIterableIterator<WalkEntry> {
): AsyncGenerator<WalkEntry> {
let {
root,
exclude = [],
Expand Down Expand Up @@ -375,7 +375,9 @@ export async function* expandGlob(
(entry: WalkEntry): boolean => !entry.isDirectory,
);
}
yield* currentMatches;
for (const match of currentMatches) {
yield match;
}
}

/**
Expand Down Expand Up @@ -428,7 +430,7 @@ export async function* expandGlob(
export function* expandGlobSync(
glob: string | URL,
options?: ExpandGlobOptions,
): IterableIterator<WalkEntry> {
): Generator<WalkEntry> {
let {
root,
exclude = [],
Expand Down Expand Up @@ -530,7 +532,9 @@ export function* expandGlobSync(
(entry: WalkEntry): boolean => !entry.isDirectory,
);
}
yield* currentMatches;
for (const match of currentMatches) {
yield match;
}
}

const globEscapeChar =
Expand Down
20 changes: 20 additions & 0 deletions fs/expand_glob_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
expandGlob,
type ExpandGlobOptions,
expandGlobSync,
type WalkEntry,
} from "./expand_glob.ts";

async function expandGlobArray(
Expand Down Expand Up @@ -464,3 +465,22 @@ Deno.test("expandGlobSync() finds directory with escaped brackets", function ()
[join("a[b]c", "foo")],
);
});

Deno.test("expandGlobSync() preserves the Generator return type", () => {
// Regression test for https://github.com/denoland/std/issues/7099:
// the return type must be `Generator<WalkEntry>` so the ES2025 iterator
// helpers (`.map`, `.filter`, etc.) are exposed at the type level wherever
// the host TypeScript lib defines them. `Generator<T>` requires the
// `.return()` / `.throw()` methods that `IterableIterator<T>` leaves
// optional, so this assignment fails if the return type is widened back
// to `IterableIterator<WalkEntry>`.
const iter: Generator<WalkEntry> = expandGlobSync("*", EG_OPTIONS);
iter.return(undefined);
});

Deno.test("expandGlob() preserves the AsyncGenerator return type", async () => {
// Regression test for https://github.com/denoland/std/issues/7099 — see the
// sync counterpart for rationale.
const iter: AsyncGenerator<WalkEntry> = expandGlob("*", EG_OPTIONS);
await iter.return(undefined);
});
4 changes: 2 additions & 2 deletions fs/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export type { WalkEntry };
export async function* walk(
root: string | URL,
options?: WalkOptions,
): AsyncIterableIterator<WalkEntry> {
): AsyncGenerator<WalkEntry> {
let {
maxDepth = Infinity,
includeFiles = true,
Expand Down Expand Up @@ -878,7 +878,7 @@ export async function* walk(
export function* walkSync(
root: string | URL,
options?: WalkOptions,
): IterableIterator<WalkEntry> {
): Generator<WalkEntry> {
let {
maxDepth = Infinity,
includeFiles = true,
Expand Down
21 changes: 20 additions & 1 deletion fs/walk_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2026 the Deno authors. MIT license.
import { walk, type WalkOptions, walkSync } from "./walk.ts";
import { walk, type WalkEntry, type WalkOptions, walkSync } from "./walk.ts";
import {
assertArrayIncludes,
assertEquals,
Expand Down Expand Up @@ -338,6 +338,25 @@ Deno.test({
},
});

Deno.test("walkSync() preserves the Generator return type", () => {
// Regression test for https://github.com/denoland/std/issues/7099:
// the return type must be `Generator<WalkEntry>` so the ES2025 iterator
// helpers (`.map`, `.filter`, etc.) are exposed at the type level wherever
// the host TypeScript lib defines them. `Generator<T>` requires the
// `.return()` / `.throw()` methods that `IterableIterator<T>` leaves
// optional, so this assignment fails if the return type is widened back
// to `IterableIterator<WalkEntry>`.
const iter: Generator<WalkEntry> = walkSync(testdataDir);
iter.return(undefined);
});

Deno.test("walk() preserves the AsyncGenerator return type", async () => {
// Regression test for https://github.com/denoland/std/issues/7099 — see the
// sync counterpart for rationale.
const iter: AsyncGenerator<WalkEntry> = walk(testdataDir);
await iter.return(undefined);
});

Deno.test("walk() rejects with `Deno.errors.NotFound` when root is removed during execution", async () => {
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_walk_",
Expand Down
Loading