From 652d8982d529a5d55877b62e8850c42fb47b4467 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 15:23:09 +0200 Subject: [PATCH 1/5] Add test case --- tests/inputs-luau/type-union-tables-1.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/inputs-luau/type-union-tables-1.lua diff --git a/tests/inputs-luau/type-union-tables-1.lua b/tests/inputs-luau/type-union-tables-1.lua new file mode 100644 index 00000000..3964443f --- /dev/null +++ b/tests/inputs-luau/type-union-tables-1.lua @@ -0,0 +1,20 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/958 + +export type AstExprTableItem = + | { kind: "list", value: AstExpr, separator: Token<"," | ";">? } + | { kind: "record", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? } + | { kind: "general", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? } + +export type AstExprTableItem = | { kind: "list", value: AstExpr, separator: Token<"," | ";">? } | { + kind: "record", + key: string, + equals: Token<"=">, + value: AstExpr, + separator: Token<"," | ";">?, +} | { + kind: "general", + key: string, + equals: Token<"=">, + value: AstExpr, + separator: Token<"," | ";">?, +} From 89793d71cc91f6f6cbfb33a2a0d317b5eb39852c Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 15:23:38 +0200 Subject: [PATCH 2/5] Ensure unions of (multiline) tables are always formatted multiline --- src/formatters/luau.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/formatters/luau.rs b/src/formatters/luau.rs index 7a6c560f..408d8623 100644 --- a/src/formatters/luau.rs +++ b/src/formatters/luau.rs @@ -51,6 +51,16 @@ fn should_hug_type(type_info: &TypeInfo) -> bool { } } +fn is_union_of_tables(type_info: &TypeInfo) -> bool { + match type_info { + TypeInfo::Union(union) => union + .types() + .iter() + .all(|ty| matches!(ty, TypeInfo::Table { .. })), + _ => false, + } +} + fn format_hangable_type_info_internal( ctx: &Context, type_info: &TypeInfo, @@ -1140,11 +1150,13 @@ fn attempt_assigned_type_tactics( // If we can hang the type definition, and its over width, then lets try doing so if can_hang_type(type_info) && (must_hang - || (shape.test_over_budget(&strip_trailing_trivia(&singleline_type_definition)))) + || shape.test_over_budget(&strip_trailing_trivia(&singleline_type_definition)) + || spans_multiple_lines(&singleline_type_definition)) { // If we should hug the type, then lets check out the proper definition and see if it fits if !must_hang && should_hug_type(type_info) + && !is_union_of_tables(type_info) && !shape.test_over_budget(&proper_type_definition) { type_definition = proper_type_definition; From 0dbc20fb5490888b5725de8fc3c6afee4bf3211e Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 15:24:44 +0200 Subject: [PATCH 3/5] Update snapshot --- ...sts__luau@type-union-tables-1.lua.snap.new | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/snapshots/tests__luau@type-union-tables-1.lua.snap.new diff --git a/tests/snapshots/tests__luau@type-union-tables-1.lua.snap.new b/tests/snapshots/tests__luau@type-union-tables-1.lua.snap.new new file mode 100644 index 00000000..ffc5be93 --- /dev/null +++ b/tests/snapshots/tests__luau@type-union-tables-1.lua.snap.new @@ -0,0 +1,30 @@ +--- +source: tests/tests.rs +assertion_line: 36 +expression: "format(&contents, LuaVersion::Luau)" +input_file: tests/inputs-luau/type-union-tables-1.lua +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/958 + +export type AstExprTableItem = + | { kind: "list", value: AstExpr, separator: Token<"," | ";">? } + | { kind: "record", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? } + | { kind: "general", key: string, equals: Token<"=">, value: AstExpr, separator: Token<"," | ";">? } + +export type AstExprTableItem = + | { kind: "list", value: AstExpr, separator: Token<"," | ";">? } + | { + kind: "record", + key: string, + equals: Token<"=">, + value: AstExpr, + separator: Token<"," | ";">?, + } + | { + kind: "general", + key: string, + equals: Token<"=">, + value: AstExpr, + separator: Token<"," | ";">?, + } + From 78df7de1c5f30368505d19fed5b88521df719f4c Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 15:25:01 +0200 Subject: [PATCH 4/5] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d4c1341..8eb840f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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)) +### Changed + +- 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)) + ### Fixed - Fixed formatting of index containing brackets string in parentheses ([#992](https://github.com/JohnnyMorganz/StyLua/pull/992)) From d0f63cd4eca29d3e89fe1b9221d309762873fe19 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 15:25:30 +0200 Subject: [PATCH 5/5] fix snapshot --- ...-1.lua.snap.new => tests__luau@type-union-tables-1.lua.snap} | 2 -- 1 file changed, 2 deletions(-) rename tests/snapshots/{tests__luau@type-union-tables-1.lua.snap.new => tests__luau@type-union-tables-1.lua.snap} (97%) diff --git a/tests/snapshots/tests__luau@type-union-tables-1.lua.snap.new b/tests/snapshots/tests__luau@type-union-tables-1.lua.snap similarity index 97% rename from tests/snapshots/tests__luau@type-union-tables-1.lua.snap.new rename to tests/snapshots/tests__luau@type-union-tables-1.lua.snap index ffc5be93..72612d83 100644 --- a/tests/snapshots/tests__luau@type-union-tables-1.lua.snap.new +++ b/tests/snapshots/tests__luau@type-union-tables-1.lua.snap @@ -1,6 +1,5 @@ --- source: tests/tests.rs -assertion_line: 36 expression: "format(&contents, LuaVersion::Luau)" input_file: tests/inputs-luau/type-union-tables-1.lua --- @@ -27,4 +26,3 @@ export type AstExprTableItem = value: AstExpr, separator: Token<"," | ";">?, } -