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

fix(mangler): handle cases where a var is declared in a block scope #8706

Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,22 @@ impl Mangler {
{
slots[symbol_id.index()] = assigned_slot;

let declared_scope_id =
ast_nodes.get_node(symbol_table.get_declaration(*symbol_id)).scope_id();
Boshen marked this conversation as resolved.
Show resolved Hide resolved

// Calculate the scope ids that this symbol is alive in.
let lived_scope_ids = symbol_table
.get_resolved_references(*symbol_id)
.flat_map(|reference| {
let used_scope_id = ast_nodes.get_node(reference.node_id()).scope_id();
scope_tree.ancestors(used_scope_id).take_while(|s_id| *s_id != scope_id)
})
// also include scopes that this symbol was declared (for cases like `function foo() { { var x; let y; } }`)
.chain(
scope_tree
.ancestors(declared_scope_id)
.take_while(|s_id| *s_id != scope_id),
)
.chain(iter::once(scope_id));

// Since the slot is now assigned to this symbol, it is alive in all the scopes that this symbol is alive in.
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_minifier/tests/mangler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ fn mangler() {
"function _() { var x; try { throw 0 } catch (e) { e } }", // e can shadow x
"function _() { var x; try { throw 0 } catch (e) { var e } }", // e can shadow x (not implemented)
"function _() { var x; try { throw 0 } catch { var e } }", // e should not shadow x
"function _() { var x; var y; }", // x and y should have different names
"function _() { var x; let y; }", // x and y should have different names
"function _() { { var x; var y; } }", // x and y should have different names
"function _() { { var x; let y; } }", // x and y should have different names
];
let top_level_cases = [
"function foo(a) {a}",
Expand Down
28 changes: 28 additions & 0 deletions crates/oxc_minifier/tests/mangler/snapshots/mangler.snap
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ function _() {
}
}

function _() { var x; var y; }
function _() {
var a;
var b;
}

function _() { var x; let y; }
function _() {
var a;
let b;
}

function _() { { var x; var y; } }
function _() {
{
var a;
var b;
}
}

function _() { { var x; let y; } }
function _() {
{
var a;
let b;
}
}

function foo(a) {a}
function a(a) {
a;
Expand Down
Loading