Skip to content

Commit 75604b0

Browse files
committed
refactor: split factory.rs
1 parent 17fa5ce commit 75604b0

File tree

8 files changed

+161
-141
lines changed

8 files changed

+161
-141
lines changed

crates/jsshaker/src/analyzer/factory.rs

Lines changed: 4 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ use std::{
44
};
55

66
use oxc::allocator::{self, Allocator};
7-
use oxc::{semantic::SymbolId, span::Atom};
8-
use oxc_syntax::operator::LogicalOperator;
97

108
use crate::{
119
TreeShakeConfig,
1210
dep::{CustomDepTrait, Dep, DepTrait, LazyDep, OnceDep},
1311
entity::Entity,
14-
mangling::{AlwaysMangableDep, MangleAtom, MangleConstraint, ManglingDep},
12+
mangling::{AlwaysMangableDep, MangleConstraint, ManglingDep},
1513
scope::CfScopeId,
16-
utils::{CalleeInstanceId, F64WithEq},
14+
utils::CalleeInstanceId,
1715
value::{
18-
ArgumentsValue, BuiltinFnImplementation, ImplementedBuiltinFnValue, LiteralValue,
19-
ObjectProperty, ObjectPrototype, ObjectValue, PureBuiltinFnValue, literal::string::ToAtomRef,
20-
logical_result::LogicalResultValue, never::NeverValue, primitive::PrimitiveValue,
21-
react_element::ReactElementValue, union::UnionValues, unknown::UnknownValue,
16+
ArgumentsValue, LiteralValue, PureBuiltinFnValue, logical_result::LogicalResultValue,
17+
never::NeverValue, primitive::PrimitiveValue, union::UnionValues, unknown::UnknownValue,
2218
},
2319
};
2420

@@ -188,63 +184,6 @@ impl<'a> Factory<'a> {
188184
CalleeInstanceId::from_usize(id)
189185
}
190186

191-
pub fn builtin_object(
192-
&self,
193-
prototype: ObjectPrototype<'a>,
194-
consumable: bool,
195-
) -> &'a mut ObjectValue<'a> {
196-
self.alloc(ObjectValue {
197-
consumable,
198-
consumed: Cell::new(false),
199-
consumed_as_prototype: Cell::new(false),
200-
cf_scope: self.root_cf_scope.unwrap(),
201-
keyed: allocator::HashMap::new_in(self.allocator).into(),
202-
unknown: ObjectProperty::new_in(self.allocator).into(),
203-
rest: Default::default(),
204-
prototype: Cell::new(prototype),
205-
mangling_group: Cell::new(None),
206-
})
207-
}
208-
209-
pub fn arguments(
210-
&self,
211-
elements: &'a [Entity<'a>],
212-
rest: Option<Entity<'a>>,
213-
) -> ArgumentsValue<'a> {
214-
ArgumentsValue { elements, rest }
215-
}
216-
217-
pub fn implemented_builtin_fn<F: BuiltinFnImplementation<'a> + 'a>(
218-
&self,
219-
name: &'static str,
220-
implementation: F,
221-
) -> Entity<'a> {
222-
self
223-
.alloc(ImplementedBuiltinFnValue {
224-
name,
225-
implementation,
226-
statics: None,
227-
consumed: Cell::new(true),
228-
})
229-
.into()
230-
}
231-
232-
pub fn implemented_builtin_fn_with_statics<F: BuiltinFnImplementation<'a> + 'a>(
233-
&self,
234-
name: &'static str,
235-
implementation: F,
236-
statics: &'a ObjectValue<'a>,
237-
) -> Entity<'a> {
238-
self
239-
.alloc(ImplementedBuiltinFnValue {
240-
name,
241-
implementation,
242-
statics: Some(statics),
243-
consumed: Cell::new(true),
244-
})
245-
.into()
246-
}
247-
248187
pub fn dep_no_once(&self, dep: impl CustomDepTrait<'a> + 'a) -> Dep<'a> {
249188
Dep(self.alloc(dep))
250189
}
@@ -276,71 +215,6 @@ impl<'a> Factory<'a> {
276215
}
277216
}
278217

