Skip to content

Commit

Permalink
Fix parentheses incorrectly removed around assertion when hanging (#426)
Browse files Browse the repository at this point in the history
* Add test case

* Fix parentheses being removed around type assertion when hanging expression

* Update changelog

* Update snapshot

* Remove debug line
  • Loading branch information
JohnnyMorganz authored Mar 31, 2022
1 parent 80483fe commit cc5689d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Migrate internal dependency for CLI arguments handling, with improved help messages.
- Type declarations consisting of unions/intersections where an inner type has a multiline comment will now force hanging
- Generic fors will no longer expand onto multiple lines if the expression looping over is a function call with a single table argument (e.g., `ipairs({ ... })`) ([#405](https://github.com/JohnnyMorganz/StyLua/issues/405))
- Excess parentheses around a type assertion will now be removed. ([#383](https://github.com/JohnnyMorganz/StyLua/issues/383))
- Excess parentheses around a type assertion will now be removed. ([#383](https://github.com/JohnnyMorganz/StyLua/issues/383), [[#425](https://github.com/JohnnyMorganz/StyLua/issues/425)])

### Fixed
- Fixed issue through static linking where Windows binary would not execute due to missing `VCRUNTIME140.dll`. ([#413](https://github.com/JohnnyMorganz/StyLua/issues/413))
Expand Down
30 changes: 21 additions & 9 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,12 @@ fn format_hanging_expression_(
#[cfg(feature = "luau")]
type_assertion,
} => {
#[cfg(feature = "luau")]
let expression_context = if type_assertion.is_some() {
ExpressionContext::TypeAssertion
} else {
expression_context
};
let value = Box::new(match &**value {
Value::ParenthesesExpression(expression) => {
Value::ParenthesesExpression(format_hanging_expression_(
Expand All @@ -1115,12 +1121,6 @@ fn format_hanging_expression_(
} else {
shape
};
#[cfg(feature = "luau")]
let expression_context = if type_assertion.is_some() {
ExpressionContext::TypeAssertion
} else {
expression_context
};
format_value(ctx, value, shape, expression_context)
}
});
Expand All @@ -1141,13 +1141,20 @@ fn format_hanging_expression_(
} else {
shape
};
#[cfg(feature = "luau")]
let keep_parentheses = matches!(
expression_context,
ExpressionContext::Prefix | ExpressionContext::TypeAssertion
);
#[cfg(not(feature = "luau"))]
let keep_parentheses = matches!(expression_context, ExpressionContext::Prefix);

// Examine whether the internal expression requires parentheses
// If not, just format and return the internal expression. Otherwise, format the parentheses
let use_internal_expression = check_excess_parentheses(expression, expression_context);

// If the context is for a prefix, we should always keep the parentheses, as they are always required
if use_internal_expression && !matches!(expression_context, ExpressionContext::Prefix) {
if use_internal_expression && !keep_parentheses {
format_hanging_expression_(
ctx,
expression,
Expand Down Expand Up @@ -1209,8 +1216,13 @@ fn format_hanging_expression_(
Expression::UnaryOperator { unop, expression } => {
let unop = format_unop(ctx, unop, shape);
let shape = shape + strip_leading_trivia(&unop).to_string().len();
let expression =
format_hanging_expression_(ctx, expression, shape, expression_context, lhs_range);
let expression = format_hanging_expression_(
ctx,
expression,
shape,
ExpressionContext::Unary,
lhs_range,
);

Expression::UnaryOperator {
unop,
Expand Down
9 changes: 9 additions & 0 deletions tests/inputs-luau/excess-parentheses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ local foo = (if true then 0 else 1) + 1
local firstPendingUpdate = ((lastPendingUpdate.next :: any) :: Update<State>)

local x = #(value :: Array<number>)

-- https://github.com/JohnnyMorganz/StyLua/issues/425
self.mutationStore[mutationId] = (
{
lolz = foreva,
variables = variables,
} :: anyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
) :: MutationStoreValue

8 changes: 8 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ local firstPendingUpdate = (lastPendingUpdate.next :: any) :: Update<State>

local x = #(value :: Array<number>)

-- https://github.com/JohnnyMorganz/StyLua/issues/425
self.mutationStore[mutationId] = (
{
lolz = foreva,
variables = variables,
} :: anyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
) :: MutationStoreValue

0 comments on commit cc5689d

Please sign in to comment.