Skip to content

Commit b9a4630

Browse files
authored
fix: don't use the globalThis shim when there are no shimmed globals (#517)
1 parent fb01616 commit b9a4630

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

rs-lib/src/visitors/globals.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ fn visit_children(node: Node, import_name: &str, context: &mut Context) {
8787
let ident_text = ident.text_fast(context.program);
8888

8989
if is_unresolved_context {
90-
// check to replace globalThis
90+
// check to replace globalThis, which is only necessary when there
91+
// are globals to merge into it
9192
if ident_text == "globalThis" {
92-
if let Some(text_change) =
93-
get_global_this_text_change(ident, import_name, context)
94-
{
95-
context.text_changes.push(text_change);
96-
context.import_shim = true;
93+
if !context.shim_global_names.is_empty() {
94+
if let Some(text_change) =
95+
get_global_this_text_change(ident, import_name, context)
96+
{
97+
context.text_changes.push(text_change);
98+
context.import_shim = true;
99+
}
97100
}
98101
return;
99102
}

rs-lib/tests/integration_test.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,28 @@ async fn transform_global_this_shim() {
461461
.await;
462462
}
463463

464+
#[tokio::test]
465+
async fn no_transform_global_this_when_no_shims() {
466+
// there's nothing to merge into `globalThis`, so it should be left
467+
// alone and no shim file should be created
468+
let text = concat!(
469+
"globalThis.Deno.readTextFile();",
470+
"true ? globalThis : globalThis;",
471+
"typeof globalThis;",
472+
"globalThis == null;",
473+
"type Test = typeof globalThis;",
474+
);
475+
let result = TestBuilder::new()
476+
.with_loader(|loader| {
477+
loader.add_local_file("/mod.ts", text);
478+
})
479+
.transform()
480+
.await
481+
.unwrap();
482+
483+
assert_files!(result.main.files, &[("mod.ts", text)]);
484+
}
485+
464486
#[tokio::test]
465487
async fn no_transform_window() {
466488
// `window` is not a global in Node.js nor Deno, so leave it alone

0 commit comments

Comments
 (0)