279-
pub fn mangable_string(&self, value: impl ToAtomRef<'a>, atom: MangleAtom) -> Entity<'a> {
280-
self.string(value, Some(atom))
281-
}
282-
283-
pub fn unmangable_string(&self, value: impl ToAtomRef<'a>) -> Entity<'a> {
284-
self.string(value, None)
285-
}
286-
287-
pub fn string(&self, value: impl ToAtomRef<'a>, atom: Option<MangleAtom>) -> Entity<'a> {
288-
if atom.is_some() {
289-
self.alloc(LiteralValue::String(value.to_atom_ref(self.allocator), atom)).into()
290-
} else {
291-
value.to_atom_ref(self.allocator).into()
292-
}
293-
}
294-
295-
pub fn number(&self, value: impl Into<F64WithEq>) -> Entity<'a> {
296-
self.alloc(LiteralValue::Number(value.into())).into()
297-
}
298-
pub fn big_int(&self, value: &'a Atom<'a>) -> Entity<'a> {
299-
self.alloc(LiteralValue::BigInt(value)).into()
300-
}
301-
302-
pub fn boolean(&self, value: bool) -> Entity<'a> {
303-
if value { self.r#true } else { self.r#false }
304-
}
305-
pub fn boolean_maybe_unknown(&self, value: Option<bool>) -> Entity<'a> {
306-
if let Some(value) = value { self.boolean(value) } else { self.unknown_boolean }
307-
}
308-
309-
pub fn symbol(&self, id: SymbolId, str_rep: &'a Atom<'a>) -> Entity<'a> {
310-
self.alloc(LiteralValue::Symbol(id, str_rep)).into()
311-
}
312-
313-
/// Only used when (maybe_left, maybe_right) == (true, true)
314-
pub fn logical_result(
315-
&self,
316-
left: Entity<'a>,
317-
right: Entity<'a>,
318-
operator: LogicalOperator,
319-
) -> Entity<'a> {
320-
let value = self.union((left, right));
321-
let result = match operator {
322-
LogicalOperator::Or => match right.test_truthy() {
323-
Some(true) => true,
324-
_ => return value,
325-
},
326-
LogicalOperator::And => match right.test_truthy() {
327-
Some(false) => false,
328-
_ => return value,
329-
},
330-
LogicalOperator::Coalesce => match right.test_nullish() {
331-
Some(false) => false,
332-
_ => return value,
333-
},
334-
};
335-
self
336-
.alloc(LogicalResultValue {
337-
value,
338-
is_coalesce: operator == LogicalOperator::Coalesce,
339-
result,
340-
})
341-
.into()
342-
}
343-
344218
pub fn try_union<V: UnionValues<'a> + Debug + 'a>(&self, values: V) -> Option<Entity<'a>> {
345219
match values.len() {
346220
0 => None,
@@ -381,17 +255,6 @@ impl<'a> Factory<'a> {
381255
LazyDep(self.alloc(RefCell::new(Some(deps))))
382256
}
383257

384-
pub fn react_element(&self, tag: Entity<'a>, props: Entity<'a>) -> Entity<'a> {
385-
self
386-
.alloc(ReactElementValue {
387-
consumed: Cell::new(false),
388-
tag,
389-
props,
390-
deps: RefCell::new(self.vec()),
391-
})
392-
.into()
393-
}
394-
395258
pub fn mangable(
396259
&self,
397260
val: Entity<'a>,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,13 @@ impl<'a> ArgumentsValue<'a> {
111111
}
112112
}
113113
}
114+
115+
impl<'a> crate::analyzer::Factory<'a> {
116+
pub fn arguments(
117+
&self,
118+
elements: &'a [Entity<'a>],
119+
rest: Option<Entity<'a>>,
120+
) -> ArgumentsValue<'a> {
121+
ArgumentsValue { elements, rest }
122+
}
123+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,36 @@ impl<'a> PureBuiltinFnValue<'a> {
276276
Self { name, return_value }
277277
}
278278
}
279+
280+
impl<'a> crate::analyzer::Factory<'a> {
281+
pub fn implemented_builtin_fn<F: BuiltinFnImplementation<'a> + 'a>(
282+
&self,
283+
name: &'static str,
284+
implementation: F,
285+
) -> Entity<'a> {
286+
self
287+
.alloc(ImplementedBuiltinFnValue {
288+
name,
289+
implementation,
290+
statics: None,
291+
consumed: Cell::new(true),
292+
})
293+
.into()
294+
}
295+
296+
pub fn implemented_builtin_fn_with_statics<F: BuiltinFnImplementation<'a> + 'a>(
297+
&self,
298+
name: &'static str,
299+
implementation: F,
300+
statics: &'a ObjectValue<'a>,
301+
) -> Entity<'a> {
302+
self
303+
.alloc(ImplementedBuiltinFnValue {
304+
name,
305+
implementation,
306+
statics: Some(statics),
307+
consumed: Cell::new(true),
308+
})
309+
.into()
310+
}
311+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,25 @@ impl<'a, 'b> IntoIterator for &'b PossibleLiterals<'a> {
465465
}
466466
}
467467
}
468+
469+
impl<'a> crate::analyzer::Factory<'a> {
470+
pub fn number(&self, value: impl Into<F64WithEq>) -> Entity<'a> {
471+
self.alloc(LiteralValue::Number(value.into())).into()
472+
}
473+
474+
pub fn big_int(&self, value: &'a Atom<'a>) -> Entity<'a> {
475+
self.alloc(LiteralValue::BigInt(value)).into()
476+
}
477+
478+
pub fn boolean(&self, value: bool) -> Entity<'a> {
479+
if value { self.r#true } else { self.r#false }
480+
}
481+
482+
pub fn boolean_maybe_unknown(&self, value: Option<bool>) -> Entity<'a> {
483+
if let Some(value) = value { self.boolean(value) } else { self.unknown_boolean }
484+
}
485+
486+
pub fn symbol(&self, id: SymbolId, str_rep: &'a Atom<'a>) -> Entity<'a> {
487+
self.alloc(LiteralValue::Symbol(id, str_rep)).into()
488+
}
489+
}

