Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Fix: Union with anonymous variant were formatted with an extra leading `:`",
Comment thread
timotheeguerin marked this conversation as resolved.
Outdated
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
4 changes: 2 additions & 2 deletions packages/compiler/src/formatter/print/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ export function printUnionVariant(
options: TypeSpecPrettierOptions,
print: PrettierChildPrint
) {
const id = path.call(print, "id");
const value = [": ", path.call(print, "value")];
const id = path.node.id === undefined ? "" : [path.call(print, "id"), ": "];
const value = [id, path.call(print, "value")];
const { decorators } = printDecorators(path, options, print, {
tryInline: DecoratorsTryInline.unionVariant,
});
Expand Down
87 changes: 87 additions & 0 deletions packages/compiler/test/formatter/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,93 @@ enum Foo {
... Bar , One: "1",
...Baz ; Two: "2",

}
`,
expected: `
enum Foo {
...Bar,
One: "1",
...Baz,
Two: "2",
}
`,
});
});
});

describe("union", () => {
it("format simple union", async () => {
await assertFormat({
code: `
union Foo { A, B}
union Bar
{ A,
B}
`,
expected: `
union Foo {
A,
B,
}
union Bar {
A,
B,
}
`,
});
});

it("format named union", async () => {
await assertFormat({
code: `
union Foo { a: A, b: B}
`,
expected: `
enum Foo {
a: A,
a: B,
Comment thread
timotheeguerin marked this conversation as resolved.
Outdated
}

`,
});
});

it("separate members if there is decorators", async () => {
await assertFormat({
code: `
union Foo {
@doc("foo")
a: A, @doc("bar")
b : B,



@doc("third")
c : C}

`,
expected: `
enum Foo {
@doc("foo")
a: A,

@doc("bar")
b: B,

@doc("third")
c: C,
}
`,
});
});

it("format spread members", async () => {
await assertFormat({
code: `
enum Foo {
... Bar , One: "1",
...Baz ; Two: "2",

}
`,
expected: `
Expand Down