Skip to content

Commit 482db06

Browse files
committed
Fix hanging expression formatting bug leading to syntax error
Fixes #135
1 parent ca427d3 commit 482db06

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- 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
810

911
## [0.8.0] - 2021-04-30
1012
### Added

src/formatters/expression.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,17 @@ fn format_hanging_expression_<'ast>(
439439
format_hanging_expression_(ctx, expression, shape, indent_level)
440440
} else {
441441
let contained = format_contained_span(ctx, &contained);
442-
let expression = format_expression(ctx, expression, shape + 1); // 1 = opening parentheses
443442

443+
// Provide a sample formatting to see how large it is
444444
// Examine the expression itself to see if needs to be split onto multiple lines
445-
let expression_str = expression.to_string();
445+
let formatted_expression = format_expression(ctx, expression, shape + 1); // 1 = opening parentheses
446+
447+
let expression_str = formatted_expression.to_string();
446448
if !shape.add_width(2 + expression_str.len()).over_budget() {
447449
// The expression inside the parentheses is small, we do not need to break it down further
448450
return Expression::Parentheses {
449451
contained,
450-
expression: Box::new(expression),
452+
expression: Box::new(formatted_expression),
451453
};
452454
}
453455

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

486487
Expression::UnaryOperator {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function SetCallsign(Player, Callsign)
2+
if Settings.PolicingSetup.Radio or (table.find(Settings.PolicingSetup.CallsignPrefix, string.sub(Callsign, 1, 2)) and tonumber(string.sub(Callsign, 3, 4))) then
3+
Player:SetAttribute("Callsign", Callsign)
4+
end
5+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: tests/tests.rs
3+
expression: format(&contents)
4+
5+
---
6+
function SetCallsign(Player, Callsign)
7+
if
8+
Settings.PolicingSetup.Radio
9+
or (
10+
table.find(Settings.PolicingSetup.CallsignPrefix, string.sub(Callsign, 1, 2))
11+
and tonumber(string.sub(Callsign, 3, 4))
12+
)
13+
then
14+
Player:SetAttribute("Callsign", Callsign)
15+
end
16+
end
17+

0 commit comments

Comments
 (0)