Skip to content

Commit

Permalink
Remove jsx context when parsing arrow functions (babel#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism authored and danez committed May 10, 2017
1 parent 4a29ccd commit 67db275
Show file tree
Hide file tree
Showing 5 changed files with 695 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint max-len: 0 */

import { types as tt } from "../tokenizer/types";
import { types as ct } from "../tokenizer/context";
import Parser from "../parser";

const primitiveTypes = [
Expand Down Expand Up @@ -1392,6 +1391,12 @@ export default function (instance) {
} catch (err) {
if (err instanceof SyntaxError) {
this.state = state;

// Remove `tc.j_expr` and `tc.j_oTag` from context added
// by parsing `jsxTagStart` to stop the JSX plugin from
// messing with the tokens
this.state.context.length -= 2;

jsxError = err;
} else {
// istanbul ignore next: no such error is expected
Expand All @@ -1400,9 +1405,6 @@ export default function (instance) {
}
}

// Need to push something onto the context to stop
// the JSX plugin from messing with the tokens
this.state.context.push(ct.parenExpression);
if (jsxError != null || this.isRelational("<")) {
let arrowExpression;
let typeParameters;
Expand All @@ -1428,7 +1430,6 @@ export default function (instance) {
);
}
}
this.state.context.pop();

return inner.apply(this, args);
};
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/flow/type-generics/1/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const functionReturningIdentityAsAField = () => ({ id: <T>(value: T): T => value });
346 changes: 346 additions & 0 deletions test/fixtures/flow/type-generics/1/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
{
"type": "File",
"start": 0,
"end": 84,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 84
}
},
"program": {
"type": "Program",
"start": 0,
"end": 84,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 84
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 84,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 84
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 83,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 83
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 39
},
"identifierName": "functionReturningIdentityAsAField"
},
"name": "functionReturningIdentityAsAField"
},
"init": {
"type": "ArrowFunctionExpression",
"start": 42,
"end": 83,
"loc": {
"start": {
"line": 1,
"column": 42
},
"end": {
"line": 1,
"column": 83
}
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [],
"body": {
"type": "ObjectExpression",
"start": 49,
"end": 82,
"loc": {
"start": {
"line": 1,
"column": 49
},
"end": {
"line": 1,
"column": 82
}
},
"properties": [
{
"type": "ObjectProperty",
"start": 51,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 51
},
"end": {
"line": 1,
"column": 80
}
},
"method": false,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 51,
"end": 53,
"loc": {
"start": {
"line": 1,
"column": 51
},
"end": {
"line": 1,
"column": 53
},
"identifierName": "id"
},
"name": "id"
},
"value": {
"type": "ArrowFunctionExpression",
"start": 55,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 55
},
"end": {
"line": 1,
"column": 80
}
},
"returnType": {
"type": "TypeAnnotation",
"start": 68,
"end": 71,
"loc": {
"start": {
"line": 1,
"column": 68
},
"end": {
"line": 1,
"column": 71
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 70,
"end": 71,
"loc": {
"start": {
"line": 1,
"column": 70
},
"end": {
"line": 1,
"column": 71
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 70,
"end": 71,
"loc": {
"start": {
"line": 1,
"column": 70
},
"end": {
"line": 1,
"column": 71
},
"identifierName": "T"
},
"name": "T"
}
},
"predicate": null
},
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "Identifier",
"start": 59,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 59
},
"end": {
"line": 1,
"column": 67
},
"identifierName": "value"
},
"name": "value",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 64,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 64
},
"end": {
"line": 1,
"column": 67
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 66,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 66
},
"end": {
"line": 1,
"column": 67
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 66,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 66
},
"end": {
"line": 1,
"column": 67
},
"identifierName": "T"
},
"name": "T"
}
}
}
}
],
"body": {
"type": "Identifier",
"start": 75,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 75
},
"end": {
"line": 1,
"column": 80
},
"identifierName": "value"
},
"name": "value"
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 55,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 55
},
"end": {
"line": 1,
"column": 58
}
},
"params": [
{
"type": "TypeParameter",
"start": 56,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 56
},
"end": {
"line": 1,
"column": 57
}
},
"name": "T",
"variance": null
}
]
}
}
}
],
"extra": {
"parenthesized": true,
"parenStart": 48
}
}
}
}
],
"kind": "const"
}
],
"directives": []
}
}
2 changes: 2 additions & 0 deletions test/fixtures/flow/type-generics/2/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const identity = <T>(t: T): T => t;
const a = 1;
Loading

0 comments on commit 67db275

Please sign in to comment.