Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 13 additions & 6 deletions packages/compiler/src/formatter/print/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,12 +969,19 @@ 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]);
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/compiler/test/formatter/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand Down
Loading