allow keyword in object/class property names with Flow type parameters (#145)

This commit is contained in:
Dan Harper 2016-10-14 20:13:00 +01:00 committed by Daniel Tschinder
parent c63c1bc728
commit f7c1af1c1f
12 changed files with 1603 additions and 0 deletions

View File

@ -888,6 +888,17 @@ export default function (instance) {
};
});
// ensure that inside property names, < isn't interpreted as JSX, but as a type parameter
instance.extend("parsePropertyName", function (inner) {
return function (prop) {
const oldInType = this.state.inType;
this.state.inType = true;
const out = inner.call(this, prop);
this.state.inType = oldInType;
return out;
};
});
// ensure that inside flow types, we bypass the jsx parser plugin
instance.extend("readToken", function (inner) {
return function (code) {

View File

@ -0,0 +1,10 @@
class X {
foobar<T>() {}
delete<T>() {}
yield<T>() {}
do<T>() {}
static foobar<T>() {}
static delete<T>() {}
static yield<T>() {}
static do<T>() {}
};

View File

@ -0,0 +1,819 @@
{
"type": "File",
"start": 0,
"end": 166,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 10,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 166,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 10,
"column": 2
}
},
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 165,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 10,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "X"
},
"name": "X"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 165,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 10,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 12,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 8
},
"identifierName": "foobar"
},
"name": "foobar"
},
"static": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 18,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 11
}
},
"params": [
{
"type": "TypeParameter",
"start": 19,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 24,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 16
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassMethod",
"start": 29,
"end": 43,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 16
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 29,
"end": 35,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
},
"identifierName": "delete"
},
"name": "delete"
},
"static": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 35,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 11
}
},
"params": [
{
"type": "TypeParameter",
"start": 36,
"end": 37,
"loc": {
"start": {
"line": 3,
"column": 9
},
"end": {
"line": 3,
"column": 10
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 41,
"end": 43,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 16
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassMethod",
"start": 46,
"end": 59,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 15
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 46,
"end": 51,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 7
},
"identifierName": "yield"
},
"name": "yield"
},
"static": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 51,
"end": 54,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 10
}
},
"params": [
{
"type": "TypeParameter",
"start": 52,
"end": 53,
"loc": {
"start": {
"line": 4,
"column": 8
},
"end": {
"line": 4,
"column": 9
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 57,
"end": 59,
"loc": {
"start": {
"line": 4,
"column": 13
},
"end": {
"line": 4,
"column": 15
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassMethod",
"start": 62,
"end": 72,
"loc": {
"start": {
"line": 5,
"column": 2
},
"end": {
"line": 5,
"column": 12
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 62,
"end": 64,
"loc": {
"start": {
"line": 5,
"column": 2
},
"end": {
"line": 5,
"column": 4
},
"identifierName": "do"
},
"name": "do"
},
"static": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 64,
"end": 67,
"loc": {
"start": {
"line": 5,
"column": 4
},
"end": {
"line": 5,
"column": 7
}
},
"params": [
{
"type": "TypeParameter",
"start": 65,
"end": 66,
"loc": {
"start": {
"line": 5,
"column": 5
},
"end": {
"line": 5,
"column": 6
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 70,
"end": 72,
"loc": {
"start": {
"line": 5,
"column": 10
},
"end": {
"line": 5,
"column": 12
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassMethod",
"start": 75,
"end": 96,
"loc": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 23
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 82,
"end": 88,
"loc": {
"start": {
"line": 6,
"column": 9
},
"end": {
"line": 6,
"column": 15
},
"identifierName": "foobar"
},
"name": "foobar"
},
"static": true,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 88,
"end": 91,
"loc": {
"start": {
"line": 6,
"column": 15
},
"end": {
"line": 6,
"column": 18
}
},
"params": [
{
"type": "TypeParameter",
"start": 89,
"end": 90,
"loc": {
"start": {
"line": 6,
"column": 16
},
"end": {
"line": 6,
"column": 17
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 94,
"end": 96,
"loc": {
"start": {
"line": 6,
"column": 21
},
"end": {
"line": 6,
"column": 23
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassMethod",
"start": 99,
"end": 120,
"loc": {
"start": {
"line": 7,
"column": 2
},
"end": {
"line": 7,
"column": 23
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 106,
"end": 112,
"loc": {
"start": {
"line": 7,
"column": 9
},
"end": {
"line": 7,
"column": 15
},
"identifierName": "delete"
},
"name": "delete"
},
"static": true,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 112,
"end": 115,
"loc": {
"start": {
"line": 7,
"column": 15
},
"end": {
"line": 7,
"column": 18
}
},
"params": [
{
"type": "TypeParameter",
"start": 113,
"end": 114,
"loc": {
"start": {
"line": 7,
"column": 16
},
"end": {
"line": 7,
"column": 17
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 118,
"end": 120,
"loc": {
"start": {
"line": 7,
"column": 21
},
"end": {
"line": 7,
"column": 23
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassMethod",
"start": 123,
"end": 143,
"loc": {
"start": {
"line": 8,
"column": 2
},
"end": {
"line": 8,
"column": 22
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 130,
"end": 135,
"loc": {
"start": {
"line": 8,
"column": 9
},
"end": {
"line": 8,
"column": 14
},
"identifierName": "yield"
},
"name": "yield"
},
"static": true,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 135,
"end": 138,
"loc": {
"start": {
"line": 8,
"column": 14
},
"end": {
"line": 8,
"column": 17
}
},
"params": [
{
"type": "TypeParameter",
"start": 136,
"end": 137,
"loc": {
"start": {
"line": 8,
"column": 15
},
"end": {
"line": 8,
"column": 16
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 141,
"end": 143,
"loc": {
"start": {
"line": 8,
"column": 20
},
"end": {
"line": 8,
"column": 22
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassMethod",
"start": 146,
"end": 163,
"loc": {
"start": {
"line": 9,
"column": 2
},
"end": {
"line": 9,
"column": 19
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 153,
"end": 155,
"loc": {
"start": {
"line": 9,
"column": 9
},
"end": {
"line": 9,
"column": 11
},
"identifierName": "do"
},
"name": "do"
},
"static": true,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 155,
"end": 158,
"loc": {
"start": {
"line": 9,
"column": 11
},
"end": {
"line": 9,
"column": 14
}
},
"params": [
{
"type": "TypeParameter",
"start": 156,
"end": 157,
"loc": {
"start": {
"line": 9,
"column": 12
},
"end": {
"line": 9,
"column": 13
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 161,
"end": 163,
"loc": {
"start": {
"line": 9,
"column": 17
},
"end": {
"line": 9,
"column": 19
}
},
"body": [],
"directives": []
}
}
]
}
},
{
"type": "EmptyStatement",
"start": 165,
"end": 166,
"loc": {
"start": {
"line": 10,
"column": 1
},
"end": {
"line": 10,
"column": 2
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["flow", "jsx"]
}

View File

@ -0,0 +1,3 @@
const s = {
delete<T>(d = <Foo />) {},
}

View File

@ -0,0 +1,275 @@
{
"type": "File",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "s"
},
"name": "s"
},
"init": {
"type": "ObjectExpression",
"start": 10,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 3,
"column": 1
}
},
"properties": [
{
"type": "ObjectMethod",
"start": 14,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 27
}
},
"method": true,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 14,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 8
},
"identifierName": "delete"
},
"name": "delete"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [
{
"type": "AssignmentPattern",
"start": 24,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 23
}
},
"left": {
"type": "Identifier",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "d"
},
"name": "d"
},
"right": {
"type": "JSXElement",
"start": 28,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 2,
"column": 23
}
},
"openingElement": {
"type": "JSXOpeningElement",
"start": 28,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 2,
"column": 23
}
},
"attributes": [],
"name": {
"type": "JSXIdentifier",
"start": 29,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 20
}
},
"name": "Foo"
},
"selfClosing": true
},
"closingElement": null,
"children": []
}
}
],
"body": {
"type": "BlockStatement",
"start": 37,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 25
},
"end": {
"line": 2,
"column": 27
}
},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 20,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 11
}
},
"params": [
{
"type": "TypeParameter",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
}
},
"name": "T"
}
]
}
}
]
}
}
],
"kind": "const"
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["flow", "jsx"]
}

View File

@ -0,0 +1,6 @@
const x = {
foobar<T>() {},
delete<T>() {},
yield<T>() {},
do<T>() {},
};

View File

@ -0,0 +1,465 @@
{
"type": "File",
"start": 0,
"end": 81,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 81,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 2
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 81,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 2
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 6,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "x"
},
"name": "x"
},
"init": {
"type": "ObjectExpression",
"start": 10,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 6,
"column": 1
}
},
"properties": [
{
"type": "ObjectMethod",
"start": 14,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
}
},
"method": true,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 14,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 8
},
"identifierName": "foobar"
},
"name": "foobar"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 26,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 16
}
},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 20,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 11
}
},
"params": [
{
"type": "TypeParameter",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
}
},
"name": "T"
}
]
}
},
{
"type": "ObjectMethod",
"start": 32,
"end": 46,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 16
}
},
"method": true,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 32,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
},
"identifierName": "delete"
},
"name": "delete"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 44,
"end": 46,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 16
}
},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 38,
"end": 41,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 11
}
},
"params": [
{
"type": "TypeParameter",
"start": 39,
"end": 40,
"loc": {
"start": {
"line": 3,
"column": 9
},
"end": {
"line": 3,
"column": 10
}
},
"name": "T"
}
]
}
},
{
"type": "ObjectMethod",
"start": 50,
"end": 63,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 15
}
},
"method": true,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 50,
"end": 55,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 7
},
"identifierName": "yield"
},
"name": "yield"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 61,
"end": 63,
"loc": {
"start": {
"line": 4,
"column": 13
},
"end": {
"line": 4,
"column": 15
}
},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 55,
"end": 58,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 10
}
},
"params": [
{
"type": "TypeParameter",
"start": 56,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 8
},
"end": {
"line": 4,
"column": 9
}
},
"name": "T"
}
]
}
},
{
"type": "ObjectMethod",
"start": 67,
"end": 77,
"loc": {
"start": {
"line": 5,
"column": 2
},
"end": {
"line": 5,
"column": 12
}
},
"method": true,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 67,
"end": 69,
"loc": {
"start": {
"line": 5,
"column": 2
},
"end": {
"line": 5,
"column": 4
},
"identifierName": "do"
},
"name": "do"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 75,
"end": 77,
"loc": {
"start": {
"line": 5,
"column": 10
},
"end": {
"line": 5,
"column": 12
}
},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 69,
"end": 72,
"loc": {
"start": {
"line": 5,
"column": 4
},
"end": {
"line": 5,
"column": 7
}
},
"params": [
{
"type": "TypeParameter",
"start": 70,
"end": 71,
"loc": {
"start": {
"line": 5,
"column": 5
},
"end": {
"line": 5,
"column": 6
}
},
"name": "T"
}
]
}
}
]
}
}
],
"kind": "const"
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["flow", "jsx"]
}

View File

@ -0,0 +1 @@
declare class delete {}

View File

@ -0,0 +1,4 @@
{
"throws": "Unexpected token (1:14)",
"plugins": ["flow", "jsx"]
}