Skip to content

Support migration to async_iterable #2087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
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
39 changes: 20 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
"@types/webidl2": "^24.4.3",
"@typescript-eslint/eslint-plugin": "^8",
"@typescript-eslint/parser": "^8",
"@webref/css": "^6.12.7",
"@webref/elements": "^2.3.0",
"@webref/events": "^1.11.3",
"@webref/idl": "^3.46.1",
"@webref/css": "^6.23.6",
"@webref/elements": "^2.5.0",
"@webref/events": "^1.18.6",
"@webref/idl": "^3.66.0",
"bcd-idl-mapper": "^3.0.0",
"cpx2": "^8.0.0",
"danger": "^13.0.4",
Expand All @@ -64,7 +64,7 @@
"print-diff": "^2.0.0",
"typescript": "^5.6.0-dev.20240806",
"typescript-eslint": "^8",
"webidl2": "^24.4.1"
"webidl2": "^24.5.0"
},
"overrides": {
"typescript@*": "$typescript"
Expand Down
3 changes: 2 additions & 1 deletion src/build/bcd/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function mapInterfaceLike(
);

if (i.iterator) {
const iteratorKey = i.iterator.async ? "@@asyncIterator" : "@@iterator";
const iteratorKey =
i.iterator.kind === "async_iterable" ? "@@asyncIterator" : "@@iterator";
// BCD often doesn't have an @@iterator entry, but it does usually have an entry
// for iterable methods such as values(). Use that as a fallback.
// See also: https://github.com/mdn/browser-compat-data/issues/6367
Expand Down
15 changes: 9 additions & 6 deletions src/build/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ export function emitWebIdl(

// Emit forEach for iterators
function emitIteratorForEach(i: Browser.Interface) {
if (!i.iterator || i.iterator.async) {
if (!i.iterator || i.iterator.kind === "async_iterable") {
return;
}
const subtype = i.iterator.type.map(convertDomTypeToTsType);
Expand Down Expand Up @@ -1619,7 +1619,7 @@ export function emitWebIdl(

function emitSelfIterator(i: Browser.Interface) {
if (!compilerBehavior.useIteratorObject) return;
const async = i.iterator?.async;
const async = i.iterator?.kind === "async_iterable";
const name = getName(i);
const iteratorBaseType = `${async ? "Async" : ""}IteratorObject`;
const iteratorType = `${name}${async ? "Async" : ""}Iterator`;
Expand All @@ -1644,7 +1644,7 @@ export function emitWebIdl(
case "setlike":
return;
}
const async = i.iterator?.async;
const async = i.iterator?.kind === "async_iterable";
const iteratorType = async
? !compilerBehavior.useIteratorObject
? "AsyncIterableIterator"
Expand All @@ -1659,7 +1659,10 @@ export function emitWebIdl(
name: `[Symbol.${async ? "asyncIterator" : "iterator"}]`,
type: stringifySingleOrTupleTypes(subtypes),
});
if (i.iterator?.kind === "iterable") {
if (
i.iterator?.kind === "iterable" ||
i.iterator?.kind === "async_iterable"
) {
if (subtypes.length === 2) {
const [keyType, valueType] = subtypes;
methods.push(
Expand Down Expand Up @@ -1718,7 +1721,7 @@ export function emitWebIdl(
}

function getIteratorSubtypes() {
if (i.iterator && !i.iterator.async) {
if (i.iterator && i.iterator.kind !== "async_iterable") {
if (i.iterator.type.length === 1) {
return [convertDomTypeToTsType(i.iterator.type[0])];
}
Expand Down Expand Up @@ -1848,7 +1851,7 @@ export function emitWebIdl(

function emitAsyncIterator(i: Browser.Interface) {
function getAsyncIteratorSubtypes() {
if (i.iterator && i.iterator.kind === "iterable" && i.iterator.async) {
if (i.iterator && i.iterator.kind === "async_iterable") {
if (i.iterator.type.length === 1) {
return [convertDomTypeToTsType(i.iterator.type[0])];
}
Expand Down
3 changes: 1 addition & 2 deletions src/build/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,8 @@ export interface Interface {
}

export interface Iterator {
kind: "iterable" | "setlike" | "maplike";
kind: "async_iterable" | "iterable" | "setlike" | "maplike";
readonly: boolean;
async: boolean;
type: Typed[];
param?: Param[];
comments?: {
Expand Down
20 changes: 15 additions & 5 deletions src/build/widlprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,26 @@ function convertInterfaceCommon(
addComments(method[member.name], commentMap, i.name, member.name);
}
} else if (
(member.type as string) === "async_iterable" ||
member.type === "iterable" ||
member.type === "maplike" ||
member.type === "setlike"
) {
// TODO(saschanaz): @types/webidl2 doesn't support async_iterable
const iterableLike = member as
| webidl2.IterableDeclarationMemberType
| webidl2.MaplikeDeclarationMemberType
| webidl2.SetlikeDeclarationMemberType;
// Compatibility between `async_iterable` and `async iterable`
const kind =
iterableLike.type === "iterable" && iterableLike.async
? "async_iterable"
: iterableLike.type;
result.iterator = {
kind: member.type,
readonly: member.readonly,
async: member.async,
param: member.arguments.map(convertArgument),
type: member.idlType.map(convertIdlType),
kind,
readonly: iterableLike.readonly,
param: iterableLike.arguments.map(convertArgument),
type: iterableLike.idlType.map(convertIdlType),
};
}
}
Expand Down