Repository: swc-project/swc (rust)

Issue:
Multiline comments don't have source mappings
### Describe the bug

Multiline comments don't have source mappings

They do with Babel

### Input code

```typescript
/*!

 something v5.1.0

more

else
*/
// a
console.log(1);
// b
```


### Config

No config,

`swc x.js -d dist-swc -s`



### Playground link

_No response_

### Expected behavior

Babel output
![Bildschirmfoto 2022-06-03 um 18 50 55](https://user-images.githubusercontent.com/4586894/171910030-2c4f1d11-3f69-4e28-adcf-362ec7e373fa.png)



### Actual behavior


![Bildschirmfoto 2022-06-03 um 18 52 09](https://user-images.githubusercontent.com/4586894/171910215-5e0ff52c-c54f-4724-acca-1f4d83938e8a.png)


### Version

1.2.196

### Additional context

(Found this in https://github.com/parcel-bundler/parcel/issues/7924)

Pull request description:
fix(es/codegen): Fix sourcemap of multiline comments
**Description:**


**Related issue (if exists):**

 - Closes #4876

Patch:
diff --git a/crates/swc_ecma_codegen/src/comments.rs b/crates/swc_ecma_codegen/src/comments.rs
index 158bfc9164..cfbae45246 100644
--- a/crates/swc_ecma_codegen/src/comments.rs
+++ b/crates/swc_ecma_codegen/src/comments.rs
@@ -29,10 +29,16 @@ macro_rules! write_comments {
                     srcmap!($e, cmt, true);
 
                     $e.wr.write_comment("/*")?;
-                    $e.wr.write_lit(DUMMY_SP, &cmt.text)?;
+                    $e.wr.write_comment(&cmt.text)?;
+
+                    {
+                        let hi = cmt.span_hi();
+                        if !hi.is_dummy() && hi.0 > 2 {
+                            $e.wr.add_srcmap(hi - swc_common::BytePos(2))?;
+                        }
+                    }
                     $e.wr.write_comment("*/")?;
 
-                    srcmap!($e, cmt, false);
                     $e.wr.write_space()?;
                 }
             }
diff --git a/scripts/update-all.sh b/scripts/update-all.sh
index d27403a39a..3d4a3a6e3a 100755
--- a/scripts/update-all.sh
+++ b/scripts/update-all.sh
@@ -4,13 +4,22 @@ set -u
 export UPDATE=1
 export DIFF=0
 
-function up(){
-    cargo test -p swc -p swc_ecma_codegen -p swc_ecma_parser -p swc_bundler -p swc_node_bundler --no-fail-fast
+function crate() {
+    until cargo test -p $1 --no-fail-fast
+    do
+        git add -A
+        git commit -m 'Update test refs'
+    done
+    
+    git add -A
+    git commit -m 'Update test refs'
 }
 
-up
+crate swc
+crate swc_ecma_codegen
+crate swc_ecma_parser
+crate swc_bundler
+crate swc_node_bundler
+crate swc_ecma_transforms_react
 
-
-git add -A
-git commit -m 'Update test refs'
 git push -u origin "$(git rev-parse --abbrev-ref HEAD)" --no-verify
\ No newline at end of file


Test patch:
diff --git a/crates/swc/tests/fixture/issues-3xxx/3715/1/output/index.map b/crates/swc/tests/fixture/issues-3xxx/3715/1/output/index.map
index 91a239d93c..c4d90f87cd 100644
--- a/crates/swc/tests/fixture/issues-3xxx/3715/1/output/index.map
+++ b/crates/swc/tests/fixture/issues-3xxx/3715/1/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAAA,CAAC,EAAE,CAAC,6BAA6B,CAA9B,CAA+B",
+  "mappings": "AAAAA,CAAC,EAAE,CAAC,2BAA2B,GAA5B,CAA+B",
   "names": [
     "a"
   ],
diff --git a/crates/swc/tests/fixture/issues-3xxx/3715/2/output/index.map b/crates/swc/tests/fixture/issues-3xxx/3715/2/output/index.map
index 91a239d93c..c4d90f87cd 100644
--- a/crates/swc/tests/fixture/issues-3xxx/3715/2/output/index.map
+++ b/crates/swc/tests/fixture/issues-3xxx/3715/2/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAAA,CAAC,EAAE,CAAC,6BAA6B,CAA9B,CAA+B",
+  "mappings": "AAAAA,CAAC,EAAE,CAAC,2BAA2B,GAA5B,CAA+B",
   "names": [
     "a"
   ],
diff --git a/crates/swc/tests/fixture/issues-3xxx/3716/output/index.map b/crates/swc/tests/fixture/issues-3xxx/3716/output/index.map
index b69e25cd75..091e30e839 100644
--- a/crates/swc/tests/fixture/issues-3xxx/3716/output/index.map
+++ b/crates/swc/tests/fixture/issues-3xxx/3716/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAAA,CAAC,EAAE,CAAC,CAAC,KAAK;AAEVA,CAAC,EAAE,CAAC,CAAC,GAAG;AAERA,CAAC,EAAE,CAAC,CAAC,KAAK;AAEVA,CAAC,EAAE,CAAC,CAAC,GAAG",
+  "mappings": "AAAAA,CAAC,EAAE,CAAC,CAAC,GAAG;AAERA,CAAC,EAAE,CAAC,CAAC,GAAG;AAERA,CAAC,EAAE,CAAC,CAAC,GAAG;AAERA,CAAC,EAAE,CAAC,CAAC,GAAG",
   "names": [
     "a"
   ],
diff --git a/crates/swc/tests/fixture/issues-4xxx/4876/input/.swcrc b/crates/swc/tests/fixture/issues-4xxx/4876/input/.swcrc
new file mode 100644
index 0000000000..deb4efad69
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4876/input/.swcrc
@@ -0,0 +1,3 @@
+{
+    "sourceMaps": true
+}
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-4xxx/4876/input/index.js b/crates/swc/tests/fixture/issues-4xxx/4876/input/index.js
new file mode 100644
index 0000000000..c8bdaf68dc
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4876/input/index.js
@@ -0,0 +1,11 @@
+/*!
+
+ something v5.1.0
+
+more
+
+else
+*/
+// a
+console.log(1);
+// b
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-4xxx/4876/output/index.js b/crates/swc/tests/fixture/issues-4xxx/4876/output/index.js
new file mode 100644
index 0000000000..5d65c3643d
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4876/output/index.js
@@ -0,0 +1,9 @@
+/*!
+
+ something v5.1.0
+
+more
+
+else
+*/ // a
+console.log(1); // b
diff --git a/crates/swc/tests/fixture/issues-4xxx/4876/output/index.map b/crates/swc/tests/fixture/issues-4xxx/4876/output/index.map
new file mode 100644
index 0000000000..b05c60da44
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4876/output/index.map
@@ -0,0 +1,14 @@
+{
+  "mappings": "AAAA;;;;;;;AAOA,GACA,IAAI;AACJA,OAAO,CAACC,GAAG,CAAC,CAAC,CAAC,CAAC,CACf,IAAI",
+  "names": [
+    "console",
+    "log"
+  ],
+  "sources": [
+    "../../input/index.js"
+  ],
+  "sourcesContent": [
+    "/*!\n\n something v5.1.0\n\nmore\n\nelse\n*/\n// a\nconsole.log(1);\n// b"
+  ],
+  "version": 3
+}
diff --git a/crates/swc/tests/fixture/sourcemap/007/output/index.map b/crates/swc/tests/fixture/sourcemap/007/output/index.map
index 2a6c062f7b..bb8bc6fe31 100644
--- a/crates/swc/tests/fixture/sourcemap/007/output/index.map
+++ b/crates/swc/tests/fixture/sourcemap/007/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAA;;GAEG,CACH,OAAO,IAAMA,WAAW,GAAG,SAACC,IAAI,EAAK;IACjCC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;IAClBC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;IAClBC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;IAClBC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;CACrB,CAAC",
+  "mappings": "AAAA;;CAEC,GACD,OAAO,IAAMA,WAAW,GAAG,SAACC,IAAI,EAAK;IACjCC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;IAClBC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;IAClBC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;IAClBC,OAAO,CAACC,GAAG,CAACF,IAAI,CAAC,CAAC;CACrB,CAAC",
   "names": [
     "LogSomeText",
     "text",
diff --git a/crates/swc/tests/fixture/sourcemap/008/output/index.map b/crates/swc/tests/fixture/sourcemap/008/output/index.map
index fc93de352f..81a378fa86 100644
--- a/crates/swc/tests/fixture/sourcemap/008/output/index.map
+++ b/crates/swc/tests/fixture/sourcemap/008/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAAA;AAAAA,QAAQ,CAAC,oBAAoB,EAAE,IAAM;IACjCC,EAAE,CAAC,OAAO,EAAE,IAAM;QACdC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;IAEHF,EAAE,CAAC,OAAO,EAAE,IAAM;QACd,IAAI,CACJC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;IAEHF,EAAE,CAAC,OAAO,EAAE,IAAM;QACd;;WAEG,CACHC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;IAEHF,EAAE,CAAC,OAAO,EAAE,IAAM;QACdC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;CACN,CAAC,CAAC",
+  "mappings": "AAAAA;AAAAA,QAAQ,CAAC,oBAAoB,EAAE,IAAM;IACjCC,EAAE,CAAC,OAAO,EAAE,IAAM;QACdC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;IAEHF,EAAE,CAAC,OAAO,EAAE,IAAM;QACd,EAAE,GACFC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;IAEHF,EAAE,CAAC,OAAO,EAAE,IAAM;QACd;;SAEC,GACDC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;IAEHF,EAAE,CAAC,OAAO,EAAE,IAAM;QACdC,MAAM,CAAC,KAAK,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B,CAAC,CAAC;CACN,CAAC,CAAC",
   "names": [
     "describe",
     "it",
diff --git a/crates/swc/tests/fixture/sourcemap/009/output/index.map b/crates/swc/tests/fixture/sourcemap/009/output/index.map
index 5aacd93014..054cc74d35 100644
--- a/crates/swc/tests/fixture/sourcemap/009/output/index.map
+++ b/crates/swc/tests/fixture/sourcemap/009/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAA;;;;;GAKG,CACHA;AAAAA,OAAO,CAACC,GAAG,CAAC,IAAIC,KAAK,EAAE,CAACC,KAAK,CAAC,CAAC",
+  "mappings": "AAAA;;;;;CAKC,GACDA;AAAAA,OAAO,CAACC,GAAG,CAAC,IAAIC,KAAK,EAAE,CAACC,KAAK,CAAC,CAAC",
   "names": [
     "console",
     "log",
diff --git a/crates/swc/tests/fixture/sourcemap/issue-3733/2/output/index.map b/crates/swc/tests/fixture/sourcemap/issue-3733/2/output/index.map
index a52931d791..ddb3b151cc 100644
--- a/crates/swc/tests/fixture/sourcemap/issue-3733/2/output/index.map
+++ b/crates/swc/tests/fixture/sourcemap/issue-3733/2/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAAA,CAAC,EAAE,CAAC,CAAC,KAAK",
+  "mappings": "AAAAA,CAAC,EAAE,CAAC,CAAC,GAAG",
   "names": [
     "a"
   ],
diff --git a/crates/swc_ecma_parser/tests/comments/issue-4876/input.js b/crates/swc_ecma_parser/tests/comments/issue-4876/input.js
new file mode 100644
index 0000000000..c8bdaf68dc
--- /dev/null
+++ b/crates/swc_ecma_parser/tests/comments/issue-4876/input.js
@@ -0,0 +1,11 @@
+/*!
+
+ something v5.1.0
+
+more
+
+else
+*/
+// a
+console.log(1);
+// b
\ No newline at end of file
diff --git a/crates/swc_ecma_parser/tests/comments/issue-4876/input.js.stderr b/crates/swc_ecma_parser/tests/comments/issue-4876/input.js.stderr
new file mode 100644
index 0000000000..7b9fa3b18e
--- /dev/null
+++ b/crates/swc_ecma_parser/tests/comments/issue-4876/input.js.stderr
@@ -0,0 +1,34 @@
+
+  > Leading (lo)
+
+Error: 
+  > !
+  | 
+  |  something v5.1.0
+  | 
+  | more
+  | 
+  | else
+  | 
+    ,-[$DIR/tests/comments/issue-4876/input.js:10:1]
+ 10 | console.log(1);
+    : ^
+    `----
+
+  > Leading (lo)
+
+Error: 
+  >  a
+    ,-[$DIR/tests/comments/issue-4876/input.js:10:1]
+ 10 | console.log(1);
+    : ^
+    `----
+
+  > Trailing (hi)
+
+Error: 
+  >  b
+    ,-[$DIR/tests/comments/issue-4876/input.js:10:1]
+ 10 | console.log(1);
+    :                ^
+    `----