Skip to content

Commit fcee0ea

Browse files
Hans Halversonfacebook-github-bot
authored andcommitted
Intern comments in private name nodes
Summary: Interns leading comments in private name nodes. Private names begin with a `#` and end with an identifier, so comments directly preceding the `#` will be attached as leading comments. It is a syntax error to have a comment between the `#` and the identifier, but comments succeeding the identifier will be attached as trailing comments for the inner identifier: ``` /* L private name */ #ident /* T id */ ``` This adds support for comment attachment for private names in member expressions and private class fields. Note that I created a new record type for `PrivateNames` that can store both the inner identifier and the attached comments. Reviewed By: mroch Differential Revision: D20355492 fbshipit-source-id: 1499df6adb0bab43e07eacb0e54fc6ad91607740
1 parent 3dbeb42 commit fcee0ea

17 files changed

Lines changed: 342 additions & 55 deletions

src/common/flow_lsp_conversions.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ module DocumentSymbols = struct
203203
match key with
204204
| Literal (_, { Ast.Literal.raw; _ }) -> Some raw
205205
| Identifier (_, { Ast.Identifier.name = id; comments = _ }) -> Some id
206-
| PrivateName (_, (_, { Ast.Identifier.name = id; comments = _ })) -> Some id
206+
| PrivateName
207+
(_, { Ast.PrivateName.id = (_, { Ast.Identifier.name = id; comments = _ }); comments = _ })
208+
->
209+
Some id
207210
| Computed (_, _) -> None
208211

209212
let name_of_id ((_, { Ast.Identifier.name; comments = _ }) : (Loc.t, Loc.t) Ast.Identifier.t) :
@@ -262,7 +265,12 @@ module DocumentSymbols = struct
262265
| Body.Property (loc, { Property.key; _ }) ->
263266
ast_key ~uri ~containerName ~acc ~loc ~key ~kind:Lsp.SymbolInformation.Property
264267
| Body.PrivateField
265-
(loc, { PrivateField.key = (_, (_, { Ast.Identifier.name; comments = _ })); _ }) ->
268+
( loc,
269+
{
270+
PrivateField.key =
271+
(_, { Ast.PrivateName.id = (_, { Ast.Identifier.name; comments = _ }); comments = _ });
272+
_;
273+
} ) ->
266274
ast_name ~uri ~containerName ~acc ~loc ~name ~kind:Lsp.SymbolInformation.Field
267275

268276
let ast_class

src/common/reason.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,8 @@ and code_desc_of_property ~optional property =
12161216
else
12171217
"." )
12181218
^ x
1219-
| Ast.Expression.Member.PropertyPrivateName (_, (_, { Ast.Identifier.name = x; comments = _ })) ->
1219+
| Ast.Expression.Member.PropertyPrivateName
1220+
(_, { Ast.PrivateName.id = (_, { Ast.Identifier.name = x; comments = _ }); comments = _ }) ->
12201221
( if optional then
12211222
"?.#"
12221223
else

src/parser/estree_translator.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,8 @@ with type t = Impl.t = struct
681681
?comments
682682
loc
683683
[("name", string name); ("typeAnnotation", null); ("optional", bool false)]
684-
and private_name (loc, name) = node "PrivateName" loc [("id", identifier name)]
684+
and private_name (loc, { PrivateName.id; comments }) =
685+
node ?comments "PrivateName" loc [("id", identifier id)]
685686
and pattern_identifier
686687
loc { Pattern.Identifier.name = (_, { Identifier.name; comments = _ }); annot; optional } =
687688
node
@@ -878,7 +879,14 @@ with type t = Impl.t = struct
878879
("decorators", array_of_list class_decorator decorators);
879880
]
880881
and class_private_field
881-
(loc, { Class.PrivateField.key = (_, key); value; annot; static; variance = variance_ }) =
882+
( loc,
883+
{
884+
Class.PrivateField.key = (_, { PrivateName.id; comments });
885+
value;
886+
annot;
887+
static;
888+
variance = variance_;
889+
} ) =
882890
let (value, declare) =
883891
match value with
884892
| Class.Property.Declared -> (None, true)
@@ -887,7 +895,7 @@ with type t = Impl.t = struct
887895
in
888896
let props =
889897
[
890-
("key", identifier key);
898+
("key", identifier id);
891899
("value", option expression value);
892900
("typeAnnotation", hint type_annotation annot);
893901
("static", bool static);
@@ -899,7 +907,7 @@ with type t = Impl.t = struct
899907
else
900908
[]
901909
in
902-
node "ClassPrivateProperty" loc props
910+
node ?comments "ClassPrivateProperty" loc props
903911
and class_property (loc, { Class.Property.key; value; annot; static; variance = variance_ }) =
904912
let (key, computed, comments) =
905913
match key with

src/parser/expression_parser.ml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module type EXPRESSION = sig
1818

1919
val conditional : env -> (Loc.t, Loc.t) Expression.t
2020

21-
val property_name_include_private : env -> Loc.t * (Loc.t, Loc.t) Identifier.t * bool
21+
val property_name_include_private :
22+
env -> Loc.t * (Loc.t, Loc.t) Identifier.t * bool * Loc.t Comment.t list
2223

2324
val is_assignable_lhs : (Loc.t, Loc.t) Expression.t -> bool
2425

@@ -886,13 +887,14 @@ module Expression
886887
env
887888
start_loc
888889
left =
889-
let (id_loc, id, is_private) = property_name_include_private env in
890+
let (id_loc, id, is_private, leading) = property_name_include_private env in
890891
if is_private then add_used_private env (Flow_ast_utils.name_of_ident id) id_loc;
891892
let loc = Loc.btwn start_loc id_loc in
892893
let open Expression.Member in
893894
let property =
894895
if is_private then
895-
PropertyPrivateName (id_loc, id)
896+
PropertyPrivateName
897+
(id_loc, { PrivateName.id; comments = Flow_ast_utils.mk_comments_opt ~leading () })
896898
else
897899
PropertyIdentifier id
898900
in
@@ -1533,15 +1535,22 @@ module Expression
15331535

15341536
and property_name_include_private env =
15351537
let start_loc = Peek.loc env in
1536-
let (loc, (is_private, id)) =
1538+
let (loc, (is_private, id, leading)) =
15371539
with_loc
15381540
(fun env ->
1539-
let is_private = Expect.maybe env T_POUND in
1541+
let (is_private, leading) =
1542+
match Peek.token env with
1543+
| T_POUND ->
1544+
let leading = Peek.comments env in
1545+
Eat.token env;
1546+
(true, leading)
1547+
| _ -> (false, [])
1548+
in
15401549
let id = identifier_name env in
1541-
(is_private, id))
1550+
(is_private, id, leading))
15421551
env
15431552
in
15441553
if is_private && start_loc.Loc._end <> (fst id).Loc.start then
15451554
error_at env (loc, Parse_error.WhitespaceInPrivateName);
1546-
(loc, id, is_private)
1555+
(loc, id, is_private, leading)
15471556
end

