@@ -113,6 +113,21 @@ let hasNestedJsxOrMoreThanOneChild expr =
113
113
in
114
114
loop false expr
115
115
116
+ let hasTailSingleLineComment tbl loc =
117
+ let rec getLastElement elements =
118
+ match elements with
119
+ | [] -> None
120
+ | [element] -> Some element
121
+ | _ :: rest -> getLastElement rest
122
+ in
123
+ match Hashtbl. find_opt tbl.CommentTable. trailing loc with
124
+ | None -> false
125
+ | Some comments -> (
126
+ let lastComment = getLastElement comments in
127
+ match lastComment with
128
+ | None -> false
129
+ | Some comment -> Comment. isSingleLineComment comment)
130
+
116
131
let hasCommentsInside tbl loc =
117
132
match Hashtbl. find_opt tbl.CommentTable. inside loc with
118
133
| None -> false
@@ -4041,8 +4056,20 @@ and printJsxExpression ~customLayout lident args cmtTbl =
4041
4056
Pexp_construct ({txt = Longident. Lident " []" }, None );
4042
4057
}
4043
4058
when isSelfClosing ->
4044
- Doc. concat [Doc. line; Doc. text " />" ]
4045
- | _ -> Doc. concat [Doc. softLine; Doc. greaterThan]);
4059
+ Doc. text " />"
4060
+ | _ ->
4061
+ (* if last trailing comment of tag is single line comment then put > on the next line
4062
+ <A
4063
+ // single line comment
4064
+ >
4065
+ </A>
4066
+ *)
4067
+ if hasTailSingleLineComment cmtTbl lident.Asttypes. loc then
4068
+ Doc. concat [Doc. softLine; Doc. greaterThan]
4069
+ else
4070
+ Doc. ifBreaks
4071
+ (Doc. lineSuffix Doc. greaterThan)
4072
+ Doc. greaterThan);
4046
4073
]);
4047
4074
(if isSelfClosing then Doc. nil
4048
4075
else
@@ -4140,6 +4167,27 @@ and printJsxChildren ~customLayout (childrenExpr : Parsetree.expression) ~sep
4140
4167
4141
4168
and printJsxProps ~customLayout args cmtTbl :
4142
4169
Doc. t * Parsetree. expression option =
4170
+ (* This function was introduced because we have different formatting behavior for self-closing tags and other tags
4171
+ we always put /> on a new line for self-closing tag when it breaks
4172
+ <A
4173
+ a=""
4174
+ />
4175
+
4176
+ <A
4177
+ a="">
4178
+ <B />
4179
+ </A>
4180
+ we should remove this function once the format is unified
4181
+ *)
4182
+ let isSelfClosing children =
4183
+ match children with
4184
+ | {
4185
+ Parsetree. pexp_desc = Pexp_construct ({txt = Longident. Lident " []" }, None );
4186
+ pexp_loc = loc;
4187
+ } ->
4188
+ not (hasCommentsInside cmtTbl loc)
4189
+ | _ -> false
4190
+ in
4143
4191
let rec loop props args =
4144
4192
match args with
4145
4193
| [] -> (Doc. nil, None )
@@ -4151,13 +4199,42 @@ and printJsxProps ~customLayout args cmtTbl :
4151
4199
Pexp_construct ({txt = Longident. Lident " ()" }, None );
4152
4200
} );
4153
4201
] ->
4202
+ let doc = if isSelfClosing children then Doc. line else Doc. nil in
4203
+ (doc, Some children)
4204
+ | ((_, expr) as lastProp)
4205
+ :: [
4206
+ (Asttypes. Labelled " children" , children);
4207
+ ( Asttypes. Nolabel ,
4208
+ {
4209
+ Parsetree. pexp_desc =
4210
+ Pexp_construct ({txt = Longident. Lident " ()" }, None );
4211
+ } );
4212
+ ] ->
4213
+ let loc =
4214
+ match expr.Parsetree. pexp_attributes with
4215
+ | ({Location. txt = "ns.namedArgLoc" ; loc} , _ ) :: _attrs ->
4216
+ {loc with loc_end = expr.pexp_loc.loc_end}
4217
+ | _ -> expr.pexp_loc
4218
+ in
4219
+ let tailSingleLineCommentPresent = hasTailSingleLineComment cmtTbl loc in
4220
+ let propDoc = printJsxProp ~custom Layout lastProp cmtTbl in
4154
4221
let formattedProps =
4155
- Doc. indent
4156
- (match props with
4157
- | [] -> Doc. nil
4158
- | props ->
4159
- Doc. concat
4160
- [Doc. line; Doc. group (Doc. join ~sep: Doc. line (props |> List. rev))])
4222
+ Doc. concat
4223
+ [
4224
+ Doc. indent
4225
+ (Doc. concat
4226
+ [
4227
+ Doc. line;
4228
+ Doc. group
4229
+ (Doc. join ~sep: Doc. line (propDoc :: props |> List. rev));
4230
+ ]);
4231
+ (* print > on new line if last comment is single line comment *)
4232
+ (match (isSelfClosing children, tailSingleLineCommentPresent) with
4233
+ (* we always put /> on a new line when a self-closing tag breaks *)
4234
+ | true , _ -> Doc. line
4235
+ | false , true -> Doc. softLine
4236
+ | false , false -> Doc. nil);
4237
+ ]
4161
4238
in
4162
4239
(formattedProps, Some children)
4163
4240
| arg :: args ->
0 commit comments