Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions crates/oxc_transformer_plugins/src/replace_global_defines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading