Repository: prettier/prettier (JavaScript)

Issue:
Skip single characters was not printed. Please report this error!
when ever i try to format my js file , it gives me this error
["ERROR" - 10:30:02 PM] Error formatting document.
["ERROR" - 10:30:02 PM] Comment "Skip single characters" was not printed. Please report this error!


Hints:
Please use the [issue template](https://github.com/prettier/prettier/tree/main/.github/ISSUE_TEMPLATE) to help us reproduce the bug. What source code was producing the error?
@kachkaev, I also have the same issue in typescript file after I updated to prettier 3.0.0 from 2.8.8

```
const a = {
  project: "SchemaCheck.String()",

  ns_dcs: [
    "SchemaCheck.StringArray()",
    (props:any) => {
      const val = props.value as string[]
      if (val.length == 2) return undefined // return success
      else return { message: 'array must include 2 items' }
    },
  ],  
}
```

output in vscode:
```
["INFO" - 1:30:39 PM] Formatting file:///home/op/packages/base-lib/src/coreTypes/messageTypes/ezDemoGre.ts
["INFO" - 1:30:39 PM] Using config file at '/home/op/packages/base-lib/.prettierrc.js'
["INFO" - 1:30:39 PM] PrettierInstance:
{
  "modulePath": "/home/op/node_modules/prettier/index.cjs",
  "importResolver": {},
  "callMethodResolvers": {},
  "currentCallMethodId": 79,
  "version": "3.0.0"
}
["INFO" - 1:30:39 PM] Using ignore file (if present) at /home/op/packages/base-lib/.prettierignore
["INFO" - 1:30:39 PM] File Info:
{
  "ignored": false,
  "inferredParser": "typescript"
}
["INFO" - 1:30:39 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 1:30:39 PM] Prettier Options:
{
  "filepath": "/home/op/packages/base-lib/src/coreTypes/messageTypes/ezDemoGre.ts",
  "parser": "typescript",
  "arrowParens": "always",
  "bracketSpacing": true,
  "bracketSameLine": false,
  "jsxSingleQuote": true,
  "printWidth": 100,
  "quoteProps": "consistent",
  "rangeStart": 0,
  "rangeEnd": null,
  "semi": false,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "endOfLine": "lf"
}
["ERROR" - 1:30:39 PM] Error formatting document.
["ERROR" - 1:30:39 PM] Comment "return success" was not printed. Please report this error!
Error: Comment "return success" was not printed. Please report this error!
    at ensureAllCommentsPrinted (file:///home/op/node_modules/prettier/index.mjs:17941:13)
    at printAstToDoc (file:///home/op/node_modules/prettier/index.mjs:18102:3)
    at async coreFormat (file:///home/op/node_modules/prettier/index.mjs:18405:14)
    at async formatWithCursor (file:///home/op/node_modules/prettier/index.mjs:18603:14)
    at async Module.format2 (file:///home/op/node_modules/prettier/index.mjs:21789:25)
["INFO" - 1:30:39 PM] Formatting completed in 13ms.
```
Ok I think I got the problem.

[playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAdKB6DUIwAQDuEATgNYCWUA5npACZzpa3QDO+AhngLx7BMY8QgA7EIAKzhgYSPKhABlMAAs4AWw4BhVWFIA6BTGKUqACgCU8gDToBQvFFYB9OmFayA2nftzFK9Vo6+obG1ACCxMQcAJ4W1t72pqIQwu4cUNHmPAB8fAk+kI74AG4cADY8eMmpeqVlAK5weByseOyhVB4Auvn25ABmeKZ1emUIVDDKPLwATFnEcDD1xFB49VAM-ZRwdADceMwLSytt9WBgcKysvUJwZaxNR8urwHhql6wcVHCyAOQckRib3q7DwlDADQYeBmYPgalYvzwAF9ekibJhBEIulYhHYUVBbBjCCQKNQ8CkYORoOhCqDBrwkmJUkh0pkcnlViwing6pVqqxauVGs1Wu0TN10EIBkMRmNqJNptDzPxOfYnid1pttnQDoJ1atWGcLldJcjTXcHnh9Xw3h8vj88P9AdFgaDwZCmjDyHCEWaoEiQFYQBSqY5kKAAWICAAFAEIVjIEDlAgxBNBgBGUV0iwUHHeABltsh+uUHhms6Qc8IOGATMgjI0gw81OR68RGyBWCYxgBFeq4ODF0twIPiVgADwU3bgfYHQ-uI5AAEd+-Bo0yEygWgBaKBwHY7QMgIwcchlEyaCBqDSJ8plI9d6hjMIwIzkdP1NdwYiFvfzssgMoMBqGUADqyjepc1YXAo8beuQxTetEiZuGmIDFI0ACSGwIDASjGMIMBhBshjRGM-6LskDygVEwiJqIlzfsUg5BpQDzEDA65fDeSAlguQbVsQ7GJjA0TCJcYAETAR6iJQMCgeQdCTMgAAcAAMQYLCu5ALFxVA8XxAEwBw6YKUpyjIDMQYgnAAAqJmboZi7qOmOwMHQ+bpFQ9T2gAYiQGivnWW6fhAIBIkiQA)
### The Code in Question: 

```typescript
const a = {
  project: "SchemaCheck.String()",

  ns_dcs: [
    "SchemaCheck.StringArray()",
    (props:any) => {
      const val = props.value as string[]
      if (val.length == 2) return undefined // return success
      else return { message: 'array must include 2 items' }
    },
  ],  
}
```

### Result

So by formatting the former coed using prettier playground, I received the following error:

```
Error: Comment "return success" was not printed. Please report this error!
    at pu (https://prettier.io/lib/standalone.js:27:5829)
    at Ze (https://prettier.io/lib/standalone.js:27:7320)
    at async Ou (https://prettier.io/lib/standalone.js:27:11026)
    at async Rr (https://prettier.io/lib/standalone.js:32:581)
    at async formatCode (https://prettier.io/worker.js:170:12)
    at async handleFormatMessage (https://prettier.io/worker.js:82:24)
    at async self.onmessage (https://prettier.io/worker.js:37:14)
````

This error specifically refers to the one-line comment "return success" being adjacent to a real return statement.

### Solution: Curly brackets

by adding curly brackets to the if-statement I was able to successfully run `prettier`

### The Code
```typescript
const f = (props:any) => {
  const val = props.value as string[]
  if (val.length == 2){
        return undefined // return success
  }
  else return { message: 'array must include 2 items' }
}
```

### The Reason - trying to prettify the comment as code because of "return ..."

I think `Prettier` thinks the comment is a code incorrectly. Might be a bug in the breakdown to AST of single-line comments in single-line If-statements.

Edit: @kachkaev That should give you more than enough info to take it from here 😄 

Patch:
diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js
--- a/src/language-js/comments/handle-comments.js
+++ b/src/language-js/comments/handle-comments.js
@@ -208,7 +208,9 @@ function handleIfStatementComments({
         addDanglingComment(
           precedingNode,
           comment,
-          markerForIfWithoutBlockAndSameLineComment,
+          precedingNode.type === "ExpressionStatement"
+            ? markerForIfWithoutBlockAndSameLineComment
+            : undefined,
         );
       } else {
         addDanglingComment(enclosingNode, comment);


Test patch:
diff --git a/tests/format/js/if/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/if/__snapshots__/jsfmt.spec.js.snap
--- a/tests/format/js/if/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/format/js/if/__snapshots__/jsfmt.spec.js.snap
@@ -93,26 +93,44 @@ parsers: ["babel", "flow", "typescript"]
 printWidth: 80
                                                                                 | printWidth
 =====================================input======================================
-if (a === 0) doSomething(); // comment A1
+     if (a === 0) doSomething(); // comment A1
 else if (a === 1) doSomethingElse(); // comment B1
 else if (a === 2) doSomethingElse(); // comment C1
 
-if (a === 0) doSomething(); /* comment A2 */
+     if (a === 0) doSomething(); /* comment A2 */
 else if (a === 1) doSomethingElse(); /* comment B2 */
 else if (a === 2) doSomethingElse(); /* comment C2 */
 
-if (a === 0) expr; // comment A3
+     if (a === 0) expr; // comment A3
 else if (a === 1) expr; // comment B3
 else if (a === 2) expr; // comment C3
 
-if (a === 0) expr; /* comment A4 */
+     if (a === 0) expr; /* comment A4 */
 else if (a === 1) expr; /* comment B4 */
 else if (a === 2) expr; /* comment C4 */
 
-if (a === 0) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment A5
+     if (a === 0) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment A5
 else if (a === 1) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment B5
 else if (a === 2) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment C5
 
+function a() {
+  if (a) return; /* comment 6a */
+  else return 2;
+
+  if (a) return 1; /* comment 6b */
+  else return 2;
+
+  if (a) throw e; /* comment 6d */
+  else return 2;
+
+  // TODO[@fisker]: fix this
+  // if (a) var a = 1; /* comment 6e */
+  // else return 2;
+
+  if (a) if (b); /* comment 6f */
+  else return 2;
+}
+
 =====================================output=====================================
 if (a === 0) doSomething(); // comment A1
 else if (a === 1) doSomethingElse(); // comment B1
@@ -137,6 +155,25 @@ else if (a === 1)
 else if (a === 2)
   looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment C5
 
+function a() {
+  if (a) return /* comment 6a */;
+  else return 2;
+
+  if (a) return 1 /* comment 6b */;
+  else return 2;
+
+  if (a) throw e /* comment 6d */;
+  else return 2;
+
+  // TODO[@fisker]: fix this
+  // if (a) var a = 1; /* comment 6e */
+  // else return 2;
+
+  if (a)
+    if (b /* comment 6f */);
+    else return 2;
+}
+
 ================================================================================
 `;
 
diff --git a/tests/format/js/if/expr_and_same_line_comments.js b/tests/format/js/if/expr_and_same_line_comments.js
--- a/tests/format/js/if/expr_and_same_line_comments.js
+++ b/tests/format/js/if/expr_and_same_line_comments.js
@@ -1,19 +1,37 @@
-if (a === 0) doSomething(); // comment A1
+     if (a === 0) doSomething(); // comment A1
 else if (a === 1) doSomethingElse(); // comment B1
 else if (a === 2) doSomethingElse(); // comment C1
 
-if (a === 0) doSomething(); /* comment A2 */
+     if (a === 0) doSomething(); /* comment A2 */
 else if (a === 1) doSomethingElse(); /* comment B2 */
 else if (a === 2) doSomethingElse(); /* comment C2 */
 
-if (a === 0) expr; // comment A3
+     if (a === 0) expr; // comment A3
 else if (a === 1) expr; // comment B3
 else if (a === 2) expr; // comment C3
 
-if (a === 0) expr; /* comment A4 */
+     if (a === 0) expr; /* comment A4 */
 else if (a === 1) expr; /* comment B4 */
 else if (a === 2) expr; /* comment C4 */
 
-if (a === 0) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment A5
+     if (a === 0) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment A5
 else if (a === 1) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment B5
 else if (a === 2) looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; // comment C5
+
+function a() {
+  if (a) return; /* comment 6a */
+  else return 2;
+
+  if (a) return 1; /* comment 6b */
+  else return 2;
+
+  if (a) throw e; /* comment 6d */
+  else return 2;
+
+  // TODO[@fisker]: fix this
+  // if (a) var a = 1; /* comment 6e */
+  // else return 2;
+
+  if (a) if (b); /* comment 6f */
+  else return 2;
+}