From 368aee473acb0eb2ebabcc29c9c02af7b4e994b7 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Tue, 24 Dec 2024 12:52:42 +0000 Subject: [PATCH 1/4] Add test case --- tests/inputs-luau/excess-parentheses-type-assertion.lua | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/inputs-luau/excess-parentheses-type-assertion.lua diff --git a/tests/inputs-luau/excess-parentheses-type-assertion.lua b/tests/inputs-luau/excess-parentheses-type-assertion.lua new file mode 100644 index 00000000..7d024f5c --- /dev/null +++ b/tests/inputs-luau/excess-parentheses-type-assertion.lua @@ -0,0 +1,3 @@ +local x = if (foo :: number) < bar + then very + very + very + long + line + right + here + hopefully + else lets + ensure + stylua + writes + this + out + using + multiple + lines From 0a07af1378765a09243803193f915a7fd2641944 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Tue, 24 Dec 2024 12:53:05 +0000 Subject: [PATCH 2/4] Don't remove parentheses in type assertion --- src/formatters/expression.rs | 59 ++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/src/formatters/expression.rs b/src/formatters/expression.rs index a1529e0c..2f4bca1b 100644 --- a/src/formatters/expression.rs +++ b/src/formatters/expression.rs @@ -1124,6 +1124,7 @@ fn hang_binop_expression( top_binop: BinOp, shape: Shape, lhs_range: Option, + expression_context: ExpressionContext, ) -> Expression { const SPACE_LEN: usize = " ".len(); @@ -1182,9 +1183,17 @@ fn hang_binop_expression( }, lhs_shape, lhs_range, + expression_context, ), if contains_comments(&*rhs) { - hang_binop_expression(ctx, *rhs, binop, shape, lhs_range) + hang_binop_expression( + ctx, + *rhs, + binop, + shape, + lhs_range, + expression_context, + ) } else { format_expression_internal( ctx, @@ -1196,7 +1205,14 @@ fn hang_binop_expression( ), ExpressionSide::Right => ( if contains_comments(&*lhs) { - hang_binop_expression(ctx, *lhs, binop.clone(), shape, lhs_range) + hang_binop_expression( + ctx, + *lhs, + binop.clone(), + shape, + lhs_range, + expression_context, + ) } else { let context = if let BinOp::Caret(_) = binop { ExpressionContext::BinaryLHSExponent @@ -1211,6 +1227,7 @@ fn hang_binop_expression( if same_op_level { top_binop } else { binop }, rhs_shape, lhs_range, + expression_context, ), ), }; @@ -1223,7 +1240,14 @@ fn hang_binop_expression( // Check if the chain still has comments deeper inside of it. // If it does, we need to hang that part of the chain still, otherwise the comments will mess it up let lhs = if contains_comments(&*lhs) { - hang_binop_expression(ctx, *lhs, binop.to_owned(), shape, lhs_range) + hang_binop_expression( + ctx, + *lhs, + binop.to_owned(), + shape, + lhs_range, + expression_context, + ) } else { let context = if let BinOp::Caret(_) = binop { ExpressionContext::BinaryLHSExponent @@ -1234,7 +1258,14 @@ fn hang_binop_expression( }; let rhs = if contains_comments(&*rhs) { - hang_binop_expression(ctx, *rhs, binop, shape, lhs_range) + hang_binop_expression( + ctx, + *rhs, + binop, + shape, + lhs_range, + expression_context, + ) } else { format_expression_internal( ctx, @@ -1255,13 +1286,7 @@ fn hang_binop_expression( } } // Base case: no more binary operators - just return to normal splitting - _ => format_hanging_expression_( - ctx, - &expression, - shape, - ExpressionContext::Standard, - lhs_range, - ), + _ => format_hanging_expression_(ctx, &expression, shape, expression_context, lhs_range), } } @@ -1402,8 +1427,14 @@ fn format_hanging_expression_( } Expression::BinaryOperator { lhs, binop, rhs } => { // Don't format the lhs and rhs here, because it will be handled later when hang_binop_expression calls back for a Value - let lhs = - hang_binop_expression(ctx, *lhs.to_owned(), binop.to_owned(), shape, lhs_range); + let lhs = hang_binop_expression( + ctx, + *lhs.to_owned(), + binop.to_owned(), + shape, + lhs_range, + ExpressionContext::UnaryOrBinary, + ); let current_shape = shape.take_last_line(&lhs) + 1; // 1 = space before binop let mut new_binop = format_binop(ctx, binop, current_shape); @@ -1416,6 +1447,7 @@ fn format_hanging_expression_( binop.to_owned(), singleline_shape, None, + ExpressionContext::Standard, ); // Examine the last line to see if we need to hang this binop, or if the precedence levels match @@ -1434,6 +1466,7 @@ fn format_hanging_expression_( binop.to_owned(), hanging_shape, None, + ExpressionContext::Standard, ) .update_leading_trivia(FormatTriviaType::Replace(Vec::new())); } From 72d96412b4eea6140a4959e02d451e45e2484760 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Tue, 24 Dec 2024 12:53:09 +0000 Subject: [PATCH 3/4] Update snapshots --- ...ests__luau@excess-parentheses-type-assertion.lua.snap | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/snapshots/tests__luau@excess-parentheses-type-assertion.lua.snap diff --git a/tests/snapshots/tests__luau@excess-parentheses-type-assertion.lua.snap b/tests/snapshots/tests__luau@excess-parentheses-type-assertion.lua.snap new file mode 100644 index 00000000..0417533a --- /dev/null +++ b/tests/snapshots/tests__luau@excess-parentheses-type-assertion.lua.snap @@ -0,0 +1,9 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Luau)" +input_file: tests/inputs-luau/excess-parentheses-type-assertion.lua +snapshot_kind: text +--- +local x = if (foo :: number) < bar + then very + very + very + long + line + right + here + hopefully + else lets + ensure + stylua + writes + this + out + using + multiple + lines From b16bfde540f77a03a8605e2f2e7fd29c5270d700 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Tue, 24 Dec 2024 12:54:01 +0000 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1dc3f5f..41ca57af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Luau: fixed parentheses incorrectly removed in `(expr :: assertion) < foo` when multilining the expression, leading to a syntax error ([#940](https://github.com/JohnnyMorganz/StyLua/issues/940)) + ## [2.0.2] - 2024-12-07 ### Fixed