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

Allow trailing commas for tuple and array literals and update formatter to put them #4373

Closed
Closed
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
8 changes: 8 additions & 0 deletions .chronus/changes/allow-trailing-comma-2024-8-9-17-59-33.md
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: feature
packages:
- "@typespec/compiler"
---

Allow trailing commas for tuple and array literals
8 changes: 8 additions & 0 deletions .chronus/changes/allow-trailing-comma-2024-8-9-17-59-34.md
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: breaking
packages:
- "@typespec/compiler"
---

Formatter will add trailing commas for multi line tuples and array values
2 changes: 1 addition & 1 deletion docs/emitters/openapi3/openapi.md
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is also another issue that just says to do that for all list, so wondering if I should split this into 2 prs.

  1. remove the resitriction (allow parsing trailing separator for all lists)
    2.update formatter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the restrition removal to another PR and we can keep this one for the formatting change #4837

Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,6 @@ model AADToken
authorizationUrl: "https://api.example.com/oauth2/authorize";
tokenUrl: "https://api.example.com/oauth2/token";
scopes: ["https://management.azure.com/read", "https://management.azure.com/write"];
}
},
]>;
```
2 changes: 1 addition & 1 deletion docs/libraries/http/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ alias MyOAuth2<Scopes extends string[]> = OAuth2Auth<
type: OAuth2FlowType.implicit;
authorizationUrl: "https://api.example.com/oauth2/authorize";
refreshUrl: "https://api.example.com/oauth2/refresh";
}
},
],
Scopes
>;
Expand Down
8 changes: 4 additions & 4 deletions docs/release-notes/release-2023-02-07.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ For versioned libraries, `@useDependency` is applied to the version enum members
@versionedDependency(
[
[Microsoft.Observability.Versions.v2021_06_13_preview, Azure.Core.Versions.v1_0_Preview_2],
[Microsoft.Observability.Versions.v2022_04_30_preview, Azure.Core.Versions.v1_0_Preview_2]
[Microsoft.Observability.Versions.v2022_04_30_preview, Azure.Core.Versions.v1_0_Preview_2],
]
)
@versionedDependency(
[
[
Microsoft.Observability.Versions.v2021_06_13_preview,
Azure.ResourceManager.Versions.v1_0_Preview_1
Azure.ResourceManager.Versions.v1_0_Preview_1,
],
[
Microsoft.Observability.Versions.v2022_04_30_preview,
Azure.ResourceManager.Versions.v1_0_Preview_1
]
Azure.ResourceManager.Versions.v1_0_Preview_1,
],
]
)
@versioned(Versions)
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,15 @@ namespace ListKind {
export const Tuple = {
...ExpresionsBase,
allowEmpty: true,
trailingDelimiterIsValid: true,
open: Token.OpenBracket,
close: Token.CloseBracket,
} as const;

export const ArrayLiteral = {
...ExpresionsBase,
allowEmpty: true,
trailingDelimiterIsValid: true,
open: Token.HashBracket,
close: Token.CloseBracket,
} as const;
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/src/formatter/print/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ export function printTuple(
path.map((arg) => [softline, print(arg)], "values")
)
),
ifBreak(",", ""),
softline,
"]",
]);
Expand Down Expand Up @@ -1093,6 +1094,7 @@ export function printArrayLiteral(
path.map((arg) => [softline, print(arg)], "values")
)
),
ifBreak(",", ""),
softline,
"]",
]);
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/formatter/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ const foo = utcDateTime. fromFoo(#[
const foo = utcDateTime.fromFoo(#[
"very very long array",
"very very long array",
"very very long array"
"very very long array",
]);
`,
});
Expand Down Expand Up @@ -2556,7 +2556,7 @@ alias Foo = ["very long text that will overflow 1","very long text that will ove
alias Foo = [
"very long text that will overflow 1",
"very long text that will overflow 2",
"very long text that will overflow 3"
"very long text that will overflow 3",
];
`,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ describe("compiler: parser", () => {
parseEach([
'namespace A { op b(param: [number, string]): [1, "hi"]; }',
"alias EmptyTuple = [];",
"alias TrailingComma = [1, 2,];",
"model Template<T=[]> { }",
]);
});
Expand Down Expand Up @@ -266,6 +267,7 @@ describe("compiler: parser", () => {
`const A = #["abc"];`,
`const A = #["abc", 123];`,
`const A = #["abc", 123, #{nested: true}];`,
`const Trailing = #["abc", 123,];`,
]);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/samples/specs/authentication/operation-auth.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ alias MyAuth<Scopes extends string[]> = OAuth2Auth<
type: OAuth2FlowType.implicit;
authorizationUrl: "https://api.example.com/oauth2/authorize";
refreshUrl: "https://api.example.com/oauth2/refresh";
}
},
],
Scopes = Scopes
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,6 @@ model AADToken
authorizationUrl: "https://api.example.com/oauth2/authorize";
tokenUrl: "https://api.example.com/oauth2/token";
scopes: ["https://management.azure.com/read", "https://management.azure.com/write"];
}
},
]>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ alias MyOAuth2<Scopes extends string[]> = OAuth2Auth<
type: OAuth2FlowType.implicit;
authorizationUrl: "https://api.example.com/oauth2/authorize";
refreshUrl: "https://api.example.com/oauth2/refresh";
}
},
],
Scopes
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ For versioned libraries, `@useDependency` is applied to the version enum members
@versionedDependency(
[
[Microsoft.Observability.Versions.v2021_06_13_preview, Azure.Core.Versions.v1_0_Preview_2],
[Microsoft.Observability.Versions.v2022_04_30_preview, Azure.Core.Versions.v1_0_Preview_2]
[Microsoft.Observability.Versions.v2022_04_30_preview, Azure.Core.Versions.v1_0_Preview_2],
]
)
@versionedDependency(
[
[
Microsoft.Observability.Versions.v2021_06_13_preview,
Azure.ResourceManager.Versions.v1_0_Preview_1
Azure.ResourceManager.Versions.v1_0_Preview_1,
],
[
Microsoft.Observability.Versions.v2022_04_30_preview,
Azure.ResourceManager.Versions.v1_0_Preview_1
]
Azure.ResourceManager.Versions.v1_0_Preview_1,
],
]
)
@versioned(Versions)
Expand Down
Loading