Skip to content

Commit a228347

Browse files
authoredJan 20, 2025··
feat(es/minifier): Turn 1 * v into +v (#9903)
1 parent 09b3b37 commit a228347

File tree

9 files changed

+33
-6
lines changed

9 files changed

+33
-6
lines changed
 

‎.changeset/tall-apples-cross.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_minifier: patch
4+
---
5+
6+
feat(es/minifier): Turn '1 *' into '+'

‎crates/swc_ecma_minifier/src/compress/pure/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ impl VisitMut for Pure<'_> {
406406
}
407407

408408
self.lift_minus(e);
409+
self.optimize_to_number(e);
409410

410411
if e.is_seq() {
411412
debug_assert_valid(e);

‎crates/swc_ecma_minifier/src/compress/pure/numbers.rs

+20
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,24 @@ impl Pure<'_> {
9090
}
9191
}
9292
}
93+
94+
pub(super) fn optimize_to_number(&mut self, e: &mut Expr) {
95+
if let Expr::Bin(bin) = e {
96+
if bin.op == op!("*")
97+
&& matches!(&*bin.left, Expr::Lit(Lit::Num(Number { value: 1.0, .. })))
98+
{
99+
report_change!("numbers: Turn '1 *' into '+'");
100+
self.changed = true;
101+
102+
let value = bin.right.take();
103+
let span = bin.span;
104+
105+
*e = Expr::Unary(UnaryExpr {
106+
span,
107+
op: op!(unary, "+"),
108+
arg: value,
109+
})
110+
}
111+
}
112+
}
93113
}