crates/jsshaker/src/value/literal/string.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,29 @@ fn get_known_instance_property<'a>(
224224
}
225225
}
226226
}
227+
228+
impl<'a> crate::analyzer::Factory<'a> {
229+
pub fn mangable_string(
230+
&self,
231+
value: impl ToAtomRef<'a>,
232+
atom: crate::mangling::MangleAtom,
233+
) -> Entity<'a> {
234+
self.string(value, Some(atom))
235+
}
236+
237+
pub fn unmangable_string(&self, value: impl ToAtomRef<'a>) -> Entity<'a> {
238+
self.string(value, None)
239+
}
240+
241+
pub fn string(
242+
&self,
243+
value: impl ToAtomRef<'a>,
244+
atom: Option<crate::mangling::MangleAtom>,
245+
) -> Entity<'a> {
246+
if atom.is_some() {
247+
self.alloc(LiteralValue::String(value.to_atom_ref(self.allocator), atom)).into()
248+
} else {
249+
value.to_atom_ref(self.allocator).into()
250+
}
251+
}
252+
}

crates/jsshaker/src/value/logical_result.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,36 @@ impl<'a> ValueTrait<'a> for LogicalResultValue<'a> {
147147
self.value.as_cacheable(analyzer)
148148
}
149149
}
150+
151+
impl<'a> crate::analyzer::Factory<'a> {
152+
/// Only used when (maybe_left, maybe_right) == (true, true)
153+
pub fn logical_result(
154+
&self,
155+
left: Entity<'a>,
156+
right: Entity<'a>,
157+
operator: oxc_syntax::operator::LogicalOperator,
158+
) -> Entity<'a> {
159+
let value = self.union((left, right));
160+
let result = match operator {
161+
oxc_syntax::operator::LogicalOperator::Or => match right.test_truthy() {
162+
Some(true) => true,
163+
_ => return value,
164+
},
165+
oxc_syntax::operator::LogicalOperator::And => match right.test_truthy() {
166+
Some(false) => false,
167+
_ => return value,
168+
},
169+
oxc_syntax::operator::LogicalOperator::Coalesce => match right.test_nullish() {
170+
Some(false) => false,
171+
_ => return value,
172+
},
173+
};
174+
self
175+
.alloc(LogicalResultValue {
176+
value,
177+
is_coalesce: operator == oxc_syntax::operator::LogicalOperator::Coalesce,
178+
result,
179+
})
180+
.into()
181+
}
182+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,23 @@ impl<'a> Analyzer<'a> {
417417
)
418418
}
419419
}
420+
421+
impl<'a> crate::analyzer::Factory<'a> {
422+
pub fn builtin_object(
423+
&self,
424+
prototype: ObjectPrototype<'a>,
425+
consumable: bool,
426+
) -> &'a mut ObjectValue<'a> {
427+
self.alloc(ObjectValue {
428+
consumable,
429+
consumed: Cell::new(false),
430+
consumed_as_prototype: Cell::new(false),
431+
cf_scope: self.root_cf_scope.unwrap(),
432+
keyed: allocator::HashMap::new_in(self.allocator).into(),
433+
unknown: ObjectProperty::new_in(self.allocator).into(),
434+
rest: Default::default(),
435+
prototype: Cell::new(prototype),
436+
mangling_group: Cell::new(None),
437+
})
438+
}
439+
}

crates/jsshaker/src/value/react_element.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,16 @@ impl<'a> ValueTrait<'a> for ReactElementValue<'a> {
179179
None
180180
}
181181
}
182+
183+
impl<'a> crate::analyzer::Factory<'a> {
184+
pub fn react_element(&self, tag: Entity<'a>, props: Entity<'a>) -> Entity<'a> {
185+
self
186+
.alloc(ReactElementValue {
187+
consumed: Cell::new(false),
188+
tag,
189+
props,
190+
deps: RefCell::new(self.vec()),
191+
})
192+
.into()
193+
}
194+
}

0 commit comments

Comments
 (0)