Skip to content

Commit 547f3dd

Browse files
committed
Wrap the return type of indexing getters as Option<T> if necessary.
1 parent e809a45 commit 547f3dd

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

crates/webidl/src/util.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ptr;
33

44
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
55
use proc_macro2::{Ident, Span};
6+
use quote::quote;
67
use syn;
78
use wasm_bindgen_backend::ast;
89
use wasm_bindgen_backend::util::{ident_ty, leading_colon_path_ty, raw_ident, rust_ident};
@@ -227,6 +228,7 @@ impl<'src> FirstPassRecord<'src> {
227228
idl_arguments: impl Iterator<Item = (&'a str, &'a IdlType<'src>)>,
228229
ret: &IdlType<'src>,
229230
kind: ast::ImportFunctionKind,
231+
is_indexing_getter: bool,
230232
structural: bool,
231233
catch: bool,
232234
variadic: bool,
@@ -294,9 +296,24 @@ impl<'src> FirstPassRecord<'src> {
294296
}
295297
},
296298
};
297-
let js_ret = ret.clone();
299+
300+
let is_indexing_getter_to_fix = is_indexing_getter && // exclude non index getters.
301+
!catch && // exclude indexing getters that return `Result<T>`.
302+
if let Some(ref ty) = ret { // exclude indexing getters that return `Option<T>`.
303+
!format!("{}", quote! { #ty })
304+
.replace(" ", "")
305+
.starts_with("Option<")
306+
} else {
307+
false
308+
};
309+
310+
let mut js_ret = ret.clone();
298311
let ret = if catch {
299312
Some(ret.map_or_else(|| result_ty(unit_ty()), result_ty))
313+
} else if is_indexing_getter_to_fix {
314+
let ret = Some(ret.map_or_else(|| option_ty(unit_ty()), option_ty));
315+
js_ret = ret.clone();
316+
ret
300317
} else {
301318
ret
302319
};
@@ -349,6 +366,7 @@ impl<'src> FirstPassRecord<'src> {
349366
None.into_iter(),
350367
&ret,
351368
kind,
369+
false,
352370
is_structural(attrs.as_ref(), container_attrs),
353371
throws(attrs),
354372
false,
@@ -379,6 +397,7 @@ impl<'src> FirstPassRecord<'src> {
379397
Some((name, &field_ty)).into_iter(),
380398
&IdlType::Void,
381399
kind,
400+
false,
382401
is_structural(attrs.as_ref(), container_attrs),
383402
throws(attrs),
384403
false,
@@ -516,6 +535,8 @@ impl<'src> FirstPassRecord<'src> {
516535
OperationId::IndexingDeleter => ("delete", true, false),
517536
};
518537

538+
let is_indexing_getter = id == &OperationId::IndexingGetter;
539+
519540
let mut ret = Vec::new();
520541
for signature in actual_signatures.iter() {
521542
// Ignore signatures with invalid return types
@@ -598,6 +619,7 @@ impl<'src> FirstPassRecord<'src> {
598619
.map(|(idl_type, orig_arg)| (orig_arg.name, idl_type)),
599620
&ret_ty,
600621
kind.clone(),
622+
is_indexing_getter,
601623
structural,
602624
catch,
603625
variadic,
@@ -624,6 +646,7 @@ impl<'src> FirstPassRecord<'src> {
624646
.map(|(name, idl_type)| (&name[..], idl_type.clone())),
625647
&ret_ty,
626648
kind.clone(),
649+
is_indexing_getter,
627650
structural,
628651
catch,
629652
false,

0 commit comments

Comments
 (0)