Fix: File with only comment and one on the first line reorder comments (#2955)

fix #2508
This commit is contained in:
Timothee Guerin 2024-02-28 13:24:07 -08:00 коммит произвёл GitHub
Родитель 8b062e33bb
Коммит f884772255
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 35 добавлений и 2 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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<Node>["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 {

Просмотреть файл

@ -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: `