src/parser/flow_ast.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ end =
3333
Identifier
3434

3535
and PrivateName : sig
36-
type 'M t = 'M * ('M, 'M) Identifier.t [@@deriving show]
36+
type 'M t = 'M * 'M t'
37+
38+
and 'M t' = {
39+
id: ('M, 'M) Identifier.t;
40+
comments: ('M, unit) Syntax.t option;
41+
}
42+
[@@deriving show]
3743
end =
3844
PrivateName
3945

src/parser/object_parser.ml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ module Object
9494
in
9595
(loc, Ast.Expression.Object.Property.Computed (loc, key))
9696
| T_POUND when class_body ->
97-
let (loc, id, _is_private) = Expression.property_name_include_private env in
97+
let (loc, id, _is_private, leading) = Expression.property_name_include_private env in
9898
add_declared_private env (Flow_ast_utils.name_of_ident id);
99-
(loc, PrivateName (loc, id))
99+
( loc,
100+
PrivateName (loc, { PrivateName.id; comments = Flow_ast_utils.mk_comments_opt ~leading () })
101+
)
100102
| _ ->
101-
let (loc, id, is_private) = Expression.property_name_include_private env in
103+
let (loc, id, is_private, _) = Expression.property_name_include_private env in
102104
if is_private then error_at env (loc, Parse_error.PrivateNotInClass);
103105
(loc, Identifier id)
104106

@@ -415,7 +417,9 @@ module Object
415417
error_at env (loc, Parse_error.InvalidFieldName { name; static; private_ = false })
416418

417419
let check_private_names env seen_names private_name (kind : [ `Field | `Getter | `Setter ]) =
418-
let (loc, (_, { Identifier.name; comments = _ })) = private_name in
420+
let (loc, { PrivateName.id = (_, { Identifier.name; comments = _ }); comments = _ }) =
421+
private_name
422+
in
419423
if String.equal name "constructor" then
420424
let () =
421425
error_at env (loc, Parse_error.InvalidFieldName { name; static = false; private_ = true })
@@ -713,18 +717,18 @@ module Object
713717
| _ -> private_names
714718
end )
715719
| Get ->
720+
let open Ast.Expression.Object.Property in
716721
let private_names =
717722
match m.key with
718-
| Ast.Expression.Object.Property.PrivateName name ->
719-
check_private_names env private_names name `Getter
723+
| PrivateName name -> check_private_names env private_names name `Getter
720724
| _ -> private_names
721725
in
722726
(seen_constructor, private_names)
723727
| Set ->
728+
let open Ast.Expression.Object.Property in
724729
let private_names =
725730
match m.key with
726-
| Ast.Expression.Object.Property.PrivateName name ->
727-
check_private_names env private_names name `Setter
731+
| PrivateName name -> check_private_names env private_names name `Setter
728732
| _ -> private_names
729733
in
730734
(seen_constructor, private_names))

src/parser/test/flow/comment_interning/member.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ function dynamicMemberExpression() {
1111

1212
/* 4.1 L member */ (/* 4.2 L id */ x[y] /* 4.3 T member */) /* 4.4 T member */;
1313
}
14+
15+
class PrivateMemberExpression {
16+
/* 5.1 L private */ #z /* 5.2 T id */;
17+
constructor() {
18+
/* 6.1 L this */ this /* 6.2 T this */ . /* 6.3 L private */ #z /* 6.4 T id */;
19+
}
20+
}

0 commit comments

Comments
 (0)