Skip to content

Commit

Permalink
Fix formatting of object and array literal in decorator (#3577)
Browse files Browse the repository at this point in the history
Currently formats to 
```
@example(
  #{
    id: "some",
    bar: "thing",
    date: utcDateTime.fromISO("2020-01-01T00:00:00Z"),
    unixDate: unixTimestamp32.fromISO("2020-01-01T00:00:00Z"),
    encodedAsRfc7231: utcDateTime.fromISO("2020-01-01T00:00:00Z"),
    dob: plainDate.fromISO("2020-01-01"),
    timeout: duration.fromISO("PT1M"),
    timeoutInSeconds: duration.fromISO("PT1M1.5S"),
    timeoutInSecondsFloat: duration.fromISO("PT0.5S"),
  }
)
```

woudl get formatted to 

```

@example(#{
    id: "some",
    bar: "thing",
    date: utcDateTime.fromISO("2020-01-01T00:00:00Z"),
    unixDate: unixTimestamp32.fromISO("2020-01-01T00:00:00Z"),
    encodedAsRfc7231: utcDateTime.fromISO("2020-01-01T00:00:00Z"),
    dob: plainDate.fromISO("2020-01-01"),
    timeout: duration.fromISO("PT1M"),
    timeoutInSeconds: duration.fromISO("PT1M1.5S"),
    timeoutInSecondsFloat: duration.fromISO("PT0.5S"),
})
```
  • Loading branch information
timotheeguerin authored Jun 14, 2024
1 parent 9c701cc commit 6bbc247
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
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

0 comments on commit 6bbc247

Please sign in to comment.