Skip to content

Commit 4481226

Browse files
committed
Support optional abstract prefix on classes for TypeScript compatibility
1 parent cda42b0 commit 4481226

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

src/parser/object_parser.ml

+1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ module Object
784784
let env = env |> with_strict true in
785785
let start_loc = Peek.loc env in
786786
let decorators = decorators @ (decorator_list env) in
787+
ignore (Expect.maybe env T_ABSTRACT);
787788
Expect.token env T_CLASS;
788789
let tmp_env = env |> with_no_let true in
789790
let id = (

src/parser/parser_env.ml

+1
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ module Peek = struct
681681
let is_class env =
682682
match token env with
683683
| T_CLASS
684+
| T_ABSTRACT
684685
| T_AT -> true
685686
| _ -> false
686687
end

src/parser/parser_flow.ml

-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ module rec Parse : PARSER = struct
195195
| T_CASE
196196
| T_DEFAULT
197197
| T_EXTENDS
198-
| T_ABSTRACT (*TJP: I may retract this if I end up adding support for the `abstract` prefix on `class`.*)
199198
| T_STATIC
200199
| T_EXPORT (* TODO *)
201200
| T_ELLIPSIS ->

src/parser/statement_parser.ml

+2
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ module Statement
669669

670670
and declare_class_statement env = with_loc (fun env ->
671671
Expect.token env T_DECLARE;
672+
ignore (Expect.maybe env T_ABSTRACT);
672673
let fn = declare_class env in
673674
Statement.DeclareClass fn
674675
) env
@@ -837,6 +838,7 @@ module Statement
837838
then error env Error.UnexpectedTypeDeclaration;
838839
(* eventually, just emit a wrapper AST node *)
839840
(match Peek.ith_token ~i:1 env with
841+
| T_ABSTRACT
840842
| T_CLASS ->
841843
declare_class_statement env
842844
| T_INTERFACE ->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
abstract class PrefixedClass {}
2+
declare abstract class PrefixedAbstractClass {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"type":"Program",
3+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":2,"column":47}},
4+
"range":[0,79],
5+
"body":[
6+
{
7+
"type":"ClassDeclaration",
8+
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":31}},
9+
"range":[0,31],
10+
"id":{
11+
"type":"Identifier",
12+
"loc":{"source":null,"start":{"line":1,"column":15},"end":{"line":1,"column":28}},
13+
"range":[15,28],
14+
"name":"PrefixedClass",
15+
"typeAnnotation":null,
16+
"optional":false
17+
},
18+
"body":{
19+
"type":"ClassBody",
20+
"loc":{"source":null,"start":{"line":1,"column":29},"end":{"line":1,"column":31}},
21+
"range":[29,31],
22+
"body":[]
23+
},
24+
"superClass":null,
25+
"typeParameters":null,
26+
"superTypeParameters":null,
27+
"implements":[],
28+
"decorators":[]
29+
},
30+
{
31+
"type":"DeclareClass",
32+
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":47}},
33+
"range":[32,79],
34+
"id":{
35+
"type":"Identifier",
36+
"loc":{"source":null,"start":{"line":2,"column":23},"end":{"line":2,"column":44}},
37+
"range":[55,76],
38+
"name":"PrefixedAbstractClass",
39+
"typeAnnotation":null,
40+
"optional":false
41+
},
42+
"typeParameters":null,
43+
"body":{
44+
"type":"ObjectTypeAnnotation",
45+
"loc":{"source":null,"start":{"line":2,"column":45},"end":{"line":2,"column":47}},
46+
"range":[77,79],
47+
"exact":false,
48+
"properties":[],
49+
"indexers":[],
50+
"callProperties":[]
51+
},
52+
"extends":[]
53+
}
54+
],
55+
"comments":[]
56+
}

0 commit comments

Comments
 (0)