Skip to content

Commit 07ed456

Browse files
committed
fix: stop rewriting the window global
`window` was being rewritten to `globalThis` (or `dntShim.dntGlobalThis` when the shim proxy was used), which broke browser detection because `typeof window === "object"` would then always be true. `window` doesn't exist in Deno anymore, so leave it alone.
1 parent f946d30 commit 07ed456

2 files changed

Lines changed: 10 additions & 44 deletions

File tree

rs-lib/src/visitors/globals.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,6 @@ 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-
// change `window` -> `globalThis`
91-
if ident_text == "window" {
92-
if !context.top_level_decls.contains("window")
93-
&& !has_ignore_comment(ident.into(), context)
94-
{
95-
if let Some(text_change) =
96-
get_global_this_text_change(ident, import_name, context)
97-
{
98-
context.text_changes.push(text_change);
99-
context.import_shim = true;
100-
} else {
101-
context.text_changes.push(TextChange {
102-
range: create_range(ident.start(), ident.end(), context),
103-
new_text: get_replacement_text(ident, "globalThis", context),
104-
});
105-
}
106-
}
107-
return;
108-
}
109-
11090
// check to replace globalThis
11191
if ident_text == "globalThis" {
11292
if let Some(text_change) =

rs-lib/tests/integration_test.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ const obj = { Deno: dntShim.Deno, [dntShim.Deno]: 1, ...dntShim.Deno };"
102102
"\nconst obj = { globalThis: dntShim.dntGlobalThis };"
103103
),
104104
),
105-
(
106-
"const obj = { window };",
107-
concat!(
108-
r#"import * as dntShim from "./_dnt.shims.js";"#,
109-
"\nconst obj = { window: dntShim.dntGlobalThis };"
110-
),
111-
),
112105
(
113106
concat!(
114107
"const decl01 = Deno;\n",
@@ -445,7 +438,6 @@ async fn transform_global_this_shim() {
445438
"type Test1 = typeof globalThis;",
446439
"type Test2 = typeof globalThis.Window;",
447440
"type Test3 = typeof globalThis.Deno;",
448-
"type Test4 = window.Something;",
449441
),
450442
concat!(
451443
r#"import * as dntShim from "./_dnt.shims.js";"#,
@@ -464,28 +456,22 @@ async fn transform_global_this_shim() {
464456
"type Test1 = typeof dntShim.dntGlobalThis;",
465457
"type Test2 = typeof globalThis.Window;",
466458
"type Test3 = typeof dntShim.Deno;",
467-
"type Test4 = globalThis.Something;",
468459
),
469460
)])
470461
.await;
471462
}
472463

473464
#[tokio::test]
474-
async fn transform_window() {
475-
assert_transforms(vec![
476-
(
477-
concat!("window.test = 5;", "window.Deno.test();",),
478-
concat!(
479-
r#"import * as dntShim from "./_dnt.shims.js";"#,
480-
"\nglobalThis.test = 5;",
481-
"dntShim.dntGlobalThis.Deno.test();",
482-
),
483-
),
484-
(
485-
// should be as-is because there's a declaration
486-
"const window = {}; window.test;",
487-
"const window = {}; window.test;",
488-
),
465+
async fn no_transform_window() {
466+
// `window` is not a global in Node.js nor Deno, so leave it alone
467+
// (ex. `typeof window === "object"` should stay a browser check)
468+
assert_identity_transforms(vec![
469+
"window.test = 5;",
470+
"window.Deno.test();",
471+
"const obj = { window };",
472+
r#"typeof window === "object";"#,
473+
"type Test = window.Something;",
474+
"const window = {}; window.test;",
489475
])
490476
.await;
491477
}

0 commit comments

Comments
 (0)