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(es/minifier): Fix analysis of var declaration after usage #6043

Merged
merged 11 commits into from
Oct 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/swc/tests/exec/issues-6xxx/6039/1/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function foo() {
let walker = 0;

let arr = [];

function bar(defaultValue) {
const myIndex = walker;
walker += 1;

console.log({ arr });

if (arr.length < myIndex + 1) {
arr[myIndex] = defaultValue;
}
}

return bar;
}

const bar = foo();

bar(null);
bar(null);
bar(null);
bar(null);
bar(null);
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/src/analyzer/storage/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ impl Storage for ProgramData {
if v.used_in_non_child_fn {
v.is_fn_local = false;
}

v.var_initialized |= has_init;
})
.or_insert_with(|| VarUsageInfo {
is_fn_local: true,
Expand Down
17 changes: 17 additions & 0 deletions crates/swc_ecma_minifier/src/pass/precompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ impl VisitMut for PrecompressOptimizer {
}
}

fn visit_mut_pat_or_expr(&mut self, n: &mut PatOrExpr) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a small normalization to reduce possibility of bugs

n.visit_mut_children_with(self);

match n {
PatOrExpr::Expr(e) => {
if let Expr::Ident(i) = &**e {
*n = PatOrExpr::Pat(i.clone().into())
}
}
PatOrExpr::Pat(p) => {
if let Pat::Expr(e) = &mut **p {
*n = PatOrExpr::Expr(e.take());
}
}
}
}

fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
n.visit_mut_with(v);
Expand Down
20 changes: 10 additions & 10 deletions crates/swc_ecma_minifier/src/util/base54.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ impl CharFreq {
return;
}

#[cfg(feature = "debug")]
{
let considered = s
.chars()
.filter(|&c| Ident::is_valid_continue(c))
.collect::<String>();
if !considered.is_empty() {
tracing::debug!("Scanning: `{}` with delta {}", considered, delta);
}
}
// #[cfg(feature = "debug")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented this out as this disturbs debugging

// {
// let considered = s
// .chars()
// .filter(|&c| Ident::is_valid_continue(c))
// .collect::<String>();
// if !considered.is_empty() {
// tracing::debug!("Scanning: `{}` with delta {}", considered, delta);
// }
// }

for &c in s.as_bytes() {
match c {
Expand Down
63 changes: 63 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10140,3 +10140,66 @@ fn issue_6051() {
"###,
);
}

#[test]
fn issue_6039_1() {
run_default_exec_test(
r###"
function foo() {
let walker = 0;

let arr = [];

function bar(defaultValue) {
const myIndex = walker;
walker += 1;

console.log({ arr });

if (arr.length < myIndex + 1) {
arr[myIndex] = defaultValue;
}
}

return bar;
}

const bar = foo();

bar(null);
bar(null);
bar(null);
bar(null);
bar(null);
"###,
);
}

#[test]
fn issue_6039_2() {
run_default_exec_test(
r###"
var foo = function foo() {
var walker = 0;
var arr = [];
function bar(defaultValue) {
var myIndex = walker;
walker += 1;
console.log({
arr: arr
});
if (arr.length < myIndex + 1) {
arr[myIndex] = defaultValue;
}
}
return bar;
};
var bar = foo();
bar(null);
bar(null);
bar(null);
bar(null);
bar(null);
"###,
);
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
({}.x = 10);
10;