Skip to content

Commit

Permalink
Fix hanging expression formatting bug leading to syntax error
Browse files Browse the repository at this point in the history
Fixes #135
  • Loading branch information
JohnnyMorganz committed Apr 30, 2021
1 parent ca427d3 commit 482db06
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fixed bug where a hanging expression inside of parentheses would lead to function arguments being incorrectly formatted with a trailing comma - leading to a syntax error

## [0.8.0] - 2021-04-30
### Added
Expand Down
9 changes: 5 additions & 4 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,17 @@ fn format_hanging_expression_<'ast>(
format_hanging_expression_(ctx, expression, shape, indent_level)
} else {
let contained = format_contained_span(ctx, &contained);
let expression = format_expression(ctx, expression, shape + 1); // 1 = opening parentheses

// Provide a sample formatting to see how large it is
// Examine the expression itself to see if needs to be split onto multiple lines
let expression_str = expression.to_string();
let formatted_expression = format_expression(ctx, expression, shape + 1); // 1 = opening parentheses

let expression_str = formatted_expression.to_string();
if !shape.add_width(2 + expression_str.len()).over_budget() {
// The expression inside the parentheses is small, we do not need to break it down further
return Expression::Parentheses {
contained,
expression: Box::new(expression),
expression: Box::new(formatted_expression),
};
}

Expand Down Expand Up @@ -480,7 +482,6 @@ fn format_hanging_expression_<'ast>(
Expression::UnaryOperator { unop, expression } => {
let unop = format_unop(ctx, unop);
let shape = shape + strip_leading_trivia(&unop).to_string().len();
let expression = format_expression(ctx, expression, shape);
let expression = format_hanging_expression_(ctx, &expression, shape, indent_level);

Expression::UnaryOperator {
Expand Down
5 changes: 5 additions & 0 deletions tests/inputs/multiline-expressions-4.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function SetCallsign(Player, Callsign)
if Settings.PolicingSetup.Radio or (table.find(Settings.PolicingSetup.CallsignPrefix, string.sub(Callsign, 1, 2)) and tonumber(string.sub(Callsign, 3, 4))) then
Player:SetAttribute("Callsign", Callsign)
end
end
17 changes: 17 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: tests/tests.rs
expression: format(&contents)

---
function SetCallsign(Player, Callsign)
if
Settings.PolicingSetup.Radio
or (
table.find(Settings.PolicingSetup.CallsignPrefix, string.sub(Callsign, 1, 2))
and tonumber(string.sub(Callsign, 3, 4))
)
then
Player:SetAttribute("Callsign", Callsign)
end
end

0 comments on commit 482db06

Please sign in to comment.