Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 13 additions & 1 deletion src/formatters/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions tests/inputs-luau/type-union-tables-1.lua
Original file line number Diff line number Diff line change
@@ -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<"," | ";">?,
}
28 changes: 28 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -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<"," | ";">?,
}
Loading