Skip to content

Commit b5759fb

Browse files
committed
feat: UnionHint::Object
1 parent 0ed9bdc commit b5759fb

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

crates/jsshaker/src/value/function/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use crate::{
2020
entity::Entity,
2121
scope::VariableScopeId,
2222
utils::{CalleeInfo, CalleeNode},
23-
value::cache::{FnCache, FnCacheTrackDeps},
23+
value::{
24+
UnionHint,
25+
cache::{FnCache, FnCacheTrackDeps},
26+
},
2427
};
2528
pub use arguments::*;
2629
pub use builtin::*;
@@ -194,6 +197,10 @@ impl<'a> ValueTrait<'a> for FunctionValue<'a> {
194197
Some(false)
195198
}
196199

200+
fn get_union_hint(&self) -> UnionHint {
201+
UnionHint::Object
202+
}
203+
197204
fn as_cacheable(&self, _analyzer: &Analyzer<'a>) -> Option<Cacheable<'a>> {
198205
Some(Cacheable::Function(self.callee.instance_id))
199206
}

crates/jsshaker/src/value/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub type IteratedElements<'a> = (Vec<Entity<'a>>, Option<Entity<'a>>, Dep<'a>);
6666
pub enum UnionHint {
6767
Unknown,
6868
Never,
69+
Object,
6970
Other,
7071
}
7172

crates/jsshaker/src/value/object/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
scope::CfScopeId,
2929
use_consumed_flag,
3030
utils::ast::AstKind2,
31-
value::literal::PossibleLiterals,
31+
value::{UnionHint, literal::PossibleLiterals},
3232
};
3333

3434
#[derive(Debug, Clone, Copy)]
@@ -267,6 +267,10 @@ impl<'a> ValueTrait<'a> for ObjectValue<'a> {
267267
fn as_cacheable(&self, _analyzer: &Analyzer<'a>) -> Option<Cacheable<'a>> {
268268
Some(Cacheable::Object(self.object_id()))
269269
}
270+
271+
fn get_union_hint(&self) -> UnionHint {
272+
UnionHint::Object
273+
}
270274
}
271275

272276
impl<'a> ObjectValue<'a> {

crates/jsshaker/src/value/union.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ impl<'a> UnionValues<'a> for allocator::Vec<'a, Entity<'a>> {
389389
has_unknown = true;
390390
filtered.push(*value)
391391
}
392+
UnionHint::Object => {
393+
filtered = self;
394+
has_unknown = false;
395+
break;
396+
}
392397
_ => filtered.push(*value),
393398
}
394399
}
@@ -435,17 +440,21 @@ impl<'a> UnionValues<'a> for (Entity<'a>, Entity<'a>) {
435440
(f(self.0), f(self.1))
436441
}
437442
fn union(self, factory: &Factory<'a>) -> Entity<'a> {
438-
match (self.0.get_union_hint(), self.1.get_union_hint()) {
439-
(UnionHint::Never, _) => self.1,
440-
(_, UnionHint::Never) => self.0,
441-
(UnionHint::Unknown, _) | (_, UnionHint::Unknown) => factory.computed_unknown(self),
442-
_ => factory
443+
let no_merge = || {
444+
factory
443445
.alloc(UnionValue {
444446
values: self,
445447
consumed: Cell::new(false),
446448
phantom: std::marker::PhantomData,
447449
})
448-
.into(),
450+
.into()
451+
};
452+
match (self.0.get_union_hint(), self.1.get_union_hint()) {
453+
(UnionHint::Object, _) | (_, UnionHint::Object) => no_merge(),
454+
(UnionHint::Never, _) => self.1,
455+
(_, UnionHint::Never) => self.0,
456+
(UnionHint::Unknown, _) | (_, UnionHint::Unknown) => factory.computed_unknown(self),
457+
_ => no_merge(),
449458
}
450459
}
451460
}

0 commit comments

Comments
 (0)