Fix: File with only comment and one on the first line reorder comments (#2955)
fix #2508
This commit is contained in:
Родитель
8b062e33bb
Коммит
f884772255
|
@ -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: `
|
||||
|
|
Загрузка…
Ссылка в новой задаче