diff --git a/packages/babel-generator/src/generators/flow.js b/packages/babel-generator/src/generators/flow.js index 20d29d7cc4..538f51d637 100644 --- a/packages/babel-generator/src/generators/flow.js +++ b/packages/babel-generator/src/generators/flow.js @@ -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: ", " }); diff --git a/packages/babel-generator/test/fixtures/flow/this-types/actual.js b/packages/babel-generator/test/fixtures/flow/this-types/actual.js new file mode 100644 index 0000000000..82df2ec32c --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/this-types/actual.js @@ -0,0 +1,5 @@ +class Foo { + bar(): this { + return this; + } +} diff --git a/packages/babel-generator/test/fixtures/flow/this-types/expected.js b/packages/babel-generator/test/fixtures/flow/this-types/expected.js new file mode 100644 index 0000000000..82df2ec32c --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/this-types/expected.js @@ -0,0 +1,5 @@ +class Foo { + bar(): this { + return this; + } +} diff --git a/packages/babel-types/src/definitions/flow.js b/packages/babel-types/src/definitions/flow.js index 367a2c50fd..c4f3430aa9 100644 --- a/packages/babel-types/src/definitions/flow.js +++ b/packages/babel-types/src/definitions/flow.js @@ -174,6 +174,11 @@ defineType("StringTypeAnnotation", { } }); +defineType("ThisTypeAnnotation", { + aliases: ["Flow", "FlowBaseAnnotation"], + fields: {} +}); + defineType("TupleTypeAnnotation", { visitor: ["types"], aliases: ["Flow"], diff --git a/packages/babylon/src/plugins/flow.js b/packages/babylon/src/plugins/flow.js index a0f32f550c..d5db18957e 100644 --- a/packages/babylon/src/plugins/flow.js +++ b/packages/babylon/src/plugins/flow.js @@ -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(); diff --git a/packages/babylon/test/fixtures/flow/type-annotations/100/actual.js b/packages/babylon/test/fixtures/flow/type-annotations/100/actual.js new file mode 100644 index 0000000000..9807075501 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/type-annotations/100/actual.js @@ -0,0 +1 @@ +class Foo { bar():this { return this; }} diff --git a/packages/babylon/test/fixtures/flow/type-annotations/100/expected.json b/packages/babylon/test/fixtures/flow/type-annotations/100/expected.json new file mode 100644 index 0000000000..5efdb76d0e --- /dev/null +++ b/packages/babylon/test/fixtures/flow/type-annotations/100/expected.json @@ -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": [] +}