Skip to content

Commit 67db275

Browse files
existentialismdanez
authored andcommitted
Remove jsx context when parsing arrow functions (babel#475)
1 parent 4a29ccd commit 67db275

File tree

5 files changed

+695
-5
lines changed

5 files changed

+695
-5
lines changed

src/plugins/flow.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint max-len: 0 */
22

33
import { types as tt } from "../tokenizer/types";
4-
import { types as ct } from "../tokenizer/context";
54
import Parser from "../parser";
65

76
const primitiveTypes = [
@@ -1392,6 +1391,12 @@ export default function (instance) {
13921391
} catch (err) {
13931392
if (err instanceof SyntaxError) {
13941393
this.state = state;
1394+
1395+
// Remove `tc.j_expr` and `tc.j_oTag` from context added
1396+
// by parsing `jsxTagStart` to stop the JSX plugin from
1397+
// messing with the tokens
1398+
this.state.context.length -= 2;
1399+
13951400
jsxError = err;
13961401
} else {
13971402
// istanbul ignore next: no such error is expected
@@ -1400,9 +1405,6 @@ export default function (instance) {
14001405
}
14011406
}
14021407

1403-
// Need to push something onto the context to stop
1404-
// the JSX plugin from messing with the tokens
1405-
this.state.context.push(ct.parenExpression);
14061408
if (jsxError != null || this.isRelational("<")) {
14071409
let arrowExpression;
14081410
let typeParameters;
@@ -1428,7 +1430,6 @@ export default function (instance) {
14281430
);
14291431
}
14301432
}
1431-
this.state.context.pop();
14321433

14331434
return inner.apply(this, args);
14341435
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const functionReturningIdentityAsAField = () => ({ id: <T>(value: T): T => value });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 84,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 84
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 84,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 1,
26+
"column": 84
27+
}
28+
},
29+
"sourceType": "module",
30+
"body": [
31+
{
32+
"type": "VariableDeclaration",
33+
"start": 0,
34+
"end": 84,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 0
39+
},
40+
"end": {
41+
"line": 1,
42+
"column": 84
43+
}
44+
},
45+
"declarations": [
46+
{
47+
"type": "VariableDeclarator",
48+
"start": 6,
49+
"end": 83,
50+
"loc": {
51+
"start": {
52+
"line": 1,
53+
"column": 6
54+
},
55+
"end": {
56+
"line": 1,
57+
"column": 83
58+
}
59+
},
60+
"id": {
61+
"type": "Identifier",
62+
"start": 6,
63+
"end": 39,
64+
"loc": {
65+
"start": {
66+
"line": 1,
67+
"column": 6
68+
},
69+
"end": {
70+
"line": 1,
71+
"column": 39
72+
},
73+
"identifierName": "functionReturningIdentityAsAField"
74+
},
75+
"name": "functionReturningIdentityAsAField"
76+
},
77+
"init": {
78+
"type": "ArrowFunctionExpression",
79+
"start": 42,
80+
"end": 83,
81+
"loc": {
82+
"start": {
83+
"line": 1,
84+
"column": 42
85+
},
86+
"end": {
87+
"line": 1,
88+
"column": 83
89+
}
90+
},
91+
"id": null,
92+
"generator": false,
93+
"expression": true,
94+
"async": false,
95+
"params": [],
96+
"body": {
97+
"type": "ObjectExpression",
98+
"start": 49,
99+
"end": 82,
100+
"loc": {
101+
"start": {
102+
"line": 1,
103+
"column": 49
104+
},
105+
"end": {
106+
"line": 1,
107+
"column": 82
108+
}
109+
},
110+
"properties": [
111+
{
112+
"type": "ObjectProperty",
113+
"start": 51,
114+
"end": 80,
115+
"loc": {
116+
"start": {
117+
"line": 1,
118+
"column": 51
119+
},
120+
"end": {
121+
"line": 1,
122+
"column": 80
123+
}
124+
},
125+
"method": false,
126+
"shorthand": false,
127+
"computed": false,
128+
"key": {
129+
"type": "Identifier",
130+
"start": 51,
131+
"end": 53,
132+
"loc": {
133+
"start": {
134+
"line": 1,
135+
"column": 51
136+
},
137+
"end": {
138+
"line": 1,
139+
"column": 53
140+
},
141+
"identifierName": "id"
142+
},
143+
"name": "id"
144+
},
145+
"value": {
146+
"type": "ArrowFunctionExpression",
147+
"start": 55,
148+
"end": 80,
149+
"loc": {
150+
"start": {
151+
"line": 1,
152+
"column": 55
153+
},
154+
"end": {
155+
"line": 1,
156+
"column": 80
157+
}
158+
},
159+
"returnType": {
160+
"type": "TypeAnnotation",
161+
"start": 68,
162+
"end": 71,
163+
"loc": {
164+
"start": {
165+
"line": 1,
166+
"column": 68
167+
},
168+
"end": {
169+
"line": 1,
170+
"column": 71
171+
}
172+
},
173+
"typeAnnotation": {
174+
"type": "GenericTypeAnnotation",
175+
"start": 70,
176+
"end": 71,
177+
"loc": {
178+
"start": {
179+
"line": 1,
180+
"column": 70
181+
},
182+
"end": {
183+
"line": 1,
184+
"column": 71
185+
}
186+
},
187+
"typeParameters": null,
188+
"id": {
189+
"type": "Identifier",
190+
"start": 70,
191+
"end": 71,
192+
"loc": {
193+
"start": {
194+
"line": 1,
195+
"column": 70
196+
},
197+
"end": {
198+
"line": 1,
199+
"column": 71
200+
},
201+
"identifierName": "T"
202+
},
203+
"name": "T"
204+
}
205+
},
206+
"predicate": null
207+
},
208+
"id": null,
209+
"generator": false,
210+
"expression": true,
211+
"async": false,
212+
"params": [
213+
{
214+
"type": "Identifier",
215+
"start": 59,
216+
"end": 67,
217+
"loc": {
218+
"start": {
219+
"line": 1,
220+
"column": 59
221+
},
222+
"end": {
223+
"line": 1,
224+
"column": 67
225+
},
226+
"identifierName": "value"
227+
},
228+
"name": "value",
229+
"typeAnnotation": {
230+
"type": "TypeAnnotation",
231+
"start": 64,
232+
"end": 67,
233+
"loc": {
234+
"start": {
235+
"line": 1,
236+
"column": 64
237+
},
238+
"end": {
239+
"line": 1,
240+
"column": 67
241+
}
242+
},
243+
"typeAnnotation": {
244+
"type": "GenericTypeAnnotation",
245+
"start": 66,
246+
"end": 67,
247+
"loc": {
248+
"start": {
249+
"line": 1,
250+
"column": 66
251+
},
252+
"end": {
253+
"line": 1,
254+
"column": 67
255+
}
256+
},
257+
"typeParameters": null,
258+
"id": {
259+
"type": "Identifier",
260+
"start": 66,
261+
"end": 67,
262+
"loc": {
263+
"start": {
264+
"line": 1,
265+
"column": 66
266+
},
267+
"end": {
268+
"line": 1,
269+
"column": 67
270+
},
271+
"identifierName": "T"
272+
},
273+
"name": "T"
274+
}
275+
}
276+
}
277+
}
278+
],
279+
"body": {
280+
"type": "Identifier",
281+
"start": 75,
282+
"end": 80,
283+
"loc": {
284+
"start": {
285+
"line": 1,
286+
"column": 75
287+
},
288+
"end": {
289+
"line": 1,
290+
"column": 80
291+
},
292+
"identifierName": "value"
293+
},
294+
"name": "value"
295+
},
296+
"typeParameters": {
297+
"type": "TypeParameterDeclaration",
298+
"start": 55,
299+
"end": 58,
300+
"loc": {
301+
"start": {
302+
"line": 1,
303+
"column": 55
304+
},
305+
"end": {
306+
"line": 1,
307+
"column": 58
308+
}
309+
},
310+
"params": [
311+
{
312+
"type": "TypeParameter",
313+
"start": 56,
314+
"end": 57,
315+
"loc": {
316+
"start": {
317+
"line": 1,
318+
"column": 56
319+
},
320+
"end": {
321+
"line": 1,
322+
"column": 57
323+
}
324+
},
325+
"name": "T",
326+
"variance": null
327+
}
328+
]
329+
}
330+
}
331+
}
332+
],
333+
"extra": {
334+
"parenthesized": true,
335+
"parenStart": 48
336+
}
337+
}
338+
}
339+
}
340+
],
341+
"kind": "const"
342+
}
343+
],
344+
"directives": []
345+
}
346+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const identity = <T>(t: T): T => t;
2+
const a = 1;

0 commit comments

Comments
 (0)