Skip to content

Commit 9a65856

Browse files
authored
Skip trailing comma in explicit partial application (#6949)
* Check for dotdotdot in args * Check on attribute rather than label * Add tests * Fix naming convention * Update CHANGELOG
1 parent 39a6e88 commit 9a65856

File tree

6 files changed

+118
-7
lines changed

6 files changed

+118
-7
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
- Removed empty line at the end of `switch` statement
2424
- Removed empty `default` case from `switch` statement in the generated code
2525

26+
#### :bug: Bug Fix
27+
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949
28+
2629
# 12.0.0-alpha.1
2730

2831
#### :rocket: New Feature

jscomp/syntax/src/res_parsetree_viewer.ml

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ let process_partial_app_attribute attrs =
5757
in
5858
process false [] attrs
5959

60+
let has_partial_attribute attrs =
61+
List.exists
62+
(function
63+
| {Location.txt = "res.partial"}, _ -> true
64+
| _ -> false)
65+
attrs
66+
6067
type function_attributes_info = {async: bool; attributes: Parsetree.attributes}
6168

6269
let process_function_attributes attrs =

jscomp/syntax/src/res_parsetree_viewer.mli

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ val functor_type :
1717
val process_partial_app_attribute :
1818
Parsetree.attributes -> bool * Parsetree.attributes
1919

20+
val has_partial_attribute : Parsetree.attributes -> bool
21+
2022
type function_attributes_info = {async: bool; attributes: Parsetree.attributes}
2123

2224
(* determines whether a function is async and/or uncurried based on the given attributes *)

jscomp/syntax/src/res_printer.ml

+21-7
Original file line numberDiff line numberDiff line change
@@ -4146,7 +4146,13 @@ and print_pexp_apply ~state expr cmt_tbl =
41464146
let partial, attrs = ParsetreeViewer.process_partial_app_attribute attrs in
41474147
let args =
41484148
if partial then
4149-
let dummy = Ast_helper.Exp.constant (Ast_helper.Const.int 0) in
4149+
let loc =
4150+
{Asttypes.txt = "res.partial"; Asttypes.loc = expr.pexp_loc}
4151+
in
4152+
let attr = (loc, Parsetree.PTyp (Ast_helper.Typ.any ())) in
4153+
let dummy =
4154+
Ast_helper.Exp.constant ~attrs:[attr] (Ast_helper.Const.int 0)
4155+
in
41504156
args @ [(Asttypes.Labelled "...", dummy)]
41514157
else args
41524158
in
@@ -4730,6 +4736,18 @@ and print_arguments ~state ?(partial = false)
47304736
in
47314737
Doc.concat [Doc.lparen; arg_doc; Doc.rparen]
47324738
| args ->
4739+
(* Avoid printing trailing comma when there is ... in function application *)
4740+
let has_partial_attr, printed_args =
4741+
List.fold_right
4742+
(fun arg (flag, acc) ->
4743+
let _, expr = arg in
4744+
let has_partial_attr =
4745+
ParsetreeViewer.has_partial_attribute expr.Parsetree.pexp_attributes
4746+
in
4747+
let doc = print_argument ~state arg cmt_tbl in
4748+
(flag || has_partial_attr, doc :: acc))
4749+
args (false, [])
4750+
in
47334751
Doc.group
47344752
(Doc.concat
47354753
[
@@ -4738,13 +4756,9 @@ and print_arguments ~state ?(partial = false)
47384756
(Doc.concat
47394757
[
47404758
Doc.soft_line;
4741-
Doc.join
4742-
~sep:(Doc.concat [Doc.comma; Doc.line])
4743-
(List.map
4744-
(fun arg -> print_argument ~state arg cmt_tbl)
4745-
args);
4759+
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) printed_args;
47464760
]);
4747-
(if partial then Doc.nil else Doc.trailing_comma);
4761+
(if partial || has_partial_attr then Doc.nil else Doc.trailing_comma);
47484762
Doc.soft_line;
47494763
Doc.rparen;
47504764
])

jscomp/syntax/tests/printer/expr/apply.res

+34
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,37 @@ f(. {
7373

7474
resolve(.)
7575
resolve(. ())
76+
77+
let g = f(
78+
a => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
79+
b,
80+
c
81+
)
82+
83+
let g = f(
84+
a => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
85+
b,
86+
c,
87+
...
88+
)
89+
90+
let g = f(
91+
a => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
92+
b,
93+
c => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(d, ...),
94+
...
95+
)
96+
97+
let g = f(
98+
a => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a, ...),
99+
b,
100+
c => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(d),
101+
...
102+
)
103+
104+
105+
let g = f(
106+
a => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
107+
b,
108+
c => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(d, ...)
109+
)

jscomp/syntax/tests/printer/expr/expected/apply.res.txt

+51
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,54 @@ f({
9393

9494
resolve()
9595
resolve()
96+
97+
let g = f(
98+
a => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
99+
b,
100+
c,
101+
)
102+
103+
let g =
104+
f(
105+
a =>
106+
LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
107+
b,
108+
c,
109+
...
110+
)
111+
112+
let g =
113+
f(
114+
a =>
115+
LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
116+
b,
117+
c =>
118+
LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(
119+
d,
120+
...
121+
),
122+
...
123+
)
124+
125+
let g =
126+
f(
127+
a =>
128+
LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(
129+
a,
130+
...
131+
),
132+
b,
133+
c =>
134+
LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(d),
135+
...
136+
)
137+
138+
let g = f(
139+
a => LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(a),
140+
b,
141+
c =>
142+
LongModuleName.functionWithAlongNameThatWrapsTheEditorToTheNextLinexxxxxxxxxxxxxxxxxxxxxx(
143+
d,
144+
...
145+
),
146+
)

0 commit comments

Comments
 (0)