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 @@ -762,7 +762,24 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
Some(ctx.ast.expression_array(span, ctx.ast.vec(), None))
}
// `new Array(8)` -> `Array(8)`
else if let Expression::NumericLiteral(_) = arg {
else if let Expression::NumericLiteral(n) = arg {
// new Array(2) -> `[,,]`
// this does not work with IE8 and below
// learned from https://github.com/babel/minify/pull/45
#[expect(clippy::cast_possible_truncation)]
if n.value.fract() == 0.0 {
let n_int = n.value as i64;
if (1..=6).contains(&n_int) {
return Some(ctx.ast.expression_array(
span,
ctx.ast.vec_from_iter((0..n_int).map(|_| {
ArrayExpressionElement::Elision(Elision { span: SPAN })
})),
None,
));
}
}

let callee = ctx.ast.expression_identifier_reference(SPAN, "Array");
let args = ctx.ast.move_vec(args);
Some(ctx.ast.expression_call(span, callee, NONE, args, false))
Expand Down Expand Up @@ -1143,6 +1160,8 @@ mod test {
// One argument
test("x = new Array(0)", "x = []");
test("x = new Array(\"a\")", "x = [\"a\"]");
test("x = new Array(1)", "x = [,]");
test("x = new Array(6)", "x = [,,,,,,]");
test("x = new Array(7)", "x = Array(7)");
test("x = new Array(7n)", "x = [7n]");
test("x = new Array(y)", "x = Array(y)");
Expand Down
4 changes: 2 additions & 2 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Original | minified | minified | gzip | gzip | Fixture

544.10 kB | 71.75 kB | 72.48 kB | 26.16 kB | 26.20 kB | lodash.js

555.77 kB | 272.91 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js
555.77 kB | 272.82 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js

1.01 MB | 460.22 kB | 458.89 kB | 126.83 kB | 126.71 kB | bundle.min.js

Expand All @@ -23,5 +23,5 @@ Original | minified | minified | gzip | gzip | Fixture

6.69 MB | 2.32 MB | 2.31 MB | 492.72 kB | 488.28 kB | antd.js

10.95 MB | 3.50 MB | 3.49 MB | 908.29 kB | 915.50 kB | typescript.js
10.95 MB | 3.50 MB | 3.49 MB | 908.28 kB | 915.50 kB | typescript.js

Loading