Skip to content

Commit

Permalink
Handle naming overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Jan 3, 2023
1 parent cebcdd1 commit fb74fd6
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 26 deletions.
7 changes: 7 additions & 0 deletions crates/swc_ecma_minifier/tests/mangle/issue-5766/2/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
let foo = 1;
{
var baz = 2;
console.log(foo, 1)
}
}
7 changes: 7 additions & 0 deletions crates/swc_ecma_minifier/tests/mangle/issue-5766/2/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
let o = 1;
{
var l = 2;
console.log(o, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ var foo1;
try {

} catch (_) {
var foo2 = 'should not reuse same name with foo1';
let foo1 = 'could reuse same name with foo1';
var foo2 = 'should not reuse same name with foo1';
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var e;
try {} catch (o) {
var a = 'should not reuse same name with foo1';
let e = 'could reuse same name with foo1';
var a = 'should not reuse same name with foo1';
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function t() {
if (FOO) {
let t;
let a;
let e;
let f;
var t;
var a;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
let s;
const l = 1;
class c {
let l;
const c = 1;
class e {
}
var s;
}let l;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (0) {
let o = 6;
const a = 12;
class l {
let a = 6;
const l = 12;
class s {
}
var o;
}
Expand Down
22 changes: 8 additions & 14 deletions crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub(super) struct Analyzer {
pub is_pat_decl: bool,
pub var_belong_to_fn_scope: bool,
pub in_catch_params: bool,
pub in_catch_block: bool,
pub scope: Scope,
/// If we try add variables declared by `var` to the block scope,
/// variables will be added to `hoisted_vars` and merged to latest
Expand All @@ -26,22 +25,15 @@ impl Analyzer {
ScopeKind::Fn => {
self.scope.add_decl(&id);
}
ScopeKind::Block => {
// This prevents renamed params in catch clause overriding declared variables
// using `var` and `function`.
if self.in_catch_block {
self.add_usage(id.clone());
}
self.hoisted_vars.push(id)
}
ScopeKind::Block => self.hoisted_vars.push(id),
}
} else {
self.scope.add_decl(&id);
}
}

fn add_usage(&mut self, id: Id) {
self.scope.add_usage(&id);
self.scope.add_usage(id);
}

fn with_scope<F>(&mut self, kind: ScopeKind, op: F)
Expand All @@ -57,13 +49,18 @@ impl Analyzer {
is_pat_decl: self.is_pat_decl,
var_belong_to_fn_scope: false,
in_catch_params: false,
in_catch_block: false,
hoisted_vars: Default::default(),
};

op(&mut v);
if !v.hoisted_vars.is_empty() {
debug_assert!(matches!(v.scope.kind, ScopeKind::Block));
v.hoisted_vars.clone().into_iter().for_each(|id| {
// For variables declared in block scope using `var` and `function`,
// We should create a fake usage in the block to prevent conflicted
// renaming.
v.add_usage(id);
});
match self.scope.kind {
ScopeKind::Fn => {
v.hoisted_vars
Expand Down Expand Up @@ -128,12 +125,9 @@ impl Visit for Analyzer {
self.with_scope(ScopeKind::Block, |v| {
let old = v.is_pat_decl;
let old_in_catch_params = v.in_catch_params;
let old_in_catch_block = v.in_catch_block;

v.is_pat_decl = false;
v.in_catch_block = true;
n.body.visit_children_with(v);
v.in_catch_block = old_in_catch_block;

v.is_pat_decl = true;
v.in_catch_params = true;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_base/src/rename/analyzer/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ impl Scope {
}
}

pub(super) fn add_usage(&mut self, id: &Id) {
pub(super) fn add_usage(&mut self, id: Id) {
if id.0 == js_word!("arguments") {
return;
}

self.data.all.insert(fast_id(id.clone()));
self.data.all.insert(fast_id(id));
}

/// Copy `children.data.all` to `self.data.all`.
Expand Down

0 comments on commit fb74fd6

Please sign in to comment.