Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting of object and array literal in decorator #3577

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,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix formatting of object and array literal in decorator to hug parenthesis
4 changes: 3 additions & 1 deletion packages/compiler/src/formatter/print/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,12 +700,14 @@ function printCallOrDecoratorArgs(
}

// So that decorator with single object arguments have ( and { hugging.
// @deco({
// @deco(#{
// value: "foo"
// })
const shouldHug =
node.arguments.length === 1 &&
(node.arguments[0].kind === SyntaxKind.ModelExpression ||
node.arguments[0].kind === SyntaxKind.ObjectLiteral ||
node.arguments[0].kind === SyntaxKind.ArrayLiteral ||
node.arguments[0].kind === SyntaxKind.StringLiteral ||
node.arguments[0].kind === SyntaxKind.StringTemplateExpression);

Expand Down
53 changes: 53 additions & 0 deletions packages/compiler/test/formatter/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,59 @@ scalar Foo {
});
});
});

describe("scalar constructor call", () => {
it("simple call", async () => {
await assertFormat({
code: `
const foo = utcDateTime. fromISO(
"abc" );
`,
expected: `
const foo = utcDateTime.fromISO("abc");
`,
});
});

it("hug object literal", async () => {
await assertFormat({
code: `
const foo = utcDateTime. fromFoo(#{ name: "abc",
multiline1: "abc",
multiline2: "abc",
multiline3: "abc", });
`,
expected: `
const foo = utcDateTime.fromFoo(#{
name: "abc",
multiline1: "abc",
multiline2: "abc",
multiline3: "abc",
});
`,
});
});

it("hug array literal", async () => {
await assertFormat({
code: `
const foo = utcDateTime. fromFoo(#[
"very very long array",
"very very long array",
"very very long array"
]);
`,
expected: `
const foo = utcDateTime.fromFoo(#[
"very very long array",
"very very long array",
"very very long array"
]);
`,
});
});
});

describe("comments", () => {
it("format comment at position 0", async () => {
await assertFormat({
Expand Down
Loading