Repository: swc-project/swc (rust)

Issue:
Source map inconsistencies for comments 
### Describe the bug

`swc` does not appear to always generate valid source mappings for comment blocks like other transformers (TypeScript, Babel) do.

As per the other issue I raised, Wallaby/Quokka use comments in source code as part of our developer tooling and invalid source maps breaks some key functionality within our products.

I've tested a few different combinations, statements ending with `;` and statements without. I've also tested multiline comments and single line comments. It seems that source maps are only partially correct in the case of multi-line comments where a semi-colon is missing. That is to say, you cannot map from comments in the original code to the generated code. TypeScript and Babel to generate correct source mappings for this scenario.

I've provided links (and images) to `source-map-visualization` to show the problem: 
## [babel](https://sokra.github.io/source-map-visualization/#base64,InVzZSBzdHJpY3QiOwoKYSgpOwovKj8qLwoKYSgpOyAvLz8KCmEoKTsKLyo/Ki8KCmEoKTsgLy8/CgovLyMgc291cmNlTWFwcGluZ1VSTD1hLnNwZWMuanMubWFw,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9hLnNwZWMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxDQUFDO0FBQUU7O0FBRUgsQ0FBQyxHLENBQUU7O0FBRUgsQ0FBQztBQUFHOztBQUVKLENBQUMsRyxDQUFHIiwiZmlsZSI6ImEuc3BlYy5qcyIsInNvdXJjZXNDb250ZW50IjpbImEoKS8qPyovXHJcblxyXG5hKCkvLz9cclxuXHJcbmEoKTsvKj8qL1xyXG5cclxuYSgpOy8vPyJdfQ==,YSgpLyo/Ki8NCg0KYSgpLy8/DQoNCmEoKTsvKj8qLw0KDQphKCk7Ly8/)
![image](https://user-images.githubusercontent.com/2074089/155447086-4a72b103-1daf-4fe2-a48b-1c939de3d731.png)

## [typescript](https://sokra.github.io/source-map-visualization/#base64,InVzZSBzdHJpY3QiOwphKCk7IC8qPyovCmEoKTsgLy8/CmEoKTsgLyo/Ki8KYSgpOyAvLz8KLy8jIHNvdXJjZU1hcHBpbmdVUkw9YS5zcGVjLmpzLm1hcA==,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYS5zcGVjLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2Euc3BlYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsQ0FBQyxFQUFFLENBQUEsQ0FBQSxLQUFLO0FBRVIsQ0FBQyxFQUFFLENBQUEsQ0FBQSxHQUFHO0FBRU4sQ0FBQyxFQUFFLENBQUMsQ0FBQSxLQUFLO0FBRVQsQ0FBQyxFQUFFLENBQUMsQ0FBQSxHQUFHIn0=,YSgpLyo/Ki8NCg0KYSgpLy8/DQoNCmEoKTsvKj8qLw0KDQphKCk7Ly8/)
![image](https://user-images.githubusercontent.com/2074089/155447117-aa1ce115-77c6-4b4d-b618-2b727cba6d8e.png)

## [swc](https://sokra.github.io/source-map-visualization/#base64,YSgpOwovKj8qLyBhKCkgLy8/CjsKYSgpOyAvKj8qLyAKYSgpOyAvLz8KCi8vIyBzb3VyY2VNYXBwaW5nVVJMPWEuc3BlYy5qcy5tYXA=,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9hLnNwZWMudHMiXSwic291cmNlc0NvbnRlbnQiOlsiYSgpLyo/Ki9cclxuXHJcbmEoKS8vP1xyXG5cclxuYSgpOy8qPyovXHJcblxyXG5hKCk7Ly8/Il0sIm5hbWVzIjpbImEiXSwibWFwcGluZ3MiOiJBQUFBQSxDQUFDO0FBQUUsRUFBSyxBQUFMLENBQUssQUFBTCxFQUFLLENBRVJBLENBQUMsRUFBRSxDQUFHLEFBQUgsRUFBRyxBQUFILENBQUc7O0FBRU5BLENBQUMsR0FBRyxDQUFLLEFBQUwsRUFBSyxBQUFMLENBQUssQUFBTCxFQUFLO0FBRVRBLENBQUMsR0FBRyxDQUFHLEFBQUgsRUFBRyxBQUFILENBQUciLCJmaWxlIjoiZXhhbXBsZS5qcyJ9,YSgpLyo/Ki8NCg0KYSgpLy8/DQoNCmEoKTsvKj8qLw0KDQphKCk7Ly8/)
![image](https://user-images.githubusercontent.com/2074089/155447155-0e887e83-8071-4576-86d7-a19ea17a77f2.png)

---

You will see in most cases, the comments in the original code map back to a space in the generated code.

### Input code

```typescript
a()/*?*/

a()//?

a();/*?*/

a();//?
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": false,
      "dynamicImport": false
    }
  },
  "sourceMaps": true
}
```


### Playground link

_No response_

### Expected behavior

As described above

### Actual behavior

_No response_

### Version

@swc/core@1.2.144

### Additional context

_No response_

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


Patch:
diff --git a/crates/swc_ecma_codegen/src/comments.rs b/crates/swc_ecma_codegen/src/comments.rs
index ae11006f8e..43f1dd4799 100644
--- a/crates/swc_ecma_codegen/src/comments.rs
+++ b/crates/swc_ecma_codegen/src/comments.rs
@@ -13,7 +13,7 @@ macro_rules! write_comments {
             match cmt.kind {
                 CommentKind::Line => {
                     if $prefix_space {
-                        $e.wr.write_comment(cmt.span, " ")?;
+                        $e.wr.write_comment(swc_common::DUMMY_SP, " ")?;
                     }
                     $e.wr.write_comment(cmt.span, "//")?;
                     $e.wr.write_comment(cmt.span, &cmt.text)?;
@@ -21,7 +21,7 @@ macro_rules! write_comments {
                 }
                 CommentKind::Block => {
                     if $prefix_space {
-                        $e.wr.write_comment(cmt.span, " ")?;
+                        $e.wr.write_comment(swc_common::DUMMY_SP, " ")?;
                     }
                     $e.wr.write_comment(cmt.span, "/*")?;
                     $e.wr.write_lit(cmt.span, &cmt.text)?;


Test patch:
diff --git a/crates/swc/tests/fixture/issue-3715/1/output/index.map b/crates/swc/tests/fixture/issue-3715/1/output/index.map
index 23a5499a30..bb66b304a9 100644
--- a/crates/swc/tests/fixture/issue-3715/1/output/index.map
+++ b/crates/swc/tests/fixture/issue-3715/1/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAAA,CAAC,EAAG,CAA6B,AAA7B,EAA6B,AAA7B,yBAA6B,AAA7B,EAA6B",
+  "mappings": "AAAAA,CAAC,GAAG,EAA6B,AAA7B,yBAA6B,AAA7B,EAA6B",
   "names": [
     "a"
   ],
diff --git a/crates/swc/tests/fixture/issue-3715/2/output/index.map b/crates/swc/tests/fixture/issue-3715/2/output/index.map
index 23a5499a30..bb66b304a9 100644
--- a/crates/swc/tests/fixture/issue-3715/2/output/index.map
+++ b/crates/swc/tests/fixture/issue-3715/2/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "AAAAA,CAAC,EAAG,CAA6B,AAA7B,EAA6B,AAA7B,yBAA6B,AAA7B,EAA6B",
+  "mappings": "AAAAA,CAAC,GAAG,EAA6B,AAA7B,yBAA6B,AAA7B,EAA6B",
   "names": [
     "a"
   ],
diff --git a/crates/swc/tests/fixture/issue-3716/input/.swcrc b/crates/swc/tests/fixture/issue-3716/input/.swcrc
new file mode 100644
index 0000000000..57bd1d8a8d
--- /dev/null
+++ b/crates/swc/tests/fixture/issue-3716/input/.swcrc
@@ -0,0 +1,11 @@
+{
+    "jsc": {
+        "parser": {
+            "syntax": "typescript",
+            "tsx": false,
+            "decorators": false,
+            "dynamicImport": false
+        }
+    },
+    "sourceMaps": true
+}
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issue-3716/input/index.js b/crates/swc/tests/fixture/issue-3716/input/index.js
new file mode 100644
index 0000000000..0ccc7b4bfb
--- /dev/null
+++ b/crates/swc/tests/fixture/issue-3716/input/index.js
@@ -0,0 +1,7 @@
+a()/*?*/
+
+a()//?
+
+a();/*?*/
+
+a();//?
diff --git a/crates/swc/tests/fixture/issue-3716/output/index.js b/crates/swc/tests/fixture/issue-3716/output/index.js
new file mode 100644
index 0000000000..dcb5a3fad4
--- /dev/null
+++ b/crates/swc/tests/fixture/issue-3716/output/index.js
@@ -0,0 +1,5 @@
+a();
+/*?*/ a() //?
+;
+a(); /*?*/ 
+a(); //?
diff --git a/crates/swc/tests/fixture/issue-3716/output/index.map b/crates/swc/tests/fixture/issue-3716/output/index.map
new file mode 100644
index 0000000000..0cb9cbef41
--- /dev/null
+++ b/crates/swc/tests/fixture/issue-3716/output/index.map
@@ -0,0 +1,13 @@
+{
+  "mappings": "AAAAA,CAAC;AAAE,EAAK,AAAL,CAAK,AAAL,EAAK,CAERA,CAAC,GAAE,EAAG,AAAH,CAAG;;AAENA,CAAC,IAAG,EAAK,AAAL,CAAK,AAAL,EAAK;AAETA,CAAC,IAAG,EAAG,AAAH,CAAG",
+  "names": [
+    "a"
+  ],
+  "sources": [
+    "../../input/index.js"
+  ],
+  "sourcesContent": [
+    "a()/*?*/\n\na()//?\n\na();/*?*/\n\na();//?\n"
+  ],
+  "version": 3
+}