Skip to content

Commit 8e42209

Browse files
authored
fix: keep the property name when shimming a shorthand property (#510)
1 parent a59df0a commit 8e42209

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

rs-lib/src/visitors/globals.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn visit_children(node: Node, import_name: &str, context: &mut Context) {
100100
} else {
101101
context.text_changes.push(TextChange {
102102
range: create_range(ident.start(), ident.end(), context),
103-
new_text: "globalThis".to_string(),
103+
new_text: get_replacement_text(ident, "globalThis", context),
104104
});
105105
}
106106
}
@@ -126,7 +126,11 @@ fn visit_children(node: Node, import_name: &str, context: &mut Context) {
126126
{
127127
context.text_changes.push(TextChange {
128128
range: create_range(ident.start(), ident.end(), context),
129-
new_text: format!("{}.{}", import_name, ident_text),
129+
new_text: get_replacement_text(
130+
ident,
131+
&format!("{}.{}", import_name, ident_text),
132+
context,
133+
),
130134
});
131135
context.import_shim = true;
132136
return;
@@ -171,11 +175,30 @@ fn get_global_this_text_change(
171175
} else {
172176
Some(TextChange {
173177
range: create_range(ident.start(), ident.end(), context),
174-
new_text: format!("{}.dntGlobalThis", import_name),
178+
new_text: get_replacement_text(
179+
ident,
180+
&format!("{}.dntGlobalThis", import_name),
181+
context,
182+
),
175183
})
176184
}
177185
}
178186

187+
/// Gets the text to replace an identifier with, expanding an object literal's
188+
/// shorthand property so that the property name is kept
189+
/// (ex. `{ prompt }` -> `{ prompt: dntShim.prompt }`).
190+
fn get_replacement_text(
191+
ident: &Ident,
192+
new_text: &str,
193+
context: &Context,
194+
) -> String {
195+
if matches!(ident.parent(), Node::ObjectLit(_)) {
196+
format!("{}: {}", ident.text_fast(context.program), new_text)
197+
} else {
198+
new_text.to_string()
199+
}
200+
}
201+
179202
fn should_ignore_global_this(ident: &Ident, context: &Context) -> bool {
180203
if has_ignore_comment(ident.into(), context)
181204
|| is_declaration_ident(ident.into())

rs-lib/tests/integration_test.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,37 @@ async fn transform_shims() {
7878
"\nconst obj = { test: dntShim.Deno };"
7979
),
8080
),
81+
(
82+
// the shorthand property name must be kept
83+
"const obj = { Deno, setTimeout };",
84+
concat!(
85+
r#"import * as dntShim from "./_dnt.shims.js";"#,
86+
"\nconst obj = { Deno: dntShim.Deno, setTimeout: dntShim.setTimeout };"
87+
),
88+
),
89+
(
90+
// only a shorthand property gets the property name
91+
"const obj = { Deno: Deno, [Deno]: 1, ...Deno };",
92+
concat!(
93+
r#"import * as dntShim from "./_dnt.shims.js";"#,
94+
"
95+
const obj = { Deno: dntShim.Deno, [dntShim.Deno]: 1, ...dntShim.Deno };"
96+
),
97+
),
98+
(
99+
"const obj = { globalThis };",
100+
concat!(
101+
r#"import * as dntShim from "./_dnt.shims.js";"#,
102+
"\nconst obj = { globalThis: dntShim.dntGlobalThis };"
103+
),
104+
),
105+
(
106+
"const obj = { window };",
107+
concat!(
108+
r#"import * as dntShim from "./_dnt.shims.js";"#,
109+
"\nconst obj = { window: dntShim.dntGlobalThis };"
110+
),
111+
),
81112
(
82113
concat!(
83114
"const decl01 = Deno;\n",

0 commit comments

Comments
 (0)