Skip to main content

stylex_transform/transform/fold/
fold_computed_prop_name_impl.rs

1use stylex_macros::stylex_panic;
2use swc_core::{
3  common::comments::Comments,
4  ecma::{
5    ast::{ComputedPropName, Ident},
6    visit::FoldWith,
7  },
8};
9
10use crate::{
11  StyleXTransform,
12  shared::utils::{ast::convertors::convert_expr_to_str, common::increase_ident_count},
13};
14use stylex_constants::constants::messages::EXPRESSION_IS_NOT_A_STRING;
15use stylex_enums::core::TransformationCycle;
16
17impl<C> StyleXTransform<C>
18where
19  C: Comments,
20{
21  pub(crate) fn fold_computed_prop_name_impl(
22    &mut self,
23    computed_prop_name: ComputedPropName,
24  ) -> ComputedPropName {
25    if self.state.cycle == TransformationCycle::Skip {
26      return computed_prop_name;
27    }
28
29    if self.state.cycle == TransformationCycle::StateFilling && computed_prop_name.expr.is_lit() {
30      let expt_str = convert_expr_to_str(
31        &computed_prop_name.expr,
32        &mut self.state,
33        &Default::default(),
34      );
35
36      increase_ident_count(
37        &mut self.state,
38        &Ident::from(
39          match expt_str {
40            Some(s) => s,
41            None => stylex_panic!("{}", EXPRESSION_IS_NOT_A_STRING),
42          }
43          .as_str(),
44        ),
45      );
46    }
47
48    computed_prop_name.fold_children_with(self)
49  }
50}