Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SWC fold from globals replacer #9830

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Improve unit-tests for global replacer
  • Loading branch information
pyamada-atlassian committed Jul 2, 2024
commit 94cf7026838bd8afce29fa8f2786835a8b7d6579
69 changes: 58 additions & 11 deletions packages/transformers/js/core/src/global_replacer.rs
Original file line number Diff line number Diff line change
@@ -212,7 +212,23 @@ mod test {

use crate::global_replacer::GlobalReplacer;
use crate::test_utils::{run_visit, RunTestContext, RunVisitResult};
use crate::DependencyKind;
use crate::{DependencyDescriptor, DependencyKind};

fn make_global_replacer(
run_test_context: RunTestContext,
items: &mut Vec<DependencyDescriptor>,
) -> GlobalReplacer {
GlobalReplacer {
source_map: run_test_context.source_map,
items,
global_mark: run_test_context.global_mark,
globals: Default::default(),
project_root: Path::new("project-root"),
filename: Path::new("filename"),
unresolved_mark: run_test_context.unresolved_mark,
scope_hoist: false,
}
}

#[test]
fn test_globals_visitor_with_require_process() {
@@ -222,16 +238,7 @@ mod test {
r#"
console.log(process.test);
"#,
|run_test_context: RunTestContext| GlobalReplacer {
source_map: run_test_context.source_map,
items: &mut items,
global_mark: run_test_context.global_mark,
globals: Default::default(),
project_root: Path::new("project-root"),
filename: Path::new("filename"),
unresolved_mark: run_test_context.unresolved_mark,
scope_hoist: false,
},
|run_test_context: RunTestContext| make_global_replacer(run_test_context, &mut items),
);
assert_eq!(
output_code,
@@ -243,4 +250,44 @@ console.log(process.test);
assert_eq!(items[0].kind, DependencyKind::Require);
assert_eq!(items[0].specifier, JsWord::from("process"));
}

#[test]
fn test_transforms_computed_property() {
let mut items = vec![];

let RunVisitResult { output_code, .. } = run_visit(
r#"
object[process.test];
object[__dirname];
"#,
|run_test_context: RunTestContext| make_global_replacer(run_test_context, &mut items),
);
assert_eq!(
output_code,
r#"var process = require("process");
var __dirname = "..";
object[process.test];
object[__dirname];
"#
);
}

#[test]
fn test_does_not_transform_member_property() {
let mut items = vec![];

let RunVisitResult { output_code, .. } = run_visit(
r#"
object.process.test;
object.__filename;
"#,
|run_test_context: RunTestContext| make_global_replacer(run_test_context, &mut items),
);
assert_eq!(
output_code,
r#"object.process.test;
object.__filename;
"#
);
}
}
Loading