‎crates/swc_ecma_minifier/tests/benches-full/d3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ function(global, factory) {
14811481
},
14821482
rgb: function() {
14831483
var y = (this.l + 16) / 116, x = isNaN(this.a) ? y : y + this.a / 500, z = isNaN(this.b) ? y : y - this.b / 200;
1484-
return new Rgb(lrgb2rgb(3.1338561 * (x = 0.96422 * lab2xyz(x)) - 1.6168667 * (y = 1 * lab2xyz(y)) - 0.4906146 * (z = 0.82521 * lab2xyz(z))), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
1484+
return new Rgb(lrgb2rgb(3.1338561 * (x = 0.96422 * lab2xyz(x)) - 1.6168667 * (y = +lab2xyz(y)) - 0.4906146 * (z = 0.82521 * lab2xyz(z))), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
14851485
}
14861486
})), define1(Hcl, hcl, extend(Color, {
14871487
brighter: function(k) {

‎crates/swc_ecma_minifier/tests/benches-full/jquery.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
*/ function(window1) {
246246
var i, support, Expr, getText, isXML, tokenize, compile, select, outermostContext, sortInput, hasDuplicate, // Local document vars
247247
setDocument, document, docElem, documentIsHTML, rbuggyQSA, rbuggyMatches, matches, contains, // Instance-specific data
248-
expando = "sizzle" + 1 * new Date(), preferredDoc = window1.document, dirruns = 0, done = 0, classCache = createCache(), tokenCache = createCache(), compilerCache = createCache(), nonnativeSelectorCache = createCache(), sortOrder = function(a, b) {
248+
expando = "sizzle" + +new Date(), preferredDoc = window1.document, dirruns = 0, done = 0, classCache = createCache(), tokenCache = createCache(), compilerCache = createCache(), nonnativeSelectorCache = createCache(), sortOrder = function(a, b) {
249249
return a === b && (hasDuplicate = !0), 0;
250250
}, // Instance methods
251251
hasOwn = {}.hasOwnProperty, arr = [], pop = arr.pop, pushNative = arr.push, push = arr.push, slice = arr.slice, // Use a stripped-down indexOf as it's faster than native

‎crates/swc_ecma_minifier/tests/benches-full/victory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@
13021302
},
13031303
rgb: function() {
13041304
var y = (this.l + 16) / 116, x = isNaN(this.a) ? y : y + this.a / 500, z = isNaN(this.b) ? y : y - this.b / 200;
1305-
return x = 0.96422 * lab2xyz(x), y = 1 * lab2xyz(y), z = 0.82521 * lab2xyz(z), new _color_js__WEBPACK_IMPORTED_MODULE_1__.Rgb(lrgb2rgb(3.1338561 * x - 1.6168667 * y - 0.4906146 * z), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
1305+
return x = 0.96422 * lab2xyz(x), y = +lab2xyz(y), z = 0.82521 * lab2xyz(z), new _color_js__WEBPACK_IMPORTED_MODULE_1__.Rgb(lrgb2rgb(3.1338561 * x - 1.6168667 * y - 0.4906146 * z), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
13061306
}
13071307
})), Object(_define_js__WEBPACK_IMPORTED_MODULE_0__.default)(Hcl, hcl, Object(_define_js__WEBPACK_IMPORTED_MODULE_0__.extend)(_color_js__WEBPACK_IMPORTED_MODULE_1__.Color, {
13081308
brighter: function(k) {

‎crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19341,7 +19341,7 @@
1934119341
if (-1 === t && (t = 6), n < 0 ? (u = 0, n = -n) : n > 15 && (u = 2, n -= 16), o < 1 || o > 9 || 8 !== r || n < 8 || n > 15 || t < 0 || t > 9 || a < 0 || a > 4) return s(e, -2);
1934219342
8 === n && (n = 9);
1934319343
var l = new w();
19344-
return e.state = l, l.strm = e, l.wrap = u, l.gzhead = null, l.w_bits = n, l.w_size = 1 << l.w_bits, l.w_mask = l.w_size - 1, l.hash_bits = o + 7, l.hash_size = 1 << l.hash_bits, l.hash_mask = l.hash_size - 1, l.hash_shift = ~~((l.hash_bits + 3 - 1) / 3), l.window = new i.Buf8(2 * l.w_size), l.head = new i.Buf16(l.hash_size), l.prev = new i.Buf16(l.w_size), l.lit_bufsize = 1 << o + 6, l.pending_buf_size = 4 * l.lit_bufsize, l.pending_buf = new i.Buf8(l.pending_buf_size), l.d_buf = 1 * l.lit_bufsize, l.l_buf = 3 * l.lit_bufsize, l.level = t, l.strategy = a, l.method = r, _(e);
19344+
return e.state = l, l.strm = e, l.wrap = u, l.gzhead = null, l.w_bits = n, l.w_size = 1 << l.w_bits, l.w_mask = l.w_size - 1, l.hash_bits = o + 7, l.hash_size = 1 << l.hash_bits, l.hash_mask = l.hash_size - 1, l.hash_shift = ~~((l.hash_bits + 3 - 1) / 3), l.window = new i.Buf8(2 * l.w_size), l.head = new i.Buf16(l.hash_size), l.prev = new i.Buf16(l.w_size), l.lit_bufsize = 1 << o + 6, l.pending_buf_size = 4 * l.lit_bufsize, l.pending_buf = new i.Buf8(l.pending_buf_size), l.d_buf = +l.lit_bufsize, l.l_buf = 3 * l.lit_bufsize, l.level = t, l.strategy = a, l.method = r, _(e);
1934519345
}
1934619346
n = [
1934719347
new D(0, 0, 0, 0, function(e, t) {

‎crates/swc_ecma_minifier/tests/fixture/next/styled-components/1/output.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,7 @@
41094109
case 58:
41104110
return d.trim() + c.replace(F, "$1" + d.trim());
41114111
default:
4112-
if (0 < 1 * e && 0 < c.indexOf("\f")) return c.replace(F, (58 === d.charCodeAt(0) ? "" : "$1") + d.trim());
4112+
if (0 < +e && 0 < c.indexOf("\f")) return c.replace(F, (58 === d.charCodeAt(0) ? "" : "$1") + d.trim());
41134113
}
41144114
return d + c;
41154115
}

‎crates/swc_ecma_minifier/tests/projects/output/angular-1.2.5.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5834,7 +5834,7 @@
58345834
}
58355835
this.index++;
58365836
}
5837-
number *= 1, this.tokens.push({
5837+
number = +number, this.tokens.push({
58385838
index: start,
58395839
text: number,
58405840
json: !0,

0 commit comments

Comments
 (0)
Please sign in to comment.