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 scalar constructor with no args #3823

Merged
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 scalar constructor called with no args
18 changes: 14 additions & 4 deletions packages/compiler/src/formatter/print/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export function printCallExpression(
options: TypeSpecPrettierOptions,
print: PrettierChildPrint
) {
const args = printCallOrDecoratorArgs(path, options, print);
const args = printCallLikeArgs(path, options, print);
return [path.call(print, "target"), args];
}

Expand Down Expand Up @@ -622,7 +622,7 @@ export function printDecorator(
options: TypeSpecPrettierOptions,
print: PrettierChildPrint
) {
const args = printCallOrDecoratorArgs(path, options, print);
const args = printDecoratorArgs(path, options, print);
return ["@", path.call(print, "target"), args];
}

Expand Down Expand Up @@ -685,8 +685,8 @@ export function printDirective(
return ["#", path.call(print, "target"), " ", args];
}

function printCallOrDecoratorArgs(
path: AstPath<DecoratorExpressionNode | CallExpressionNode>,
function printDecoratorArgs(
path: AstPath<DecoratorExpressionNode>,
options: TypeSpecPrettierOptions,
print: PrettierChildPrint
) {
Expand All @@ -695,6 +695,16 @@ function printCallOrDecoratorArgs(
return "";
}

return printCallLikeArgs(path, options, print);
}

function printCallLikeArgs(
path: AstPath<DecoratorExpressionNode | CallExpressionNode>,
options: TypeSpecPrettierOptions,
print: PrettierChildPrint
) {
const node = path.node;

// So that decorator with single object arguments have ( and { hugging.
// @deco(#{
// value: "foo"
Expand Down
14 changes: 13 additions & 1 deletion packages/compiler/test/formatter/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,19 @@ scalar Foo {
});

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

it("call with arguments", async () => {
await assertFormat({
code: `
const foo = utcDateTime. fromISO(
Expand Down
Loading