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 @@ -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
Expand Down
59 changes: 46 additions & 13 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ fn hang_binop_expression(
top_binop: BinOp,
shape: Shape,
lhs_range: Option<LeftmostRangeHang>,
expression_context: ExpressionContext,
) -> Expression {
const SPACE_LEN: usize = " ".len();

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -1211,6 +1227,7 @@ fn hang_binop_expression(
if same_op_level { top_binop } else { binop },
rhs_shape,
lhs_range,
expression_context,
),
),
};
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -1434,6 +1466,7 @@ fn format_hanging_expression_(
binop.to_owned(),
hanging_shape,
None,
ExpressionContext::Standard,
)
.update_leading_trivia(FormatTriviaType::Replace(Vec::new()));
}
Expand Down
3 changes: 3 additions & 0 deletions tests/inputs-luau/excess-parentheses-type-assertion.lua
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -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
Loading