Skip to content

Commit fcc3663

Browse files
committed
test(minifier): merge variable declarations into for statement initializers (#13770)
Added some test cases that describes that the transformations are safe, which I misunderstood once.
1 parent f0af9a4 commit fcc3663

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

crates/oxc_minifier/src/peephole/minimize_statements.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,3 +1732,30 @@ impl<'a> PeepholeOptimizations {
17321732
Some(false)
17331733
}
17341734
}
1735+
1736+
#[cfg(test)]
1737+
mod test {
1738+
use crate::tester::test;
1739+
1740+
#[test]
1741+
fn test_for_variable_declaration() {
1742+
test(
1743+
"function _() { var x; for (var i = 0; i < 10; i++) console.log(i) }",
1744+
"function _() { for (var x, i = 0; i < 10; i++) console.log(i) }",
1745+
);
1746+
test(
1747+
"function _() { var x = 1; for (var i = 0; i < 10; i++) console.log(i) }",
1748+
"function _() { for (var x = 1, i = 0; i < 10; i++) console.log(i) }",
1749+
);
1750+
// this is fine because `let j` inside the block cannot be referenced from `var i = j`
1751+
test(
1752+
"function _() { var x = function () { return console.log(j), 1 }; for (var i = 0; i < 10; i++) { let j = k; console.log(i, j, j) } }",
1753+
"function _() { for (var x = function () { return console.log(j), 1 }, i = 0; i < 10; i++) { let j = k; console.log(i, j, j) } }",
1754+
);
1755+
// this is fine because `let j` inside the block cannot be referenced from `var i = j`
1756+
test(
1757+
"function _() { var x = j; for (var i = 0; i < 10; i++) { let j = k; console.log(i, j, j) } }",
1758+
"function _() { for (var x = j, i = 0; i < 10; i++) { let j = k; console.log(i, j, j) } }",
1759+
);
1760+
}
1761+
}

0 commit comments

Comments
 (0)