Skip to content

Commit 3dbeb42

Browse files
Hans Halversonfacebook-github-bot
authored andcommitted
Intern comments in computed property keys
Summary: Interns leading and trailing comments for computed object property keys. Comments preceding the opening brace will be attached as leading comments, and comments succeeding the closing brace will be attached as trailing comments. Comments within the braces will be attached to the inner expression. ``` { /* L computed */ [ /* L expr */ expr /* T expr */ ] /* T computed */ : value } ``` Both object expressions and patterns can have computed keys of the same form, so I created a new shared `ComputedKey` node in the AST to share logic between computd object keys and computed expression keys. This `ComputedKey` module contains a new record type so that both the coments and inner expression can be stored. The new `ComputedKey` node also keeps track of the loc for the overall computed key (beginning at the opening brace and ending at the closing brace), which is needed in the ast differ and the printer. Reviewed By: pieterv Differential Revision: D20350642 fbshipit-source-id: bff29e48e62e11e3456efec4fdfd13b82daae78b
1 parent dfb5b41 commit 3dbeb42

15 files changed

+386
-121
lines changed

src/parser/estree_translator.ml

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -848,13 +848,14 @@ with type t = Impl.t = struct
848848
| PrivateField p -> class_private_field p
849849
| Property p -> class_property p)
850850
and class_method (loc, { Class.Method.key; value; kind; static; decorators }) =
851-
let (key, computed) =
851+
let (key, computed, comments) =
852852
let open Expression.Object.Property in
853853
match key with
854-
| Literal lit -> (literal lit, false)
855-
| Identifier id -> (identifier id, false)
856-
| PrivateName name -> (private_name name, false)
857-
| Computed expr -> (expression expr, true)
854+
| Literal lit -> (literal lit, false, None)
855+
| Identifier id -> (identifier id, false, None)
856+
| PrivateName name -> (private_name name, false, None)
857+
| Computed (_, { ComputedKey.expression = expr; comments }) ->
858+
(expression expr, true, comments)
858859
in
859860
let kind =
860861
Class.Method.(
@@ -865,6 +866,7 @@ with type t = Impl.t = struct
865866
| Set -> "set")
866867
in
867868
node
869+
?comments
868870
"MethodDefinition"
869871
loc
870872
[
@@ -899,13 +901,14 @@ with type t = Impl.t = struct
899901
in
900902
node "ClassPrivateProperty" loc props
901903
and class_property (loc, { Class.Property.key; value; annot; static; variance = variance_ }) =
902-
let (key, computed) =
904+
let (key, computed, comments) =
903905
match key with
904-
| Expression.Object.Property.Literal lit -> (literal lit, false)
905-
| Expression.Object.Property.Identifier id -> (identifier id, false)
906+
| Expression.Object.Property.Literal lit -> (literal lit, false, None)
907+
| Expression.Object.Property.Identifier id -> (identifier id, false, None)
906908
| Expression.Object.Property.PrivateName _ ->
907909
failwith "Internal Error: Private name found in class prop"
908-
| Expression.Object.Property.Computed expr -> (expression expr, true)
910+
| Expression.Object.Property.Computed (_, { ComputedKey.expression = expr; comments }) ->
911+
(expression expr, true, comments)
909912
in
910913
let (value, declare) =
911914
match value with
@@ -928,7 +931,7 @@ with type t = Impl.t = struct
928931
else
929932
[]
930933
in
931-
node "ClassProperty" loc props
934+
node ?comments "ClassProperty" loc props
932935
and enum_declaration (loc, { Statement.EnumDeclaration.id; body }) =
933936
let open Statement.EnumDeclaration in
934937
let enum_body =
@@ -1067,14 +1070,16 @@ with type t = Impl.t = struct
10671070
| Set { key; value = (loc, func) } ->
10681071
(key, function_expression (loc, func), "set", false, false)
10691072
in
1070-
let (key, computed) =
1073+
let (key, computed, comments) =
10711074
match key with
1072-
| Literal lit -> (literal lit, false)
1073-
| Identifier id -> (identifier id, false)
1075+
| Literal lit -> (literal lit, false, None)
1076+
| Identifier id -> (identifier id, false, None)
10741077
| PrivateName _ -> failwith "Internal Error: Found private field in object props"
1075-
| Computed expr -> (expression expr, true)
1078+
| Computed (_, { ComputedKey.expression = expr; comments }) ->
1079+
(expression expr, true, comments)
10761080
in
10771081
node
1082+
?comments
10781083
"Property"
10791084
loc
10801085
[
@@ -1091,11 +1096,12 @@ with type t = Impl.t = struct
10911096
Pattern.Object.(
10921097
function
10931098
| Property (loc, { Property.key; pattern = patt; default; shorthand }) ->
1094-
let (key, computed) =
1099+
let (key, computed, comments) =
10951100
match key with
1096-
| Property.Literal lit -> (literal lit, false)
1097-
| Property.Identifier id -> (identifier id, false)
1098-
| Property.Computed expr -> (expression expr, true)
1101+
| Property.Literal lit -> (literal lit, false, None)
1102+
| Property.Identifier id -> (identifier id, false, None)
1103+
| Property.Computed (_, { ComputedKey.expression = expr; comments }) ->
1104+
(expression expr, true, comments)
10991105
in
11001106
let value =
11011107
match default with
@@ -1105,6 +1111,7 @@ with type t = Impl.t = struct
11051111
| None -> pattern patt
11061112
in
11071113
node
1114+
?comments
11081115
"Property"
11091116
loc
11101117
[

src/parser/flow_ast.ml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ and Variance : sig
102102
end =
103103
Variance
104104

105+
and ComputedKey : sig
106+
type ('M, 'T) t = 'M * ('M, 'T) ComputedKey.t'
107+
108+
and ('M, 'T) t' = {
109+
expression: ('M, 'T) Expression.t;
110+
comments: ('M, unit) Syntax.t option;
111+
}
112+
[@@deriving show]
113+
end =
114+
ComputedKey
115+
105116
and Type : sig
106117
module Function : sig
107118
module Param : sig
@@ -889,7 +900,7 @@ and Expression : sig
889900
| Literal of ('T * 'M Literal.t)
890901
| Identifier of ('M, 'T) Identifier.t
891902
| PrivateName of 'M PrivateName.t
892-
| Computed of ('M, 'T) Expression.t
903+
| Computed of ('M, 'T) ComputedKey.t
893904

894905
and ('M, 'T) t = 'M * ('M, 'T) t'
895906

@@ -1367,7 +1378,7 @@ and Pattern : sig
13671378
type ('M, 'T) key =
13681379
| Literal of ('M * 'M Literal.t)
13691380
| Identifier of ('M, 'T) Identifier.t
1370-
| Computed of ('M, 'T) Expression.t
1381+
| Computed of ('M, 'T) ComputedKey.t
13711382

13721383
and ('M, 'T) t = 'M * ('M, 'T) t'
13731384

src/parser/object_parser.ml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,21 @@ module Object
7878
{ Literal.value; raw; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () }
7979
) )
8080
| T_LBRACKET ->
81-
with_loc
82-
(fun env ->
83-
Expect.token env T_LBRACKET;
84-
let expr = Parse.assignment (env |> with_no_in false) in
85-
Expect.token env T_RBRACKET;
86-
Ast.Expression.Object.Property.Computed expr)
87-
env
81+
let (loc, key) =
82+
with_loc
83+
(fun env ->
84+
let leading = Peek.comments env in
85+
Expect.token env T_LBRACKET;
86+
let expr = Parse.assignment (env |> with_no_in false) in
87+
Expect.token env T_RBRACKET;
88+
let trailing = Peek.comments env in
89+
{
90+
ComputedKey.expression = expr;
91+
comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
92+
})
93+
env
94+
in
95+
(loc, Ast.Expression.Object.Property.Computed (loc, key))
8896
| T_POUND when class_body ->
8997
let (loc, id, _is_private) = Expression.property_name_include_private env in
9098
add_declared_private env (Flow_ast_utils.name_of_ident id);
@@ -184,7 +192,7 @@ module Object
184192
strict_error_at env (loc, Parse_error.StrictReservedWord);
185193
(loc, Ast.Expression.Identifier id)
186194
| PrivateName _ -> failwith "Internal Error: private name found in object props"
187-
| Computed expr ->
195+
| Computed (_, { ComputedKey.expression = expr; comments = _ }) ->
188196
error_at env (fst expr, Parse_error.ComputedShorthandProperty);
189197
expr
190198
in

src/parser/pattern_parser.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Pattern (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) = struct
3333
| Property.Literal lit -> Pattern.Object.Property.Literal lit
3434
| Property.Identifier id -> Pattern.Object.Property.Identifier id
3535
| Property.PrivateName _ -> failwith "Internal Error: Found object private prop"
36-
| Property.Computed expr -> Pattern.Object.Property.Computed expr
36+
| Property.Computed key -> Pattern.Object.Property.Computed key
3737
in
3838
let (pattern, default) =
3939
match value with
@@ -201,7 +201,7 @@ module Pattern (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) = struct
201201
| (_, Literal lit) -> Pattern.Object.Property.Literal lit
202202
| (_, Identifier id) -> Pattern.Object.Property.Identifier id
203203
| (_, PrivateName _) -> failwith "Internal Error: Found object private prop"
204-
| (_, Computed expr) -> Pattern.Object.Property.Computed expr
204+
| (_, Computed key) -> Pattern.Object.Property.Computed key
205205
in
206206
Some
207207
Pattern.Object.(Property (loc, Property.{ key; pattern; default; shorthand = false }))
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
const id = /*pre*/ {
2-
a: 1,
3-
}/*post*/;
1+
(/* 1.1 L obj */ {a: 1} /* 1.2 T obj */);
2+
3+
({ /* 2.1 L id */ a /* 2.2 T id */ : /* 2.3 L num */ 1 /* 2.4 T num */ });
4+
5+
({ /* 3.1 L computed */ [/* 3.2 L str */ 'a' /* 3.3 T str */] /* 3.4 T computed */ : 1 });

0 commit comments

Comments
 (0)