Skip to content

Commit

Permalink
refactor: rename ModuleCycle to TransformationCycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwlad90 committed Sep 13, 2024
1 parent 5780297 commit a8a54a3
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 77 deletions.
6 changes: 6 additions & 0 deletions .changeset/curly-lions-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@stylexswc/swc-plugin': patch
'@stylexswc/nextjs-plugin': patch
---

rename ModuleCycle to TransformationCycle
2 changes: 1 addition & 1 deletion packages/swc-plugin/src/shared/enums/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Represents the current state of a plugin for a file.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Copy)]
pub(crate) enum ModuleCycle {
pub(crate) enum TransformationCycle {
// The plugin is being processed
TransformEnter,
// The plugin has been processed
Expand Down
6 changes: 3 additions & 3 deletions packages/swc-plugin/src/shared/structures/state_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use swc_core::{
};

use crate::shared::enums::{
core::ModuleCycle,
core::TransformationCycle,
data_structures::{
import_path_resolution::{ImportPathResolution, ImportPathResolutionType},
style_vars_to_keep::StyleVarsToKeep,
Expand Down Expand Up @@ -94,7 +94,7 @@ pub struct StateManager {
pub(crate) injected_keyframes: IndexMap<String, Box<InjectableStyle>>,
pub(crate) top_imports: Vec<ImportDecl>,

pub(crate) cycle: ModuleCycle,
pub(crate) cycle: TransformationCycle,
}

impl Default for StateManager {
Expand Down Expand Up @@ -146,7 +146,7 @@ impl StateManager {

injected_keyframes: IndexMap::new(),

cycle: ModuleCycle::Initializing,
cycle: TransformationCycle::Initializing,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use swc_core::{

use crate::{
shared::{
enums::core::ModuleCycle,
enums::core::TransformationCycle,
utils::{ast::convertors::expr_to_str, common::increase_ident_count},
},
ModuleTransformVisitor,
Expand All @@ -22,11 +22,11 @@ where
&mut self,
computed_prop_name: ComputedPropName,
) -> ComputedPropName {
if self.state.cycle == ModuleCycle::Skip {
if self.state.cycle == TransformationCycle::Skip {
return computed_prop_name;
}

if self.state.cycle == ModuleCycle::StateFilling && computed_prop_name.expr.is_lit() {
if self.state.cycle == TransformationCycle::StateFilling && computed_prop_name.expr.is_lit() {
let expt_str = expr_to_str(
&computed_prop_name.expr,
&mut self.state,
Expand Down
6 changes: 3 additions & 3 deletions packages/swc-plugin/src/transform/fold/fold_export_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use swc_core::{
};

use crate::{
shared::{enums::core::ModuleCycle, utils::common::increase_ident_count_by_count},
shared::{enums::core::TransformationCycle, utils::common::increase_ident_count_by_count},
ModuleTransformVisitor,
};

Expand All @@ -16,11 +16,11 @@ where
C: Comments,
{
pub(crate) fn fold_export_decl_impl(&mut self, export_decl: ExportDecl) -> ExportDecl {
if self.state.cycle == ModuleCycle::Skip {
if self.state.cycle == TransformationCycle::Skip {
return export_decl;
}

if self.state.cycle == ModuleCycle::StateFilling {
if self.state.cycle == TransformationCycle::StateFilling {
if let Decl::Var(var_decl) = &export_decl.decl {
for decl in &var_decl.decls {
if let Some(ident) = decl.name.as_ident() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_core::{
};

use crate::{
shared::{enums::core::ModuleCycle, utils::common::normalize_expr},
shared::{enums::core::TransformationCycle, utils::common::normalize_expr},
ModuleTransformVisitor,
};

Expand All @@ -16,15 +16,15 @@ where
&mut self,
mut export_default_expr: ExportDefaultExpr,
) -> ExportDefaultExpr {
if self.state.cycle == ModuleCycle::Skip {
if self.state.cycle == TransformationCycle::Skip {
return export_default_expr;
}

if self.state.cycle == ModuleCycle::StateFilling {
if self.state.cycle == TransformationCycle::StateFilling {
return export_default_expr.fold_children_with(self);
}

if self.state.cycle == ModuleCycle::TransformEnter || self.state.cycle == ModuleCycle::TransformExit {
if self.state.cycle == TransformationCycle::TransformEnter || self.state.cycle == TransformationCycle::TransformExit {
let normalized_expr = normalize_expr(&mut export_default_expr.expr);

if let Some(value) = self.transform_call_expression(normalized_expr) {
Expand Down
8 changes: 4 additions & 4 deletions packages/swc-plugin/src/transform/fold/fold_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ use swc_core::{
ecma::{ast::Expr, visit::FoldWith},
};

use crate::{shared::enums::core::ModuleCycle, ModuleTransformVisitor};
use crate::{shared::enums::core::TransformationCycle, ModuleTransformVisitor};

impl<C> ModuleTransformVisitor<C>
where
C: Comments,
{
pub(crate) fn fold_expr_impl(&mut self, mut expr: Expr) -> Expr {
if self.state.cycle == ModuleCycle::Skip {
if self.state.cycle == TransformationCycle::Skip {
return expr;
}

if self.state.cycle == ModuleCycle::StateFilling {
if self.state.cycle == TransformationCycle::StateFilling {
if let Some(call_expr) = expr.as_call() {
self.state.all_call_expressions.push(call_expr.clone());
}
}

if self.state.cycle == ModuleCycle::TransformEnter || self.state.cycle == ModuleCycle::TransformExit {
if self.state.cycle == TransformationCycle::TransformEnter || self.state.cycle == TransformationCycle::TransformExit {
if let Some(value) = self.transform_call_expression(&mut expr) {
return value;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/swc-plugin/src/transform/fold/fold_ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use swc_core::{common::comments::Comments, ecma::ast::Ident};

use crate::{
shared::{
enums::core::ModuleCycle,
enums::core::TransformationCycle,
utils::common::{increase_ident_count, reduce_ident_count},
},
ModuleTransformVisitor,
Expand All @@ -14,10 +14,10 @@ where
{
pub(crate) fn fold_ident_impl(&mut self, ident: Ident) -> Ident {
match self.state.cycle {
ModuleCycle::StateFilling => {
TransformationCycle::StateFilling => {
increase_ident_count(&mut self.state, &ident);
}
ModuleCycle::Recounting => {
TransformationCycle::Recounting => {
reduce_ident_count(&mut self.state, &ident);
}
_ => {}
Expand Down
6 changes: 3 additions & 3 deletions packages/swc-plugin/src/transform/fold/fold_import_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use swc_core::{

use crate::{
shared::{
constants::messages::MUST_BE_DEFAULT_IMPORT, enums::core::ModuleCycle,
constants::messages::MUST_BE_DEFAULT_IMPORT, enums::core::TransformationCycle,
structures::named_import_source::ImportSources,
},
ModuleTransformVisitor,
Expand All @@ -16,11 +16,11 @@ where
C: Comments,
{
pub(crate) fn fold_import_decl_impl(&mut self, import_decl: ImportDecl) -> ImportDecl {
if self.state.cycle == ModuleCycle::Skip {
if self.state.cycle == TransformationCycle::Skip {
return import_decl;
}

if self.state.cycle == ModuleCycle::Initializing {
if self.state.cycle == TransformationCycle::Initializing {
if import_decl.type_only {
return import_decl;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/swc-plugin/src/transform/fold/fold_member_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use swc_core::{
use crate::{
shared::{
enums::{
core::ModuleCycle,
core::TransformationCycle,
data_structures::style_vars_to_keep::{NonNullProp, NonNullProps, StyleVarsToKeep},
},
utils::common::{increase_ident_count, increase_member_ident_count, reduce_member_ident_count},
Expand All @@ -23,20 +23,20 @@ where
{
pub(crate) fn fold_member_expr_impl(&mut self, member_expression: MemberExpr) -> MemberExpr {
match self.state.cycle {
ModuleCycle::Skip => member_expression,
ModuleCycle::StateFilling | ModuleCycle::Recounting => {
TransformationCycle::Skip => member_expression,
TransformationCycle::StateFilling | TransformationCycle::Recounting => {
if let Some(obj_ident) = member_expression.obj.as_ident() {
match self.state.cycle {
ModuleCycle::StateFilling => {
TransformationCycle::StateFilling => {
increase_member_ident_count(&mut self.state, &obj_ident.sym)
}
ModuleCycle::Recounting => {reduce_member_ident_count(&mut self.state, &obj_ident.sym)},
TransformationCycle::Recounting => {reduce_member_ident_count(&mut self.state, &obj_ident.sym)},
_ => {}
}
}
member_expression.fold_children_with(self)
}
ModuleCycle::PreCleaning => {
TransformationCycle::PreCleaning => {
if let Expr::Ident(ident) = member_expression.obj.as_ref() {
let obj_ident_name = ident.sym.to_string();
if self.state.style_map.contains_key(&obj_ident_name) {
Expand Down
6 changes: 3 additions & 3 deletions packages/swc-plugin/src/transform/fold/fold_member_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use swc_core::{
ecma::{ast::MemberProp, visit::FoldWith},
};

use crate::{shared::enums::core::ModuleCycle, ModuleTransformVisitor};
use crate::{shared::enums::core::TransformationCycle, ModuleTransformVisitor};

impl<C> ModuleTransformVisitor<C>
where
C: Comments,
{
pub(crate) fn fold_member_prop_impl(&mut self, member_prop: MemberProp) -> MemberProp {
if self.state.cycle == ModuleCycle::Skip {
if self.state.cycle == TransformationCycle::Skip {
return member_prop;
}

if (self.state.cycle == ModuleCycle::StateFilling || self.state.cycle == ModuleCycle::Recounting)
if (self.state.cycle == TransformationCycle::StateFilling || self.state.cycle == TransformationCycle::Recounting)
&& member_prop.is_ident()
{
return member_prop;
Expand Down
16 changes: 8 additions & 8 deletions packages/swc-plugin/src/transform/fold/fold_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use swc_core::{

use crate::{
shared::{
enums::core::ModuleCycle, structures::meta_data::MetaData,
enums::core::TransformationCycle, structures::meta_data::MetaData,
utils::common::fill_top_level_expressions,
},
ModuleTransformVisitor,
Expand All @@ -19,19 +19,19 @@ where
let mut module = module.fold_children_with(self);

if !self.state.import_paths.is_empty() {
self.state.cycle = ModuleCycle::StateFilling;
self.state.cycle = TransformationCycle::StateFilling;
module = module.fold_children_with(self);

fill_top_level_expressions(&module, &mut self.state);

self.state.cycle = ModuleCycle::TransformEnter;
self.state.cycle = TransformationCycle::TransformEnter;
module = module.fold_children_with(self);

self.state.cycle = ModuleCycle::TransformExit;
self.state.cycle = TransformationCycle::TransformExit;
module = module.fold_children_with(self);

if self.state.options.runtime_injection.is_some() {
self.state.cycle = ModuleCycle::InjectStyles;
self.state.cycle = TransformationCycle::InjectStyles;
module = module.fold_children_with(self);
} else {
// Preparing stylex metadata for css extraction
Expand All @@ -57,10 +57,10 @@ where
);
}

self.state.cycle = ModuleCycle::PreCleaning;
self.state.cycle = TransformationCycle::PreCleaning;
module = module.fold_children_with(self);

self.state.cycle = ModuleCycle::Cleaning;
self.state.cycle = TransformationCycle::Cleaning;

// NOTE: Reversing the module body to clean the module items in the correct order,
// so removing unused variable declarations will more efficient
Expand All @@ -73,7 +73,7 @@ where

module
} else {
self.state.cycle = ModuleCycle::Skip;
self.state.cycle = TransformationCycle::Skip;
module
}
}
Expand Down
22 changes: 11 additions & 11 deletions packages/swc-plugin/src/transform/fold/fold_module_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use swc_core::{
};

use crate::{
shared::{enums::core::ModuleCycle, utils::ast::factories::binding_ident_factory},
shared::{enums::core::TransformationCycle, utils::ast::factories::binding_ident_factory},
ModuleTransformVisitor,
};

Expand All @@ -18,19 +18,19 @@ where
{
pub(crate) fn fold_module_items(&mut self, module_items: Vec<ModuleItem>) -> Vec<ModuleItem> {
match self.state.cycle {
ModuleCycle::Skip => module_items,
ModuleCycle::Initializing => {
TransformationCycle::Skip => module_items,
TransformationCycle::Initializing => {
let transformed_module_items = module_items.fold_children_with(self);

if self.state.import_paths.is_empty() {
self.state.cycle = ModuleCycle::Skip;
self.state.cycle = TransformationCycle::Skip;

return transformed_module_items;
}

transformed_module_items
}
ModuleCycle::StateFilling => {
TransformationCycle::StateFilling => {
module_items.iter().for_each(|module_item| {
if let ModuleItem::Stmt(Stmt::Decl(Decl::Var(var_decl))) = module_item {
var_decl.decls.iter().for_each(|decl| {
Expand All @@ -45,10 +45,10 @@ where

module_items.fold_children_with(self)
}
ModuleCycle::TransformEnter => module_items.fold_children_with(self),
ModuleCycle::TransformExit => module_items.fold_children_with(self),
ModuleCycle::PreCleaning => module_items.fold_children_with(self),
ModuleCycle::InjectStyles => {
TransformationCycle::TransformEnter => module_items.fold_children_with(self),
TransformationCycle::TransformExit => module_items.fold_children_with(self),
TransformationCycle::PreCleaning => module_items.fold_children_with(self),
TransformationCycle::InjectStyles => {
let mut result_module_items: Vec<ModuleItem> =
self.state.prepend_include_module_items.clone();

Expand Down Expand Up @@ -125,7 +125,7 @@ where

result_module_items
}
ModuleCycle::Cleaning => {
TransformationCycle::Cleaning => {
// We need it twice for a clear dead code after declaration transforms
let mut module_items = module_items.fold_children_with(self);

Expand All @@ -138,7 +138,7 @@ where

module_items
}
ModuleCycle::Recounting => module_items.fold_children_with(self),
TransformationCycle::Recounting => module_items.fold_children_with(self),
}
}
}
6 changes: 3 additions & 3 deletions packages/swc-plugin/src/transform/fold/fold_prop_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use swc_core::{
ecma::{ast::PropName, visit::FoldWith},
};

use crate::{shared::enums::core::ModuleCycle, ModuleTransformVisitor};
use crate::{shared::enums::core::TransformationCycle, ModuleTransformVisitor};

impl<C> ModuleTransformVisitor<C>
where
C: Comments,
{
pub(crate) fn fold_prop_name_impl(&mut self, prop_name: PropName) -> PropName {
match self.state.cycle {
ModuleCycle::Skip => prop_name,
ModuleCycle::StateFilling | ModuleCycle::Recounting if prop_name.is_ident() => prop_name,
TransformationCycle::Skip => prop_name,
TransformationCycle::StateFilling | TransformationCycle::Recounting if prop_name.is_ident() => prop_name,
_ => prop_name.fold_children_with(self),
}
}
Expand Down
Loading

0 comments on commit a8a54a3

Please sign in to comment.