From af2526357af031306268f6bd7ebce073c13e3250 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Jan 2026 14:59:42 +0800 Subject: [PATCH] Don't force comemnts between head and body print on same line (#18808) --- .../comments/attach/handle-for-x-statement.js | 32 + src/language-js/comments/handle-comments.js | 18 +- src/language-js/print/clause.js | 45 +- src/language-js/print/do-while-statement.js | 2 +- src/language-js/print/estree.js | 4 +- src/language-js/print/for-statement.js | 2 +- src/language-js/print/if-statement.js | 4 +- src/language-js/print/while-statement.js | 2 +- .../config/format-test/failed-format-tests.js | 2 +- .../__snapshots__/format.test.js.snap | 10 +- .../__snapshots__/format.test.js.snap | 698 ++++++++++++++++++ .../between-head-and-body.js | 123 +++ .../between-head-and-body/empty-statement.js | 99 +++ .../between-head-and-body/format.test.js | 1 + .../between-head-and-body/non-block.js | 99 +++ tests/format/js/comments/if.js | 2 +- .../js/for/{9812-unstable.js => 9812-2.js} | 6 +- .../js/for/__snapshots__/format.test.js.snap | 82 +- tests/format/js/for/comment.js | 32 +- .../js/if/__snapshots__/format.test.js.snap | 43 +- .../if/comment-between-condition-and-body.js | 16 +- 21 files changed, 1202 insertions(+), 120 deletions(-) create mode 100644 src/language-js/comments/attach/handle-for-x-statement.js create mode 100644 tests/format/js/comments/between-head-and-body/__snapshots__/format.test.js.snap create mode 100644 tests/format/js/comments/between-head-and-body/between-head-and-body.js create mode 100644 tests/format/js/comments/between-head-and-body/empty-statement.js create mode 100644 tests/format/js/comments/between-head-and-body/format.test.js create mode 100644 tests/format/js/comments/between-head-and-body/non-block.js rename tests/format/js/for/{9812-unstable.js => 9812-2.js} (61%) diff --git a/src/language-js/comments/attach/handle-for-x-statement.js b/src/language-js/comments/attach/handle-for-x-statement.js new file mode 100644 index 000000000..f9009b1b0 --- /dev/null +++ b/src/language-js/comments/attach/handle-for-x-statement.js @@ -0,0 +1,32 @@ +import { addLeadingComment } from "../../../main/comments/utilities.js"; +import { locStart } from "../../loc.js"; +import { stripComments } from "../../utilities/strip-comments.js"; + +function handleForXComments({ + comment, + enclosingNode, + followingNode, + options, +}) { + if ( + (enclosingNode?.type === "ForInStatement" || + enclosingNode?.type === "ForOfStatement" || + enclosingNode?.type === "ForStatement") && + followingNode && + followingNode === enclosingNode.body + ) { + const closingParenthesisIndex = stripComments(options).lastIndexOf( + ")", + locStart(followingNode), + ); + + if (locStart(comment) > closingParenthesisIndex) { + addLeadingComment(followingNode, comment); + return true; + } + } + + return false; +} + +export { handleForXComments }; diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 2c524b4e2..13f3e355f 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -29,6 +29,7 @@ import { isUnionType, } from "../utilities/node-types.js"; import { stripComments } from "../utilities/strip-comments.js"; +import { handleForXComments } from "./attach/handle-for-x-statement.js"; import { handleIfStatementComments } from "./attach/handle-if-statement-comments.js"; import { handleWhileLikeComments } from "./attach/handle-while-like-comments.js"; import { @@ -70,7 +71,7 @@ function handleOwnLineComment(context) { handleWhileLikeComments, handleTryStatementComments, handleClassComments, - handleForComments, + handleForXComments, handleUnionTypeComments, handleMatchOrPatternComments, handleOnlyComments, @@ -100,6 +101,7 @@ function handleEndOfLineComment(context) { handleWhileLikeComments, handleTryStatementComments, handleClassComments, + handleForXComments, handleLabeledStatementComments, handleCallExpressionComments, handlePropertyComments, @@ -125,6 +127,7 @@ function handleRemainingComment(context) { handleIgnoreComments, handleIfStatementComments, handleWhileLikeComments, + handleForXComments, handleMethodNameComments, handleOnlyComments, handleTSMappedTypeComments, @@ -683,19 +686,6 @@ function handleOnlyComments({ comment, enclosingNode, ast, isLastComment }) { return false; } -function handleForComments({ comment, enclosingNode, followingNode }) { - if ( - (enclosingNode?.type === "ForInStatement" || - enclosingNode?.type === "ForOfStatement") && - followingNode && - followingNode !== enclosingNode.body - ) { - addLeadingComment(enclosingNode, comment); - return true; - } - return false; -} - function handleModuleSpecifiersComments({ comment, precedingNode, diff --git a/src/language-js/print/clause.js b/src/language-js/print/clause.js index 5b917ca65..45d74cc52 100644 --- a/src/language-js/print/clause.js +++ b/src/language-js/print/clause.js @@ -1,7 +1,30 @@ -import { indent, line } from "../../document/index.js"; -import { CommentCheckFlags, hasComment } from "../utilities/comments.js"; +import { hardline, indent, line } from "../../document/index.js"; +import hasNewline from "../../utilities/has-newline.js"; +import hasNewlineInRange from "../../utilities/has-newline-in-range.js"; +import { locEnd, locStart } from "../loc.js"; +import { + CommentCheckFlags, + getComments, + hasComment, +} from "../utilities/comments.js"; -function printClause(path, print, property = "body") { +function shouldPrintLeadingHardline(node, options) { + const leadingComments = getComments(node, CommentCheckFlags.Leading); + if (leadingComments.length === 0) { + return false; + } + + const [firstComment] = leadingComments; + const text = options.originalText; + const start = locStart(firstComment); + + return ( + hasNewlineInRange(text, start, locEnd(firstComment)) || + hasNewline(text, start, { backwards: true }) + ); +} + +function printClause(path, options, print, property = "body") { return path.call(({ node }) => { const doc = print(); @@ -9,8 +32,14 @@ function printClause(path, print, property = "body") { return hasComment(node, CommentCheckFlags.Leading) ? [" ", doc] : doc; } + const isBlockStatement = node.type === "BlockStatement"; + + if (shouldPrintLeadingHardline(node, options)) { + return isBlockStatement ? [hardline, doc] : indent([hardline, doc]); + } + if ( - node.type === "BlockStatement" || + isBlockStatement || (node.type === "IfStatement" && path.parent.type === "IfStatement" && path.key === "alternate") @@ -22,10 +51,10 @@ function printClause(path, print, property = "body") { }, property); } -const printIfStatementConsequent = (path, print) => - printClause(path, print, "consequent"); -const printIfStatementAlternate = (path, print) => - printClause(path, print, "alternate"); +const printIfStatementConsequent = (path, options, print) => + printClause(path, options, print, "consequent"); +const printIfStatementAlternate = (path, options, print) => + printClause(path, options, print, "alternate"); export { printClause as printDoWhileStatementBody, diff --git a/src/language-js/print/do-while-statement.js b/src/language-js/print/do-while-statement.js index f8ca5e674..adae9d416 100644 --- a/src/language-js/print/do-while-statement.js +++ b/src/language-js/print/do-while-statement.js @@ -4,7 +4,7 @@ import { printDoWhileStatementCondition } from "./miscellaneous.js"; function printDoWhileStatement(path, options, print) { return [ - group(["do", printDoWhileStatementBody(path, print)]), + group(["do", printDoWhileStatementBody(path, options, print)]), path.node.body.type === "BlockStatement" ? " " : hardline, "while (", printDoWhileStatementCondition(path, options, print), diff --git a/src/language-js/print/estree.js b/src/language-js/print/estree.js index 16eca70a1..1b4ab6755 100644 --- a/src/language-js/print/estree.js +++ b/src/language-js/print/estree.js @@ -265,7 +265,7 @@ function printEstree(path, options, print, args) { " in ", print("right"), ")", - printForInStatementBody(path, print), + printForInStatementBody(path, options, print), ]); case "ForOfStatement": @@ -277,7 +277,7 @@ function printEstree(path, options, print, args) { " of ", print("right"), ")", - printForOfStatementBody(path, print), + printForOfStatementBody(path, options, print), ]); case "DoExpression": diff --git a/src/language-js/print/for-statement.js b/src/language-js/print/for-statement.js index 68d9215ad..7f7450029 100644 --- a/src/language-js/print/for-statement.js +++ b/src/language-js/print/for-statement.js @@ -10,7 +10,7 @@ import { printForStatementBody } from "./clause.js"; function printForStatement(path, options, print) { const { node } = path; - const body = printForStatementBody(path, print); + const body = printForStatementBody(path, options, print); // We want to keep dangling comments above the loop to stay consistent. // Any comment positioned between the for statement and the parentheses diff --git a/src/language-js/print/if-statement.js b/src/language-js/print/if-statement.js index da609d3db..936d971e5 100644 --- a/src/language-js/print/if-statement.js +++ b/src/language-js/print/if-statement.js @@ -15,7 +15,7 @@ import { printIfStatementCondition } from "./miscellaneous.js"; function printIfStatement(path, options, print) { const { node } = path; - const consequent = printIfStatementConsequent(path, print); + const consequent = printIfStatementConsequent(path, options, print); const opening = group([ "if (", printIfStatementCondition(path, options, print), @@ -42,7 +42,7 @@ function printIfStatement(path, options, print) { ); } - parts.push("else", group(printIfStatementAlternate(path, print))); + parts.push("else", group(printIfStatementAlternate(path, options, print))); } return parts; diff --git a/src/language-js/print/while-statement.js b/src/language-js/print/while-statement.js index 3aa82279b..687fe07b5 100644 --- a/src/language-js/print/while-statement.js +++ b/src/language-js/print/while-statement.js @@ -11,7 +11,7 @@ function printWhileStatement(path, options, print) { " (", printWhileStatementCondition(path, options, print), ")", - printWhileStatementBody(path, print), + printWhileStatementBody(path, options, print), ]); } diff --git a/tests/config/format-test/failed-format-tests.js b/tests/config/format-test/failed-format-tests.js index cb9482501..3f826df90 100644 --- a/tests/config/format-test/failed-format-tests.js +++ b/tests/config/format-test/failed-format-tests.js @@ -12,7 +12,6 @@ const unstableTests = new Map( ["js/ignore/semi/head-ignored.js", (options) => options.semi === false], "js/comments/return-statement.js", "js/comments/tagged-template-literal.js", - "js/for/9812-unstable.js", [ "js/multiparser-markdown/codeblock.js", (options) => options.proseWrap === "always", @@ -22,6 +21,7 @@ const unstableTests = new Map( "typescript/prettier-ignore/mapped-types.ts", "typescript/prettier-ignore/issue-14238.ts", "js/for/continue-and-break-comment-without-blocks.js", + "js/for/comment.js", "js/sequence-expression/parenthesized.js", "typescript/satisfies-operators/comments-unstable.ts", "jsx/comments/in-attributes.js", diff --git a/tests/format/js/comments/__snapshots__/format.test.js.snap b/tests/format/js/comments/__snapshots__/format.test.js.snap index 442cfdc99..533342b08 100644 --- a/tests/format/js/comments/__snapshots__/format.test.js.snap +++ b/tests/format/js/comments/__snapshots__/format.test.js.snap @@ -2951,7 +2951,7 @@ semi: false | printWidth =====================================input====================================== if (1) -// comment +// 11 { false } @@ -3013,7 +3013,8 @@ else if (15) // comment true =====================================output===================================== -if (1) // comment +if (1) +// 11 { false } @@ -3098,7 +3099,7 @@ printWidth: 80 | printWidth =====================================input====================================== if (1) -// comment +// 11 { false } @@ -3160,7 +3161,8 @@ else if (15) // comment true =====================================output===================================== -if (1) // comment +if (1) +// 11 { false; } diff --git a/tests/format/js/comments/between-head-and-body/__snapshots__/format.test.js.snap b/tests/format/js/comments/between-head-and-body/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..bea87a20c --- /dev/null +++ b/tests/format/js/comments/between-head-and-body/__snapshots__/format.test.js.snap @@ -0,0 +1,698 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`between-head-and-body.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +do // 11 +{} while(1) +for(a in b) // 12 +{} +for(a of b) // 13 +{} +for(;;) // 14 +{} +if(a) // 15 +{} +else // 152 +{} +while(a) // 16 +{} +with(a) // 17 +{} + +do +// 21 +{} while(1) +for(a in b) +// 22 +{} +for(a of b) +// 23 +{} +for(;;) +// 24 +{} +if(a) +// 25 +{} +else +// 252 +{} +while(a) +// 26 +{} +with(a) +// 27 +{} + +do /* 31 */ +{} while(1) +for(a in b) /* 32 */ +{} +for(a of b) /* 33 */ +{} +for(;;) // 34 */ +{} +if(a) /* 35 */ +{} +else /* 352 */ +{} +while(a) /* 36 */ +{} +with(a) /* 37 */ +{} + +do /* 41 */ +{} while(1) +for(a in b) /* 42 */ +{} +for(a of b) /* 43 */ +{} +for(;;) // 34 */ +{} +if(a) /* 45 */ +{} +else /* 452 */ +{} +while(a) /* 46 */ +{} +with(a) /* 47 */ +{} + +do /* +51 */ +{} while(1) +for(a in b) /* +52 */ +{} +for(a of b) /* +53 */ +{} +for(;;) // 54 */ +{} +if(a) /* +55 */ +{} +else /* +552 */ +{} +while(a) /* +56 */ +{} +with(a) /* +57 */ +{} + +do /* */ /* +61 */ +{} while(1) +for(a in b) /* */ /* +62 */ +{} +for(a of b) /* */ /* +63 */ +{} +for(;;) // 54 */ +{} +if(a) /* */ /* +65 */ +{} +else /* */ /* +652 */ +{} +while(a) /* */ /* +66 */ +{} +with(a) /* */ /* +67 */ +{} + +=====================================output===================================== +do // 11 +{} while (1); +for (a in b) // 12 +{ +} +for (a of b) // 13 +{ +} +for (;;) // 14 +{} +if (a) // 15 +{ +} else // 152 +{ +} +while (a) // 16 +{} +with (a) // 17 +{ +} + +do +// 21 +{} while (1); +for (a in b) +// 22 +{ +} +for (a of b) +// 23 +{ +} +for (;;) +// 24 +{} +if (a) +// 25 +{ +} else +// 252 +{ +} +while (a) +// 26 +{} +with (a) +// 27 +{ +} + +do /* 31 */ {} while (1); +for (a in b) /* 32 */ +{ +} +for (a of b) /* 33 */ +{ +} +for (;;) // 34 */ +{} +if (a) /* 35 */ +{ +} else /* 352 */ +{ +} +while (a) /* 36 */ {} +with (a) /* 37 */ +{ +} + +do /* 41 */ {} while (1); +for (a in b) /* 42 */ +{ +} +for (a of b) /* 43 */ +{ +} +for (;;) // 34 */ +{} +if (a) /* 45 */ +{ +} else /* 452 */ +{ +} +while (a) /* 46 */ {} +with (a) /* 47 */ +{ +} + +do +/* +51 */ +{} while (1); +for (a in b) +/* +52 */ +{ +} +for (a of b) +/* +53 */ +{ +} +for (;;) // 54 */ +{} +if (a) +/* +55 */ +{ +} else +/* +552 */ +{ +} +while (a) +/* +56 */ +{} +with (a) +/* +57 */ +{ +} + +do /* */ /* +61 */ +{} while (1); +for (a in b) /* */ /* +62 */ +{ +} +for (a of b) /* */ /* +63 */ +{ +} +for (;;) // 54 */ +{} +if (a) /* */ /* +65 */ +{ +} else /* */ /* +652 */ +{ +} +while (a) /* */ /* +66 */ +{} +with (a) /* */ /* +67 */ +{ +} + +================================================================================ +`; + +exports[`empty-statement.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +do // 11 +; while(1) +for(a in b) // 12 +; +for(a of b) // 13 +; +for(;;) // 14 +; +if(a) // 15 +; +else // 152 +; +while(a) // 16 +; +with(a) // 17 +; + +do +// 21 +; while(1) +for(a in b) +// 22 +; +for(a of b) +// 23 +; +for(;;) +// 24 +; +if(a) +// 25 +; +else +// 252 +; +while(a) +// 26 +; +with(a) +// 27 +; + +do /* 31 */ +; while(1) +for(a in b) /* 32 */ +; +for(a of b) /* 33 */ +; +for(;;) // 34 */ +; +if(a) /* 35 */ +; +else /* 352 */ +; +while(a) /* 36 */ +; +with(a) /* 37 */ +; + +do /* 41 */ +; while(1) +for(a in b) /* 42 */ +; +for(a of b) /* 43 */ +; +for(;;) // 34 */ +; +if(a) /* 45 */ +; +else /* 452 */ +; +while(a) /* 46 */ +; +with(a) /* 47 */ +; + +do /* +51 */ +; while(1) +for(a in b) /* +52 */ +; +for(a of b) /* +53 */ +; +for(;;) // 34 */ +; +if(a) /* +55 */ +; +else /* +552 */ +; +while(a) /* +56 */ +; +with(a) /* +57 */ +; + +=====================================output===================================== +do // 11 +; +while (1); +for (a in b) // 12 +; +for (a of b) // 13 +; +for (;;) // 14 +; +if (a) // 15 +; +else // 152 +; +while (a) // 16 +; +with (a) // 17 +; + +do // 21 +; +while (1); +for (a in b) // 22 +; +for (a of b) // 23 +; +for (;;) // 24 +; +if (a) // 25 +; +else // 252 +; +while (a) // 26 +; +with (a) // 27 +; + +do /* 31 */ ; +while (1); +for (a in b) /* 32 */ ; +for (a of b) /* 33 */ ; +for (;;) // 34 */ +; +if (a) /* 35 */ ; +else /* 352 */ ; +while (a) /* 36 */ ; +with (a) /* 37 */ ; + +do /* 41 */ ; +while (1); +for (a in b) /* 42 */ ; +for (a of b) /* 43 */ ; +for (;;) // 34 */ +; +if (a) /* 45 */ ; +else /* 452 */ ; +while (a) /* 46 */ ; +with (a) /* 47 */ ; + +do /* +51 */ +; +while (1); +for (a in b) /* +52 */ +; +for (a of b) /* +53 */ +; +for (;;) // 34 */ +; +if (a) /* +55 */ +; +else /* +552 */ +; +while (a) /* +56 */ +; +with (a) /* +57 */ +; + +================================================================================ +`; + +exports[`non-block.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +do // 11 +foo(); while(1) +for(a in b) // 12 +foo(); +for(a of b) // 13 +foo(); +for(;;) // 14 +foo(); +if(a) // 15 +foo(); +else // 152 +foo(); +while(a) // 16 +foo(); +with(a) // 17 +foo(); + +do +// 21 +foo(); while(1) +for(a in b) +// 22 +foo(); +for(a of b) +// 23 +foo(); +for(;;) +// 24 +foo(); +if(a) +// 25 +foo(); +else +// 252 +foo(); +while(a) +// 26 +foo(); +with(a) +// 27 +foo(); + +do /* 31 */ +foo(); while(1) +for(a in b) /* 32 */ +foo(); +for(a of b) /* 33 */ +foo(); +for(;;) // 34 */ +foo(); +if(a) /* 35 */ +foo(); +else /* 352 */ +foo(); +while(a) /* 36 */ +foo(); +with(a) /* 37 */ +foo(); + +do /* 41 */ +foo(); while(1) +for(a in b) /* 42 */ +foo(); +for(a of b) /* 43 */ +foo(); +for(;;) // 34 */ +foo(); +if(a) /* 45 */ +foo(); +else /* 452 */ +foo(); +while(a) /* 46 */ +foo(); +with(a) /* 47 */ +foo(); + +do /* +51 */ +foo(); while(1) +for(a in b) /* +52 */ +foo(); +for(a of b) /* +53 */ +foo(); +for(;;) // 34 */ +foo(); +if(a) /* +55 */ +foo(); +else /* +552 */ +foo(); +while(a) /* +56 */ +foo(); +with(a) /* +57 */ +foo(); + +=====================================output===================================== +do + // 11 + foo(); +while (1); +for (a in b) + // 12 + foo(); +for (a of b) + // 13 + foo(); +for (;;) + // 14 + foo(); +if (a) + // 15 + foo(); +else + // 152 + foo(); +while (a) + // 16 + foo(); +with (a) + // 17 + foo(); + +do + // 21 + foo(); +while (1); +for (a in b) + // 22 + foo(); +for (a of b) + // 23 + foo(); +for (;;) + // 24 + foo(); +if (a) + // 25 + foo(); +else + // 252 + foo(); +while (a) + // 26 + foo(); +with (a) + // 27 + foo(); + +do /* 31 */ foo(); +while (1); +for (a in b) /* 32 */ foo(); +for (a of b) /* 33 */ foo(); +for (;;) + // 34 */ + foo(); +if (a) /* 35 */ foo(); +else /* 352 */ foo(); +while (a) /* 36 */ foo(); +with (a) /* 37 */ foo(); + +do /* 41 */ foo(); +while (1); +for (a in b) /* 42 */ foo(); +for (a of b) /* 43 */ foo(); +for (;;) + // 34 */ + foo(); +if (a) /* 45 */ foo(); +else /* 452 */ foo(); +while (a) /* 46 */ foo(); +with (a) /* 47 */ foo(); + +do + /* +51 */ + foo(); +while (1); +for (a in b) + /* +52 */ + foo(); +for (a of b) + /* +53 */ + foo(); +for (;;) + // 34 */ + foo(); +if (a) + /* +55 */ + foo(); +else + /* +552 */ + foo(); +while (a) + /* +56 */ + foo(); +with (a) + /* +57 */ + foo(); + +================================================================================ +`; diff --git a/tests/format/js/comments/between-head-and-body/between-head-and-body.js b/tests/format/js/comments/between-head-and-body/between-head-and-body.js new file mode 100644 index 000000000..2fb54fde2 --- /dev/null +++ b/tests/format/js/comments/between-head-and-body/between-head-and-body.js @@ -0,0 +1,123 @@ +do // 11 +{} while(1) +for(a in b) // 12 +{} +for(a of b) // 13 +{} +for(;;) // 14 +{} +if(a) // 15 +{} +else // 152 +{} +while(a) // 16 +{} +with(a) // 17 +{} + +do +// 21 +{} while(1) +for(a in b) +// 22 +{} +for(a of b) +// 23 +{} +for(;;) +// 24 +{} +if(a) +// 25 +{} +else +// 252 +{} +while(a) +// 26 +{} +with(a) +// 27 +{} + +do /* 31 */ +{} while(1) +for(a in b) /* 32 */ +{} +for(a of b) /* 33 */ +{} +for(;;) // 34 */ +{} +if(a) /* 35 */ +{} +else /* 352 */ +{} +while(a) /* 36 */ +{} +with(a) /* 37 */ +{} + +do /* 41 */ +{} while(1) +for(a in b) /* 42 */ +{} +for(a of b) /* 43 */ +{} +for(;;) // 34 */ +{} +if(a) /* 45 */ +{} +else /* 452 */ +{} +while(a) /* 46 */ +{} +with(a) /* 47 */ +{} + +do /* +51 */ +{} while(1) +for(a in b) /* +52 */ +{} +for(a of b) /* +53 */ +{} +for(;;) // 54 */ +{} +if(a) /* +55 */ +{} +else /* +552 */ +{} +while(a) /* +56 */ +{} +with(a) /* +57 */ +{} + +do /* */ /* +61 */ +{} while(1) +for(a in b) /* */ /* +62 */ +{} +for(a of b) /* */ /* +63 */ +{} +for(;;) // 54 */ +{} +if(a) /* */ /* +65 */ +{} +else /* */ /* +652 */ +{} +while(a) /* */ /* +66 */ +{} +with(a) /* */ /* +67 */ +{} diff --git a/tests/format/js/comments/between-head-and-body/empty-statement.js b/tests/format/js/comments/between-head-and-body/empty-statement.js new file mode 100644 index 000000000..1baf05f2f --- /dev/null +++ b/tests/format/js/comments/between-head-and-body/empty-statement.js @@ -0,0 +1,99 @@ +do // 11 +; while(1) +for(a in b) // 12 +; +for(a of b) // 13 +; +for(;;) // 14 +; +if(a) // 15 +; +else // 152 +; +while(a) // 16 +; +with(a) // 17 +; + +do +// 21 +; while(1) +for(a in b) +// 22 +; +for(a of b) +// 23 +; +for(;;) +// 24 +; +if(a) +// 25 +; +else +// 252 +; +while(a) +// 26 +; +with(a) +// 27 +; + +do /* 31 */ +; while(1) +for(a in b) /* 32 */ +; +for(a of b) /* 33 */ +; +for(;;) // 34 */ +; +if(a) /* 35 */ +; +else /* 352 */ +; +while(a) /* 36 */ +; +with(a) /* 37 */ +; + +do /* 41 */ +; while(1) +for(a in b) /* 42 */ +; +for(a of b) /* 43 */ +; +for(;;) // 34 */ +; +if(a) /* 45 */ +; +else /* 452 */ +; +while(a) /* 46 */ +; +with(a) /* 47 */ +; + +do /* +51 */ +; while(1) +for(a in b) /* +52 */ +; +for(a of b) /* +53 */ +; +for(;;) // 34 */ +; +if(a) /* +55 */ +; +else /* +552 */ +; +while(a) /* +56 */ +; +with(a) /* +57 */ +; diff --git a/tests/format/js/comments/between-head-and-body/format.test.js b/tests/format/js/comments/between-head-and-body/format.test.js new file mode 100644 index 000000000..67c251cce --- /dev/null +++ b/tests/format/js/comments/between-head-and-body/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["babel", "flow", "typescript"]); diff --git a/tests/format/js/comments/between-head-and-body/non-block.js b/tests/format/js/comments/between-head-and-body/non-block.js new file mode 100644 index 000000000..c4af3eb6c --- /dev/null +++ b/tests/format/js/comments/between-head-and-body/non-block.js @@ -0,0 +1,99 @@ +do // 11 +foo(); while(1) +for(a in b) // 12 +foo(); +for(a of b) // 13 +foo(); +for(;;) // 14 +foo(); +if(a) // 15 +foo(); +else // 152 +foo(); +while(a) // 16 +foo(); +with(a) // 17 +foo(); + +do +// 21 +foo(); while(1) +for(a in b) +// 22 +foo(); +for(a of b) +// 23 +foo(); +for(;;) +// 24 +foo(); +if(a) +// 25 +foo(); +else +// 252 +foo(); +while(a) +// 26 +foo(); +with(a) +// 27 +foo(); + +do /* 31 */ +foo(); while(1) +for(a in b) /* 32 */ +foo(); +for(a of b) /* 33 */ +foo(); +for(;;) // 34 */ +foo(); +if(a) /* 35 */ +foo(); +else /* 352 */ +foo(); +while(a) /* 36 */ +foo(); +with(a) /* 37 */ +foo(); + +do /* 41 */ +foo(); while(1) +for(a in b) /* 42 */ +foo(); +for(a of b) /* 43 */ +foo(); +for(;;) // 34 */ +foo(); +if(a) /* 45 */ +foo(); +else /* 452 */ +foo(); +while(a) /* 46 */ +foo(); +with(a) /* 47 */ +foo(); + +do /* +51 */ +foo(); while(1) +for(a in b) /* +52 */ +foo(); +for(a of b) /* +53 */ +foo(); +for(;;) // 34 */ +foo(); +if(a) /* +55 */ +foo(); +else /* +552 */ +foo(); +while(a) /* +56 */ +foo(); +with(a) /* +57 */ +foo(); diff --git a/tests/format/js/comments/if.js b/tests/format/js/comments/if.js index 543ecd04b..5739c2a4c 100644 --- a/tests/format/js/comments/if.js +++ b/tests/format/js/comments/if.js @@ -1,5 +1,5 @@ if (1) -// comment +// 11 { false } diff --git a/tests/format/js/for/9812-unstable.js b/tests/format/js/for/9812-2.js similarity index 61% rename from tests/format/js/for/9812-unstable.js rename to tests/format/js/for/9812-2.js index c7ec7608e..2501e7c38 100644 --- a/tests/format/js/for/9812-unstable.js +++ b/tests/format/js/for/9812-2.js @@ -1,11 +1,11 @@ for(x of y) -// comment +// 11 {bar();} for(x of y) -// comment +// 21 {} for(x of y) -// comment +// 31 ; diff --git a/tests/format/js/for/__snapshots__/format.test.js.snap b/tests/format/js/for/__snapshots__/format.test.js.snap index 4fbe4630e..f2f0795fd 100644 --- a/tests/format/js/for/__snapshots__/format.test.js.snap +++ b/tests/format/js/for/__snapshots__/format.test.js.snap @@ -114,35 +114,37 @@ for ( ================================================================================ `; -exports[`9812-unstable.js format 1`] = ` +exports[`9812-2.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 | printWidth =====================================input====================================== for(x of y) -// comment +// 11 {bar();} for(x of y) -// comment +// 21 {} for(x of y) -// comment +// 31 ; =====================================output===================================== -for (x of y) // comment +for (x of y) +// 11 { bar(); } -for (x of y) // comment +for (x of y) +// 21 { } -for (x of y) // comment +for (x of y) // 31 ; ================================================================================ @@ -155,69 +157,71 @@ printWidth: 80 | printWidth =====================================input====================================== for (x -/*a*/ -in //b -y) //c +/*1a*/ +in //1b +y) //1c ; -for (x in /*a*/ //b -y); //c +for (x in /*2a*/ //2b +y); //2c -for (x /*a*/ in y); //b //c +for (x /*3a*/ in y); //3b //3c for (x -//a +//4a in y); for(x in -//a +//5a y); for (x -/*a*/ -of //b -y) //c +/*6a*/ +of //6b +y) //6c ; -for (x of /*a*/ //b -y); //c +for (x of /*7a*/ //7b +y); //7c -for (x /*a*/ of y); //b //c +for (x /*8a*/ of y); //8b //8c for (x -//a +//9a of y); for(x of -//a +//10a y); =====================================output===================================== -/*a*/ -for (x in y); //b //c +for (x in /*1a*/ //1b +y) //1c +; -for (x /*a*/ in y); //b //c +for (x /*2a*/ in y); //2b //2c -for (x /*a*/ in y); //b //c +for (x /*3a*/ in y); //3b //3c -//a -for (x in y); +for (x in //4a +y); -//a -for (x in y); +for (x in //5a +y); -/*a*/ -for (x of y); //b //c +for (x of /*6a*/ //6b +y) //6c +; -for (x /*a*/ of y); //b //c +for (x /*7a*/ of y); //7b //7c -for (x /*a*/ of y); //b //c +for (x /*8a*/ of y); //8b //8c -//a -for (x of y); +for (x of //9a +y); -//a -for (x of y); +for (x of //10a +y); ================================================================================ `; diff --git a/tests/format/js/for/comment.js b/tests/format/js/for/comment.js index 0e10648d2..8251d85f3 100644 --- a/tests/format/js/for/comment.js +++ b/tests/format/js/for/comment.js @@ -1,37 +1,37 @@ for (x -/*a*/ -in //b -y) //c +/*1a*/ +in //1b +y) //1c ; -for (x in /*a*/ //b -y); //c +for (x in /*2a*/ //2b +y); //2c -for (x /*a*/ in y); //b //c +for (x /*3a*/ in y); //3b //3c for (x -//a +//4a in y); for(x in -//a +//5a y); for (x -/*a*/ -of //b -y) //c +/*6a*/ +of //6b +y) //6c ; -for (x of /*a*/ //b -y); //c +for (x of /*7a*/ //7b +y); //7c -for (x /*a*/ of y); //b //c +for (x /*8a*/ of y); //8b //8c for (x -//a +//9a of y); for(x of -//a +//10a y); diff --git a/tests/format/js/if/__snapshots__/format.test.js.snap b/tests/format/js/if/__snapshots__/format.test.js.snap index ad27d7324..84df0c0aa 100644 --- a/tests/format/js/if/__snapshots__/format.test.js.snap +++ b/tests/format/js/if/__snapshots__/format.test.js.snap @@ -44,76 +44,78 @@ parsers: ["babel", "flow", "typescript"] printWidth: 80 | printWidth =====================================input====================================== -if (1) { // foo may not exist +if (1) { // 11 doThing(foo); } -if (1){}else { // foo may not exist +if (1){}else { // 12 doThing(foo); } if (2) -// foo may not exist +// 21 { doThing(foo); } if (2){}else -// foo may not exist +// 22 { doThing(foo); } -if (3)// foo may not exist +if (3)// 31 { doThing(foo); } -if (3){}else// foo may not exist +if (3){}else// 32 { doThing(foo); } -if (4)/* foo may not exist */{ +if (4)/* 41 */{ doThing(foo); } -if (4){}else/* foo may not exist */{ +if (4){}else/* 42 */{ doThing(foo); } =====================================output===================================== if (1) { - // foo may not exist + // 11 doThing(foo); } if (1) { } else { - // foo may not exist + // 12 doThing(foo); } -if (2) // foo may not exist +if (2) +// 21 { doThing(foo); } if (2) { -} else // foo may not exist +} else +// 22 { doThing(foo); } -if (3) // foo may not exist +if (3) // 31 { doThing(foo); } if (3) { -} else // foo may not exist +} else // 32 { doThing(foo); } -if (4) /* foo may not exist */ { +if (4) /* 41 */ { doThing(foo); } if (4) { -} else /* foo may not exist */ { +} else /* 42 */ { doThing(foo); } @@ -358,12 +360,15 @@ async function f3() { } async function f4() { - if (untrackedChoice === 0) /* Cancel */ { + if (untrackedChoice === 0) + /* Cancel */ { return null; - } else if (untrackedChoice === 1) /* Add */ { + } else if (untrackedChoice === 1) + /* Add */ { await repository.addAll(Array.from(untrackedChanges.keys())); shouldAmend = true; - } else if (untrackedChoice === 2) /* Allow Untracked */ { + } else if (untrackedChoice === 2) + /* Allow Untracked */ { allowUntracked = true; } } diff --git a/tests/format/js/if/comment-between-condition-and-body.js b/tests/format/js/if/comment-between-condition-and-body.js index 53cf04947..37fc04a4f 100644 --- a/tests/format/js/if/comment-between-condition-and-body.js +++ b/tests/format/js/if/comment-between-condition-and-body.js @@ -1,33 +1,33 @@ -if (1) { // foo may not exist +if (1) { // 11 doThing(foo); } -if (1){}else { // foo may not exist +if (1){}else { // 12 doThing(foo); } if (2) -// foo may not exist +// 21 { doThing(foo); } if (2){}else -// foo may not exist +// 22 { doThing(foo); } -if (3)// foo may not exist +if (3)// 31 { doThing(foo); } -if (3){}else// foo may not exist +if (3){}else// 32 { doThing(foo); } -if (4)/* foo may not exist */{ +if (4)/* 41 */{ doThing(foo); } -if (4){}else/* foo may not exist */{ +if (4){}else/* 42 */{ doThing(foo); }