Skip to content

Commit ce82613

Browse files
fix(minify): write x -= 1 as --x only where x holds only numbers
On a BigInt `-= 1` throws where `--` does not. swc keeps it; oxc rewrites it on any reference. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 3dd8965 commit ce82613

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28018,21 +28018,19 @@ const installImprove = (modules) => {
2801828018

2801928019
A.wrapOptimizer("Assign", (optimizeAssign) => (self, compressor) => {
2802028020
const optimized = optimizeAssign(self, compressor);
28021-
// x -= 1 -> --x, as oxc writes it, and x += 1 -> ++x where `x` holds only
28022-
// numbers, so the `+` cannot concatenate.
28021+
// x -= 1 -> --x and x += 1 -> ++x only where `x` holds only numbers: on a
28022+
// BigInt `-= 1` throws where `--` does not, and `+` may concatenate. oxc
28023+
// rewrites `-=` on any reference, a bug swc does not share.
2802328024
if (
2802428025
improvements.enabled &&
2802528026
improvements.updates.enabled &&
2802628027
A.isAssignNode(optimized) &&
2802728028
!optimized.logical &&
2802828029
A.isNumberNode(optimized.right) &&
2802928030
optimized.right.value === 1 &&
28030-
(A.isSymbolRefNode(optimized.left) ||
28031-
A.isPropAccessNode(optimized.left)) &&
28032-
(optimized.operator === "-=" ||
28033-
(optimized.operator === "+=" &&
28034-
A.isSymbolRefNode(optimized.left) &&
28035-
holdsOnlyNumbers(optimized.left, compressor)))
28031+
(optimized.operator === "-=" || optimized.operator === "+=") &&
28032+
A.isSymbolRefNode(optimized.left) &&
28033+
holdsOnlyNumbers(optimized.left, compressor)
2803628034
) {
2803728035
improvements.updates.applied = true;
2803828036
return makeNode(A.UnaryPrefixNode, optimized, {

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,10 +1075,10 @@ const IMPROVED_CASES = [
10751075
"function f(a){for(var i=0;i<a.length;++i)console.log(i);for(i=0;i<a.length;++i)console.log(i)}f([1,2]);"
10761076
],
10771077
[
1078-
"`-= 1` as `--`, and `+= 1` as `++` on a counter holding only numbers, but on a parameter that may hold a string",
1078+
"`+= 1` as `++` on a counter holding only numbers, but not on a property or a parameter that may hold a BigInt or a string",
10791079
'function f(o, s) { o.a -= 1; s += 1; for (var i = 0; i < 3; i += 1) console.log(i, o.a, s); } f({ a: 5 }, "s");',
10801080
{ mangle: false },
1081-
'function f(o,s){--o.a,s+=1;for(var i=0;i<3;++i)console.log(i,o.a,s)}f({a:5},"s");'
1081+
'function f(o,s){o.a-=1,s+=1;for(var i=0;i<3;++i)console.log(i,o.a,s)}f({a:5},"s");'
10821082
],
10831083
[
10841084
"a `var` declared bare, then set once to a parameter before every read, read through the parameter",
@@ -1976,6 +1976,10 @@ const IMPROVED_CASES = [
19761976
// of its function's own, or the call passes, keeps or constructs something.
19771977
/** @type {[string, string][]} */
19781978
const KEPT_CASES = [
1979+
[
1980+
"`-= 1` on a property that may hold a BigInt, which throws where `--` would not",
1981+
'const o = { x: 1n }; try { o.x -= 1; } catch { console.log("caught"); }'
1982+
],
19791983
[
19801984
"`+= 1` on a name a `for-in` head assigns, which holds a string",
19811985
"function f(o) { var x = 0; for (x in o) console.log(x += 1); } f({ 2: 0 });"

0 commit comments

Comments
 (0)