From 7f62cffe44e59eda418cf0612252dcd8794be665 Mon Sep 17 00:00:00 2001 From: Tokuhiro Matsuno Date: Mon, 13 Apr 2026 19:00:07 +0900 Subject: [PATCH 1/2] fix(compiler): preserve dangling comments in empty operation parameter list `tsp format` crashed on `op find(/* conditions */): unknown;` because `printModelExpression` discarded dangling comments when the property list was empty, tripping prettier's `ensureAllCommentsPrinted` check. Print dangling comments in that branch so the block comment survives formatting. --- ...r-dangling-comment-empty-params-2026-04-13.md | 7 +++++++ packages/compiler/src/formatter/print/printer.ts | 16 ++++++++++------ .../compiler/test/formatter/formatter.test.ts | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 .chronus/changes/fix-formatter-dangling-comment-empty-params-2026-04-13.md diff --git a/.chronus/changes/fix-formatter-dangling-comment-empty-params-2026-04-13.md b/.chronus/changes/fix-formatter-dangling-comment-empty-params-2026-04-13.md new file mode 100644 index 00000000000..ea8ee168957 --- /dev/null +++ b/.chronus/changes/fix-formatter-dangling-comment-empty-params-2026-04-13.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Fix formatter crash when an operation's parameter list contains only a block comment (e.g. `op find(/* conditions */): unknown;`). Dangling comments in empty parameter lists are now preserved instead of being dropped. diff --git a/packages/compiler/src/formatter/print/printer.ts b/packages/compiler/src/formatter/print/printer.ts index 5ceb7b70abc..b018bcb98e6 100644 --- a/packages/compiler/src/formatter/print/printer.ts +++ b/packages/compiler/src/formatter/print/printer.ts @@ -969,12 +969,16 @@ export function printModelExpression( if (inBlock) { return group(printModelPropertiesBlock(path, options, print)); } else { - const properties = - node.properties.length === 0 - ? "" - : indent( - joinMembersInBlock(path, "properties", options, print, ifBreak(",", ", "), softline), - ); + const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling); + if (node.properties.length === 0) { + if (nodeHasComments) { + return group([indent(printDanglingComments(path, options, { sameIndent: true })), softline]); + } + return group(["", softline]); + } + const properties = indent( + joinMembersInBlock(path, "properties", options, print, ifBreak(",", ", "), softline), + ); return group([properties, softline]); } } diff --git a/packages/compiler/test/formatter/formatter.test.ts b/packages/compiler/test/formatter/formatter.test.ts index 1360a82e543..6be43cc8ded 100644 --- a/packages/compiler/test/formatter/formatter.test.ts +++ b/packages/compiler/test/formatter/formatter.test.ts @@ -737,6 +737,21 @@ op foo( }); }); + it("keeps block comment in empty parameter list", async () => { + await assertFormat({ + code: ` +namespace MyApp; + +op find( /* conditions */) : unknown; +`, + expected: ` +namespace MyApp; + +op find(/* conditions */): unknown; +`, + }); + }); + it("wrap in new lines parameters with block comments", async () => { await assertFormat({ code: ` From 0b4b4d9b2878c163c5c8ed5b30407c093b88f835 Mon Sep 17 00:00:00 2001 From: Tokuhiro Matsuno Date: Mon, 13 Apr 2026 22:18:39 +0900 Subject: [PATCH 2/2] pnpm format --- packages/compiler/src/formatter/print/printer.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/compiler/src/formatter/print/printer.ts b/packages/compiler/src/formatter/print/printer.ts index b018bcb98e6..0a104e021d7 100644 --- a/packages/compiler/src/formatter/print/printer.ts +++ b/packages/compiler/src/formatter/print/printer.ts @@ -972,7 +972,10 @@ export function printModelExpression( const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling); if (node.properties.length === 0) { if (nodeHasComments) { - return group([indent(printDanglingComments(path, options, { sameIndent: true })), softline]); + return group([ + indent(printDanglingComments(path, options, { sameIndent: true })), + softline, + ]); } return group(["", softline]); }