Skip to content

Commit 96fac34

Browse files
Hans Halversonfacebook-github-bot
authored andcommitted
Intern leading and trailing comments for JSX element and fragment expressions
Summary: Interns leading and trailing comments around JSX element and fragment expressions. This only applies to around JSX expressions - internal comments within JSX are not yet handled. ``` /* L JSX element */ <div>test</div> /* T JSX element */ /* L JSX fragment */ <>test</> /* T JSX fragment */ ``` Reviewed By: pieterv Differential Revision: D20313151 fbshipit-source-id: d71855edea966a525c7a8e04a7031ba71056a021
1 parent abc554a commit 96fac34

22 files changed

Lines changed: 666 additions & 277 deletions

packages/flow-parser/test/custom_ast_types.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,27 @@ def("BigIntLiteralTypeAnnotation")
285285
def("SymbolTypeAnnotation")
286286
.bases("Type");
287287

288+
def("JSXFragment")
289+
.bases("Expression")
290+
.build("openingFragment", "closingFragment", "children")
291+
.field("openingFragment", def("JSXOpeningFragment"))
292+
.field("closingFragment", def("JSXClosingFragment"))
293+
.field("children", [or(
294+
def("JSXElement"),
295+
def("JSXExpressionContainer"),
296+
def("JSXFragment"),
297+
def("JSXText"),
298+
def("Literal")
299+
)], defaults.emptyArray)
300+
301+
def("JSXOpeningFragment")
302+
.bases("Node")
303+
.build();
304+
305+
def("JSXClosingFragment")
306+
.bases("Node")
307+
.build();
308+
288309
// Enums
289310
def("EnumDeclaration")
290311
.bases("Declaration")

