Skip to content

Commit

Permalink
Keep parentheses around compound type in table indexer (JohnnyMorganz…
Browse files Browse the repository at this point in the history
…#837)

* Add test case

* Don't remove parens in table indexer

* Update snapshots

* Update changelog
  • Loading branch information
JohnnyMorganz authored Dec 23, 2023
1 parent a5ca6a4 commit 987c489
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed handling of floor division (`//`) syntax when only Luau FFlag is enabled
- Fixed missing space when table is inside of Luau interpolated string expression (`{{` is invalid syntax)
- The CLI tool will now only write files if the contents differ, and not modify if no change (#827)
- Fixed parentheses around a Luau compound type inside of a type table indexer being removed causing a syntax error (#828)

## [0.19.1] - 2023-11-15

Expand Down
28 changes: 25 additions & 3 deletions src/formatters/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ struct TypeInfoContext {
// Foo<(string), (number)>
// we should NOT remove these parentheses are they may correspond to single-type type packs
within_generic: bool,
/// A TypeInfo in a table indexer
/// we should NOT remove parentheses around a compound type [("foo" | "bar")]: string
within_table_indexer: bool,

/// A TypeInfo part of a union/intersection operation
/// If its a mixed composite type, then we should not remove excess parentheses. e.g.
Expand All @@ -202,6 +205,7 @@ impl TypeInfoContext {
within_optional: false,
within_variadic: false,
within_generic: false,
within_table_indexer: false,
contains_union: false,
contains_intersect: false,
}
Expand All @@ -228,6 +232,13 @@ impl TypeInfoContext {
}
}

fn mark_within_table_indexer(self) -> TypeInfoContext {
Self {
within_table_indexer: true,
..self
}
}

fn mark_contains_union(self) -> TypeInfoContext {
Self {
contains_union: true,
Expand All @@ -254,12 +265,18 @@ fn keep_parentheses(internal_type: &TypeInfo, context: TypeInfoContext) -> bool
true
}
TypeInfo::Union { .. } | TypeInfo::Optional { .. }
if context.within_optional || context.within_variadic || context.contains_intersect =>
if context.within_optional
|| context.within_variadic
|| context.within_table_indexer
|| context.contains_intersect =>
{
true
}
TypeInfo::Intersection { .. }
if context.within_optional || context.within_variadic || context.contains_union =>
if context.within_optional
|| context.within_variadic
|| context.within_table_indexer
|| context.contains_union =>
{
true
}
Expand Down Expand Up @@ -876,7 +893,12 @@ pub fn format_type_field_key(
TypeFieldKey::IndexSignature { brackets, inner } => TypeFieldKey::IndexSignature {
brackets: format_contained_span(ctx, brackets, shape)
.update_leading_trivia(leading_trivia),
inner: format_type_info(ctx, inner, shape + 1), // 1 = "["
inner: format_type_info_internal(
ctx,
inner,
TypeInfoContext::new().mark_within_table_indexer(),
shape + 1,
), // 1 = "["
},
other => panic!("unknown node {:?}", other),
}
Expand Down
4 changes: 4 additions & 0 deletions tests/inputs-luau/types_parentheses_table_indexer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--- https://github.com/JohnnyMorganz/StyLua/issues/828
type foo = {
[("bar" | "baz")]: any,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/tests.rs
expression: format(&contents)
input_file: tests/inputs-luau/types_parentheses_table_indexer.lua
---
--- https://github.com/JohnnyMorganz/StyLua/issues/828
type foo = {
[("bar" | "baz")]: any,
}

0 comments on commit 987c489

Please sign in to comment.