From 1f3c76bca62fa62a05aaae8194c454d693ecc712 Mon Sep 17 00:00:00 2001 From: Liam Murphy Date: Thu, 2 Feb 2023 03:48:22 +1100 Subject: [PATCH] fix: Make maplike `set` and setlike `add` return `this`, not `undefined` (#3270) --- crates/webidl-tests/globals.js | 6 ++++-- crates/webidl-tests/maplike.rs | 6 ++++-- crates/webidl-tests/setlike.rs | 3 ++- crates/webidl/src/first_pass.rs | 20 ++++++++++++++------ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/webidl-tests/globals.js b/crates/webidl-tests/globals.js index bf70171c1ca..5816c108e42 100644 --- a/crates/webidl-tests/globals.js +++ b/crates/webidl-tests/globals.js @@ -207,7 +207,8 @@ global.TestReadWriteMapLike = class extends global.TestReadOnlyMapLike { } set(key, value) { - return this.map.set(key, value); + this.map.set(key, value); + return this; } delete(key) { @@ -258,7 +259,8 @@ global.TestReadWriteSetLike = class extends global.TestReadOnlySetLike { } add(value) { - return this.set.add(value); + this.set.add(value); + return this; } delete(value) { diff --git a/crates/webidl-tests/maplike.rs b/crates/webidl-tests/maplike.rs index 758ed00a9b2..6e63eb1b76b 100644 --- a/crates/webidl-tests/maplike.rs +++ b/crates/webidl-tests/maplike.rs @@ -87,10 +87,12 @@ fn write_maplike() { let maplike = TestReadWriteMapLike::new().unwrap(); // undefined set(K key, V value); - maplike.set("a", 4); - maplike.set("d", 5); + let ret1 = maplike.set("a", 4); + let ret2 = maplike.set("d", 5); assert_eq!(maplike.get("a"), Some(4)); assert_eq!(maplike.get("d"), Some(5)); + assert_eq!(ret1, maplike); + assert_eq!(ret2, maplike); // boolean delete(K key); assert!(maplike.delete("a")); diff --git a/crates/webidl-tests/setlike.rs b/crates/webidl-tests/setlike.rs index d77dd61c16e..74700627391 100644 --- a/crates/webidl-tests/setlike.rs +++ b/crates/webidl-tests/setlike.rs @@ -78,8 +78,9 @@ fn write_setlike() { // { "a", "b", "c" } let setlike = TestReadWriteSetLike::new().unwrap(); - setlike.add("d"); + let ret = setlike.add("d"); assert_eq!(setlike.size(), 4); + assert_eq!(ret, setlike); assert!(setlike.delete("d")); assert_eq!(setlike.size(), 3); diff --git a/crates/webidl/src/first_pass.rs b/crates/webidl/src/first_pass.rs index 68e4a8c129d..91f3ed143bf 100644 --- a/crates/webidl/src/first_pass.rs +++ b/crates/webidl/src/first_pass.rs @@ -848,15 +848,19 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> stability, ); - // TODO: `set` actually returns `this` but we don't have a way to express that just yet - // undefined set(K key, V value); + // set(K key, V value); first_pass_operation( record, FirstPassOperationType::Interface, self_name, &[OperationId::Operation(Some("set"))], [key_arg(), value_arg()], - &undefined_ret(), + &ReturnType::Type(Type::Single(SingleType::NonAny(NonAnyType::Identifier( + MayBeNull { + type_: Identifier(self_name), + q_mark: None, + }, + )))), &None, false, stability, @@ -1051,15 +1055,19 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> ctx.1, ); - // TODO: `add` actually returns `this` but we don't have a way to express that just yet - // undefined add(V value); + // add(V value); first_pass_operation( record, FirstPassOperationType::Interface, ctx.0, &[OperationId::Operation(Some("add"))], [value_arg()], - &undefined_ret(), + &ReturnType::Type(Type::Single(SingleType::NonAny(NonAnyType::Identifier( + MayBeNull { + type_: Identifier(ctx.0), + q_mark: None, + }, + )))), &None, false, ctx.1,