Skip to content

Commit

Permalink
Don't put function definition parentheses on multiple lines when no p…
Browse files Browse the repository at this point in the history
…arens (#838)

* Add test case

* Don't put on multiple lines

* Update snapshots and changelog
  • Loading branch information
JohnnyMorganz authored Dec 23, 2023
1 parent 987c489 commit ee290b0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed missing space when table is inside of Luau interpolated string expression (`{{` is invalid syntax)
- The CLI tool will now only write files if the contents differ, and not modify if no change (#827)
- Fixed parentheses around a Luau compound type inside of a type table indexer being removed causing a syntax error (#828)
- Fixed function body parentheses being placed on multiple lines unnecessarily when there are no parameters (#830)

## [0.19.1] - 2023-11-15

Expand Down
4 changes: 4 additions & 0 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ fn should_parameters_format_multiline(
const PARENS_LEN: usize = "()".len();
const SINGLELINE_END_LEN: usize = " end".len();

if function_body.parameters().is_empty() {
return false;
}

// Check the length of the parameters. We need to format them first onto a single line to check if required
let mut line_length = format_singleline_parameters(ctx, function_body, shape)
.to_string()
Expand Down
4 changes: 4 additions & 0 deletions tests/inputs/function-definition-multiline-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/830
local a_very_long_variable_name_given_that_is_bigger_than_width_upper_limit_but_unfortunately_can_not_be_made_shorter = function()
print("Hello")
end
10 changes: 10 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/tests.rs
expression: format(&contents)
input_file: tests/inputs/function-definition-multiline-2.lua
---
-- https://github.com/JohnnyMorganz/StyLua/issues/830
local a_very_long_variable_name_given_that_is_bigger_than_width_upper_limit_but_unfortunately_can_not_be_made_shorter = function()
print("Hello")
end

0 comments on commit ee290b0

Please sign in to comment.