Skip to main content

stylex_transform/transform/fold/
fold_stmt.rs

1use swc_core::{
2  common::{comments::Comments, util::take::Take},
3  ecma::{
4    ast::{Decl, Stmt},
5    utils::IsDirective,
6    visit::FoldWith,
7  },
8};
9
10use crate::StyleXTransform;
11use stylex_enums::core::TransformationCycle;
12
13impl<C> StyleXTransform<C>
14where
15  C: Comments,
16{
17  pub(crate) fn fold_stmt_impl(&mut self, stmt: Stmt) -> Stmt {
18    if self.state.cycle == TransformationCycle::Skip {
19      return stmt;
20    }
21
22    if self.state.cycle == TransformationCycle::Cleaning {
23      let mut stmt = stmt.fold_children_with(self);
24
25      if let Some(Stmt::Decl(Decl::Var(var))) = stmt.as_ref()
26        && var.decls.is_empty()
27      {
28        // Variable declaration without declarator is invalid.
29        //
30        // After this, `stmt` becomes `Stmt::Empty`.
31        stmt.take();
32      }
33
34      stmt
35    } else {
36      stmt.fold_children_with(self)
37    }
38  }
39}