index.js.map 6.69 KB
{"version":3,"names":["declare","api","assertVersion","yieldStarVisitor","traverse","visitors","merge","ArrowFunctionExpression","path","skip","YieldExpression","node","state","delegate","asyncIter","t","callExpression","addHelper","argument","environmentVisitor","forAwaitVisitor","ForOfStatement","file","await","build","rewriteForAwait","getAsyncIterator","declar","loop","block","body","ensureBlock","push","length","blockStatement","inherits","replaceParent","parentPath","replaceWithMultiple","visitor","Function","async","generator","remapAsyncToGenerator","wrapAsync","wrapAwait","name","syntaxAsyncGenerators","default","Program"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport remapAsyncToGenerator from \"@babel/helper-remap-async-to-generator\";\nimport syntaxAsyncGenerators from \"@babel/plugin-syntax-async-generators\";\nimport type { NodePath, Visitor } from \"@babel/traverse\";\nimport { traverse, types as t, type PluginPass } from \"@babel/core\";\nimport rewriteForAwait from \"./for-await\";\nimport environmentVisitor from \"@babel/helper-environment-visitor\";\n\nexport default declare(api => {\n  api.assertVersion(7);\n\n  const yieldStarVisitor = traverse.visitors.merge<PluginPass>([\n    {\n      ArrowFunctionExpression(path) {\n        path.skip();\n      },\n\n      YieldExpression({ node }, state) {\n        if (!node.delegate) return;\n        const asyncIter = t.callExpression(state.addHelper(\"asyncIterator\"), [\n          node.argument,\n        ]);\n        node.argument = t.callExpression(\n          state.addHelper(\"asyncGeneratorDelegate\"),\n          process.env.BABEL_8_BREAKING\n            ? [asyncIter]\n            : [asyncIter, state.addHelper(\"awaitAsyncGenerator\")],\n        );\n      },\n    },\n    environmentVisitor,\n  ]);\n\n  const forAwaitVisitor = traverse.visitors.merge<PluginPass>([\n    {\n      ArrowFunctionExpression(path) {\n        path.skip();\n      },\n\n      ForOfStatement(path: NodePath<t.ForOfStatement>, { file }) {\n        const { node } = path;\n        if (!node.await) return;\n\n        const build = rewriteForAwait(path, {\n          getAsyncIterator: file.addHelper(\"asyncIterator\"),\n        });\n\n        const { declar, loop } = build;\n        const block = loop.body as t.BlockStatement;\n\n        // ensure that it's a block so we can take all its statements\n        path.ensureBlock();\n\n        // add the value declaration to the new loop body\n        if (declar) {\n          block.body.push(declar);\n          if (path.node.body.body.length) {\n            block.body.push(t.blockStatement(path.node.body.body));\n          }\n        } else {\n          block.body.push(...path.node.body.body);\n        }\n\n        t.inherits(loop, node);\n        t.inherits(loop.body, node.body);\n\n        if (build.replaceParent) {\n          path.parentPath.replaceWithMultiple(build.node);\n        } else {\n          path.replaceWithMultiple(build.node);\n        }\n      },\n    },\n    environmentVisitor,\n  ]);\n\n  const visitor: Visitor<PluginPass> = {\n    Function(path, state) {\n      if (!path.node.async) return;\n\n      path.traverse(forAwaitVisitor, state);\n\n      if (!path.node.generator) return;\n\n      path.traverse(yieldStarVisitor, state);\n\n      // We don't need to pass the noNewArrows assumption, since\n      // async generators are never arrow functions.\n      remapAsyncToGenerator(path, {\n        wrapAsync: state.addHelper(\"wrapAsyncGenerator\"),\n        wrapAwait: state.addHelper(\"awaitAsyncGenerator\"),\n      });\n    },\n  };\n\n  return {\n    name: \"proposal-async-generator-functions\",\n    inherits: syntaxAsyncGenerators.default,\n\n    visitor: {\n      Program(path, state) {\n        // We need to traverse the ast here (instead of just vising Function\n        // in the top level visitor) because for-await needs to run before the\n        // async-to-generator plugin. This is because for-await is transpiled\n        // using \"await\" expressions, which are then converted to \"yield\".\n        //\n        // This is bad for performance, but plugin ordering will allow as to\n        // directly visit Function in the top level visitor.\n        path.traverse(visitor, state);\n      },\n    },\n  };\n});\n"],"mappings":";;;;;;AAAA;AACA;AACA;AAEA;AACA;AACA;AAAmE,eAEpD,IAAAA,0BAAO,EAACC,GAAG,IAAI;EAC5BA,GAAG,CAACC,aAAa,CAAC,CAAC,CAAC;EAEpB,MAAMC,gBAAgB,GAAGC,cAAQ,CAACC,QAAQ,CAACC,KAAK,CAAa,CAC3D;IACEC,uBAAuB,CAACC,IAAI,EAAE;MAC5BA,IAAI,CAACC,IAAI,EAAE;IACb,CAAC;IAEDC,eAAe,CAAC;M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