Repository: swc-project/swc (rust)

Issue:
faulty number minification
Hi guys

Number minification works faulty

this code
``` js
var millisecondsPerWeek = 7 * 24 * 60 * 60 * 1000
console.log(millisecondsPerWeek)
```
is minified to 
```js
var millisecondsPerWeek=604e6;console.log(millisecondsPerWeek)
```
**Expected behavior**
which is not the same as calculated value expected to be 6048e5

**Version**
The version of @swc/core: v1.2.90


Pull request description:
fix(es/codegen): Fix codegen of numbers
swc_ecma_codegen:
 - Fix codegen of numbers when minification is enabled. (Closes #2294)

Patch:
diff --git a/Cargo.lock b/Cargo.lock
index b7997aad55..a13d2b36d9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2575,7 +2575,7 @@ dependencies = [
 
 [[package]]
 name = "swc_ecma_codegen"
-version = "0.73.0"
+version = "0.73.1"
 dependencies = [
  "bitflags",
  "num-bigint",
diff --git a/ecmascript/codegen/Cargo.toml b/ecmascript/codegen/Cargo.toml
index 44960fa472..2cb45e181f 100644
--- a/ecmascript/codegen/Cargo.toml
+++ b/ecmascript/codegen/Cargo.toml
@@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
 license = "Apache-2.0/MIT"
 name = "swc_ecma_codegen"
 repository = "https://github.com/swc-project/swc.git"
-version = "0.73.0"
+version = "0.73.1"
 
 [dependencies]
 bitflags = "1"
diff --git a/ecmascript/codegen/src/lib.rs b/ecmascript/codegen/src/lib.rs
index 35683577fc..f15391a436 100644
--- a/ecmascript/codegen/src/lib.rs
+++ b/ecmascript/codegen/src/lib.rs
@@ -460,8 +460,8 @@ impl<'a> Emitter<'a> {
             } else {
                 let mut s = num.value.to_string();
                 if self.cfg.minify {
-                    if !s.contains('.') && !s.contains('e') && s.ends_with("000") {
-                        let cnt = s.as_bytes().iter().rev().filter(|&&v| v == b'0').count();
+                    if !s.contains('.') && !s.contains('e') && s.ends_with("0000") {
+                        let cnt = s.as_bytes().iter().rev().filter(|&&v| v == b'0').count() - 1;
 
                         s.truncate(s.len() - cnt);
                         s.push('e');


Test patch:
diff --git a/ecmascript/codegen/tests/fixture/issues/2294/1/input.js b/ecmascript/codegen/tests/fixture/issues/2294/1/input.js
new file mode 100644
index 0000000000..d271303d64
--- /dev/null
+++ b/ecmascript/codegen/tests/fixture/issues/2294/1/input.js
@@ -0,0 +1,2 @@
+var millisecondsPerWeek = 604800000
+console.log(millisecondsPerWeek)
\ No newline at end of file
diff --git a/ecmascript/codegen/tests/fixture/issues/2294/1/output.js b/ecmascript/codegen/tests/fixture/issues/2294/1/output.js
new file mode 100644
index 0000000000..5ba3975d08
--- /dev/null
+++ b/ecmascript/codegen/tests/fixture/issues/2294/1/output.js
@@ -0,0 +1,2 @@
+var millisecondsPerWeek = 604800000;
+console.log(millisecondsPerWeek);
diff --git a/ecmascript/codegen/tests/fixture/issues/2294/1/output.min.js b/ecmascript/codegen/tests/fixture/issues/2294/1/output.min.js
new file mode 100644
index 0000000000..022d4d38a2
--- /dev/null
+++ b/ecmascript/codegen/tests/fixture/issues/2294/1/output.min.js
@@ -0,0 +1,1 @@
+var millisecondsPerWeek=6048e5;console.log(millisecondsPerWeek)
diff --git a/ecmascript/codegen/tests/fixture/issues/2294/2/input.js b/ecmascript/codegen/tests/fixture/issues/2294/2/input.js
new file mode 100644
index 0000000000..fa62a80f55
--- /dev/null
+++ b/ecmascript/codegen/tests/fixture/issues/2294/2/input.js
@@ -0,0 +1,2 @@
+var millisecondsPerWeek = 7 * 24 * 60 * 60 * 1000
+console.log(millisecondsPerWeek)
\ No newline at end of file
diff --git a/ecmascript/codegen/tests/fixture/issues/2294/2/output.js b/ecmascript/codegen/tests/fixture/issues/2294/2/output.js
new file mode 100644
index 0000000000..edf0fc3a7e
--- /dev/null
+++ b/ecmascript/codegen/tests/fixture/issues/2294/2/output.js
@@ -0,0 +1,2 @@
+var millisecondsPerWeek = 7 * 24 * 60 * 60 * 1000;
+console.log(millisecondsPerWeek);
diff --git a/ecmascript/codegen/tests/fixture/issues/2294/2/output.min.js b/ecmascript/codegen/tests/fixture/issues/2294/2/output.min.js
new file mode 100644
index 0000000000..e15346bf4d
--- /dev/null
+++ b/ecmascript/codegen/tests/fixture/issues/2294/2/output.min.js
@@ -0,0 +1,1 @@
+var millisecondsPerWeek=7*24*60*60*1000;console.log(millisecondsPerWeek)
diff --git a/ecmascript/codegen/tests/test262-min/15a12468ff312d51.js b/ecmascript/codegen/tests/test262-min/15a12468ff312d51.js
index 05b3a0da28..37d9500890 100644
--- a/ecmascript/codegen/tests/test262-min/15a12468ff312d51.js
+++ b/ecmascript/codegen/tests/test262-min/15a12468ff312d51.js
@@ -1,1 +1,1 @@
-60221417e16
+602214179e15
diff --git a/ecmascript/codegen/tests/test262-min/6a240463b40550d2.js b/ecmascript/codegen/tests/test262-min/6a240463b40550d2.js
index e15db18f4e..d8669413ed 100644
--- a/ecmascript/codegen/tests/test262-min/6a240463b40550d2.js
+++ b/ecmascript/codegen/tests/test262-min/6a240463b40550d2.js
@@ -1,1 +1,1 @@
-1e20,1e21
+10e19,10e20
diff --git a/tests/fixture/issue-2086/output/index.ts b/tests/fixture/issue-2086/output/index.ts
index 0f84aac229..85cf6d2002 100644
--- a/tests/fixture/issue-2086/output/index.ts
+++ b/tests/fixture/issue-2086/output/index.ts
@@ -1,1 +1,1 @@
-class Test{#a;constructor(a){this.#a=a} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1e3)}run(){this.#b()}}export{Test as default}
+class Test{#a;constructor(a){this.#a=a} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1000)}run(){this.#b()}}export{Test as default}