Skip to content

Commit b830786

Browse files
authored
fix(es/minifier): use numeric literals for non-finite values (#12048)
**Description:** The optimizer currently represents `NaN` and infinities in several forms, including identifiers, divisions, and numeric literals. As these forms move through multiple optimization passes, they can be folded inconsistently and acquire incorrect truthiness or stringification. This PR canonicalizes non-finite optimization results as raw-free `Lit::Num` values. Minifier and simplifier helpers then preserve ECMAScript behavior for these numbers, while codegen remains responsible for choosing valid output syntax. **Related issue (if exists):** - #12043 - #12047 - #12050 - **#12048** (current) - #12051 - #12052 - #12053 - #12054 - #12055 - #12056 - #12057 - #12058 - #12049 - #12059 - #12060
1 parent b71793f commit b830786

109 files changed

Lines changed: 943 additions & 769 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
swc: patch
3+
swc_core: patch
4+
swc_ecma_minifier: patch
5+
swc_ecma_transforms_optimization: patch
6+
swc_ecma_utils: patch
7+
---
8+
9+
fix(es/minifier): Use numeric literals for non-finite values during optimization.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
//// [computedPropertyNamesContextualType10_ES5.ts]
22
var _obj;
33
import { _ as _define_property } from "@swc/helpers/_/_define_property";
4-
_define_property(_obj = {}, NaN, ""), _define_property(_obj, NaN, 0);
4+
_define_property(_obj = {}, 0 / 0, ""), _define_property(_obj, 0 / 0, 0);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//// [computedPropertyNamesContextualType2_ES5.ts]
22
var _obj;
33
import { _ as _define_property } from "@swc/helpers/_/_define_property";
4-
_define_property(_obj = {}, NaN, function(y) {
4+
_define_property(_obj = {}, 0 / 0, function(y) {
55
return y.length;
6-
}), _define_property(_obj, NaN, function(y) {
6+
}), _define_property(_obj, 0 / 0, function(y) {
77
return y.length;
88
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//// [computedPropertyNamesContextualType3_ES5.ts]
22
var _obj;
33
import { _ as _define_property } from "@swc/helpers/_/_define_property";
4-
_define_property(_obj = {}, NaN, function(y) {
4+
_define_property(_obj = {}, 0 / 0, function(y) {
55
return y.length;
6-
}), _define_property(_obj, NaN, function(y) {
6+
}), _define_property(_obj, 0 / 0, function(y) {
77
return y.length;
88
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
//// [computedPropertyNamesContextualType5_ES5.ts]
22
var _obj;
33
import { _ as _define_property } from "@swc/helpers/_/_define_property";
4-
_define_property(_obj = {}, NaN, ""), _define_property(_obj, NaN, 0);
4+
_define_property(_obj = {}, 0 / 0, ""), _define_property(_obj, 0 / 0, 0);

crates/swc/tests/tsc-references/computedPropertyNamesContextualType6_ES5.2.minified.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { _ as _define_property } from "@swc/helpers/_/_define_property";
44
foo((_define_property(_obj = {
55
p: "",
66
0: function() {}
7-
}, "hibye", !0), _define_property(_obj, 1, 0), _define_property(_obj, NaN, [
7+
}, "hibye", !0), _define_property(_obj, 1, 0), _define_property(_obj, 0 / 0, [
88
0
99
]), _obj));

crates/swc/tests/tsc-references/computedPropertyNamesContextualType6_ES6.2.minified.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ foo({
44
0: ()=>{},
55
hibye: !0,
66
1: 0,
7-
[NaN]: [
7+
NaN: [
88
0
99
]
1010
});

crates/swc/tests/tsc-references/computedPropertyNamesContextualType7_ES5.2.minified.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var _obj;
33
import { _ as _define_property } from "@swc/helpers/_/_define_property";
44
foo((_define_property(_obj = {
55
0: function() {}
6-
}, "hibye", !0), _define_property(_obj, 1, 0), _define_property(_obj, NaN, [
6+
}, "hibye", !0), _define_property(_obj, 1, 0), _define_property(_obj, 0 / 0, [
77
0
88
]), _obj)), g({
99
p: ""

crates/swc/tests/tsc-references/computedPropertyNamesContextualType7_ES6.2.minified.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ foo({
33
0: ()=>{},
44
hibye: !0,
55
1: 0,
6-
[NaN]: [
6+
NaN: [
77
0
88
]
99
}), g({
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
//// [computedPropertyNamesContextualType9_ES5.ts]
22
var _obj;
33
import { _ as _define_property } from "@swc/helpers/_/_define_property";
4-
_define_property(_obj = {}, NaN, ""), _define_property(_obj, NaN, 0);
4+
_define_property(_obj = {}, 0 / 0, ""), _define_property(_obj, 0 / 0, 0);

0 commit comments

Comments
 (0)