Skip to content

Commit 8256bda

Browse files
Hans Halversonfacebook-github-bot
authored andcommitted
Intern comments in JSX identifiers
Summary: Interns leading and trailing comments around JSX identifiers, just like leading and trailing comments are interned in non-jsx identifiers. This gets us comment attachment in JSX element names: ``` </* L id */ div /* T id */> test </ /*L id */ div /* T id */> ``` And around the names of JSX attributes: ``` <div /* L id */ name /* T id */ ="value" /* L id */ name2 /* T id */ /> ``` However note that it is ambiguous whether a comment between the tag name and the first attribute name should be attached as a trailing comment of the name or a leading comment of the first attribute name. (The same is true of adjacent attributes without values). This will be fixed in the future once we have an API for eating portions of comments, as this same problem appears for the trailing comments on statements. Reviewed By: pieterv Differential Revision: D20315238 fbshipit-source-id: 728a629398686123e5f998a154fff81077d65521
1 parent 96fac34 commit 8256bda

23 files changed

+564
-75
lines changed

src/common/reason.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,26 +1176,27 @@ and code_desc_of_operation =
11761176
and code_desc_of_jsx_element x =
11771177
let open Ast.JSX in
11781178
match (snd x.openingElement).Opening.name with
1179-
| Identifier (_, { Identifier.name }) -> "<" ^ name ^ " />"
1179+
| Identifier (_, { Identifier.name; comments = _ }) -> "<" ^ name ^ " />"
11801180
| NamespacedName
11811181
( _,
11821182
{
1183-
NamespacedName.namespace = (_, { Identifier.name = a });
1184-
name = (_, { Identifier.name = b });
1183+
NamespacedName.namespace = (_, { Identifier.name = a; comments = __POS_OF__ });
1184+
name = (_, { Identifier.name = b; comments = _ });
11851185
} ) ->
11861186
"<" ^ a ^ ":" ^ b ^ " />"
11871187
| MemberExpression x ->
11881188
let rec loop = function
11891189
| ( _,
11901190
{
1191-
MemberExpression._object = MemberExpression.Identifier (_, { Identifier.name = a });
1192-
property = (_, { Identifier.name = b });
1191+
MemberExpression._object =
1192+
MemberExpression.Identifier (_, { Identifier.name = a; comments = _ });
1193+
property = (_, { Identifier.name = b; comments = _ });
11931194
} ) ->
11941195
a ^ "." ^ b
11951196
| ( _,
11961197
{
11971198
MemberExpression._object = MemberExpression.MemberExpression a;
1198-
property = (_, { Identifier.name = b });
1199+
property = (_, { Identifier.name = b; comments = _ });
11991200
} ) ->
12001201
loop a ^ "." ^ b
12011202
in

src/parser/estree_translator.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,8 @@ with type t = Impl.t = struct
15681568
"JSXNamespacedName"
15691569
loc
15701570
[("namespace", jsx_identifier namespace); ("name", jsx_identifier name)]
1571-
and jsx_identifier (loc, { JSX.Identifier.name }) =
1572-
node "JSXIdentifier" loc [("name", string name)]
1571+
and jsx_identifier (loc, { JSX.Identifier.name; comments }) =
1572+
node ?comments "JSXIdentifier" loc [("name", string name)]
15731573
and export_specifier (loc, { Statement.ExportNamedDeclaration.ExportSpecifier.exported; local })
15741574
=
15751575
let exported =

src/parser/flow_ast.ml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,17 +1219,21 @@ end =
12191219

12201220
and JSX : sig
12211221
module Identifier : sig
1222-
type 'T t = 'T * t'
1222+
type ('M, 'T) t = 'T * 'M t'
12231223

1224-
and t' = { name: string } [@@deriving show]
1224+
and 'M t' = {
1225+
name: string;
1226+
comments: ('M, unit) Syntax.t option;
1227+
}
1228+
[@@deriving show]
12251229
end
12261230

12271231
module NamespacedName : sig
1228-
type ('M, 'T) t = 'M * 'T t'
1232+
type ('M, 'T) t = 'M * ('M, 'T) t'
12291233

1230-
and 'T t' = {
1231-
namespace: 'T Identifier.t;
1232-
name: 'T Identifier.t;
1234+
and ('M, 'T) t' = {
1235+
namespace: ('M, 'T) Identifier.t;
1236+
name: ('M, 'T) Identifier.t;
12331237
}
12341238
[@@deriving show]
12351239
end
@@ -1255,7 +1259,7 @@ and JSX : sig
12551259
type ('M, 'T) t = 'M * ('M, 'T) t'
12561260

12571261
and ('M, 'T) name =
1258-
| Identifier of 'T Identifier.t
1262+
| Identifier of ('M, 'T) Identifier.t
12591263
| NamespacedName of ('M, 'T) NamespacedName.t
12601264

12611265
and ('M, 'T) value =
@@ -1279,18 +1283,18 @@ and JSX : sig
12791283
type ('M, 'T) t = 'M * ('M, 'T) t'
12801284

12811285
and ('M, 'T) _object =
1282-
| Identifier of 'T Identifier.t
1286+
| Identifier of ('M, 'T) Identifier.t
12831287
| MemberExpression of ('M, 'T) t
12841288

12851289
and ('M, 'T) t' = {
12861290
_object: ('M, 'T) _object;
1287-
property: 'T Identifier.t;
1291+
property: ('M, 'T) Identifier.t;
12881292
}
12891293
[@@deriving show]
12901294
end
12911295

12921296
type ('M, 'T) name =
1293-
| Identifier of 'T Identifier.t
1297+
| Identifier of ('M, 'T) Identifier.t
12941298
| NamespacedName of ('M, 'T) NamespacedName.t
12951299
| MemberExpression of ('M, 'T) MemberExpression.t
12961300
[@@deriving show]

src/parser/jsx_parser.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ module JSX (Parse : Parser_common.PARSER) = struct
8282
error_unexpected ~expected:"an identifier" env;
8383
""
8484
in
85+
let leading = Peek.comments env in
8586
Eat.token env;
86-
(loc, JSX.Identifier.{ name })
87+
let trailing = Peek.comments env in
88+
(loc, JSX.Identifier.{ name; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () })
8789

8890
let name =
8991
let rec member_expression env member =
@@ -306,7 +308,7 @@ module JSX (Parse : Parser_common.PARSER) = struct
306308
let rec normalize name =
307309
JSX.(
308310
match name with
309-
| Identifier (_, { Identifier.name }) -> name
311+
| Identifier (_, { Identifier.name; comments = _ }) -> name
310312
| NamespacedName (_, { NamespacedName.namespace; name }) ->
311313
(snd namespace).Identifier.name ^ ":" ^ (snd name).Identifier.name
312314
| MemberExpression (_, { MemberExpression._object; property }) ->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
/* 1.1 L JSX element */ <div>test</div> /* 1.2 T JSX element */;
2+
3+
</* 2.1 L JSX id */ div /* 2.2 T JSX id */>test</ /* 2.3 L JSX id */ div /* 2.4 T JSX id */>;
4+
5+
</* 3.1 L JSX id */ Member /* 3.2 T JSX id */ . /* 3.3 L JSX id */ div /* 3.4 JSX id */ />;
6+
7+
</* 4.1 L JSX id */ Namespace /* 4.2 T JSX id */ : /* 4.3 L JSX id */ div /* 4.4 JSX id */ />;
8+
9+
<div /* 5.1 L JSX id */ name /* 5.2 JSX id */ />;
10+
11+
<div /* 6.1 L JSX id */ name /* 6.2 T JSX id */="test" />;

0 commit comments

Comments
 (0)