diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/@babel/plugin-proposal-async-generator-functions | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/@babel/plugin-proposal-async-generator-functions')
4 files changed, 353 insertions, 0 deletions
diff --git a/node_modules/@babel/plugin-proposal-async-generator-functions/README.md b/node_modules/@babel/plugin-proposal-async-generator-functions/README.md new file mode 100644 index 00000000..543d5bfc --- /dev/null +++ b/node_modules/@babel/plugin-proposal-async-generator-functions/README.md @@ -0,0 +1,107 @@ +# @babel/plugin-proposal-async-generator-functions + +> Turn async generator functions and for-await statements to ES2015 generators + +## Example + +**In** + +```javascript +async function* agf() { + await 1; + yield 2; +} +``` + +**Out** + +```javascript +var _asyncGenerator = ... + +let agf = (() => { + var _ref = _asyncGenerator.wrap(function* () { + yield _asyncGenerator.await(1); + yield 2; + }); + + return function agf() { + return _ref.apply(this, arguments); + }; +})(); +``` + +For await example + +```js +async function f() { + for await (let x of y) { + g(x); + } +} +``` + +**Example Usage** + +```js +async function* genAnswers() { + var stream = [ Promise.resolve(4), Promise.resolve(9), Promise.resolve(12) ]; + var total = 0; + for await (let val of stream) { + total += await val; + yield total; + } +} + +function forEach(ai, fn) { + return ai.next().then(function (r) { + if (!r.done) { + fn(r); + return forEach(ai, fn); + } + }); +} + +var output = 0; +forEach(genAnswers(), function(val) { output += val.value }) +.then(function () { + console.log(output); // 42 +}); +``` + +[Try it Out in the REPL](https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=stage-3&code=async%20function*%20genAnswers()%20%7B%0A%20%20var%20stream%20%3D%20%5B%20Promise.resolve(4)%2C%20Promise.resolve(9)%2C%20Promise.resolve(12)%20%5D%3B%0A%20%20var%20total%20%3D%200%3B%0A%20%20for%20await%20(let%20val%20of%20stream)%20%7B%0A%20%20%20%20total%20%2B%3D%20await%20val%3B%0A%20%20%20%20yield%20total%3B%0A%20%20%7D%0A%7D%0A%0Afunction%20forEach(ai%2C%20fn)%20%7B%0A%20%20return%20ai.next().then(function%20(r)%20%7B%0A%20%20%20%20if%20(!r.done)%20%7B%0A%20%20%20%20%20%20fn(r)%3B%0A%20%20%20%20%20%20return%20forEach(ai%2C%20fn)%3B%0A%20%20%20%20%7D%0A%20%20%7D)%3B%0A%7D%0A%0Avar%20output%20%3D%200%3B%0AforEach(genAnswers()%2C%20function(val)%20%7B%20output%20%2B%3D%20val.value%20%7D)%0A.then(function%20()%20%7B%0A%20%20console.log(output)%3B%20%2F%2F%2042%0A%7D)%3B&experimental=true&loose=false&spec=false&playground=true&stage=0) + +## Installation + +```sh +npm install --save-dev @babel/plugin-proposal-async-generator-functions +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["@babel/plugin-proposal-async-generator-functions"] +} +``` + +### Via CLI + +```sh +babel --plugins @babel/plugin-proposal-async-generator-functions script.js +``` + +### Via Node API + +```javascript +require("@babel/core").transform("code", { + plugins: ["@babel/plugin-proposal-async-generator-functions"] +}); +``` + +## References + +* [Proposal: Asynchronous iteration for ECMAScript](https://github.com/tc39/proposal-async-iteration) diff --git a/node_modules/@babel/plugin-proposal-async-generator-functions/lib/for-await.js b/node_modules/@babel/plugin-proposal-async-generator-functions/lib/for-await.js new file mode 100644 index 00000000..54571bc3 --- /dev/null +++ b/node_modules/@babel/plugin-proposal-async-generator-functions/lib/for-await.js @@ -0,0 +1,95 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = _default; + +function _core() { + const data = require("@babel/core"); + + _core = function _core() { + return data; + }; + + return data; +} + +const buildForAwait = (0, _core().template)(` + async function wrapper() { + var ITERATOR_COMPLETION = true; + var ITERATOR_HAD_ERROR_KEY = false; + var ITERATOR_ERROR_KEY; + try { + for ( + var ITERATOR_KEY = GET_ITERATOR(OBJECT), STEP_KEY, STEP_VALUE; + ( + STEP_KEY = await ITERATOR_KEY.next(), + ITERATOR_COMPLETION = STEP_KEY.done, + STEP_VALUE = await STEP_KEY.value, + !ITERATOR_COMPLETION + ); + ITERATOR_COMPLETION = true) { + } + } catch (err) { + ITERATOR_HAD_ERROR_KEY = true; + ITERATOR_ERROR_KEY = err; + } finally { + try { + if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) { + await ITERATOR_KEY.return(); + } + } finally { + if (ITERATOR_HAD_ERROR_KEY) { + throw ITERATOR_ERROR_KEY; + } + } + } + } +`); + +function _default(path, { + getAsyncIterator +}) { + const node = path.node, + scope = path.scope, + parent = path.parent; + const stepKey = scope.generateUidIdentifier("step"); + const stepValue = scope.generateUidIdentifier("value"); + const left = node.left; + let declar; + + if (_core().types.isIdentifier(left) || _core().types.isPattern(left) || _core().types.isMemberExpression(left)) { + declar = _core().types.expressionStatement(_core().types.assignmentExpression("=", left, stepValue)); + } else if (_core().types.isVariableDeclaration(left)) { + declar = _core().types.variableDeclaration(left.kind, [_core().types.variableDeclarator(left.declarations[0].id, stepValue)]); + } + + let template = buildForAwait({ + ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"), + ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"), + ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"), + ITERATOR_KEY: scope.generateUidIdentifier("iterator"), + GET_ITERATOR: getAsyncIterator, + OBJECT: node.right, + STEP_VALUE: stepValue, + STEP_KEY: stepKey + }); + template = template.body.body; + + const isLabeledParent = _core().types.isLabeledStatement(parent); + + const tryBody = template[3].block.body; + const loop = tryBody[0]; + + if (isLabeledParent) { + tryBody[0] = _core().types.labeledStatement(parent.label, loop); + } + + return { + replaceParent: isLabeledParent, + node: template, + declar, + loop + }; +}
\ No newline at end of file diff --git a/node_modules/@babel/plugin-proposal-async-generator-functions/lib/index.js b/node_modules/@babel/plugin-proposal-async-generator-functions/lib/index.js new file mode 100644 index 00000000..534575b8 --- /dev/null +++ b/node_modules/@babel/plugin-proposal-async-generator-functions/lib/index.js @@ -0,0 +1,128 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +function _helperPluginUtils() { + const data = require("@babel/helper-plugin-utils"); + + _helperPluginUtils = function _helperPluginUtils() { + return data; + }; + + return data; +} + +function _helperRemapAsyncToGenerator() { + const data = _interopRequireDefault(require("@babel/helper-remap-async-to-generator")); + + _helperRemapAsyncToGenerator = function _helperRemapAsyncToGenerator() { + return data; + }; + + return data; +} + +function _pluginSyntaxAsyncGenerators() { + const data = _interopRequireDefault(require("@babel/plugin-syntax-async-generators")); + + _pluginSyntaxAsyncGenerators = function _pluginSyntaxAsyncGenerators() { + return data; + }; + + return data; +} + +function _core() { + const data = require("@babel/core"); + + _core = function _core() { + return data; + }; + + return data; +} + +var _forAwait = _interopRequireDefault(require("./for-await")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var _default = (0, _helperPluginUtils().declare)(api => { + api.assertVersion(7); + const yieldStarVisitor = { + Function(path) { + path.skip(); + }, + + YieldExpression({ + node + }, state) { + if (!node.delegate) return; + const callee = state.addHelper("asyncGeneratorDelegate"); + node.argument = _core().types.callExpression(callee, [_core().types.callExpression(state.addHelper("asyncIterator"), [node.argument]), state.addHelper("awaitAsyncGenerator")]); + } + + }; + const forAwaitVisitor = { + Function(path) { + path.skip(); + }, + + ForOfStatement(path, { + file + }) { + const node = path.node; + if (!node.await) return; + const build = (0, _forAwait.default)(path, { + getAsyncIterator: file.addHelper("asyncIterator") + }); + const declar = build.declar, + loop = build.loop; + const block = loop.body; + path.ensureBlock(); + + if (declar) { + block.body.push(declar); + } + + block.body = block.body.concat(node.body.body); + + _core().types.inherits(loop, node); + + _core().types.inherits(loop.body, node.body); + + if (build.replaceParent) { + path.parentPath.replaceWithMultiple(build.node); + } else { + path.replaceWithMultiple(build.node); + } + } + + }; + const visitor = { + Function(path, state) { + if (!path.node.async) return; + path.traverse(forAwaitVisitor, state); + if (!path.node.generator) return; + path.traverse(yieldStarVisitor, state); + (0, _helperRemapAsyncToGenerator().default)(path, { + wrapAsync: state.addHelper("wrapAsyncGenerator"), + wrapAwait: state.addHelper("awaitAsyncGenerator") + }); + } + + }; + return { + inherits: _pluginSyntaxAsyncGenerators().default, + visitor: { + Program(path, state) { + path.traverse(visitor, state); + } + + } + }; +}); + +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/plugin-proposal-async-generator-functions/package.json b/node_modules/@babel/plugin-proposal-async-generator-functions/package.json new file mode 100644 index 00000000..6afb5c38 --- /dev/null +++ b/node_modules/@babel/plugin-proposal-async-generator-functions/package.json @@ -0,0 +1,23 @@ +{ + "name": "@babel/plugin-proposal-async-generator-functions", + "version": "7.0.0-beta.47", + "description": "Turn async generator functions into ES2015 generators", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-async-generator-functions", + "license": "MIT", + "main": "lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "7.0.0-beta.47", + "@babel/helper-remap-async-to-generator": "7.0.0-beta.47", + "@babel/plugin-syntax-async-generators": "7.0.0-beta.47" + }, + "peerDependencies": { + "@babel/core": "7.0.0-beta.47" + }, + "devDependencies": { + "@babel/core": "7.0.0-beta.47", + "@babel/helper-plugin-test-runner": "7.0.0-beta.47" + } +} |
