BVB Source Codes

reactide Show index.js Source code

Return Download reactide: download index.js Source code - Download reactide Source code - Type:.js
  1. "use strict";
  2.  
  3. exports.__esModule = true;
  4.  
  5. exports.default = function (path, file, helpers) {
  6.   if (!helpers) {
  7.     helpers = { wrapAsync: file };
  8.     file = null;
  9.   }
  10.   path.traverse(awaitVisitor, {
  11.     file: file,
  12.     wrapAwait: helpers.wrapAwait
  13.   });
  14.  
  15.   if (path.isClassMethod() || path.isObjectMethod()) {
  16.     classOrObjectMethod(path, helpers.wrapAsync);
  17.   } else {
  18.     plainFunction(path, helpers.wrapAsync);
  19.   }
  20. };
  21.  
  22. var _babelHelperFunctionName = require("babel-helper-function-name");
  23.  
  24. var _babelHelperFunctionName2 = _interopRequireDefault(_babelHelperFunctionName);
  25.  
  26. var _babelTemplate = require("babel-template");
  27.  
  28. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  29.  
  30. var _babelTypes = require("babel-types");
  31.  
  32. var t = _interopRequireWildcard(_babelTypes);
  33.  
  34. var _forAwait = require("./for-await");
  35.  
  36. var _forAwait2 = _interopRequireDefault(_forAwait);
  37.  
  38. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  39.  
  40. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  41.  
  42. var buildWrapper = (0, _babelTemplate2.default)("\n  (() => {\n    var REF = FUNCTION;\n    return function NAME(PARAMS) {\n      return REF.apply(this, arguments);\n    };\n  })\n");
  43.  
  44. var namedBuildWrapper = (0, _babelTemplate2.default)("\n  (() => {\n    var REF = FUNCTION;\n    function NAME(PARAMS) {\n      return REF.apply(this, arguments);\n    }\n    return NAME;\n  })\n");
  45.  
  46. var awaitVisitor = {
  47.   Function: function Function(path) {
  48.     if (path.isArrowFunctionExpression() && !path.node.async) {
  49.       path.arrowFunctionToShadowed();
  50.       return;
  51.     }
  52.     path.skip();
  53.   },
  54.   AwaitExpression: function AwaitExpression(_ref, _ref2) {
  55.     var node = _ref.node;
  56.     var wrapAwait = _ref2.wrapAwait;
  57.  
  58.     node.type = "YieldExpression";
  59.     if (wrapAwait) {
  60.       node.argument = t.callExpression(wrapAwait, [node.argument]);
  61.     }
  62.   },
  63.   ForAwaitStatement: function ForAwaitStatement(path, _ref3) {
  64.     var file = _ref3.file,
  65.         wrapAwait = _ref3.wrapAwait;
  66.     var node = path.node;
  67.  
  68.  
  69.     var build = (0, _forAwait2.default)(path, {
  70.       getAsyncIterator: file.addHelper("asyncIterator"),
  71.       wrapAwait: wrapAwait
  72.     });
  73.  
  74.     var declar = build.declar,
  75.         loop = build.loop;
  76.  
  77.     var block = loop.body;
  78.  
  79.     path.ensureBlock();
  80.  
  81.     if (declar) {
  82.       block.body.push(declar);
  83.     }
  84.  
  85.     block.body = block.body.concat(node.body.body);
  86.  
  87.     t.inherits(loop, node);
  88.     t.inherits(loop.body, node.body);
  89.  
  90.     if (build.replaceParent) {
  91.       path.parentPath.replaceWithMultiple(build.node);
  92.       path.remove();
  93.     } else {
  94.       path.replaceWithMultiple(build.node);
  95.     }
  96.   }
  97. };
  98.  
  99. function classOrObjectMethod(path, callId) {
  100.   var node = path.node;
  101.   var body = node.body;
  102.  
  103.   node.async = false;
  104.  
  105.   var container = t.functionExpression(null, [], t.blockStatement(body.body), true);
  106.   container.shadow = true;
  107.   body.body = [t.returnStatement(t.callExpression(t.callExpression(callId, [container]), []))];
  108.  
  109.   node.generator = false;
  110. }
  111.  
  112. function plainFunction(path, callId) {
  113.   var node = path.node;
  114.   var isDeclaration = path.isFunctionDeclaration();
  115.   var asyncFnId = node.id;
  116.   var wrapper = buildWrapper;
  117.  
  118.   if (path.isArrowFunctionExpression()) {
  119.     path.arrowFunctionToShadowed();
  120.   } else if (!isDeclaration && asyncFnId) {
  121.     wrapper = namedBuildWrapper;
  122.   }
  123.  
  124.   node.async = false;
  125.   node.generator = true;
  126.  
  127.   node.id = null;
  128.  
  129.   if (isDeclaration) {
  130.     node.type = "FunctionExpression";
  131.   }
  132.  
  133.   var built = t.callExpression(callId, [node]);
  134.   var container = wrapper({
  135.     NAME: asyncFnId,
  136.     REF: path.scope.generateUidIdentifier("ref"),
  137.     FUNCTION: built,
  138.     PARAMS: node.params.reduce(function (acc, param) {
  139.       acc.done = acc.done || t.isAssignmentPattern(param) || t.isRestElement(param);
  140.  
  141.       if (!acc.done) {
  142.         acc.params.push(path.scope.generateUidIdentifier("x"));
  143.       }
  144.  
  145.       return acc;
  146.     }, {
  147.       params: [],
  148.       done: false
  149.     }).params
  150.   }).expression;
  151.  
  152.   if (isDeclaration) {
  153.     var declar = t.variableDeclaration("let", [t.variableDeclarator(t.identifier(asyncFnId.name), t.callExpression(container, []))]);
  154.     declar._blockHoist = true;
  155.  
  156.     path.replaceWith(declar);
  157.   } else {
  158.     var retFunction = container.body.body[1].argument;
  159.     if (!asyncFnId) {
  160.       (0, _babelHelperFunctionName2.default)({
  161.         node: retFunction,
  162.         parent: path.parent,
  163.         scope: path.scope
  164.       });
  165.     }
  166.  
  167.     if (!retFunction || retFunction.id || node.params.length) {
  168.       path.replaceWith(t.callExpression(container, []));
  169.     } else {
  170.       path.replaceWith(built);
  171.     }
  172.   }
  173. }
  174.  
  175. module.exports = exports["default"];
downloadindex.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top