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

feat(minifier): fold empty array constructors #6236

Closed
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 @@ -339,6 +339,13 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
ctx.ast.expression_object(expr.span(), Vec::new_in(ctx.ast.allocator), None);
self.changed = true;
}
// `new Array` -> `[]`
else if new_expr.arguments.is_empty()
&& new_expr.callee.is_global_reference_name("Array", ctx.symbols())
{
*expr = ctx.ast.expression_array(expr.span(), Vec::new_in(ctx.ast.allocator), None);
self.changed = true;
}
}
}

Expand All @@ -352,6 +359,13 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
ctx.ast.expression_object(expr.span(), Vec::new_in(ctx.ast.allocator), None);
self.changed = true;
}
// `Array()` -> `[]`
else if call_expr.arguments.is_empty()
&& call_expr.callee.is_global_reference_name("Array", ctx.symbols())
{
*expr = ctx.ast.expression_array(expr.span(), Vec::new_in(ctx.ast.allocator), None);
self.changed = true;
}
}
}
}
Expand Down Expand Up @@ -433,4 +447,42 @@ mod test {

test_same("x = (function f(){function Object(){this.x=4}return new Object();})();");
}

// tests from closure compiler
#[test]
fn fold_literal_array_constructors() {
test("x = new Array", "x = []");
test("x = new Array()", "x = []");
test("x = Array()", "x = []");
// do not fold optional chains
test_same("x = Array?.()");

// One argument
// test("x = new Array(0)", "x = []");
// test("x = Array(0)", "x = []");
// test("x = new Array(\"a\")", "x = [\"a\"]");
// test("x = Array(\"a\")", "x = [\"a\"]");
// test("x = new Array(7)", "x = Array(7)");
// test_same("x = Array(7)");
// test("x = new Array(y)", "x = Array(y)");
// test_same("x = Array(y)");
// test("x = new Array(foo())", "x = Array(foo())");
// test_same("x = Array(foo())");

// // 1+ arguments
// test("x = new Array(1, 2, 3, 4)", "x = [1, 2, 3, 4]");
// test("x = Array(1, 2, 3, 4)", "x = [1, 2, 3, 4]");
// test("x = new Array('a', 1, 2, 'bc', 3, {}, 'abc')", "x = ['a', 1, 2, 'bc', 3, {}, 'abc']");
// test("x = Array('a', 1, 2, 'bc', 3, {}, 'abc')", "x = ['a', 1, 2, 'bc', 3, {}, 'abc']");
// test("x = new Array(Array(1, '2', 3, '4'))", "x = [[1, '2', 3, '4']]");
// test("x = Array(Array(1, '2', 3, '4'))", "x = [[1, '2', 3, '4']]");
// test(
// "x = new Array(Object(), Array(\"abc\", Object(), Array(Array())))",
// "x = [{}, [\"abc\", {}, [[]]]]",
// );
// test(
// "x = new Array(Object(), Array(\"abc\", Object(), Array(Array())))",
// "x = [{}, [\"abc\", {}, [[]]]]",
// );
}
}
4 changes: 2 additions & 2 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Original | Minified | esbuild | Gzip | esbuild

544.10 kB | 74.12 kB | 72.48 kB | 26.22 kB | 26.20 kB | lodash.js

555.77 kB | 278.70 kB | 270.13 kB | 91.39 kB | 90.80 kB | d3.js
555.77 kB | 278.68 kB | 270.13 kB | 91.39 kB | 90.80 kB | d3.js

1.01 MB | 470.09 kB | 458.89 kB | 126.96 kB | 126.71 kB | bundle.min.js

Expand All @@ -20,7 +20,7 @@ Original | Minified | esbuild | Gzip | esbuild

3.20 MB | 1.05 MB | 1.01 MB | 334.06 kB | 331.56 kB | echarts.js

6.69 MB | 2.44 MB | 2.31 MB | 498.83 kB | 488.28 kB | antd.js
4.12 MB | 1.68 MB | 2.31 MB | 484.15 kB | 488.28 kB | antd.js

10.95 MB | 3.59 MB | 3.49 MB | 913.92 kB | 915.50 kB | typescript.js

Loading