diff --git a/.chronus/changes/fix-comment-only-2024-1-26-16-20-24.md b/.chronus/changes/fix-comment-only-2024-1-26-16-20-24.md new file mode 100644 index 0000000000..af582e5b87 --- /dev/null +++ b/.chronus/changes/fix-comment-only-2024-1-26-16-20-24.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/compiler" +--- + +[Formatter] Formatting file with only comments would reorder the first line. diff --git a/packages/compiler/src/formatter/print/comment-handler.ts b/packages/compiler/src/formatter/print/comment-handler.ts index 22c0dde961..7d239f27de 100644 --- a/packages/compiler/src/formatter/print/comment-handler.ts +++ b/packages/compiler/src/formatter/print/comment-handler.ts @@ -1,8 +1,9 @@ import type { Printer } from "prettier"; -import { Node, SyntaxKind, TypeSpecScriptNode } from "../../core/types.js"; +import { Node, SyntaxKind, TextRange, TypeSpecScriptNode } from "../../core/types.js"; import { util } from "./util.js"; -interface CommentNode { +interface CommentNode extends TextRange { + readonly kind: SyntaxKind.LineComment | SyntaxKind.BlockComment; precedingNode?: Node; enclosingNode?: Node; followingNode?: Node; @@ -19,6 +20,14 @@ export const commentHandler: Printer["handleComments"] = { addCommentBetweenAnnotationsAndNode, handleOnlyComments, ].some((x) => x({ comment, text, options, ast: ast as TypeSpecScriptNode, isLastComment })), + remaining: (comment, text, options, ast, isLastComment) => + [handleOnlyComments].some((x) => + x({ comment, text, options, ast: ast as TypeSpecScriptNode, isLastComment }) + ), + endOfLine: (comment, text, options, ast, isLastComment) => + [handleOnlyComments].some((x) => + x({ comment, text, options, ast: ast as TypeSpecScriptNode, isLastComment }) + ), }; interface CommentContext { diff --git a/packages/compiler/test/formatter/formatter.test.ts b/packages/compiler/test/formatter/formatter.test.ts index d7b5f1bce2..c1117f9477 100644 --- a/packages/compiler/test/formatter/formatter.test.ts +++ b/packages/compiler/test/formatter/formatter.test.ts @@ -960,6 +960,22 @@ alias foo = ""; /* one */ /* two */ /* three */ }); }); + it("format empty file with comment inside starting first line", async () => { + await assertFormat({ + code: ` + // empty file + + + // commented out things +`, + expected: ` +// empty file + +// commented out things +`, + }); + }); + it("format empty model with comment inside", async () => { await assertFormat({ code: `