Skip to main content

stylex_transform/transform/fold/
fold_expr.rs

1use swc_core::{
2  common::comments::Comments,
3  ecma::{ast::Expr, visit::FoldWith},
4};
5
6use crate::{
7  StyleXTransform,
8  shared::utils::common::{normalize_expr, stable_hash},
9};
10use stylex_enums::core::TransformationCycle;
11
12impl<C> StyleXTransform<C>
13where
14  C: Comments,
15{
16  pub(crate) fn fold_expr_impl(&mut self, mut expr: Expr) -> Expr {
17    if self.state.cycle == TransformationCycle::Skip {
18      return expr;
19    }
20
21    let normalized_expr = normalize_expr(&mut expr);
22
23    // During Initializing, transform compiled JSX/VDOM calls with sx prop:
24    // React/Vue: _jsx("div", { sx: expr }) → _jsx("div", { ...stylex.props(expr) })
25    // Solid.js:  _$setAttribute(el, "sx", expr) → _$spread(el, _$mergeProps(() => stylex.props(expr)), false, true)
26    if self.state.cycle == TransformationCycle::Initializing {
27      if let Some(transformed) = self.transform_sx_in_compiled_jsx(normalized_expr) {
28        return transformed.fold_children_with(self);
29      }
30      if let Some(transformed) = self.transform_sx_in_solid_set_attribute(normalized_expr) {
31        return transformed.fold_children_with(self);
32      }
33    }
34
35    if self.state.cycle == TransformationCycle::StateFilling
36      && let Some(call_expr) = normalized_expr.as_call()
37    {
38      self
39        .state
40        .all_call_expressions
41        .insert(stable_hash(&call_expr), call_expr.clone());
42    }
43
44    if (self.state.cycle == TransformationCycle::TransformEnter
45      || self.state.cycle == TransformationCycle::TransformExit)
46      && let Some(value) = self.transform_call_expression(normalized_expr)
47    {
48      return value;
49    }
50
51    expr.fold_children_with(self)
52  }
53}