Skip to content

Commit 8e7ab5e

Browse files
committed
introduce ConsumeTrait
1 parent 0c9b3a0 commit 8e7ab5e

File tree

18 files changed

+95
-87
lines changed

18 files changed

+95
-87
lines changed

crates/tree_shaker/src/builtins/prototypes/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a> BuiltinPrototype<'a> {
7373
key: Entity<'a>,
7474
dep: Consumable<'a>,
7575
) -> Entity<'a> {
76-
let dep = analyzer.consumable((dep, target, key));
76+
let dep = (dep, target, key);
7777
if let Some(key_literals) = key.get_to_literals(analyzer) {
7878
let mut values = vec![];
7979
for key_literal in key_literals {

crates/tree_shaker/src/builtins/react/class_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ pub fn create_class_names_namespace<'a>(
2828
}
2929
}
3030

31-
analyzer.factory.computed_unknown_string(analyzer.consumable((deps_1, deps_2, rest)))
31+
analyzer.factory.computed_unknown_string((deps_1, deps_2, rest))
3232
})
3333
}

crates/tree_shaker/src/consumable/collector.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
use super::{Consumable, ConsumableTrait};
1+
use super::{Consumable, ConsumeTrait};
22
use crate::{analyzer::Analyzer, entity::EntityFactory};
33
use std::mem;
44

