From d8b17a9eaa79080cd005d0c81d855c88979bb6ee Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 3 Nov 2015 11:19:04 +0000 Subject: [PATCH] add support for async generator concise methods - fixes #2603 --- packages/babylon/src/parser/expression.js | 1 + .../async-generators/class-method/actual.js | 7 + .../class-method/expected.json | 383 +++++++++++++++++ .../async-generators/object-method/actual.js | 7 + .../object-method/expected.json | 401 ++++++++++++++++++ .../async-generators/options.json | 3 + 6 files changed, 802 insertions(+) create mode 100644 packages/babylon/test/fixtures/experimental/async-generators/class-method/actual.js create mode 100644 packages/babylon/test/fixtures/experimental/async-generators/class-method/expected.json create mode 100644 packages/babylon/test/fixtures/experimental/async-generators/object-method/actual.js create mode 100644 packages/babylon/test/fixtures/experimental/async-generators/object-method/expected.json create mode 100644 packages/babylon/test/fixtures/experimental/async-generators/options.json diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index 2d9b0b1c7e..e987a78465 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -688,6 +688,7 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) { prop.key = asyncId; } else { isAsync = true; + if (this.hasPlugin("asyncGenerators")) isGenerator = this.eat(tt.star); this.parsePropertyName(prop); } } else { diff --git a/packages/babylon/test/fixtures/experimental/async-generators/class-method/actual.js b/packages/babylon/test/fixtures/experimental/async-generators/class-method/actual.js new file mode 100644 index 0000000000..ed837202e0 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/async-generators/class-method/actual.js @@ -0,0 +1,7 @@ +class Query { + async *queryAll(ids) { + for (const id of ids) { + yield await this.query(id); + } + } +} diff --git a/packages/babylon/test/fixtures/experimental/async-generators/class-method/expected.json b/packages/babylon/test/fixtures/experimental/async-generators/class-method/expected.json new file mode 100644 index 0000000000..cf205e9395 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/async-generators/class-method/expected.json @@ -0,0 +1,383 @@ +{ + "type": "File", + "start": 0, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "name": "Query" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 12, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 18, + "end": 128, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 6, + "column": 5 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 25, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "name": "queryAll" + }, + "static": false, + "kind": "method", + "id": null, + "generator": true, + "expression": false, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 34, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "name": "ids" + } + ], + "body": { + "type": "BlockStatement", + "start": 39, + "end": 128, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 6, + "column": 5 + } + }, + "body": [ + { + "type": "ForOfStatement", + "start": 49, + "end": 122, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 5, + "column": 9 + } + }, + "left": { + "type": "VariableDeclaration", + "start": 54, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 60, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 60, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "name": "id" + }, + "init": null + } + ], + "kind": "const" + }, + "right": { + "type": "Identifier", + "start": 66, + "end": 69, + "loc": { + "start": { + "line": 3, + "column": 25 + }, + "end": { + "line": 3, + "column": 28 + } + }, + "name": "ids" + }, + "body": { + "type": "BlockStatement", + "start": 71, + "end": 122, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 5, + "column": 9 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 85, + "end": 112, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 39 + } + }, + "expression": { + "type": "YieldExpression", + "start": 85, + "end": 111, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 38 + } + }, + "delegate": false, + "argument": { + "type": "AwaitExpression", + "start": 91, + "end": 111, + "loc": { + "start": { + "line": 4, + "column": 18 + }, + "end": { + "line": 4, + "column": 38 + } + }, + "all": false, + "argument": { + "type": "CallExpression", + "start": 97, + "end": 111, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 38 + } + }, + "callee": { + "type": "MemberExpression", + "start": 97, + "end": 107, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "object": { + "type": "ThisExpression", + "start": 97, + "end": 101, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 28 + } + } + }, + "property": { + "type": "Identifier", + "start": 102, + "end": 107, + "loc": { + "start": { + "line": 4, + "column": 29 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "name": "query" + }, + "computed": false + }, + "arguments": [ + { + "type": "Identifier", + "start": 108, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 35 + }, + "end": { + "line": 4, + "column": 37 + } + }, + "name": "id" + } + ] + } + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/experimental/async-generators/object-method/actual.js b/packages/babylon/test/fixtures/experimental/async-generators/object-method/actual.js new file mode 100644 index 0000000000..e3350431fd --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/async-generators/object-method/actual.js @@ -0,0 +1,7 @@ +const query = { + async *queryAll(ids) { + for (const id of ids) { + yield await this.query(id); + } + } +}; diff --git a/packages/babylon/test/fixtures/experimental/async-generators/object-method/expected.json b/packages/babylon/test/fixtures/experimental/async-generators/object-method/expected.json new file mode 100644 index 0000000000..75c555d271 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/async-generators/object-method/expected.json @@ -0,0 +1,401 @@ +{ + "type": "File", + "start": 0, + "end": 133, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 133, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 133, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 132, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "name": "query" + }, + "init": { + "type": "ObjectExpression", + "start": 14, + "end": 132, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectMethod", + "start": 20, + "end": 130, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 6, + "column": 5 + } + }, + "method": true, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 27, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "name": "queryAll" + }, + "kind": "method", + "id": null, + "generator": true, + "expression": false, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 36, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "name": "ids" + } + ], + "body": { + "type": "BlockStatement", + "start": 41, + "end": 130, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 6, + "column": 5 + } + }, + "body": [ + { + "type": "ForOfStatement", + "start": 51, + "end": 124, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 5, + "column": 9 + } + }, + "left": { + "type": "VariableDeclaration", + "start": 56, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 62, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 62, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "name": "id" + }, + "init": null + } + ], + "kind": "const" + }, + "right": { + "type": "Identifier", + "start": 68, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 25 + }, + "end": { + "line": 3, + "column": 28 + } + }, + "name": "ids" + }, + "body": { + "type": "BlockStatement", + "start": 73, + "end": 124, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 5, + "column": 9 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 87, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 39 + } + }, + "expression": { + "type": "YieldExpression", + "start": 87, + "end": 113, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 38 + } + }, + "delegate": false, + "argument": { + "type": "AwaitExpression", + "start": 93, + "end": 113, + "loc": { + "start": { + "line": 4, + "column": 18 + }, + "end": { + "line": 4, + "column": 38 + } + }, + "all": false, + "argument": { + "type": "CallExpression", + "start": 99, + "end": 113, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 38 + } + }, + "callee": { + "type": "MemberExpression", + "start": 99, + "end": 109, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "object": { + "type": "ThisExpression", + "start": 99, + "end": 103, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 28 + } + } + }, + "property": { + "type": "Identifier", + "start": 104, + "end": 109, + "loc": { + "start": { + "line": 4, + "column": 29 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "name": "query" + }, + "computed": false + }, + "arguments": [ + { + "type": "Identifier", + "start": 110, + "end": 112, + "loc": { + "start": { + "line": 4, + "column": 35 + }, + "end": { + "line": 4, + "column": 37 + } + }, + "name": "id" + } + ] + } + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/experimental/async-generators/options.json b/packages/babylon/test/fixtures/experimental/async-generators/options.json new file mode 100644 index 0000000000..ea3c5c3b75 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/async-generators/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["asyncFunctions", "asyncGenerators"] +}