Add support for this type to Babel 6.x

This commit is contained in:
Sam Goldman 2015-12-23 13:54:57 -05:00
parent ae8b7ea352
commit 32c03222bf
7 changed files with 225 additions and 0 deletions

View File

@ -140,6 +140,10 @@ export function StringTypeAnnotation() {
this.push("string");
}
export function ThisTypeAnnotation() {
this.push("this");
}
export function TupleTypeAnnotation(node: Object) {
this.push("[");
this.printJoin(node.types, node, { separator: ", " });

View File

@ -0,0 +1,5 @@
class Foo {
bar(): this {
return this;
}
}

View File

@ -0,0 +1,5 @@
class Foo {
bar(): this {
return this;
}
}

View File

@ -174,6 +174,11 @@ defineType("StringTypeAnnotation", {
}
});
defineType("ThisTypeAnnotation", {
aliases: ["Flow", "FlowBaseAnnotation"],
fields: {}
});
defineType("TupleTypeAnnotation", {
visitor: ["types"],
aliases: ["Flow"],

View File

@ -519,6 +519,11 @@ pp.flowParsePrimaryType = function () {
this.next();
return this.finishNode(node, "NullLiteralTypeAnnotation");
case tt._this:
node.value = this.match(tt._this);
this.next();
return this.finishNode(node, "ThisTypeAnnotation");
default:
if (this.state.type.keyword === "typeof") {
return this.flowParseTypeofType();

View File

@ -0,0 +1 @@
class Foo { bar():this { return this; }}

View File

@ -0,0 +1,200 @@
{
"type": "File",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 40
}
},
"program": {
"type": "Program",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 40
}
},
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 40
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
}
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 10,
"end": 40,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 40
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 39
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 12,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 15
}
},
"name": "bar"
},
"static": false,
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"returnType": {
"type": "TypeAnnotation",
"start": 17,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 22
}
},
"typeAnnotation": {
"type": "ThisTypeAnnotation",
"start": 18,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 1,
"column": 22
}
}
}
},
"body": {
"type": "BlockStatement",
"start": 23,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 23
},
"end": {
"line": 1,
"column": 39
}
},
"body": [
{
"type": "ReturnStatement",
"start": 25,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 37
}
},
"argument": {
"type": "ThisExpression",
"start": 32,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 36
}
}
}
}
]
}
}
]
}
}
]
},
"comments": []
}