55
#[derive(Debug)]
6-
pub struct ConsumableCollector<'a, T: ConsumableTrait<'a> + 'a = Consumable<'a>> {
6+
pub struct ConsumableCollector<'a, T: ConsumeTrait<'a> + 'a = Consumable<'a>> {
77
pub current: Vec<T>,
88
pub node: Option<Consumable<'a>>,
99
}
1010

11-
impl<'a, T: ConsumableTrait<'a> + 'a> Default for ConsumableCollector<'a, T> {
11+
impl<'a, T: ConsumeTrait<'a> + 'a> Default for ConsumableCollector<'a, T> {
1212
fn default() -> Self {
1313
Self { current: Vec::new(), node: None }
1414
}
1515
}
1616

17-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableCollector<'a, T> {
17+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableCollector<'a, T> {
1818
pub fn new(current: Vec<T>) -> Self {
1919
Self { current, node: None }
2020
}

crates/tree_shaker/src/consumable/impls.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,54 @@
1-
use super::{Consumable, ConsumableTrait};
1+
use super::{ConsumableTrait, ConsumeTrait};
22
use crate::analyzer::Analyzer;
33
use std::{cell::RefCell, rc::Rc};
44

55
impl<'a> ConsumableTrait<'a> for () {
66
fn consume(&self, _: &mut Analyzer<'a>) {}
77
}
88

9-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Box<T> {
9+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Box<T> {
1010
fn consume(&self, analyzer: &mut Analyzer<'a>) {
1111
self.as_ref().consume(analyzer)
1212
}
1313
}
1414

15-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Option<T> {
15+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Option<T> {
1616
fn consume(&self, analyzer: &mut Analyzer<'a>) {
1717
if let Some(value) = self {
1818
value.consume(analyzer)
1919
}
2020
}
2121
}
2222

23-
impl<'a, T: Default + ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for &'a RefCell<T> {
23+
impl<'a, T: Default + ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for &'a RefCell<T> {
2424
fn consume(&self, analyzer: &mut Analyzer<'a>) {
2525
self.take().consume(analyzer)
2626
}
2727
}
2828

29-
impl<'a, T: Default + ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Rc<T> {
29+
impl<'a, T: Default + ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Rc<T> {
3030
fn consume(&self, analyzer: &mut Analyzer<'a>) {
3131
self.as_ref().consume(analyzer)
3232
}
3333
}
3434

35-
impl<'a> ConsumableTrait<'a> for Consumable<'a> {
36-
fn consume(&self, analyzer: &mut Analyzer<'a>) {
37-
self.0.consume(analyzer)
38-
}
39-
}
40-
41-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Vec<T> {
35+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Vec<T> {
4236
fn consume(&self, analyzer: &mut Analyzer<'a>) {
4337
for item in self {
4438
item.consume(analyzer)
4539
}
4640
}
4741
}
4842

49-
impl<'a, T1: ConsumableTrait<'a> + 'a, T2: ConsumableTrait<'a> + 'a> ConsumableTrait<'a>
50-
for (T1, T2)
51-
{
43+
impl<'a, T1: ConsumeTrait<'a> + 'a, T2: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for (T1, T2) {
5244
fn consume(&self, analyzer: &mut Analyzer<'a>) {
5345
self.0.consume(analyzer);
5446
self.1.consume(analyzer)
5547
}
5648
}
5749

58-
impl<
59-
'a,
60-
T1: ConsumableTrait<'a> + 'a,
61-
T2: ConsumableTrait<'a> + 'a,
62-
T3: ConsumableTrait<'a> + 'a,
63-
> ConsumableTrait<'a> for (T1, T2, T3)
50+
impl<'a, T1: ConsumeTrait<'a> + 'a, T2: ConsumeTrait<'a> + 'a, T3: ConsumeTrait<'a> + 'a>
51+
ConsumableTrait<'a> for (T1, T2, T3)
6452
{
6553
fn consume(&self, analyzer: &mut Analyzer<'a>) {
6654
self.0.consume(analyzer);
@@ -71,10 +59,10 @@ impl<
7159

7260
impl<
7361
'a,
74-
T1: ConsumableTrait<'a> + 'a,
75-
T2: ConsumableTrait<'a> + 'a,
76-
T3: ConsumableTrait<'a> + 'a,
77-
T4: ConsumableTrait<'a> + 'a,
62+
T1: ConsumeTrait<'a> + 'a,
63+
T2: ConsumeTrait<'a> + 'a,
64+
T3: ConsumeTrait<'a> + 'a,
65+
T4: ConsumeTrait<'a> + 'a,
7866
> ConsumableTrait<'a> for (T1, T2, T3, T4)
7967
{
8068
fn consume(&self, analyzer: &mut Analyzer<'a>) {

crates/tree_shaker/src/consumable/mod.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,44 @@ use crate::analyzer::Analyzer;
77
pub use collector::*;
88
pub use lazy::*;
99
pub use once::*;
10+
use oxc::allocator::Allocator;
1011
use std::fmt::Debug;
1112

1213
pub trait ConsumableTrait<'a>: Debug {
1314
fn consume(&self, analyzer: &mut Analyzer<'a>);
1415
}
1516

17+
pub trait ConsumeTrait<'a>: Debug {
18+
fn consume(&self, analyzer: &mut Analyzer<'a>);
19+
fn uniform(self, allocator: &'a Allocator) -> Consumable<'a>;
20+
}
21+
22+
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumeTrait<'a> for T {
23+
fn consume(&self, analyzer: &mut Analyzer<'a>) {
24+
self.consume(analyzer);
25+
}
26+
fn uniform(self, allocator: &'a Allocator) -> Consumable<'a> {
27+
Consumable(allocator.alloc(self))
28+
}
29+
}
30+
1631
#[derive(Debug, Clone, Copy)]
1732
pub struct Consumable<'a>(pub &'a (dyn ConsumableTrait<'a> + 'a));
1833

34+
impl<'a> ConsumeTrait<'a> for Consumable<'a> {
35+
fn consume(&self, analyzer: &mut Analyzer<'a>) {
36+
self.0.consume(analyzer);
37+
}
38+
fn uniform(self, _: &'a Allocator) -> Consumable<'a> {
39+
self
40+
}
41+
}
42+
1943
pub type ConsumableVec<'a> = Vec<Consumable<'a>>;
2044

2145
impl<'a> Analyzer<'a> {
2246
#[inline]
23-
pub fn consume(&mut self, dep: impl ConsumableTrait<'a> + 'a) {
47+
pub fn consume(&mut self, dep: impl ConsumeTrait<'a> + 'a) {
2448
dep.consume(self);
2549
}
2650

crates/tree_shaker/src/entity/array.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'a> EntityTrait<'a> for ArrayEntity<'a> {
9797
result.push(self.get_length().map_or_else(
9898
|| {
9999
let dep = self.rest.borrow().clone();
100-
analyzer.factory.computed_unknown_number(analyzer.consumable(dep))
100+
analyzer.factory.computed_unknown_number(dep)
101101
},
102102
|length| analyzer.factory.number(length as f64, None),
103103
));
@@ -114,10 +114,10 @@ impl<'a> EntityTrait<'a> for ArrayEntity<'a> {
114114
}
115115
analyzer.factory.computed_union(result, dep)
116116
} else {
117-
analyzer.factory.computed_unknown(analyzer.consumable((
117+
analyzer.factory.computed_unknown((
118118
self.elements.borrow().iter().chain(self.rest.borrow().iter()).cloned().collect::<Vec<_>>(),
119119
dep,
120-
)))
120+
))
121121
}
122122
}
123123

crates/tree_shaker/src/entity/factory.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
consumable::{Consumable, ConsumableTrait, LazyConsumable, OnceConsumable},
2+
consumable::{Consumable, ConsumableTrait, ConsumeTrait, LazyConsumable, OnceConsumable},
33
mangling::{AlwaysMangableDep, MangleAtom, MangleConstraint, ManglingDep},
44
scope::CfScopeId,
55
utils::F64WithEq,
@@ -320,7 +320,7 @@ impl<'a> EntityFactory<'a> {
320320
}
321321
}
322322

323-
pub fn computed_union<T: ConsumableTrait<'a> + Copy + 'a>(
323+
pub fn computed_union<T: ConsumeTrait<'a> + 'a>(
324324
&self,
325325
values: Vec<Entity<'a>>,
326326
dep: T,
@@ -332,7 +332,7 @@ impl<'a> EntityFactory<'a> {
332332
self.immutable_unknown
333333
}
334334

335-
pub fn computed_unknown(&self, dep: impl ConsumableTrait<'a> + Copy + 'a) -> Entity<'a> {
335+
pub fn computed_unknown(&self, dep: impl ConsumeTrait<'a> + 'a) -> Entity<'a> {
336336
self.computed(self.immutable_unknown, dep)
337337
}
338338

@@ -369,7 +369,7 @@ macro_rules! unknown_entity_ctors {
369369
($($name:ident -> $var:ident,)*) => {
370370
$(
371371
#[allow(unused)]
372-
pub fn $name<T: ConsumableTrait<'a> + Copy + 'a>(&self, dep: T) -> Entity<'a> {
372+
pub fn $name(&self, dep: impl ConsumeTrait<'a> + 'a) -> Entity<'a> {
373373
self.computed(self.$var, dep)
374374
}
375375
)*

crates/tree_shaker/src/entity/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod utils;
1717

1818
use crate::{
1919
analyzer::Analyzer,
20-
consumable::{Consumable, ConsumableTrait},
20+
consumable::{Consumable, ConsumableTrait, ConsumeTrait},
2121
};
2222
pub use builtin_fn::PureBuiltinFnEntity;
2323
pub use factory::EntityFactory;
@@ -151,7 +151,7 @@ pub trait EntityTrait<'a>: ConsumableTrait<'a> {
151151
rest_arr.deps.borrow_mut().push(if extras.is_empty() && rest.is_none() {
152152
analyzer.consumable((self, dep))
153153
} else {
154-
analyzer.consumable(dep)
154+
dep
155155
});
156156
rest_arr.elements.borrow_mut().extend(extras);
157157
if let Some(rest) = rest {
@@ -223,14 +223,14 @@ impl<'a> Entity<'a> {
223223
}
224224
}
225225

226-
fn forward_value(&self, value: Entity<'a>, analyzer: &Analyzer<'a>) -> Entity<'a> {
227-
if let Some(d1) = self.dep {
228-
analyzer.factory.entity_with_dep(
229-
value.value,
230-
if let Some(d2) = value.dep { analyzer.factory.consumable((d1, d2)) } else { d1 },
231-
)
232-
} else {
233-
value
226+
fn forward_value(&self, entity: Entity<'a>, analyzer: &Analyzer<'a>) -> Entity<'a> {
227+
Entity {
228+
value: entity.value,
229+
dep: match (self.dep, entity.dep) {
230+
(Some(d1), Some(d2)) => Some(analyzer.factory.consumable((d1, d2))),
231+
(Some(d), None) | (None, Some(d)) => Some(d),
232+
(None, None) => None,
233+
},
234234
}
235235
}
236236

@@ -413,13 +413,13 @@ impl<'a> EntityFactory<'a> {
413413
Entity { value, dep: Some(dep) }
414414
}
415415

416-
pub fn computed(&self, entity: Entity<'a>, dep: impl ConsumableTrait<'a> + 'a) -> Entity<'a> {
416+
pub fn computed(&self, entity: Entity<'a>, dep: impl ConsumeTrait<'a> + 'a) -> Entity<'a> {
417417
Entity {
418418
value: entity.value,
419419
dep: if let Some(d) = entity.dep {
420420
Some(self.consumable((d, dep)))
421421
} else {
422-
Some(self.consumable(dep))
422+
Some(dep.uniform(self.allocator))
423423
},
424424
}
425425
}

crates/tree_shaker/src/entity/object/get.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ impl<'a> ObjectEntity<'a> {
8888

8989
let value = analyzer.factory.try_union(context.values).unwrap_or(analyzer.factory.undefined);
9090
if mangable {
91-
analyzer.factory.computed(value, analyzer.consumable((context.extra_deps, dep)))
91+
analyzer.factory.computed(value, (context.extra_deps, dep))
9292
} else {
93-
analyzer.factory.computed(value, analyzer.consumable((context.extra_deps, dep, key)))
93+
analyzer.factory.computed(value, (context.extra_deps, dep, key))
9494
}
9595
}
9696

crates/tree_shaker/src/entity/object/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a> ConsumableTrait<'a> for ObjectPrototype<'a> {
4040
ObjectPrototype::ImplicitOrNull => {}
4141
ObjectPrototype::Builtin(_prototype) => {}
4242
ObjectPrototype::Custom(object) => object.consume_as_prototype(analyzer),
43-
ObjectPrototype::Unknown(dep) => dep.consume(analyzer),
43+
ObjectPrototype::Unknown(dep) => analyzer.consume(*dep),
4444
}
4545
}
4646
}
@@ -217,17 +217,15 @@ impl<'a> EntityTrait<'a> for ObjectEntity<'a> {
217217
};
218218
let key_entity = analyzer.factory.computed(
219219
key_entity,
220-
analyzer.factory.consumable(
221-
property
222-
.possible_values
223-
.iter()
224-
.map(|value| match value {
225-
ObjectPropertyValue::Field(value, _) => *value,
226-
ObjectPropertyValue::Property(Some(getter), _) => *getter,
227-
ObjectPropertyValue::Property(None, _) => analyzer.factory.undefined,
228-
})
229-
.collect::<Vec<_>>(),
230-
),
220+
property
221+
.possible_values
222+
.iter()
223+
.map(|value| match value {
224+
ObjectPropertyValue::Field(value, _) => *value,
225+
ObjectPropertyValue::Property(Some(getter), _) => *getter,
226+
ObjectPropertyValue::Property(None, _) => analyzer.factory.undefined,
227+
})
228+
.collect::<Vec<_>>(),
231229
);
232230
keys.push((property.definite, key_entity));
233231
}

crates/tree_shaker/src/entity/object/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a> ObjectEntity<'a> {
4040
indeterminate = true;
4141
}
4242

43-
let value = analyzer.factory.computed(value, analyzer.consumable((exec_deps, dep)));
43+
let value = analyzer.factory.computed(value, (exec_deps, dep));
4444
let non_mangable_value = analyzer.factory.computed(value, key);
4545

4646
if let Some(key_literals) = key.get_to_literals(analyzer) {

crates/tree_shaker/src/folding/dep.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'a> ConsumableTrait<'a> for FoldableDep<'a> {
4545
}
4646
}
4747

48-
#[derive(Debug)]
48+
#[derive(Debug, Clone, Copy)]
4949
pub struct UnFoldableDep<'a> {
5050
pub data: &'a RefCell<FoldingData<'a>>,
5151
}

crates/tree_shaker/src/folding/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@ impl<'a> Analyzer<'a> {
6767
value
6868
} else if let Some(literal) = self.get_foldable_literal(value) {
6969
let (literal_value, mangle_atom) = literal.with_mangle_atom(self);
70-
self.factory.computed(
71-
literal_value,
72-
self.factory.consumable(FoldableDep { data, literal, value, mangle_atom }),
73-
)
70+
self.factory.computed(literal_value, FoldableDep { data, literal, value, mangle_atom })
7471
} else {
75-
self.factory.computed(value, self.factory.consumable(UnFoldableDep { data }))
72+
self.factory.computed(value, UnFoldableDep { data })
7673
}
7774
}
7875

crates/tree_shaker/src/nodes/expr/import_expression.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'a> Analyzer<'a> {
1818

1919
if let Some(LiteralEntity::String(specifier, _m)) = specifier.get_literal(self) {
2020
if let Some(module_id) = self.resolve_and_import_module(specifier) {
21-
return self.factory.computed_unknown(self.consumable((module_id, dep)));
21+
return self.factory.computed_unknown((module_id, dep));
2222
}
2323
}
2424

crates/tree_shaker/src/nodes/jsx/jsx_children.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'a> Analyzer<'a> {
1818
JSXChild::Spread(node) => self.exec_jsx_spread_child(node),
1919
})
2020
.collect();
21-
self.factory.computed_unknown(self.consumable(values))
21+
self.factory.computed_unknown(values)
2222
}
2323
}
2424

0 commit comments

Comments
 (0)