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)) 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; 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<"," | ";">?, +} diff --git a/tests/snapshots/tests__luau@type-union-tables-1.lua.snap b/tests/snapshots/tests__luau@type-union-tables-1.lua.snap new file mode 100644 index 00000000..72612d83 --- /dev/null +++ b/tests/snapshots/tests__luau@type-union-tables-1.lua.snap @@ -0,0 +1,28 @@ +--- +source: tests/tests.rs +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<"," | ";">?, + }