Skip to content

Commit 095f86f

Browse files
committed
Use object type whenever possible
1 parent 14c2c04 commit 095f86f

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

crates/webidl/src/idl_type.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,19 @@ impl<'a> IdlType<'a> {
551551

552552
IdlType::Nullable(idl_type) => {
553553
match **idl_type {
554-
IdlType::Union(..) => idl_type.to_syn_type(pos),
554+
IdlType::Union(ref idl_types) =>
555+
if idl_types
556+
.iter()
557+
.all(|idl_type|
558+
match idl_type {
559+
IdlType::Interface(..) => true,
560+
_ => false,
561+
}
562+
) {
563+
IdlType::Nullable(Box::new(IdlType::Object)).to_syn_type(pos)
564+
} else {
565+
IdlType::Any.to_syn_type(pos)
566+
},
555567
_ => Some(option_ty(idl_type.to_syn_type(pos)?))
556568
}
557569
},
@@ -567,11 +579,22 @@ impl<'a> IdlType<'a> {
567579
}
568580
}
569581
IdlType::Record(_idl_type_from, _idl_type_to) => None,
570-
IdlType::Union(_idl_types) => {
582+
IdlType::Union(idl_types) => {
571583
// Handles union types in all places except operation argument types.
572-
// Currently treats them as any type.
584+
// Currently treats them as object or any types.
573585
// TODO: add better support for union types here?
574-
IdlType::Any.to_syn_type(pos)
586+
if idl_types
587+
.iter()
588+
.all(|idl_type|
589+
match idl_type {
590+
IdlType::Interface(..) => true,
591+
_ => false,
592+
}
593+
) {
594+
IdlType::Object.to_syn_type(pos)
595+
} else {
596+
IdlType::Any.to_syn_type(pos)
597+
}
575598
},
576599

577600
IdlType::Any => {

0 commit comments

Comments
 (0)