Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,7 @@ impl<'a> CollapseVariableDeclarations {
/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/CollapseVariableDeclarationsTest.java>
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;

use crate::tester;

fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let mut pass = super::CollapseVariableDeclarations::new();
tester::test(&allocator, source_text, expected, &mut pass);
}

fn test_same(source_text: &str) {
test(source_text, source_text);
}
use crate::tester::{test, test_same};

mod join_vars {
use super::{test, test_same};
Expand Down Expand Up @@ -286,7 +274,7 @@ mod test {

test(
"var x = 2; foo(x); x = 3; x = 1; var y = 2; var z = 4; x = 5",
"var x = 2; foo(x); x = 3; x = 1; var y = 2, z = 4; x = 5",
"var x = 2; foo(x), x = 3, x = 1; var y = 2, z = 4; x = 5",
);
}

Expand All @@ -304,9 +292,9 @@ mod test {

#[test]
fn test_aggressive_redeclaration_in_for() {
test_same("for(var x = 1; x = 2; x = 3) {x = 4}");
test_same("for(var x = 1; x = 2; x = 3) x = 4");
test_same("for(var x = 1; y = 2; z = 3) {var a = 4}");
test_same("var x; for(x = 1; x = 2; z = 3) {x = 4}");
test_same("var x; for(x = 1; x = 2; z = 3) x = 4");
}

#[test]
Expand Down Expand Up @@ -354,9 +342,9 @@ mod test {

#[test]
fn test_aggressive_redeclaration_of_let_in_for() {
test_same("for(let x = 1; x = 2; x = 3) {x = 4}");
test_same("for(let x = 1; x = 2; x = 3) x = 4");
test_same("for(let x = 1; y = 2; z = 3) {let a = 4}");
test_same("let x; for(x = 1; x = 2; z = 3) {x = 4}");
test_same("let x; for(x = 1; x = 2; z = 3) x = 4");
}

#[test]
Expand All @@ -374,16 +362,19 @@ mod test {

// do not redeclare function parameters
// incompatible with strict mode
test_same("function f(x) { let y = 3; x = 4; x + y; }");
test_same("function f(x) { let y = 3; x = 4, x + y; }");
}

#[test]
fn test_arrow_function() {
test("() => {let x = 1; let y = 2; x + y; }", "() => {let x = 1, y = 2; x + y; }");
test(
"(() => { let x = 1; let y = 2; x + y; })()",
"(() => { let x = 1, y = 2; x + y; })()",
);

// do not redeclare function parameters
// incompatible with strict mode
test_same("(x) => {x = 4; let y = 2; x + y; }");
test_same("((x) => { x = 4; let y = 2; x + y; })()");
}

#[test]
Expand Down Expand Up @@ -430,7 +421,7 @@ mod test {
// Verify FOR inside IFs.
test(
"if(x){var a = 0; for(; c < b; c++) foo()}",
"if(x){for(var a = 0; c < b; c++) foo()}",
"if(x)for(var a = 0; c < b; c++) foo()",
);

// Any other expression.
Expand All @@ -441,7 +432,7 @@ mod test {
"function f(){ var a; for(; a < 2 ; a++) foo() }",
"function f(){ for(var a; a < 2 ; a++) foo() }",
);
test_same("function f(){ return; for(; a < 2 ; a++) foo() }");
test_same("function f(){ for(; a < 2 ; a++) foo() }");

// TODO
// Verify destructuring assignments are moved.
Expand All @@ -459,21 +450,21 @@ mod test {
#[test]
fn test_for_in() {
test("var a; for(a in b) foo()", "for (var a in b) foo()");
test_same("a = 0; for(a in b) foo()");
test("a = 0; for(a in b) foo()", "for (a in a = 0, b) foo();");
test_same("var a = 0; for(a in b) foo()");

// We don't handle labels yet.
test_same("var a; a:for(a in b) foo()");
test_same("var a; a:b:for(a in b) foo()");

// Verify FOR inside IFs.
test("if(x){var a; for(a in b) foo()}", "if(x){for(var a in b) foo()}");
test("if(x){var a; for(a in b) foo()}", "if(x) for(var a in b) foo()");

// Any other expression.
test_same("init(); for(a in b) foo()");
test("init(); for(a in b) foo()", "for (a in init(), b) foo();");

// Other statements are left as is.
test_same("function f(){ return; for(a in b) foo() }");
test_same("function f(){ for(a in b) foo() }");

// We don't handle destructuring patterns yet.
test("var a; var b; for ([a, b] in c) foo();", "var a, b; for ([a, b] in c) foo();");
Expand All @@ -490,13 +481,13 @@ mod test {
test_same("var a; a: b: for (a of b) foo()");

// Verify FOR inside IFs.
test("if (x) { var a; for (a of b) foo() }", "if (x) { for (var a of b) foo() }");
test("if (x) { var a; for (a of b) foo() }", "if (x) for (var a of b) foo()");

// Any other expression.
test_same("init(); for (a of b) foo()");

// Other statements are left as is.
test_same("function f() { return; for (a of b) foo() }");
test_same("function f() { for (a of b) foo() }");

// We don't handle destructuring patterns yet.
test("var a; var b; for ([a, b] of c) foo();", "var a, b; for ([a, b] of c) foo();");
Expand Down
30 changes: 9 additions & 21 deletions crates/oxc_minifier/src/ast_passes/convert_to_dotted_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,7 @@ impl<'a> ConvertToDottedProperties {

#[cfg(test)]
mod test {
use oxc_allocator::Allocator;

use crate::tester;

fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let mut pass = super::ConvertToDottedProperties::new(false);
tester::test(&allocator, source_text, expected, &mut pass);
}

fn test_same(source_text: &str) {
test(source_text, source_text);
}
use crate::tester::{test, test_same};

#[test]
fn test_computed_to_member_expression() {
Expand Down Expand Up @@ -117,7 +105,7 @@ mod test {
test_same("a[':']");
test_same("a['.']");
test_same("a['p ']");
test_same("a['p' + '']");
test("a['p' + '']", "a.p");
test_same("a[p]");
test_same("a[P]");
test_same("a[$]");
Expand All @@ -134,10 +122,10 @@ mod test {

#[test]
fn test_convert_to_dotted_properties_quoted_props() {
test_same("({'':0})");
test_same("({'1.0':0})");
test_same("({'\\u1d17A':0})");
test_same("({'a\\u0004b':0})");
test("({'':0})", "");
test("({'1.0':0})", "");
test("({'\\u1d17A':0})", "");
test("({'a\\u0004b':0})", "");
}

#[test]
Expand All @@ -160,7 +148,7 @@ mod test {
test_same("a?.['.']");
test_same("a?.['0']");
test_same("a?.['p ']");
test_same("a?.['p' + '']");
test("a?.['p' + '']", "a?.p");
test_same("a?.[p]");
test_same("a?.[P]");
test_same("a?.[$]");
Expand Down Expand Up @@ -296,8 +284,8 @@ mod test {
test_same("x?.['y z']");
test("x?.['y']()", "x?.y();");
test_same("x?.['y z']()");
test_same("x['y' + 'z']");
test_same("x?.['y' + 'z']");
test("x['y' + 'z']", "x.yz");
test("x?.['y' + 'z']", "x?.yz");
test("x['0']", "x[0];");
test("x['123']", "x[123];");
test("x['-123']", "x[-123];");
Expand Down
14 changes: 1 addition & 13 deletions crates/oxc_minifier/src/ast_passes/exploit_assigns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ impl ExploitAssigns {
/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/ExploitAssignsTest.java>
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;

use crate::tester;

fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let mut pass = super::ExploitAssigns::new();
tester::test(&allocator, source_text, expected, &mut pass);
}

fn test_same(source_text: &str) {
test(source_text, source_text);
}
use crate::tester::{test, test_same};

#[test]
#[ignore]
Expand Down
Loading
Loading