Skip to content

Commit 872c596

Browse files
authored
Fix duplicated comments in for..of formatter (#8395)
1 parent b8fdd38 commit 872c596

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#### :bug: Bug fix
2424

2525
- Fix directive `@warning("-102")` not working. https://github.com/rescript-lang/rescript/pull/8322
26+
- Fix duplicated comments in `for`..`of` formatter. https://github.com/rescript-lang/rescript/pull/8395
2627

2728
#### :memo: Documentation
2829

compiler/syntax/src/res_comments_table.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,9 +1825,28 @@ and walk_expression expr t comments =
18251825
| Pexp_await expr -> walk_expression expr t comments
18261826
| Pexp_for_of (pattern, expr1, expr2)
18271827
| Pexp_for_await_of (pattern, expr1, expr2) ->
1828-
walk_pattern pattern t comments;
1829-
walk_expression expr1 t comments;
1830-
walk_expression expr2 t comments
1828+
let leading, inside, trailing =
1829+
partition_by_loc comments pattern.ppat_loc
1830+
in
1831+
attach t.leading pattern.ppat_loc leading;
1832+
walk_pattern pattern t inside;
1833+
let after_pattern, rest =
1834+
partition_adjacent_trailing pattern.ppat_loc trailing
1835+
in
1836+
attach t.trailing pattern.ppat_loc after_pattern;
1837+
let leading, inside, trailing = partition_by_loc rest expr1.pexp_loc in
1838+
attach t.leading expr1.pexp_loc leading;
1839+
walk_expression expr1 t inside;
1840+
let after_expr, rest =
1841+
partition_adjacent_trailing expr1.pexp_loc trailing
1842+
in
1843+
attach t.trailing expr1.pexp_loc after_expr;
1844+
if is_block_expr expr2 then walk_expression expr2 t rest
1845+
else
1846+
let leading, inside, trailing = partition_by_loc rest expr2.pexp_loc in
1847+
attach t.leading expr2.pexp_loc leading;
1848+
walk_expression expr2 t inside;
1849+
attach t.trailing expr2.pexp_loc trailing
18311850
| Pexp_send _ -> ()
18321851

18331852
and walk_expr_parameter (_attrs, _argLbl, expr_opt, pattern) t comments =

tests/syntax_tests/data/printer/comments/expected/expr.res.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ for /* c0 */ i /* c1 */ in /* c2 */ 0 /* c3 */ to /* c4 */ 10 /* c5 */ {
174174
/* c6 */ doStuff() /* c7 */
175175
} // trailing
176176

177+
// Pexp_for_of
178+
for /* c0 */ x /* c1 */ of /* c2 */ xs /* c3 */ {
179+
// c4
180+
doStuff()
181+
} // trailing
182+
183+
// Pexp_for_await_of
184+
for await /* c0 */ x /* c1 */ of /* c2 */ xs /* c3 */ {
185+
// c4
186+
doStuff()
187+
} // trailing
188+
177189
// Pexp_pack
178190
/* c0 */ module(/* c1 */ ModExpr /* c2 */) /* c3 */
179191
/* c0 */ let /* c1 */ three /* c2 */ = /* c3 */ module(/* c4 */ Three /* c5 */) /* c6 */

tests/syntax_tests/data/printer/comments/expr.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ for /* c0 */ i /* c1 */ in /* c2 */ 0 /* c3 */ to /* c4 */ 10 /* c5 */ {
176176
/* c6 */ doStuff() /* c7 */
177177
} // trailing
178178

179+
// Pexp_for_of
180+
for /* c0 */ x /* c1 */ of /* c2 */ xs /* c3 */ {
181+
// c4
182+
doStuff()
183+
} // trailing
184+
185+
// Pexp_for_await_of
186+
for await /* c0 */ x /* c1 */ of /* c2 */ xs /* c3 */ {
187+
// c4
188+
doStuff()
189+
} // trailing
190+
179191
// Pexp_pack
180192
/* c0 */ module(/* c1 */ ModExpr /* c2 */) /* c3 */
181193
/* c0 */ let /* c1 */ three /* c2 */ = /* c3 */ module( /* c4 */Three /* c5 */) /* c6 */

0 commit comments

Comments
 (0)