Skip to content

Commit

Permalink
fix: Make maplike set and setlike add return this, not `undefin…
Browse files Browse the repository at this point in the history
…ed` (#3270)
  • Loading branch information
Liamolucko authored Feb 1, 2023
1 parent e1b44b7 commit 1f3c76b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
6 changes: 4 additions & 2 deletions crates/webidl-tests/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions crates/webidl-tests/maplike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
3 changes: 2 additions & 1 deletion crates/webidl-tests/setlike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 14 additions & 6 deletions crates/webidl/src/first_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// <interface name> 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,
Expand Down Expand Up @@ -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);
// <interface name> 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,
Expand Down

0 comments on commit 1f3c76b

Please sign in to comment.