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 (_ref) {
  6.   var messages = _ref.messages,
  7.       template = _ref.template,
  8.       t = _ref.types;
  9.  
  10.   var buildForOfArray = template("\n    for (var KEY = 0; KEY < ARR.length; KEY++) BODY;\n  ");
  11.  
  12.   var buildForOfLoose = template("\n    for (var LOOP_OBJECT = OBJECT,\n             IS_ARRAY = Array.isArray(LOOP_OBJECT),\n             INDEX = 0,\n             LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {\n      var ID;\n      if (IS_ARRAY) {\n        if (INDEX >= LOOP_OBJECT.length) break;\n        ID = LOOP_OBJECT[INDEX++];\n      } else {\n        INDEX = LOOP_OBJECT.next();\n        if (INDEX.done) break;\n        ID = INDEX.value;\n      }\n    }\n  ");
  13.  
  14.   var buildForOf = template("\n    var ITERATOR_COMPLETION = true;\n    var ITERATOR_HAD_ERROR_KEY = false;\n    var ITERATOR_ERROR_KEY = undefined;\n    try {\n      for (var ITERATOR_KEY = OBJECT[Symbol.iterator](), STEP_KEY; !(ITERATOR_COMPLETION = (STEP_KEY = ITERATOR_KEY.next()).done); ITERATOR_COMPLETION = true) {\n      }\n    } catch (err) {\n      ITERATOR_HAD_ERROR_KEY = true;\n      ITERATOR_ERROR_KEY = err;\n    } finally {\n      try {\n        if (!ITERATOR_COMPLETION && ITERATOR_KEY.return) {\n          ITERATOR_KEY.return();\n        }\n      } finally {\n        if (ITERATOR_HAD_ERROR_KEY) {\n          throw ITERATOR_ERROR_KEY;\n        }\n      }\n    }\n  ");
  15.  
  16.  
  17.   function _ForOfStatementArray(path) {
  18.     var node = path.node,
  19.         scope = path.scope;
  20.  
  21.     var nodes = [];
  22.     var right = node.right;
  23.  
  24.     if (!t.isIdentifier(right) || !scope.hasBinding(right.name)) {
  25.       var uid = scope.generateUidIdentifier("arr");
  26.       nodes.push(t.variableDeclaration("var", [t.variableDeclarator(uid, right)]));
  27.       right = uid;
  28.     }
  29.  
  30.     var iterationKey = scope.generateUidIdentifier("i");
  31.  
  32.     var loop = buildForOfArray({
  33.       BODY: node.body,
  34.       KEY: iterationKey,
  35.       ARR: right
  36.     });
  37.  
  38.     t.inherits(loop, node);
  39.     t.ensureBlock(loop);
  40.  
  41.     var iterationValue = t.memberExpression(right, iterationKey, true);
  42.  
  43.     var left = node.left;
  44.     if (t.isVariableDeclaration(left)) {
  45.       left.declarations[0].init = iterationValue;
  46.       loop.body.body.unshift(left);
  47.     } else {
  48.       loop.body.body.unshift(t.expressionStatement(t.assignmentExpression("=", left, iterationValue)));
  49.     }
  50.  
  51.     if (path.parentPath.isLabeledStatement()) {
  52.       loop = t.labeledStatement(path.parentPath.node.label, loop);
  53.     }
  54.  
  55.     nodes.push(loop);
  56.  
  57.     return nodes;
  58.   }
  59.  
  60.   return {
  61.     visitor: {
  62.       ForOfStatement: function ForOfStatement(path, state) {
  63.         if (path.get("right").isArrayExpression()) {
  64.           if (path.parentPath.isLabeledStatement()) {
  65.             return path.parentPath.replaceWithMultiple(_ForOfStatementArray(path));
  66.           } else {
  67.             return path.replaceWithMultiple(_ForOfStatementArray(path));
  68.           }
  69.         }
  70.  
  71.         var callback = spec;
  72.         if (state.opts.loose) callback = loose;
  73.  
  74.         var node = path.node;
  75.  
  76.         var build = callback(path, state);
  77.         var declar = build.declar;
  78.         var loop = build.loop;
  79.         var block = loop.body;
  80.  
  81.         path.ensureBlock();
  82.  
  83.         if (declar) {
  84.           block.body.push(declar);
  85.         }
  86.  
  87.         block.body = block.body.concat(node.body.body);
  88.  
  89.         t.inherits(loop, node);
  90.         t.inherits(loop.body, node.body);
  91.  
  92.         if (build.replaceParent) {
  93.           path.parentPath.replaceWithMultiple(build.node);
  94.           path.remove();
  95.         } else {
  96.           path.replaceWithMultiple(build.node);
  97.         }
  98.       }
  99.     }
  100.   };
  101.  
  102.   function loose(path, file) {
  103.     var node = path.node,
  104.         scope = path.scope,
  105.         parent = path.parent;
  106.     var left = node.left;
  107.  
  108.     var declar = void 0,
  109.         id = void 0;
  110.  
  111.     if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) {
  112.       id = left;
  113.     } else if (t.isVariableDeclaration(left)) {
  114.       id = scope.generateUidIdentifier("ref");
  115.       declar = t.variableDeclaration(left.kind, [t.variableDeclarator(left.declarations[0].id, id)]);
  116.     } else {
  117.       throw file.buildCodeFrameError(left, messages.get("unknownForHead", left.type));
  118.     }
  119.  
  120.     var iteratorKey = scope.generateUidIdentifier("iterator");
  121.     var isArrayKey = scope.generateUidIdentifier("isArray");
  122.  
  123.     var loop = buildForOfLoose({
  124.       LOOP_OBJECT: iteratorKey,
  125.       IS_ARRAY: isArrayKey,
  126.       OBJECT: node.right,
  127.       INDEX: scope.generateUidIdentifier("i"),
  128.       ID: id
  129.     });
  130.  
  131.     if (!declar) {
  132.       loop.body.body.shift();
  133.     }
  134.  
  135.     var isLabeledParent = t.isLabeledStatement(parent);
  136.     var labeled = void 0;
  137.  
  138.     if (isLabeledParent) {
  139.       labeled = t.labeledStatement(parent.label, loop);
  140.     }
  141.  
  142.     return {
  143.       replaceParent: isLabeledParent,
  144.       declar: declar,
  145.       node: labeled || loop,
  146.       loop: loop
  147.     };
  148.   }
  149.  
  150.   function spec(path, file) {
  151.     var node = path.node,
  152.         scope = path.scope,
  153.         parent = path.parent;
  154.  
  155.     var left = node.left;
  156.     var declar = void 0;
  157.  
  158.     var stepKey = scope.generateUidIdentifier("step");
  159.     var stepValue = t.memberExpression(stepKey, t.identifier("value"));
  160.  
  161.     if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) {
  162.       declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue));
  163.     } else if (t.isVariableDeclaration(left)) {
  164.       declar = t.variableDeclaration(left.kind, [t.variableDeclarator(left.declarations[0].id, stepValue)]);
  165.     } else {
  166.       throw file.buildCodeFrameError(left, messages.get("unknownForHead", left.type));
  167.     }
  168.  
  169.     var iteratorKey = scope.generateUidIdentifier("iterator");
  170.  
  171.     var template = buildForOf({
  172.       ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
  173.       ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
  174.       ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
  175.       ITERATOR_KEY: iteratorKey,
  176.       STEP_KEY: stepKey,
  177.       OBJECT: node.right,
  178.       BODY: null
  179.     });
  180.  
  181.     var isLabeledParent = t.isLabeledStatement(parent);
  182.  
  183.     var tryBody = template[3].block.body;
  184.     var loop = tryBody[0];
  185.  
  186.     if (isLabeledParent) {
  187.       tryBody[0] = t.labeledStatement(parent.label, loop);
  188.     }
  189.  
  190.     return {
  191.       replaceParent: isLabeledParent,
  192.       declar: declar,
  193.       loop: loop,
  194.       node: template
  195.     };
  196.   }
  197. };
  198.  
  199. 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