Skip to content

Commit d66b29c

Browse files
authored
fix(es/parser): Allow return type annotation on Flow constructors (#11790)
**Description:** Flow allows explicit return type annotations on class constructors, unlike TypeScript which forbids them (TS1093). This PR skips the TS1093 error emission when parsing in Flow mode. **BREAKING CHANGE:** No **Related issue:** - Closes #11789
1 parent 68e2e12 commit d66b29c

5 files changed

Lines changed: 167 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_parser: patch
3+
swc_core: patch
4+
---
5+
6+
fix(es/parser): Allow return type annotation on Flow constructors

crates/swc_ecma_lexer/src/common/parser/class_and_fn.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,10 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
10471047
let start = p.cur_pos();
10481048
let type_ann = parse_ts_type_ann(p, true, start)?;
10491049

1050-
p.emit_err(type_ann.type_ann.span(), SyntaxError::TS1093);
1050+
// Flow allows return type annotations on constructors.
1051+
if !p.syntax().flow() {
1052+
p.emit_err(type_ann.type_ann.span(), SyntaxError::TS1093);
1053+
}
10511054
}
10521055

10531056
let body: Option<_> =

crates/swc_ecma_parser/src/parser/class_and_fn.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,10 @@ impl<I: Tokens> Parser<I> {
12151215
let start = self.cur_pos();
12161216
let type_ann = self.parse_ts_type_ann(true, start)?;
12171217

1218-
self.emit_err(type_ann.type_ann.span(), SyntaxError::TS1093);
1218+
// Flow allows return type annotations on constructors.
1219+
if !self.syntax().flow() {
1220+
self.emit_err(type_ann.type_ann.span(), SyntaxError::TS1093);
1221+
}
12191222
}
12201223

12211224
let body = self.parse_fn_block_body(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @flow
2+
class Foo extends Error {
3+
constructor(message: string): void {
4+
super(message);
5+
}
6+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 10,
5+
"end": 100
6+
},
7+
"body": [
8+
{
9+
"type": "ClassDeclaration",
10+
"identifier": {
11+
"type": "Identifier",
12+
"span": {
13+
"start": 16,
14+
"end": 19
15+
},
16+
"ctxt": 0,
17+
"value": "Foo",
18+
"optional": false
19+
},
20+
"declare": false,
21+
"span": {
22+
"start": 10,
23+
"end": 100
24+
},
25+
"ctxt": 0,
26+
"decorators": [],
27+
"body": [
28+
{
29+
"type": "Constructor",
30+
"span": {
31+
"start": 38,
32+
"end": 98
33+
},
34+
"ctxt": 0,
35+
"key": {
36+
"type": "Identifier",
37+
"span": {
38+
"start": 38,
39+
"end": 49
40+
},
41+
"value": "constructor"
42+
},
43+
"params": [
44+
{
45+
"type": "Parameter",
46+
"span": {
47+
"start": 50,
48+
"end": 65
49+
},
50+
"decorators": [],
51+
"pat": {
52+
"type": "Identifier",
53+
"span": {
54+
"start": 50,
55+
"end": 57
56+
},
57+
"ctxt": 0,
58+
"value": "message",
59+
"optional": false,
60+
"typeAnnotation": {
61+
"type": "TsTypeAnnotation",
62+
"span": {
63+
"start": 57,
64+
"end": 65
65+
},
66+
"typeAnnotation": {
67+
"type": "TsKeywordType",
68+
"span": {
69+
"start": 59,
70+
"end": 65
71+
},
72+
"kind": "string"
73+
}
74+
}
75+
}
76+
}
77+
],
78+
"body": {
79+
"type": "BlockStatement",
80+
"span": {
81+
"start": 73,
82+
"end": 98
83+
},
84+
"ctxt": 0,
85+
"stmts": [
86+
{
87+
"type": "ExpressionStatement",
88+
"span": {
89+
"start": 79,
90+
"end": 94
91+
},
92+
"expression": {
93+
"type": "CallExpression",
94+
"span": {
95+
"start": 79,
96+
"end": 93
97+
},
98+
"ctxt": 0,
99+
"callee": {
100+
"type": "Super",
101+
"span": {
102+
"start": 79,
103+
"end": 84
104+
}
105+
},
106+
"arguments": [
107+
{
108+
"spread": null,
109+
"expression": {
110+
"type": "Identifier",
111+
"span": {
112+
"start": 85,
113+
"end": 92
114+
},
115+
"ctxt": 0,
116+
"value": "message",
117+
"optional": false
118+
}
119+
}
120+
],
121+
"typeArguments": null
122+
}
123+
}
124+
]
125+
},
126+
"accessibility": null,
127+
"isOptional": false
128+
}
129+
],
130+
"superClass": {
131+
"type": "Identifier",
132+
"span": {
133+
"start": 28,
134+
"end": 33
135+
},
136+
"ctxt": 0,
137+
"value": "Error",
138+
"optional": false
139+
},
140+
"isAbstract": false,
141+
"typeParams": null,
142+
"superTypeParams": null,
143+
"implements": []
144+
}
145+
],
146+
"interpreter": null
147+
}

0 commit comments

Comments
 (0)