Skip to content

Commit e628cc9

Browse files
Do not remove parentheses around single-value generic declaration default (#1040)
* Add test case * Pass through variadic context when formatting default for generic declaration * Update snapshot * Update changelog
1 parent e396103 commit e628cc9

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Fixed `document_range_formatting_provider` capability missing from `ServerCapabilities` in language server mode
2424
- Fixed current working directory incorrectly used as config search root in language server mode -- now, the root of the opened workspace is used instead ([#1032](https://github.com/JohnnyMorganz/StyLua/issues/1032))
2525
- Language server mode now correctly respects `.styluaignore` files ([#1035](https://github.com/JohnnyMorganz/StyLua/issues/1035))
26+
- Luau: Fixed parentheses incorrectly removed on a single type that is the default for a variadic generic parameter ([#1038](https://github.com/JohnnyMorganz/StyLua/issues/1038))
2627

2728
## [2.2.0] - 2025-09-14
2829

src/formatters/luau.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ fn attempt_assigned_type_tactics(
10971097
ctx: &Context,
10981098
equal_token: TokenReference,
10991099
type_info: &TypeInfo,
1100+
context: TypeInfoContext,
11001101
shape: Shape,
11011102
) -> (TokenReference, TypeInfo) {
11021103
const EQUAL_TOKEN_LENGTH: usize = " = ".len();
@@ -1109,11 +1110,11 @@ fn attempt_assigned_type_tactics(
11091110

11101111
// Format declaration, hanging if it contains comments (ignoring leading and trailing comments, as they won't affect anything)
11111112
let declaration = if contains_comments(strip_trivia(type_info)) {
1112-
hang_type_info(ctx, type_info, TypeInfoContext::new(), shape, 0)
1113+
hang_type_info(ctx, type_info, context, shape, 0)
11131114
} else {
1114-
let proper_declaration = format_type_info(ctx, type_info, shape);
1115+
let proper_declaration = format_type_info_internal(ctx, type_info, context, shape);
11151116
if shape.test_over_budget(&proper_declaration) {
1116-
hang_type_info(ctx, type_info, TypeInfoContext::new(), shape, 0)
1117+
hang_type_info(ctx, type_info, context, shape, 0)
11171118
} else {
11181119
proper_declaration
11191120
}
@@ -1141,8 +1142,9 @@ fn attempt_assigned_type_tactics(
11411142
let mut equal_token = equal_token;
11421143
let type_definition;
11431144
let singleline_type_definition =
1144-
format_type_info(ctx, type_info, shape.with_infinite_width());
1145-
let proper_type_definition = format_type_info(ctx, type_info, shape + EQUAL_TOKEN_LENGTH);
1145+
format_type_info_internal(ctx, type_info, context, shape.with_infinite_width());
1146+
let proper_type_definition =
1147+
format_type_info_internal(ctx, type_info, context, shape + EQUAL_TOKEN_LENGTH);
11461148

11471149
// Test to see whether the type definition must be hung due to comments
11481150
let must_hang = should_hang_type(type_info, CommentSearch::All);
@@ -1165,8 +1167,7 @@ fn attempt_assigned_type_tactics(
11651167
equal_token = hang_equal_token(ctx, &equal_token, shape, true);
11661168

11671169
let shape = shape.reset().increment_additional_indent();
1168-
let hanging_type_definition =
1169-
hang_type_info(ctx, type_info, TypeInfoContext::new(), shape, 0);
1170+
let hanging_type_definition = hang_type_info(ctx, type_info, context, shape, 0);
11701171
type_definition = hanging_type_definition;
11711172
}
11721173
} else {
@@ -1178,7 +1179,7 @@ fn attempt_assigned_type_tactics(
11781179

11791180
// Add the expression list into the indent range, as it will be indented by one
11801181
let shape = shape.reset().increment_additional_indent();
1181-
type_definition = format_type_info(ctx, type_info, shape);
1182+
type_definition = format_type_info_internal(ctx, type_info, context, shape);
11821183
} else {
11831184
// Use the proper formatting
11841185
type_definition = proper_type_definition;
@@ -1232,8 +1233,13 @@ fn format_type_declaration(
12321233
};
12331234

12341235
let equal_token = fmt_symbol!(ctx, type_declaration.equal_token(), " = ", shape);
1235-
let (equal_token, type_definition) =
1236-
attempt_assigned_type_tactics(ctx, equal_token, type_declaration.type_definition(), shape);
1236+
let (equal_token, type_definition) = attempt_assigned_type_tactics(
1237+
ctx,
1238+
equal_token,
1239+
type_declaration.type_definition(),
1240+
TypeInfoContext::new(),
1241+
shape,
1242+
);
12371243

12381244
// Handle comments in between the type name and generics + generics and equal token
12391245
// (or just type name and equal token if generics not present)
@@ -1413,11 +1419,16 @@ fn format_generic_parameter(
14131419
other => panic!("unknown node {:?}", other),
14141420
};
14151421

1422+
let context = match generic_parameter.parameter() {
1423+
GenericParameterInfo::Variadic { .. } => TypeInfoContext::new().mark_within_generic(),
1424+
_ => TypeInfoContext::new(),
1425+
};
1426+
14161427
let default_type = match (generic_parameter.equals(), generic_parameter.default_type()) {
14171428
(Some(equals), Some(default_type)) => {
14181429
let equals = fmt_symbol!(ctx, equals, " = ", shape);
14191430
let (equals, default_type) =
1420-
attempt_assigned_type_tactics(ctx, equals, default_type, shape);
1431+
attempt_assigned_type_tactics(ctx, equals, default_type, context, shape);
14211432
Some((equals, default_type))
14221433
}
14231434
(None, None) => None,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/1038
2+
3+
type Object1<T = (nil)> = {
4+
method: (T) -> (),
5+
}
6+
7+
type Object2<T... = (nil)> = {
8+
method: (T...) -> (),
9+
}
10+

tests/snapshots/[email protected]

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: tests/tests.rs
3+
expression: "format(&contents, LuaVersion::Luau)"
4+
input_file: tests/inputs-luau/excess-parentheses-type-pack-default.lua
5+
---
6+
-- https://github.com/JohnnyMorganz/StyLua/issues/1038
7+
8+
type Object1<T = nil> = {
9+
method: (T) -> (),
10+
}
11+
12+
type Object2<T... = (nil)> = {
13+
method: (T...) -> (),
14+
}

0 commit comments

Comments
 (0)