Skip to content

Commit

Permalink
fix overlap replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Dec 3, 2024
1 parent b4f3812 commit 04312f6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
45 changes: 37 additions & 8 deletions crates/oxc_transformer/src/plugins/replace_global_defines.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cmp::Ordering, sync::Arc};

use lazy_static::lazy_static;
use oxc_allocator::Allocator;
use oxc_allocator::{Address, Allocator, GetAddress};
use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_parser::Parser;
Expand Down Expand Up @@ -220,12 +220,25 @@ pub struct ReplaceGlobalDefinesReturn {
pub struct ReplaceGlobalDefines<'a> {
allocator: &'a Allocator,
config: ReplaceGlobalDefinesConfig,
ast_node_lock: Option<Address>,
}

impl<'a> Traverse<'a> for ReplaceGlobalDefines<'a> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.replace_identifier_defines(expr, ctx);
self.replace_dot_defines(expr, ctx);
if self.ast_node_lock.is_some() {
return;
}
let mut flag = self.replace_identifier_defines(expr, ctx);
flag |= self.replace_dot_defines(expr, ctx);
if flag {
self.ast_node_lock = Some(expr.address());
}
}

fn exit_expression(&mut self, node: &mut Expression<'a>, _ctx: &mut TraverseCtx<'a>) {
if self.ast_node_lock == Some(node.address()) {
self.ast_node_lock = None;
}
}

fn enter_assignment_expression(
Expand All @@ -239,7 +252,7 @@ impl<'a> Traverse<'a> for ReplaceGlobalDefines<'a> {

impl<'a> ReplaceGlobalDefines<'a> {
pub fn new(allocator: &'a Allocator, config: ReplaceGlobalDefinesConfig) -> Self {
Self { allocator, config }
Self { allocator, config, ast_node_lock: None }
}

pub fn build(
Expand All @@ -260,11 +273,16 @@ impl<'a> ReplaceGlobalDefines<'a> {
Parser::new(self.allocator, source_text, SourceType::default()).parse_expression().unwrap()
}

fn replace_identifier_defines(&self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
fn replace_identifier_defines(
&self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> bool {
match expr {
Expression::Identifier(ident) => {
if let Some(new_expr) = self.replace_identifier_define_impl(ident, ctx) {
*expr = new_expr;
return true;
}
}
Expression::ThisExpression(_)
Expand All @@ -275,12 +293,14 @@ impl<'a> ReplaceGlobalDefines<'a> {
if key.as_str() == "this" {
let value = self.parse_value(value);
*expr = value;
break;

return true;
}
}
}
_ => {}
}
false
}

fn replace_identifier_define_impl(
Expand Down Expand Up @@ -327,7 +347,11 @@ impl<'a> ReplaceGlobalDefines<'a> {
}
}

fn replace_dot_defines(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
fn replace_dot_defines(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> bool {
match expr {
Expression::ChainExpression(chain) => {
let Some(new_expr) =
Expand All @@ -341,18 +365,21 @@ impl<'a> ReplaceGlobalDefines<'a> {
MemberExpression::PrivateFieldExpression(_) => None,
})
else {
return;
return false;
};
*expr = new_expr;
return true;
}
Expression::StaticMemberExpression(member) => {
if let Some(new_expr) = self.replace_dot_static_member_expr(ctx, member) {
*expr = new_expr;
return true;
}
}
Expression::ComputedMemberExpression(member) => {
if let Some(new_expr) = self.replace_dot_computed_member_expr(ctx, member) {
*expr = new_expr;
return true;
}
}
Expression::MetaProperty(meta_property) => {
Expand All @@ -361,11 +388,13 @@ impl<'a> ReplaceGlobalDefines<'a> {
{
let value = self.parse_value(replacement);
*expr = value;
return true;
}
}
}
_ => {}
}
false
}

fn replace_dot_computed_member_expr(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn dot() {
test("process['env'].NODE_ENV", "production", config.clone());
}

#[ignore]
#[test]
fn dot_with_overlap() {
let config = ReplaceGlobalDefinesConfig::new(&[
Expand All @@ -68,7 +67,7 @@ fn dot_with_overlap() {
])
.unwrap();
test("import.meta.env", "__foo__", config.clone());
test("import.meta.env.NODE_ENV", "import.meta.env.NODE_ENV", config.clone());
test("import.meta.env.FOO", "import.meta.env.FOO", config.clone());
}

#[test]
Expand Down

0 comments on commit 04312f6

Please sign in to comment.