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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed unstable formatting of tables at column width boundary ([#436](https://github.com/JohnnyMorganz/StyLua/issues/436))
- Fixed assignments no longer hanging at equals token if a comment is present, but the expression is not hangable at a binop. ([#439](https://github.com/JohnnyMorganz/StyLua/issues/439))
- Fixed unstable formatting around comments within type declarations ([#397](https://github.com/JohnnyMorganz/StyLua/issues/397), [#430](https://github.com/JohnnyMorganz/StyLua/issues/430))
- Fixed parentheses around type assertions in a binary expression being removed leading to incorrect semantics. ([#441](https://github.com/JohnnyMorganz/StyLua/issues/441))

## [0.13.0] - 2022-03-31
### Added
Expand Down
53 changes: 41 additions & 12 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ enum ExpressionContext {
TypeAssertion,
/// The internal expression is having a unary operation applied to it: the `expr` part of #expr.
/// If this occurs, and `expr` is a type assertion, then we need to keep the parentheses
Unary,
UnaryOrBinary,
}

pub fn format_binop(ctx: &Context, binop: &BinOp, shape: Shape) -> BinOp {
Expand Down Expand Up @@ -99,11 +99,11 @@ fn check_excess_parentheses(internal_expression: &Expression, context: Expressio
#[cfg(feature = "luau")]
type_assertion,
} => {
// If we have a type assertion, and the context is a unary operation
// If we have a type assertion, and the context is a unary or binary operation
// we should always keep parentheses
// [e.g. #(value :: Array<string>) or -(value :: number)]
#[cfg(feature = "luau")]
if type_assertion.is_some() && matches!(context, ExpressionContext::Unary) {
if type_assertion.is_some() && matches!(context, ExpressionContext::UnaryOrBinary) {
return false;
}

Expand Down Expand Up @@ -208,8 +208,12 @@ fn format_expression_internal(
Expression::UnaryOperator { unop, expression } => {
let unop = format_unop(ctx, unop, shape);
let shape = shape + strip_leading_trivia(&unop).to_string().len();
let mut expression =
format_expression_internal(ctx, expression, ExpressionContext::Unary, shape);
let mut expression = format_expression_internal(
ctx,
expression,
ExpressionContext::UnaryOrBinary,
shape,
);

// Special case: if we have `- -foo`, or `-(-foo)` where we have already removed the parentheses, then
// it will lead to `--foo`, which is invalid syntax. We must explicitly add/keep the parentheses `-(-foo)`.
Expand Down Expand Up @@ -251,13 +255,18 @@ fn format_expression_internal(
}
}
Expression::BinaryOperator { lhs, binop, rhs } => {
let lhs = format_expression(ctx, lhs, shape);
let lhs = format_expression_internal(ctx, lhs, ExpressionContext::UnaryOrBinary, shape);
let binop = format_binop(ctx, binop, shape);
let shape = shape.take_last_line(&lhs) + binop.to_string().len();
Expression::BinaryOperator {
lhs: Box::new(lhs),
binop,
rhs: Box::new(format_expression(ctx, rhs, shape)),
rhs: Box::new(format_expression_internal(
ctx,
rhs,
ExpressionContext::UnaryOrBinary,
shape,
)),
}
}
other => panic!("unknown node {:?}", other),
Expand Down Expand Up @@ -1032,10 +1041,20 @@ fn hang_binop_expression(
lhs_shape,
lhs_range,
),
format_expression(ctx, &*rhs, rhs_shape),
format_expression_internal(
ctx,
&*rhs,
ExpressionContext::UnaryOrBinary,
rhs_shape,
),
),
ExpressionSide::Right => (
format_expression(ctx, &*lhs, lhs_shape),
format_expression_internal(
ctx,
&*lhs,
ExpressionContext::UnaryOrBinary,
lhs_shape,
),
hang_binop_expression(
ctx,
*rhs,
Expand All @@ -1056,13 +1075,23 @@ fn hang_binop_expression(
let lhs = if contains_comments(&*lhs) {
hang_binop_expression(ctx, *lhs, binop.to_owned(), shape, lhs_range)
} else {
format_expression(ctx, &*lhs, shape)
format_expression_internal(
ctx,
&*lhs,
ExpressionContext::UnaryOrBinary,
shape,
)
};

let rhs = if contains_comments(&*rhs) {
hang_binop_expression(ctx, *rhs, binop, shape, lhs_range)
} else {
format_expression(ctx, &*rhs, shape)
format_expression_internal(
ctx,
&*rhs,
ExpressionContext::UnaryOrBinary,
shape,
)
};

(lhs, rhs)
Expand Down Expand Up @@ -1223,7 +1252,7 @@ fn format_hanging_expression_(
ctx,
expression,
shape,
ExpressionContext::Unary,
ExpressionContext::UnaryOrBinary,
lhs_range,
);

Expand Down
11 changes: 11 additions & 0 deletions tests/inputs-luau/excess-parentheses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ self.mutationStore[mutationId] = (
) :: MutationStoreValue

local _name = debug.info(fn :: ((any) -> any), "n")

-- https://github.com/JohnnyMorganz/StyLua/issues/441
if string.len(string_) > (length :: number) then
return string_:sub(1, (length :: number) + 1) .. "…"
else
return string_
end

if fiber.actualStartTime ~= nil and (fiber.actualStartTime :: number) < 0 then
fiber.actualStartTime = now()
end
11 changes: 11 additions & 0 deletions tests/snapshots/tests__luau@excess-parentheses.lua.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ self.mutationStore[mutationId] = (

local _name = debug.info(fn :: ((any) -> any), "n")

-- https://github.com/JohnnyMorganz/StyLua/issues/441
if string.len(string_) > (length :: number) then
return string_:sub(1, (length :: number) + 1) .. "…"
else
return string_
end

if fiber.actualStartTime ~= nil and (fiber.actualStartTime :: number) < 0 then
fiber.actualStartTime = now()
end