From 4bb1dca6730cf630cc8c53db2bfd03f2d0f3dd8f Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY <17974631+IWANABETHATGUY@users.noreply.github.com> Date: Sat, 23 Nov 2024 15:21:30 +0000 Subject: [PATCH] feat(oxc_transformer): ReplaceGlobalDefines for ChainExpr (#7433) --- .../src/plugins/replace_global_defines.rs | 91 +++++++++++++------ .../plugins/replace_global_defines.rs | 15 +++ 2 files changed, 78 insertions(+), 28 deletions(-) diff --git a/crates/oxc_transformer/src/plugins/replace_global_defines.rs b/crates/oxc_transformer/src/plugins/replace_global_defines.rs index fbb5f38f410ba..72f08f963bfa6 100644 --- a/crates/oxc_transformer/src/plugins/replace_global_defines.rs +++ b/crates/oxc_transformer/src/plugins/replace_global_defines.rs @@ -254,39 +254,31 @@ impl<'a> ReplaceGlobalDefines<'a> { fn replace_dot_defines(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { match expr { + Expression::ChainExpression(chain) => { + let Some(new_expr) = + chain.expression.as_member_expression_mut().and_then(|item| match item { + MemberExpression::ComputedMemberExpression( + ref mut computed_member_expr, + ) => self.replace_dot_computed_member_expr(ctx, computed_member_expr), + MemberExpression::StaticMemberExpression(ref mut member) => { + self.replace_dot_static_member_expr(ctx, member) + } + MemberExpression::PrivateFieldExpression(_) => None, + }) + else { + return; + }; + *expr = new_expr; + } Expression::StaticMemberExpression(member) => { - for dot_define in &self.config.0.dot { - if Self::is_dot_define( - ctx.symbols(), - dot_define, - DotDefineMemberExpression::StaticMemberExpression(member), - ) { - let value = self.parse_value(&dot_define.value); - *expr = value; - return; - } - } - for meta_property_define in &self.config.0.meta_property { - if Self::is_meta_property_define(meta_property_define, member) { - let value = self.parse_value(&meta_property_define.value); - *expr = value; - return; - } + if let Some(new_expr) = self.replace_dot_static_member_expr(ctx, member) { + *expr = new_expr; } } Expression::ComputedMemberExpression(member) => { - for dot_define in &self.config.0.dot { - if Self::is_dot_define( - ctx.symbols(), - dot_define, - DotDefineMemberExpression::ComputedMemberExpression(member), - ) { - let value = self.parse_value(&dot_define.value); - *expr = value; - return; - } + if let Some(new_expr) = self.replace_dot_computed_member_expr(ctx, member) { + *expr = new_expr; } - // TODO: meta_property_define } Expression::MetaProperty(meta_property) => { if let Some(ref replacement) = self.config.0.import_meta { @@ -301,6 +293,49 @@ impl<'a> ReplaceGlobalDefines<'a> { } } + fn replace_dot_computed_member_expr( + &mut self, + ctx: &mut TraverseCtx<'a>, + member: &mut ComputedMemberExpression<'a>, + ) -> Option> { + for dot_define in &self.config.0.dot { + if Self::is_dot_define( + ctx.symbols(), + dot_define, + DotDefineMemberExpression::ComputedMemberExpression(member), + ) { + let value = self.parse_value(&dot_define.value); + return Some(value); + } + } + // TODO: meta_property_define + None + } + + fn replace_dot_static_member_expr( + &mut self, + ctx: &mut TraverseCtx<'a>, + member: &mut StaticMemberExpression<'a>, + ) -> Option> { + for dot_define in &self.config.0.dot { + if Self::is_dot_define( + ctx.symbols(), + dot_define, + DotDefineMemberExpression::StaticMemberExpression(member), + ) { + let value = self.parse_value(&dot_define.value); + return Some(value); + } + } + for meta_property_define in &self.config.0.meta_property { + if Self::is_meta_property_define(meta_property_define, member) { + let value = self.parse_value(&meta_property_define.value); + return Some(value); + } + } + None + } + pub fn is_meta_property_define( meta_define: &MetaPropertyDefine, member: &StaticMemberExpression<'a>, diff --git a/crates/oxc_transformer/tests/integrations/plugins/replace_global_defines.rs b/crates/oxc_transformer/tests/integrations/plugins/replace_global_defines.rs index ae610853fc666..84ac9bbf8d5bf 100644 --- a/crates/oxc_transformer/tests/integrations/plugins/replace_global_defines.rs +++ b/crates/oxc_transformer/tests/integrations/plugins/replace_global_defines.rs @@ -89,3 +89,18 @@ fn dot_with_postfix_mixed() { test("import.meta.somethingelse", "metaProperty", config.clone()); test("import.meta", "1", config); } + +#[test] +fn optional_chain() { + let config = ReplaceGlobalDefinesConfig::new(&[("a.b.c", "1")]).unwrap(); + test("a.b.c", "1", config.clone()); + test("a?.b.c", "1", config.clone()); + test("a.b?.c", "1", config.clone()); + test("a['b']['c']", "1", config.clone()); + test("a?.['b']['c']", "1", config.clone()); + test("a['b']?.['c']", "1", config.clone()); + + test_same("a[b][c]", config.clone()); + test_same("a?.[b][c]", config.clone()); + test_same("a[b]?.[c]", config.clone()); +}