Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 33de70a

Browse files
authored
fix(mangler): handle cases where a var is declared in a block scope (#8706)
This PR fixes the mangler that it was outputting `function _() { { var a; let a; } }` for `function _() { { var x; let y; } }`. This caused this error: https://github.com/oxc-project/monitor-oxc/actions/runs/12962575667/job/36159286596#step:8:31 refs #8705
1 parent d982cdb commit 33de70a

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

crates/oxc_mangler/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,22 @@ impl Mangler {
232232
{
233233
slots[symbol_id.index()] = assigned_slot;
234234

235+
let declared_scope_id =
236+
ast_nodes.get_node(symbol_table.get_declaration(*symbol_id)).scope_id();
237+
235238
// Calculate the scope ids that this symbol is alive in.
236239
let lived_scope_ids = symbol_table
237240
.get_resolved_references(*symbol_id)
238241
.flat_map(|reference| {
239242
let used_scope_id = ast_nodes.get_node(reference.node_id()).scope_id();
240243
scope_tree.ancestors(used_scope_id).take_while(|s_id| *s_id != scope_id)
241244
})
245+
// also include scopes that this symbol was declared (for cases like `function foo() { { var x; let y; } }`)
246+
.chain(
247+
scope_tree
248+
.ancestors(declared_scope_id)
249+
.take_while(|s_id| *s_id != scope_id),
250+
)
242251
.chain(iter::once(scope_id));
243252

244253
// Since the slot is now assigned to this symbol, it is alive in all the scopes that this symbol is alive in.

crates/oxc_minifier/tests/mangler/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ fn mangler() {
4040
"function _() { var x; try { throw 0 } catch (e) { e } }", // e can shadow x
4141
"function _() { var x; try { throw 0 } catch (e) { var e } }", // e can shadow x (not implemented)
4242
"function _() { var x; try { throw 0 } catch { var e } }", // e should not shadow x
43+
"function _() { var x; var y; }", // x and y should have different names
44+
"function _() { var x; let y; }", // x and y should have different names
45+
"function _() { { var x; var y; } }", // x and y should have different names
46+
"function _() { { var x; let y; } }", // x and y should have different names
4347
];
4448
let top_level_cases = [
4549
"function foo(a) {a}",

crates/oxc_minifier/tests/mangler/snapshots/mangler.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,34 @@ function _() {
153153
}
154154
}
155155

156+
function _() { var x; var y; }
157+
function _() {
158+
var a;
159+
var b;
160+
}
161+
162+
function _() { var x; let y; }
163+
function _() {
164+
var a;
165+
let b;
166+
}
167+
168+
function _() { { var x; var y; } }
169+
function _() {
170+
{
171+
var a;
172+
var b;
173+
}
174+
}
175+
176+
function _() { { var x; let y; } }
177+
function _() {
178+
{
179+
var a;
180+
let b;
181+
}
182+
}
183+
156184
function foo(a) {a}
157185
function a(a) {
158186
a;

0 commit comments

Comments
 (0)