diff --git a/changelog_unreleased/javascript/15076.md b/changelog_unreleased/javascript/15076.md new file mode 100644 index 000000000..2676631e6 --- /dev/null +++ b/changelog_unreleased/javascript/15076.md @@ -0,0 +1,18 @@ +#### Fix comments print in `IfStatement` (#15076 by @fisker) + + +```js +function a(b) { + if (b) return 1; // comment + else return 2; +} + +/* Prettier stable */ +Error: Comment "comment" was not printed. Please report this error! + +/* Prettier main */ +function a(b) { + if (b) return 1; // comment + else return 2; +} +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 62b8df5b5..dfbdab0f6 100644 --- 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); diff --git a/tests/format/js/if/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/if/__snapshots__/jsfmt.spec.js.snap index fc49b1e98..0c6146c4d 100644 --- 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 index 9611becde..93b949b8e 100644 --- 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; +}