BVB Source Codes

reactide Show rest.js Source code

Return Download reactide: download rest.js Source code - Download reactide Source code - Type:.js
  1. "use strict";
  2.  
  3. exports.__esModule = true;
  4. exports.visitor = undefined;
  5.  
  6. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  7.  
  8. var _getIterator3 = _interopRequireDefault(_getIterator2);
  9.  
  10. var _babelTemplate = require("babel-template");
  11.  
  12. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  13.  
  14. var _babelTypes = require("babel-types");
  15.  
  16. var t = _interopRequireWildcard(_babelTypes);
  17.  
  18. 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; } }
  19.  
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21.  
  22. var buildRest = (0, _babelTemplate2.default)("\n  for (var LEN = ARGUMENTS.length,\n           ARRAY = Array(ARRAY_LEN),\n           KEY = START;\n       KEY < LEN;\n       KEY++) {\n    ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];\n  }\n");
  23.  
  24. var restIndex = (0, _babelTemplate2.default)("\n  ARGUMENTS.length <= INDEX ? undefined : ARGUMENTS[INDEX]\n");
  25.  
  26. var restIndexImpure = (0, _babelTemplate2.default)("\n  REF = INDEX, ARGUMENTS.length <= REF ? undefined : ARGUMENTS[REF]\n");
  27.  
  28. var restLength = (0, _babelTemplate2.default)("\n  ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET\n");
  29.  
  30. var memberExpressionOptimisationVisitor = {
  31.   Scope: function Scope(path, state) {
  32.     if (!path.scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
  33.       path.skip();
  34.     }
  35.   },
  36.   Flow: function Flow(path) {
  37.     if (path.isTypeCastExpression()) return;
  38.  
  39.     path.skip();
  40.   },
  41.  
  42.  
  43.   "Function|ClassProperty": function FunctionClassProperty(path, state) {
  44.     var oldNoOptimise = state.noOptimise;
  45.     state.noOptimise = true;
  46.     path.traverse(memberExpressionOptimisationVisitor, state);
  47.     state.noOptimise = oldNoOptimise;
  48.  
  49.     path.skip();
  50.   },
  51.  
  52.   ReferencedIdentifier: function ReferencedIdentifier(path, state) {
  53.     var node = path.node;
  54.  
  55.     if (node.name === "arguments") {
  56.       state.deopted = true;
  57.     }
  58.  
  59.     if (node.name !== state.name) return;
  60.  
  61.     if (state.noOptimise) {
  62.       state.deopted = true;
  63.     } else {
  64.       var parentPath = path.parentPath;
  65.  
  66.       if (parentPath.listKey === "params" && parentPath.key < state.offset) {
  67.         return;
  68.       }
  69.  
  70.       if (parentPath.isMemberExpression({ object: node })) {
  71.         var grandparentPath = parentPath.parentPath;
  72.  
  73.         var argsOptEligible = !state.deopted && !(grandparentPath.isAssignmentExpression() && parentPath.node === grandparentPath.node.left || grandparentPath.isLVal() || grandparentPath.isForXStatement() || grandparentPath.isUpdateExpression() || grandparentPath.isUnaryExpression({ operator: "delete" }) || (grandparentPath.isCallExpression() || grandparentPath.isNewExpression()) && parentPath.node === grandparentPath.node.callee);
  74.  
  75.         if (argsOptEligible) {
  76.           if (parentPath.node.computed) {
  77.             if (parentPath.get("property").isBaseType("number")) {
  78.               state.candidates.push({ cause: "indexGetter", path: path });
  79.               return;
  80.             }
  81.           } else if (parentPath.node.property.name === "length") {
  82.               state.candidates.push({ cause: "lengthGetter", path: path });
  83.               return;
  84.             }
  85.         }
  86.       }
  87.  
  88.       if (state.offset === 0 && parentPath.isSpreadElement()) {
  89.         var call = parentPath.parentPath;
  90.         if (call.isCallExpression() && call.node.arguments.length === 1) {
  91.           state.candidates.push({ cause: "argSpread", path: path });
  92.           return;
  93.         }
  94.       }
  95.  
  96.       state.references.push(path);
  97.     }
  98.   },
  99.   BindingIdentifier: function BindingIdentifier(_ref, state) {
  100.     var node = _ref.node;
  101.  
  102.     if (node.name === state.name) {
  103.       state.deopted = true;
  104.     }
  105.   }
  106. };
  107. function hasRest(node) {
  108.   return t.isRestElement(node.params[node.params.length - 1]);
  109. }
  110.  
  111. function optimiseIndexGetter(path, argsId, offset) {
  112.   var index = void 0;
  113.  
  114.   if (t.isNumericLiteral(path.parent.property)) {
  115.     index = t.numericLiteral(path.parent.property.value + offset);
  116.   } else if (offset === 0) {
  117.     index = path.parent.property;
  118.   } else {
  119.     index = t.binaryExpression("+", path.parent.property, t.numericLiteral(offset));
  120.   }
  121.  
  122.   var scope = path.scope;
  123.  
  124.   if (!scope.isPure(index)) {
  125.     var temp = scope.generateUidIdentifierBasedOnNode(index);
  126.     scope.push({ id: temp, kind: "var" });
  127.     path.parentPath.replaceWith(restIndexImpure({
  128.       ARGUMENTS: argsId,
  129.       INDEX: index,
  130.       REF: temp
  131.     }));
  132.   } else {
  133.     path.parentPath.replaceWith(restIndex({
  134.       ARGUMENTS: argsId,
  135.       INDEX: index
  136.     }));
  137.   }
  138. }
  139.  
  140. function optimiseLengthGetter(path, argsId, offset) {
  141.   if (offset) {
  142.     path.parentPath.replaceWith(restLength({
  143.       ARGUMENTS: argsId,
  144.       OFFSET: t.numericLiteral(offset)
  145.     }));
  146.   } else {
  147.     path.replaceWith(argsId);
  148.   }
  149. }
  150.  
  151. var visitor = exports.visitor = {
  152.   Function: function Function(path) {
  153.     var node = path.node,
  154.         scope = path.scope;
  155.  
  156.     if (!hasRest(node)) return;
  157.  
  158.     var rest = node.params.pop().argument;
  159.  
  160.     var argsId = t.identifier("arguments");
  161.  
  162.     argsId._shadowedFunctionLiteral = path;
  163.  
  164.     var state = {
  165.       references: [],
  166.       offset: node.params.length,
  167.  
  168.       argumentsNode: argsId,
  169.       outerBinding: scope.getBindingIdentifier(rest.name),
  170.  
  171.       candidates: [],
  172.  
  173.       name: rest.name,
  174.  
  175.       deopted: false
  176.     };
  177.  
  178.     path.traverse(memberExpressionOptimisationVisitor, state);
  179.  
  180.     if (!state.deopted && !state.references.length) {
  181.       for (var _iterator = state.candidates, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  182.         var _ref3;
  183.  
  184.         if (_isArray) {
  185.           if (_i >= _iterator.length) break;
  186.           _ref3 = _iterator[_i++];
  187.         } else {
  188.           _i = _iterator.next();
  189.           if (_i.done) break;
  190.           _ref3 = _i.value;
  191.         }
  192.  
  193.         var _ref4 = _ref3;
  194.         var _path = _ref4.path,
  195.             cause = _ref4.cause;
  196.  
  197.         switch (cause) {
  198.           case "indexGetter":
  199.             optimiseIndexGetter(_path, argsId, state.offset);
  200.             break;
  201.           case "lengthGetter":
  202.             optimiseLengthGetter(_path, argsId, state.offset);
  203.             break;
  204.           default:
  205.             _path.replaceWith(argsId);
  206.         }
  207.       }
  208.       return;
  209.     }
  210.  
  211.     state.references = state.references.concat(state.candidates.map(function (_ref5) {
  212.       var path = _ref5.path;
  213.       return path;
  214.     }));
  215.  
  216.     state.deopted = state.deopted || !!node.shadow;
  217.  
  218.     var start = t.numericLiteral(node.params.length);
  219.     var key = scope.generateUidIdentifier("key");
  220.     var len = scope.generateUidIdentifier("len");
  221.  
  222.     var arrKey = key;
  223.     var arrLen = len;
  224.     if (node.params.length) {
  225.       arrKey = t.binaryExpression("-", key, start);
  226.  
  227.       arrLen = t.conditionalExpression(t.binaryExpression(">", len, start), t.binaryExpression("-", len, start), t.numericLiteral(0));
  228.     }
  229.  
  230.     var loop = buildRest({
  231.       ARGUMENTS: argsId,
  232.       ARRAY_KEY: arrKey,
  233.       ARRAY_LEN: arrLen,
  234.       START: start,
  235.       ARRAY: rest,
  236.       KEY: key,
  237.       LEN: len
  238.     });
  239.  
  240.     if (state.deopted) {
  241.       loop._blockHoist = node.params.length + 1;
  242.       node.body.body.unshift(loop);
  243.     } else {
  244.       loop._blockHoist = 1;
  245.  
  246.       var target = path.getEarliestCommonAncestorFrom(state.references).getStatementParent();
  247.  
  248.       target.findParent(function (path) {
  249.         if (path.isLoop()) {
  250.           target = path;
  251.         } else {
  252.           return path.isFunction();
  253.         }
  254.       });
  255.  
  256.       target.insertBefore(loop);
  257.     }
  258.   }
  259. };
downloadrest.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