src/parser/estree_translator.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,8 +1464,10 @@ with type t = Impl.t = struct
14641464
Type.Generic.Identifier.Unqualified (Flow_ast_utils.ident_of_source (loc, "_"));
14651465
targs = None;
14661466
} )
1467-
and jsx_element (loc, { JSX.openingElement; closingElement; children = (_loc, children) }) =
1467+
and jsx_element
1468+
(loc, { JSX.openingElement; closingElement; children = (_loc, children); comments }) =
14681469
node
1470+
?comments
14691471
"JSXElement"
14701472
loc
14711473
[
@@ -1475,9 +1477,14 @@ with type t = Impl.t = struct
14751477
]
14761478
and jsx_fragment
14771479
( loc,
1478-
{ JSX.frag_openingElement; frag_closingElement; frag_children = (_loc, frag_children) } )
1479-
=
1480+
{
1481+
JSX.frag_openingElement;
1482+
frag_closingElement;
1483+
frag_children = (_loc, frag_children);
1484+
frag_comments;
1485+
} ) =
14801486
node
1487+
?comments:frag_comments
14811488
"JSXFragment"
14821489
loc
14831490
[

src/parser/expression_parser.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,10 @@ module Expression
12691269
Identifier (loc, { e with Identifier.comments = merge_comments comments })
12701270
| Import ({ Import.comments; _ } as e) ->
12711271
Import { e with Import.comments = merge_comments comments }
1272+
| JSXElement ({ JSX.comments; _ } as e) ->
1273+
JSXElement { e with JSX.comments = merge_comments comments }
1274+
| JSXFragment ({ JSX.frag_comments; _ } as e) ->
1275+
JSXFragment { e with JSX.frag_comments = merge_comments frag_comments }
12721276
| Literal ({ Literal.comments; _ } as e) ->
12731277
Literal { e with Literal.comments = merge_comments comments }
12741278
| Logical ({ Logical.comments; _ } as e) ->

src/parser/flow_ast.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,12 +1329,14 @@ and JSX : sig
13291329
openingElement: ('M, 'T) Opening.t;
13301330
closingElement: ('M, 'T) Closing.t option;
13311331
children: 'M * ('M, 'T) child list;
1332+
comments: ('M, unit) Syntax.t option;
13321333
}
13331334

13341335
and ('M, 'T) fragment = {
13351336
frag_openingElement: 'M;
13361337
frag_closingElement: 'M;
13371338
frag_children: 'M * ('M, 'T) child list;
1339+
frag_comments: ('M, unit) Syntax.t option;
13381340
}
13391341
[@@deriving show]
13401342
end =

src/parser/jsx_parser.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ module JSX (Parse : Parser_common.PARSER) = struct
322322
| (_, `Fragment) -> false
323323
in
324324
fun env ->
325+
let leading = Peek.comments env in
325326
let openingElement = opening_element env in
326327
Eat.pop_lex_mode env;
327328
let (children, closingElement) =
@@ -332,6 +333,7 @@ module JSX (Parse : Parser_common.PARSER) = struct
332333
children_and_closing env
333334
)
334335
in
336+
let trailing = Peek.comments env in
335337
let end_loc =
336338
match closingElement with
337339
| `Element (loc, { JSX.Closing.name }) ->
@@ -362,6 +364,7 @@ module JSX (Parse : Parser_common.PARSER) = struct
362364
| `Element e -> Some e
363365
| _ -> None);
364366
children;
367+
comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
365368
}
366369
| (start_loc, `Fragment) ->
367370
`Fragment
@@ -375,6 +378,7 @@ module JSX (Parse : Parser_common.PARSER) = struct
375378
| `Element (loc, _) -> loc
376379
| _ -> end_loc);
377380
frag_children = children;
381+
frag_comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
378382
}
379383
in
380384
(Loc.btwn (fst openingElement) end_loc, result)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* 1.1 L JSX element */ <div>test</div> /* 1.2 T JSX element */;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"intern_comments": true
3+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"type":"Program",
3+
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":64}},
4+
"range":[24,64],
5+
"body":[
6+
{
7+
"type":"ExpressionStatement",
8+
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":64}},
9+
"range":[24,64],
10+
"expression":{
11+
"type":"JSXElement",
12+
"trailingComments":[
13+
{
14+
"type":"Block",
15+
"loc":{"source":null,"start":{"line":1,"column":40},"end":{"line":1,"column":63}},
16+
"range":[40,63],
17+
"value":" 1.2 T JSX element "
18+
}
19+
],
20+
"leadingComments":[
21+
{
22+
"type":"Block",
23+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
24+
"range":[0,23],
25+
"value":" 1.1 L JSX element "
26+
}
27+
],
28+
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":39}},
29+
"range":[24,39],
30+
"openingElement":{
31+
"type":"JSXOpeningElement",
32+
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":29}},
33+
"range":[24,29],
34+
"name":{
35+
"type":"JSXIdentifier",
36+
"loc":{"source":null,"start":{"line":1,"column":25},"end":{"line":1,"column":28}},
37+
"range":[25,28],
38+
"name":"div"
39+
},
40+
"attributes":[],
41+
"selfClosing":false
42+
},
43+
"closingElement":{
44+
"type":"JSXClosingElement",
45+
"loc":{"source":null,"start":{"line":1,"column":33},"end":{"line":1,"column":39}},
46+
"range":[33,39],
47+
"name":{
48+
"type":"JSXIdentifier",
49+
"loc":{"source":null,"start":{"line":1,"column":35},"end":{"line":1,"column":38}},
50+
"range":[35,38],
51+
"name":"div"
52+
}
53+
},
54+
"children":[
55+
{
56+
"type":"JSXText",
57+
"loc":{"source":null,"start":{"line":1,"column":29},"end":{"line":1,"column":33}},
58+
"range":[29,33],
59+
"value":"test",
60+
"raw":"test"
61+
}
62+
]
63+
},
64+
"directive":null
65+
}
66+
],
67+
"comments":[
68+
{
69+
"type":"Block",
70+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
71+
"range":[0,23],
72+
"value":" 1.1 L JSX element "
73+
},
74+
{
75+
"type":"Block",
76+
"loc":{"source":null,"start":{"line":1,"column":40},"end":{"line":1,"column":63}},
77+
"range":[40,63],
78+
"value":" 1.2 T JSX element "
79+
}
80+
]
81+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* 1.1 L JSX fragment */ <>test</> /* 1.2 T JSX fragment */;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"intern_comments": true
3+
}

0 commit comments

Comments
 (0)