Skip to content
Open
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 inserting a blank line and over-indenting a `union` expression used directly as one of multiple template arguments (e.g. `PickProperties<Source, "a" | "b">`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/http-client-java"
---

Reformat union template arguments in test files to match updated formatter style.
30 changes: 23 additions & 7 deletions packages/compiler/src/formatter/print/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,14 +1403,30 @@ export function printUnion(
options: TypeSpecPrettierOptions,
print: PrettierChildPrint,
) {
const types = path.map((typePath) => {
const printedType: Doc = align(2, print(typePath));
return printedType;
}, "options");

const shouldAddStartLine = true;
// A union that is one of several template arguments must not add its own
// leading line + indent: the argument list already provides them, so stacking
// both yields a blank line and an extra indent level for the variants.
// The per-variant align(2) is always kept though (matching prettier's union
// printer): it accounts for the "| " prefix so a variant that breaks (e.g. a
// nested template) stays aligned under its content.
// https://github.com/microsoft/typespec/issues/11009
const inMultiTemplateArgumentList = isInMultiTemplateArgumentList(path);
const types = path.map((typePath) => align(2, print(typePath)), "options");

const shouldAddStartLine = !inMultiTemplateArgumentList;
const code = [ifBreak([shouldAddStartLine ? line : "", "| "], ""), join([line, "| "], types)];
return group(indent(code));
return inMultiTemplateArgumentList ? group(code) : group(indent(code));
}

/** Whether the node is a direct argument of a template reference with more than one argument. */
function isInMultiTemplateArgumentList(path: AstPath<Node>): boolean {
// A `TemplateArgument` only ever lives in `TypeReference.arguments`, so the
// owning `TypeReference` is always the next node ancestor.
if (path.getParentNode()?.kind !== SyntaxKind.TemplateArgument) {
return false;
}
const reference = path.getParentNode(1);
return reference?.kind === SyntaxKind.TypeReference && reference.arguments.length > 1;
}

export function printTypeReference(
Expand Down
64 changes: 64 additions & 0 deletions packages/compiler/test/formatter/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,70 @@ union Foo {
@doc("third")
c: C,
}
`,
});
});

// Regression test for https://github.com/microsoft/typespec/issues/11009
it("does not add a blank line or extra indent for a union used as a template argument", async () => {
await assertFormat({
code: `
model Picked is PickProperties<Sample, "alpha" | "bravo" | "charlie" | "delta" | "echo" | "foxtrot" | "golf" | "hotel">;
`,
expected: `
model Picked
is PickProperties<
Sample,
| "alpha"
| "bravo"
| "charlie"
| "delta"
| "echo"
| "foxtrot"
| "golf"
| "hotel"
>;
`,
});
});

it("keeps leading | and indent for a union used as the only template argument", async () => {
await assertFormat({
code: `
model Picked is PickProperties<"alpha" | "bravo" | "charlie" | "delta" | "echo" | "foxtrot" | "golf" | "hotel">;
`,
expected: `
model Picked
is PickProperties<
| "alpha"
| "bravo"
| "charlie"
| "delta"
| "echo"
| "foxtrot"
| "golf"
| "hotel">;
`,
});
});

// Regression test for https://github.com/microsoft/typespec/issues/11009
it("keeps the variant alignment so a nested template argument stays indented", async () => {
await assertFormat({
code: `
model Created is Operation<Request, Response | CreatedResponse<ResponseBodyModel, LroHeaders = AsyncOperationHeader<FinalResult = ResponseBodyModel>>, Error>;
`,
expected: `
model Created
is Operation<
Request,
| Response
| CreatedResponse<
ResponseBodyModel,
LroHeaders = AsyncOperationHeader<FinalResult = ResponseBodyModel>
>,
Error
>;
`,
});
});
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,11 @@ interface ImmutableResourceModel {
Azure.ResourceManager.Foundations.DefaultBaseParameters<NginxConfigurationResponse>
>,
NginxConfigurationRequest,

| NginxConfigurationResponse
| ArmResourceCreatedResponse<
NginxConfigurationResponse,
LroHeaders = ArmAsyncOperationHeader<FinalResult = NginxConfigurationResponse>
>,
| NginxConfigurationResponse
| ArmResourceCreatedResponse<
NginxConfigurationResponse,
LroHeaders = ArmAsyncOperationHeader<FinalResult = NginxConfigurationResponse>
>,
ErrorResponse,
OptionalRequestBody = true
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ op RpcOperationWithAdditionalResponse<
TErrorResponse = Azure.Core.Foundations.ErrorResponse
> is Foundations.Operation<
TParams & Azure.Core.Traits.Private.TraitProperties<Traits, TraitLocation.Parameters>,

| (TResponse & Azure.Core.Traits.Private.TraitProperties<Traits, TraitLocation.Response>)
| TAdditionalResponse,
| (TResponse & Azure.Core.Traits.Private.TraitProperties<Traits, TraitLocation.Response>)
| TAdditionalResponse,
Traits,
TErrorResponse
>;
Expand Down