diff --git a/crates/oxc_transformer_plugins/src/replace_global_defines.rs b/crates/oxc_transformer_plugins/src/replace_global_defines.rs index ec6def011c653..83d25ecb9c098 100644 --- a/crates/oxc_transformer_plugins/src/replace_global_defines.rs +++ b/crates/oxc_transformer_plugins/src/replace_global_defines.rs @@ -473,6 +473,11 @@ impl<'a> ReplaceGlobalDefines<'a> { meta_define: &MetaPropertyDefine, member: &StaticMemberExpression<'a>, ) -> bool { + enum WildCardStatus { + None, + Pending, + Matched, + } if meta_define.parts.is_empty() && meta_define.postfix_wildcard { match &member.object { Expression::MetaProperty(meta) => { @@ -488,6 +493,11 @@ impl<'a> ReplaceGlobalDefines<'a> { let mut is_full_match = true; let mut i = meta_define.parts.len() - 1; let mut has_matched_part = false; + let mut wildcard_status = if meta_define.postfix_wildcard { + WildCardStatus::Pending + } else { + WildCardStatus::None + }; loop { let part = &meta_define.parts[i]; let matched = cur_part_name.as_str() == part; @@ -501,10 +511,15 @@ impl<'a> ReplaceGlobalDefines<'a> { // import.res.meta.env // should not matched // ``` // So we use has_matched_part to track if any part has matched. - - if !meta_define.postfix_wildcard || has_matched_part { + // `None` means there is no postfix wildcard defined, so any part not matched should return false + // `Matched` means there is a postfix wildcard defined, and already matched a part, so any further + // not matched part should return false + if matches!(wildcard_status, WildCardStatus::None | WildCardStatus::Matched) + || has_matched_part + { return false; } + wildcard_status = WildCardStatus::Matched; } current_part_member_expression = if let Some(member) = current_part_member_expression { diff --git a/crates/oxc_transformer_plugins/tests/integrations/replace_global_defines.rs b/crates/oxc_transformer_plugins/tests/integrations/replace_global_defines.rs index c478145695b0b..14ae55d2c1d89 100644 --- a/crates/oxc_transformer_plugins/tests/integrations/replace_global_defines.rs +++ b/crates/oxc_transformer_plugins/tests/integrations/replace_global_defines.rs @@ -122,9 +122,10 @@ fn dot_with_postfix_mixed() { ("import.meta", "1"), ]); test("foo(import.meta.env.result)", "foo(void 0)", &config); - test("foo(import.meta.env.result.many.nested)", "foo(void 0)", &config); + test("foo(import.meta.env.result.many.nested)", "foo((void 0).many.nested)", &config); test("foo(import.meta.env)", "foo(env)", &config); test("foo(import.meta.somethingelse)", "foo(metaProperty)", &config); + test("foo(import.meta.somethingelse.nested.one)", "foo(metaProperty.nested.one)", &config); test("foo(import.meta)", "foo(1)", &config); }