Skip to content

Commit 2d4bd31

Browse files
Fix mistransformation of generic for comments (#580)
* Fix incorrect token used in multiline generic for * Add test case * Fix check for comments in generic_for expr * Fix trivia location for multiline generic for * Add snapshot * Update changelog
1 parent 5840654 commit 2d4bd31

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Fixed
2020

2121
- Precommit hook now supports downloading aarch64 binary for M1 macs ([#558](https://github.com/JohnnyMorganz/StyLua/issues/558))
22+
- Fixed mistransformations of generic for loop with comments in the expression list ([#579](https://github.com/JohnnyMorganz/StyLua/issues/579))
2223

2324
## [0.14.3] - 2022-08-27
2425

src/formatters/stmt.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,12 @@ pub fn format_generic_for(ctx: &Context, generic_for: &GenericFor, shape: Shape)
221221
let singleline_expr =
222222
format_punctuated(ctx, generic_for.expressions(), shape, format_expression);
223223
let singleline_expr_shape = shape.take_first_line(&singleline_expr);
224-
let requires_expr_multiline = (trivia_util::contains_comments(generic_for.expressions())
225-
|| trivia_util::spans_multiple_lines(&singleline_expr)
226-
|| singleline_expr_shape.over_budget())
227-
&& !hug_generic_for(generic_for.expressions());
224+
let requires_expr_multiline =
225+
(trivia_util::token_contains_trailing_comments(generic_for.in_token())
226+
|| trivia_util::contains_comments(generic_for.expressions())
227+
|| trivia_util::spans_multiple_lines(&singleline_expr)
228+
|| singleline_expr_shape.over_budget())
229+
&& !hug_generic_for(generic_for.expressions());
228230

229231
let in_token = match (require_names_multiline, requires_expr_multiline) {
230232
(true, true) => fmt_symbol!(ctx, generic_for.in_token(), "in", shape)
@@ -245,23 +247,25 @@ pub fn format_generic_for(ctx: &Context, generic_for: &GenericFor, shape: Shape)
245247
let expr_list = match requires_expr_multiline {
246248
true => {
247249
let shape = shape.reset().increment_additional_indent();
248-
format_punctuated_multiline(
250+
let expr_list = format_punctuated_multiline(
249251
ctx,
250252
generic_for.expressions(),
251253
shape,
252254
format_expression,
253255
None,
256+
);
257+
trivia_util::prepend_newline_indent(
258+
ctx,
259+
&expr_list,
260+
trivia_util::punctuated_leading_trivia(&expr_list).iter(),
261+
shape,
254262
)
255-
.update_leading_trivia(FormatTriviaType::Append(vec![
256-
create_newline_trivia(ctx),
257-
create_indent_trivia(ctx, shape),
258-
]))
259263
}
260264
false => singleline_expr,
261265
};
262266

263267
let do_token = match requires_expr_multiline {
264-
true => fmt_symbol!(ctx, generic_for.in_token(), "do", shape).update_leading_trivia(
268+
true => fmt_symbol!(ctx, generic_for.do_token(), "do", shape).update_leading_trivia(
265269
FormatTriviaType::Append(vec![
266270
create_newline_trivia(ctx),
267271
create_indent_trivia(ctx, shape),

src/formatters/trivia_util.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use full_moon::ast::types::{
1212
};
1313
use full_moon::{
1414
ast::{
15-
BinOp, Block, Call, Expression, Field, FunctionArgs, Index, LastStmt, Parameter, Prefix,
16-
Stmt, Suffix, TableConstructor, UnOp, Value, Var,
15+
punctuated::Punctuated, BinOp, Block, Call, Expression, Field, FunctionArgs, Index,
16+
LastStmt, Parameter, Prefix, Stmt, Suffix, TableConstructor, UnOp, Value, Var,
1717
},
1818
node::Node,
1919
tokenizer::{Token, TokenKind, TokenReference, TokenType},
@@ -420,6 +420,13 @@ pub fn get_expression_leading_trivia(expression: &Expression) -> Vec<Token> {
420420
}
421421
}
422422

423+
pub fn punctuated_leading_trivia(punctuated: &Punctuated<Expression>) -> Vec<Token> {
424+
punctuated
425+
.iter()
426+
.next()
427+
.map_or_else(Vec::new, get_expression_leading_trivia)
428+
}
429+
423430
pub fn binop_leading_comments(binop: &BinOp) -> Vec<Token> {
424431
match binop {
425432
BinOp::And(token)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/579
2+
for _, item in
3+
-- comment
4+
call()
5+
do
6+
end
7+
8+
9+
for _, item in -- comment
10+
call()
11+
do
12+
end
13+
14+
15+
for _, item in -- comment
16+
17+
call()
18+
do
19+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: tests/tests.rs
3+
expression: format(&contents)
4+
---
5+
-- https://github.com/JohnnyMorganz/StyLua/issues/579
6+
for _, item in
7+
-- comment
8+
call()
9+
do
10+
end
11+
12+
for _, item in -- comment
13+
call()
14+
do
15+
end
16+
17+
for _, item in -- comment
18+
call()
19+
do
20+
end
21+

0 commit comments

Comments
 (0)