Skip to content

Commit b59234e

Browse files
Keep parens around type assertion if in binexp (#443)
* Add test case * Keep parens around type assertion if in binexp * Update test * Update changelog * Rustfmt
1 parent 602d1a2 commit b59234e

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Fixed unstable formatting of tables at column width boundary ([#436](https://github.com/JohnnyMorganz/StyLua/issues/436))
1212
- 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))
1313
- Fixed unstable formatting around comments within type declarations ([#397](https://github.com/JohnnyMorganz/StyLua/issues/397), [#430](https://github.com/JohnnyMorganz/StyLua/issues/430))
14+
- Fixed parentheses around type assertions in a binary expression being removed leading to incorrect semantics. ([#441](https://github.com/JohnnyMorganz/StyLua/issues/441))
1415

1516
## [0.13.0] - 2022-03-31
1617
### Added

src/formatters/expression.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ enum ExpressionContext {
5959
TypeAssertion,
6060
/// The internal expression is having a unary operation applied to it: the `expr` part of #expr.
6161
/// If this occurs, and `expr` is a type assertion, then we need to keep the parentheses
62-
Unary,
62+
UnaryOrBinary,
6363
}
6464

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

@@ -208,8 +208,12 @@ fn format_expression_internal(
208208
Expression::UnaryOperator { unop, expression } => {
209209
let unop = format_unop(ctx, unop, shape);
210210
let shape = shape + strip_leading_trivia(&unop).to_string().len();
211-
let mut expression =
212-
format_expression_internal(ctx, expression, ExpressionContext::Unary, shape);
211+
let mut expression = format_expression_internal(
212+
ctx,
213+
expression,
214+
ExpressionContext::UnaryOrBinary,
215+
shape,
216+
);
213217

214218
// Special case: if we have `- -foo`, or `-(-foo)` where we have already removed the parentheses, then
215219
// it will lead to `--foo`, which is invalid syntax. We must explicitly add/keep the parentheses `-(-foo)`.
@@ -251,13 +255,18 @@ fn format_expression_internal(
251255
}
252256
}
253257
Expression::BinaryOperator { lhs, binop, rhs } => {
254-
let lhs = format_expression(ctx, lhs, shape);
258+
let lhs = format_expression_internal(ctx, lhs, ExpressionContext::UnaryOrBinary, shape);
255259
let binop = format_binop(ctx, binop, shape);
256260
let shape = shape.take_last_line(&lhs) + binop.to_string().len();
257261
Expression::BinaryOperator {
258262
lhs: Box::new(lhs),
259263
binop,
260-
rhs: Box::new(format_expression(ctx, rhs, shape)),
264+
rhs: Box::new(format_expression_internal(
265+
ctx,
266+
rhs,
267+
ExpressionContext::UnaryOrBinary,
268+
shape,
269+
)),
261270
}
262271
}
263272
other => panic!("unknown node {:?}", other),
@@ -1032,10 +1041,20 @@ fn hang_binop_expression(
10321041
lhs_shape,
10331042
lhs_range,
10341043
),
1035-
format_expression(ctx, &*rhs, rhs_shape),
1044+
format_expression_internal(
1045+
ctx,
1046+
&*rhs,
1047+
ExpressionContext::UnaryOrBinary,
1048+
rhs_shape,
1049+
),
10361050
),
10371051
ExpressionSide::Right => (
1038-
format_expression(ctx, &*lhs, lhs_shape),
1052+
format_expression_internal(
1053+
ctx,
1054+
&*lhs,
1055+
ExpressionContext::UnaryOrBinary,
1056+
lhs_shape,
1057+
),
10391058
hang_binop_expression(
10401059
ctx,
10411060
*rhs,
@@ -1056,13 +1075,23 @@ fn hang_binop_expression(
10561075
let lhs = if contains_comments(&*lhs) {
10571076
hang_binop_expression(ctx, *lhs, binop.to_owned(), shape, lhs_range)
10581077
} else {
1059-
format_expression(ctx, &*lhs, shape)
1078+
format_expression_internal(
1079+
ctx,
1080+
&*lhs,
1081+
ExpressionContext::UnaryOrBinary,
1082+
shape,
1083+
)
10601084
};
10611085

10621086
let rhs = if contains_comments(&*rhs) {
10631087
hang_binop_expression(ctx, *rhs, binop, shape, lhs_range)
10641088
} else {
1065-
format_expression(ctx, &*rhs, shape)
1089+
format_expression_internal(
1090+
ctx,
1091+
&*rhs,
1092+
ExpressionContext::UnaryOrBinary,
1093+
shape,
1094+
)
10661095
};
10671096

10681097
(lhs, rhs)
@@ -1223,7 +1252,7 @@ fn format_hanging_expression_(
12231252
ctx,
12241253
expression,
12251254
shape,
1226-
ExpressionContext::Unary,
1255+
ExpressionContext::UnaryOrBinary,
12271256
lhs_range,
12281257
);
12291258

tests/inputs-luau/excess-parentheses.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ self.mutationStore[mutationId] = (
1717
) :: MutationStoreValue
1818

1919
local _name = debug.info(fn :: ((any) -> any), "n")
20+
21+
-- https://github.com/JohnnyMorganz/StyLua/issues/441
22+
if string.len(string_) > (length :: number) then
23+
return string_:sub(1, (length :: number) + 1) .. ""
24+
else
25+
return string_
26+
end
27+
28+
if fiber.actualStartTime ~= nil and (fiber.actualStartTime :: number) < 0 then
29+
fiber.actualStartTime = now()
30+
end

tests/snapshots/tests__luau@excess-parentheses.lua.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@ self.mutationStore[mutationId] = (
2424

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

27+
-- https://github.com/JohnnyMorganz/StyLua/issues/441
28+
if string.len(string_) > (length :: number) then
29+
return string_:sub(1, (length :: number) + 1) .. "…"
30+
else
31+
return string_
32+
end
33+
34+
if fiber.actualStartTime ~= nil and (fiber.actualStartTime :: number) < 0 then
35+
fiber.actualStartTime = now()
36+
end
37+

0 commit comments

Comments
 (0)