Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 6870d14

Browse files
authored
fix: Avoid tail comments inside tag from being eaten (#664)
1 parent c2b5750 commit 6870d14

File tree

8 files changed

+79
-17
lines changed

8 files changed

+79
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
- Fix location issue in error messages with JSX V4 where the multiple props types are defined https://github.com/rescript-lang/syntax/pull/655
3232
- Fix location issue in make function in JSX V4 that breaks dead code elimination https://github.com/rescript-lang/syntax/pull/660
3333
- Fix parsing (hence pretty printing) of expressions with underscore `_` and comments.
34+
- Fix printing of comments inside JSX tag https://github.com/rescript-lang/syntax/pull/664
35+
- Fix issue where formatter erases tail comments inside JSX tag https://github.com/rescript-lang/syntax/issues/663
3436

3537
## ReScript 10.0
3638

src/res_comments_table.ml

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Comment = Res_comment
22
module Doc = Res_doc
3+
module ParsetreeViewer = Res_parsetree_viewer
34

45
type t = {
56
leading: (Location.t, Comment.t list) Hashtbl.t;
@@ -1310,9 +1311,43 @@ and walkExpression expr t comments =
13101311
walkExpression callExpr t inside;
13111312
after)
13121313
in
1313-
let afterExpr, rest = partitionAdjacentTrailing callExpr.pexp_loc after in
1314-
attach t.trailing callExpr.pexp_loc afterExpr;
1315-
walkList (arguments |> List.map (fun (_, e) -> ExprArgument e)) t rest
1314+
if ParsetreeViewer.isJsxExpression expr then (
1315+
let props =
1316+
arguments
1317+
|> List.filter (fun (label, _) ->
1318+
match label with
1319+
| Asttypes.Labelled "children" -> false
1320+
| Asttypes.Nolabel -> false
1321+
| _ -> true)
1322+
in
1323+
let maybeChildren =
1324+
arguments
1325+
|> List.find_opt (fun (label, _) ->
1326+
label = Asttypes.Labelled "children")
1327+
in
1328+
match maybeChildren with
1329+
(* There is no need to deal with this situation as the children cannot be NONE *)
1330+
| None -> ()
1331+
| Some (_, children) ->
1332+
let leading, inside, _ = partitionByLoc after children.pexp_loc in
1333+
if props = [] then
1334+
(* All comments inside a tag are trailing comments of the tag if there are no props
1335+
<A
1336+
// comment
1337+
// comment
1338+
/>
1339+
*)
1340+
let afterExpr, _ =
1341+
partitionAdjacentTrailing callExpr.pexp_loc after
1342+
in
1343+
attach t.trailing callExpr.pexp_loc afterExpr
1344+
else
1345+
walkList (props |> List.map (fun (_, e) -> ExprArgument e)) t leading;
1346+
walkExpression children t inside)
1347+
else
1348+
let afterExpr, rest = partitionAdjacentTrailing callExpr.pexp_loc after in
1349+
attach t.trailing callExpr.pexp_loc afterExpr;
1350+
walkList (arguments |> List.map (fun (_, e) -> ExprArgument e)) t rest
13161351
| Pexp_fun (_, _, _, _) | Pexp_newtype _ -> (
13171352
let _, parameters, returnExpr = funExpr expr in
13181353
let comments =

src/res_printer.ml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,20 +4039,15 @@ and printJsxExpression ~customLayout lident args cmtTbl =
40394039
{
40404040
Parsetree.pexp_desc =
40414041
Pexp_construct ({txt = Longident.Lident "[]"}, None);
4042-
pexp_loc = loc;
4043-
} ->
4044-
if isSelfClosing then
4045-
Doc.concat
4046-
[Doc.line; printComments (Doc.text "/>") cmtTbl loc]
4047-
else
4048-
Doc.concat [Doc.softLine; printComments Doc.nil cmtTbl loc]
4049-
| _ -> Doc.nil);
4042+
}
4043+
when isSelfClosing ->
4044+
Doc.concat [Doc.line; Doc.text "/>"]
4045+
| _ -> Doc.concat [Doc.softLine; Doc.greaterThan]);
40504046
]);
40514047
(if isSelfClosing then Doc.nil
40524048
else
40534049
Doc.concat
40544050
[
4055-
Doc.greaterThan;
40564051
(if hasChildren then printChildren children
40574052
else
40584053
match children with

tests/conversion/reason/expected/bracedJsx.res.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ let make = () => {
111111
<div
112112
className=Styles.terminal
113113
onClick={event => (event->ReactEvent.Mouse.target)["querySelector"]("input")["focus"]()}
114-
ref={containerRef->ReactDOMRe.Ref.domRef}>
114+
ref={containerRef->ReactDOMRe.Ref.domRef}
115+
>
115116
{state.history
116117
->Array.mapWithIndex((index, item) =>
117118
<div key={j`$index`} className=Styles.line>

tests/printer/comments/expected/jsx.res.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Cite = {
99

1010
<A
1111
value=""
12-
// Comment
12+
// Comment
1313
/>
1414

1515
<A /* comment */ />
@@ -18,6 +18,19 @@ module Cite = {
1818
// Comment
1919
</A>
2020

21+
<A
22+
// comment1
23+
// comment 2
24+
/>
25+
26+
<A
27+
// comment1
28+
value=""
29+
// comment2
30+
>
31+
<B /* comment3 */ />
32+
</A>
33+
2134
<div>
2235
// Must not jump inside braces
2336
{React.string("Hello, World!")}

tests/printer/comments/jsx.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ module Cite = {
2020
// Comment
2121
</A>
2222

23+
<A
24+
// comment1
25+
// comment 2
26+
/>
27+
28+
<A
29+
// comment1
30+
value=""
31+
// comment2
32+
>
33+
<B /* comment3 */ />
34+
</A>
35+
2336
<div>
2437
// Must not jump inside braces
2538
{React.string("Hello, World!")}

tests/printer/expr/expected/jsx.res.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ let avatarSection =
104104
onMouseLeave={_ => setHoveringAdmin(false)}
105105
onClick={_e => {
106106
stopImpersonating(csrfToken)
107-
}}>
107+
}}
108+
>
108109
<Avatar user={viewer} size={45} />
109110
</div>
110111
: React.nullElement}

tests/printer/other/expected/signaturePicker.res.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ let make = () => {
2828
<div className="pr-2 font-bold text-gray-400 text-lg"> {"Signature"->string} </div>
2929
<select
3030
id="country"
31-
className="transition duration-150 ease-in-out sm:text-sm sm:leading-5 border-none font-bold text-2xl text-gray-600 bg-transparent">
31+
className="transition duration-150 ease-in-out sm:text-sm sm:leading-5 border-none font-bold text-2xl text-gray-600 bg-transparent"
32+
>
3233
{options
3334
->Belt.List.map(option =>
3435
<option key={option->TimeSignature.toString}>
@@ -47,7 +48,8 @@ let make = () => {
4748
fill="none"
4849
strokeLinecap="round"
4950
strokeLinejoin="round"
50-
className="text-gray-400">
51+
className="text-gray-400"
52+
>
5153
<polyline points="6 9 12 15 18 9" />
5254
</svg>
5355
</label>

0 commit comments

Comments
 (0)