Skip to content

Commit d1a6a51

Browse files
Check for comments before removing parentheses when call_parentheses is configured for removal (#981)
* Add test cases * Check comments before removing parentheses * Update changelog * Add more complex snapshot example
1 parent b799f2a commit d1a6a51

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- Luau: fixed parentheses incorrectly removed in `(expr :: assertion) < foo` when multilining the expression, leading to a syntax error ([#940](https://github.com/JohnnyMorganz/StyLua/issues/940))
2525
- Fixed panic when attempting to format a file outside of the current working directory when `--respect-ignores` is enabled ([#969](https://github.com/JohnnyMorganz/StyLua/pull/969))
2626
- Fixed unnecessary semicolons being introduced at the end of statements when incorrectly determined as ambiguous ([#963](https://github.com/JohnnyMorganz/StyLua/issues/963))
27+
- Fixed malformed formatting of function calls where parentheses are removed but there are comments in between the parentheses and the expression. Now, we will keep the parentheses in these cases, except for trailing comments ([#964](https://github.com/JohnnyMorganz/StyLua/issues/964))
2728

2829
## [2.0.2] - 2024-12-07
2930

src/formatters/functions.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,20 @@ pub fn format_function_args(
397397
parentheses,
398398
arguments,
399399
} => {
400+
let (start_parens, end_parens) = parentheses.tokens();
401+
400402
// Handle config where parentheses are omitted, and there is only one argument
401403
if ctx.config().call_parentheses != CallParenType::Input
402404
&& (ctx.should_omit_string_parens() || ctx.should_omit_table_parens())
403405
&& arguments.len() == 1
404406
&& !matches!(call_next_node, FunctionCallNextNode::ObscureWithoutParens)
407+
&& !start_parens.has_trailing_comments(CommentSearch::Single)
408+
&& !end_parens.has_leading_comments(CommentSearch::Single)
409+
&& !arguments
410+
.first()
411+
.unwrap()
412+
.value()
413+
.has_leading_comments(CommentSearch::Single)
405414
{
406415
let argument = arguments.iter().next().unwrap();
407416

@@ -473,7 +482,6 @@ pub fn format_function_args(
473482
// parentheses as well. Otherwise, we just use 1 = opening parentheses.
474483
let shape_increment = if hug_table_constructor { 2 } else { 1 };
475484

476-
let (start_parens, end_parens) = parentheses.tokens();
477485
let start_parens = format_token_reference(ctx, start_parens, shape);
478486
let start_parens = if start_parens.has_trailing_comments(CommentSearch::All)
479487
&& !arguments.is_empty()

tests/test_no_call_parens.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,113 @@ local still_got_em = my_function({
184184
"###
185185
);
186186
}
187+
188+
#[test]
189+
fn test_keep_parens_for_leading_comment() {
190+
insta::assert_snapshot!(
191+
format(
192+
r#"
193+
foo( -- test
194+
"string")
195+
196+
foo(
197+
-- test
198+
"string")
199+
200+
foo( -- test
201+
{})
202+
203+
foo(
204+
-- test
205+
{})
206+
"#
207+
),
208+
@r#"
209+
foo( -- test
210+
"string"
211+
)
212+
213+
foo(
214+
-- test
215+
"string"
216+
)
217+
218+
foo( -- test
219+
{}
220+
)
221+
222+
foo(
223+
-- test
224+
{}
225+
)
226+
"#
227+
);
228+
}
229+
230+
#[test]
231+
fn test_keep_parens_for_trailing_comment() {
232+
insta::assert_snapshot!(
233+
format(
234+
r#"
235+
foo("string" -- test
236+
)
237+
238+
foo("string"
239+
-- test
240+
)
241+
242+
foo({} -- test
243+
)
244+
245+
foo({}
246+
-- test
247+
)
248+
"#
249+
),
250+
@r#"
251+
foo "string" -- test
252+
253+
foo(
254+
"string"
255+
-- test
256+
)
257+
258+
foo {} -- test
259+
260+
foo(
261+
{}
262+
-- test
263+
)
264+
"#
265+
);
266+
}
267+
268+
#[test]
269+
fn test_keep_parentheses_large_example() {
270+
insta::assert_snapshot!(
271+
format(
272+
r#"
273+
wk.add(
274+
{ { key, "zv" .. key, "Same, but open folds" } }
275+
-- { noremap = true, mode = { 'n', key:find '^<' and 'v' or 'x' } }
276+
)
277+
278+
wk.add(
279+
-- { noremap = true, mode = { 'n', key:find '^<' and 'v' or 'x' } }
280+
{ { key, "zv" .. key, "Same, but open folds" } }
281+
)
282+
"#
283+
),
284+
@r#"
285+
wk.add(
286+
{ { key, "zv" .. key, "Same, but open folds" } }
287+
-- { noremap = true, mode = { 'n', key:find '^<' and 'v' or 'x' } }
288+
)
289+
290+
wk.add(
291+
-- { noremap = true, mode = { 'n', key:find '^<' and 'v' or 'x' } }
292+
{ { key, "zv" .. key, "Same, but open folds" } }
293+
)
294+
"#
295+
);
296+
}

0 commit comments

Comments
 (0)