@@ -113,21 +113,6 @@ let hasNestedJsxOrMoreThanOneChild expr =
113113 in
114114 loop false expr
115115
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-
131116let hasCommentsInside tbl loc =
132117 match Hashtbl. find_opt tbl.CommentTable. inside loc with
133118 | None -> false
@@ -4056,20 +4041,8 @@ and printJsxExpression ~customLayout lident args cmtTbl =
40564041 Pexp_construct ({txt = Longident. Lident " []" }, None );
40574042 }
40584043 when isSelfClosing ->
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);
4044+ Doc. concat [Doc. line; Doc. text " />" ]
4045+ | _ -> Doc. concat [Doc. softLine; Doc. greaterThan]);
40734046 ]);
40744047 (if isSelfClosing then Doc. nil
40754048 else
@@ -4167,27 +4140,6 @@ and printJsxChildren ~customLayout (childrenExpr : Parsetree.expression) ~sep
41674140
41684141and printJsxProps ~customLayout args cmtTbl :
41694142 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
41914143 let rec loop props args =
41924144 match args with
41934145 | [] -> (Doc. nil, None )
@@ -4199,42 +4151,13 @@ and printJsxProps ~customLayout args cmtTbl :
41994151 Pexp_construct ({txt = Longident. Lident " ()" }, None );
42004152 } );
42014153 ] ->
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
42214154 let formattedProps =
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- ]
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))])
42384161 in
42394162 (formattedProps, Some children)
42404163 | arg :: args ->
0 commit comments