Skip to content

Commit 6bfee0a

Browse files
gkzmeta-codesync[bot]
authored andcommitted
[flow][records] Add custom parse error for using computed property in record
Summary: Add custom parse error for using computed property in record. Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D87111849 fbshipit-source-id: 448d3541087a13536ec40e70a476a091ccb38db5
1 parent d229e2e commit 6bfee0a

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

src/parser/object_parser.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,15 @@ module Object
12111211
Eat.token env;
12121212
error_at env (error_loc, Parse_error.RecordPrivateElementUnsupported)
12131213
);
1214+
if Peek.token env = T_LBRACKET then (
1215+
let start_loc = Peek.loc env in
1216+
Expect.token env T_LBRACKET;
1217+
ignore @@ Parse.assignment (env |> with_no_in false);
1218+
let end_loc = Peek.loc env in
1219+
Expect.token env T_RBRACKET;
1220+
let error_loc = Loc.btwn start_loc end_loc in
1221+
error_at env (error_loc, Parse_error.RecordComputedPropertyUnsupported)
1222+
);
12141223
let key = identifier_name env in
12151224
let (key_loc, { Identifier.name = key_name; _ }) = key in
12161225
let check_invalid_name env ~method_ =

src/parser/parse_error.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type t =
127127
| PrivateDelete
128128
| PrivateNotInClass
129129
| PropertyAfterRestElement
130+
| RecordComputedPropertyUnsupported
130131
| RecordExtendsUnsupported
131132
| RecordInvalidPropertyName of {
132133
name: string;
@@ -446,6 +447,7 @@ module PP = struct
446447
| PrivateDelete -> "Private fields may not be deleted."
447448
| PrivateNotInClass -> "Private fields can only be referenced from within a class."
448449
| PropertyAfterRestElement -> "Rest property must be final property of an object pattern"
450+
| RecordComputedPropertyUnsupported -> "Records do not support computed properties."
449451
| RecordExtendsUnsupported ->
450452
"Records to not support `extends`: they do not allow hierarchies. Implementing an interface by using `implements` is supported."
451453
| RecordInvalidPropertyName { name; static; method_ } ->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
record R {
2+
[X]: number,
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"records": true
3+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"errors":[
3+
{
4+
"loc":{"source":null,"start":{"line":2,"column":2},"end":{"line":2,"column":5}},
5+
"message":"Records do not support computed properties."
6+
},
7+
{
8+
"loc":{"source":null,"start":{"line":2,"column":5},"end":{"line":2,"column":6}},
9+
"message":"Unexpected token `:`, expected an identifier"
10+
},
11+
{
12+
"loc":{"source":null,"start":{"line":2,"column":7},"end":{"line":2,"column":13}},
13+
"message":"Unexpected identifier, expected the token `(`"
14+
},
15+
{
16+
"loc":{"source":null,"start":{"line":2,"column":13},"end":{"line":2,"column":14}},
17+
"message":"Unexpected token `,`, expected an identifier"
18+
},
19+
{
20+
"loc":{"source":null,"start":{"line":3,"column":0},"end":{"line":3,"column":1}},
21+
"message":"Unexpected token `}`, expected the token `,`"
22+
},
23+
{
24+
"loc":{"source":null,"start":{"line":4,"column":0},"end":{"line":4,"column":0}},
25+
"message":"Rest parameter must be final parameter of an argument list"
26+
},
27+
{
28+
"loc":{"source":null,"start":{"line":4,"column":0},"end":{"line":4,"column":0}},
29+
"message":"Unexpected end of input, expected the token `)`"
30+
},
31+
{
32+
"loc":{"source":null,"start":{"line":4,"column":0},"end":{"line":4,"column":0}},
33+
"message":"Unexpected end of input, expected the token `{`"
34+
},
35+
{
36+
"loc":{"source":null,"start":{"line":4,"column":0},"end":{"line":4,"column":0}},
37+
"message":"Unexpected end of input, expected the token `}`"
38+
}
39+
],
40+
"type":"Program",
41+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":4,"column":0}},
42+
"range":[0,28],
43+
"body":[
44+
{
45+
"type":"RecordDeclaration",
46+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":4,"column":0}},
47+
"range":[0,28],
48+
"id":{
49+
"type":"Identifier",
50+
"loc":{"source":null,"start":{"line":1,"column":7},"end":{"line":1,"column":8}},
51+
"range":[7,8],
52+
"name":"R",
53+
"typeAnnotation":null,
54+
"optional":false
55+
},
56+
"typeParameters":null,
57+
"implements":[],
58+
"body":{
59+
"type":"RecordBody",
60+
"loc":{"source":null,"start":{"line":1,"column":9},"end":{"line":4,"column":0}},
61+
"range":[9,28],
62+
"body":[
63+
{
64+
"type":"MethodDefinition",
65+
"loc":{"source":null,"start":{"line":2,"column":5},"end":{"line":4,"column":0}},
66+
"range":[16,28],
67+
"key":{
68+
"type":"Identifier",
69+
"loc":{"source":null,"start":{"line":2,"column":5},"end":{"line":2,"column":6}},
70+
"range":[16,17],
71+
"name":"",
72+
"typeAnnotation":null,
73+
"optional":false
74+
},
75+
"value":{
76+
"type":"FunctionExpression",
77+
"loc":{"source":null,"start":{"line":2,"column":7},"end":{"line":4,"column":0}},
78+
"range":[18,28],
79+
"id":null,
80+
"params":[
81+
{
82+
"type":"Identifier",
83+
"loc":{"source":null,"start":{"line":2,"column":13},"end":{"line":2,"column":14}},
84+
"range":[24,25],
85+
"name":"",
86+
"typeAnnotation":null,
87+
"optional":false
88+
}
89+
],
90+
"body":{
91+
"type":"BlockStatement",
92+
"loc":{"source":null,"start":{"line":4,"column":0},"end":{"line":4,"column":0}},
93+
"range":[28,28],
94+
"body":[]
95+
},
96+
"async":false,
97+
"generator":false,
98+
"predicate":null,
99+
"expression":false,
100+
"returnType":null,
101+
"typeParameters":null
102+
},
103+
"kind":"method",
104+
"static":false,
105+
"computed":false,
106+
"decorators":[]
107+
}
108+
]
109+
}
110+
}
111+
],
112+
"comments":[]
113+
}

0 commit comments

Comments
 (0)