Skip to content

Commit d6a3da9

Browse files
Improve formatting for unions of tables to enforce hanging (#1027)
* Add test case * Ensure unions of (multiline) tables are always formatted multiline * Update snapshot * Update changelog * fix snapshot
1 parent c185fc1 commit d6a3da9

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Added option `block_newline_gaps` to determine whether newline gaps at the start / end of blocks should be preserved. Defaults to `Never`, which is the original behaviour. ([#857](https://github.com/JohnnyMorganz/StyLua/pull/857))
1313

14+
### Changed
15+
16+
- Luau: Improved union of tables formatting to hang the union type rather than attempt to hug all the tables together ([#958](https://github.com/JohnnyMorganz/StyLua/issues/958))
17+
1418
### Fixed
1519

1620
- Fixed formatting of index containing brackets string in parentheses ([#992](https://github.com/JohnnyMorganz/StyLua/pull/992))

src/formatters/luau.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ fn should_hug_type(type_info: &TypeInfo) -> bool {
5151
}
5252
}
5353

54+
fn is_union_of_tables(type_info: &TypeInfo) -> bool {
55+
match type_info {
56+
TypeInfo::Union(union) => union
57+
.types()
58+
.iter()
59+
.all(|ty| matches!(ty, TypeInfo::Table { .. })),
60+
_ => false,
61+
}
62+
}
63+
5464
fn format_hangable_type_info_internal(
5565
ctx: &Context,
5666
type_info: &TypeInfo,
@@ -1140,11 +1150,13 @@ fn attempt_assigned_type_tactics(
11401150
// If we can hang the type definition, and its over width, then lets try doing so
11411151
if can_hang_type(type_info)
11421152
&& (must_hang
1143-
|| (shape.test_over_budget(&strip_trailing_trivia(&singleline_type_definition))))
1153+
|| shape.test_over_budget(&strip_trailing_trivia(&singleline_type_definition))
1154+
|| spans_multiple_lines(&singleline_type_definition))
11441155
{
11451156
// If we should hug the type, then lets check out the proper definition and see if it fits
11461157
if !must_hang
11471158
&& should_hug_type(type_info)
1159+
&& !is_union_of_tables(type_info)
11481160
&& !shape.test_over_budget(&proper_type_definition)
11491161
{
11501162
type_definition = proper_type_definition;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/958
2+
3+
export type AstExprTableItem =
4+
| { kind: "list", value: AstExpr, separator: Token<"," | ";">? }
5+
| { kind: "record", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? }
6+
| { kind: "general", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? }
7+
8+
export type AstExprTableItem = | { kind: "list", value: AstExpr, separator: Token<"," | ";">? } | {
9+
kind: "record",
10+
key: string,
11+
equals: Token<"=">,
12+
value: AstExpr,
13+
separator: Token<"," | ";">?,
14+
} | {
15+
kind: "general",
16+
key: string,
17+
equals: Token<"=">,
18+
value: AstExpr,
19+
separator: Token<"," | ";">?,
20+
}

tests/snapshots/[email protected]

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
source: tests/tests.rs
3+
expression: "format(&contents, LuaVersion::Luau)"
4+
input_file: tests/inputs-luau/type-union-tables-1.lua
5+
---
6+
-- https://github.com/JohnnyMorganz/StyLua/issues/958
7+
8+
export type AstExprTableItem =
9+
| { kind: "list", value: AstExpr, separator: Token<"," | ";">? }
10+
| { kind: "record", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? }
11+
| { kind: "general", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? }
12+
13+
export type AstExprTableItem =
14+
| { kind: "list", value: AstExpr, separator: Token<"," | ";">? }
15+
| {
16+
kind: "record",
17+
key: string,
18+
equals: Token<"=">,
19+
value: AstExpr,
20+
separator: Token<"," | ";">?,
21+
}
22+
| {
23+
kind: "general",
24+
key: string,
25+
equals: Token<"=">,
26+
value: AstExpr,
27+
separator: Token<"," | ";">?,
28+
}

0 commit comments

Comments
 (0)