BVB Source Codes

reactide Show shadow-functions.js Source code

Return Download reactide: download shadow-functions.js Source code - Download reactide Source code - Type:.js
  1. "use strict";
  2.  
  3. exports.__esModule = true;
  4.  
  5. var _symbol = require("babel-runtime/core-js/symbol");
  6.  
  7. var _symbol2 = _interopRequireDefault(_symbol);
  8.  
  9. var _plugin = require("../plugin");
  10.  
  11. var _plugin2 = _interopRequireDefault(_plugin);
  12.  
  13. var _babelTypes = require("babel-types");
  14.  
  15. var t = _interopRequireWildcard(_babelTypes);
  16.  
  17. 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; } }
  18.  
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20.  
  21. var SUPER_THIS_BOUND = (0, _symbol2.default)("super this bound");
  22.  
  23. var superVisitor = {
  24.   CallExpression: function CallExpression(path) {
  25.     if (!path.get("callee").isSuper()) return;
  26.  
  27.     var node = path.node;
  28.  
  29.     if (node[SUPER_THIS_BOUND]) return;
  30.     node[SUPER_THIS_BOUND] = true;
  31.  
  32.     path.replaceWith(t.assignmentExpression("=", this.id, node));
  33.   }
  34. };
  35.  
  36. exports.default = new _plugin2.default({
  37.   visitor: {
  38.     ThisExpression: function ThisExpression(path) {
  39.       remap(path, "this");
  40.     },
  41.     ReferencedIdentifier: function ReferencedIdentifier(path) {
  42.       if (path.node.name === "arguments") {
  43.         remap(path, "arguments");
  44.       }
  45.     }
  46.   }
  47. });
  48.  
  49.  
  50. function shouldShadow(path, shadowPath) {
  51.   if (path.is("_forceShadow")) {
  52.     return true;
  53.   } else {
  54.     return shadowPath;
  55.   }
  56. }
  57.  
  58. function remap(path, key) {
  59.   // ensure that we're shadowed
  60.   var shadowPath = path.inShadow(key);
  61.   if (!shouldShadow(path, shadowPath)) return;
  62.  
  63.   var shadowFunction = path.node._shadowedFunctionLiteral;
  64.  
  65.   var currentFunction = void 0;
  66.   var passedShadowFunction = false;
  67.  
  68.   var fnPath = path.findParent(function (path) {
  69.     if (path.isProgram() || path.isFunction()) {
  70.       // catch current function in case this is the shadowed one and we can ignore it
  71.       currentFunction = currentFunction || path;
  72.     }
  73.  
  74.     if (path.isProgram()) {
  75.       passedShadowFunction = true;
  76.  
  77.       return true;
  78.     } else if (path.isFunction() && !path.isArrowFunctionExpression()) {
  79.       if (shadowFunction) {
  80.         if (path === shadowFunction || path.node === shadowFunction.node) return true;
  81.       } else {
  82.         if (!path.is("shadow")) return true;
  83.       }
  84.  
  85.       passedShadowFunction = true;
  86.       return false;
  87.     }
  88.  
  89.     return false;
  90.   });
  91.  
  92.   if (shadowFunction && fnPath.isProgram() && !shadowFunction.isProgram()) {
  93.     // If the shadow wasn't found, take the closest function as a backup.
  94.     // This is a bit of a hack, but it will allow the parameter transforms to work properly
  95.     // without introducing yet another shadow-controlling flag.
  96.     fnPath = path.findParent(function (p) {
  97.       return p.isProgram() || p.isFunction();
  98.     });
  99.   }
  100.  
  101.   // no point in realiasing if we're in this function
  102.   if (fnPath === currentFunction) return;
  103.  
  104.   // If the only functions that were encountered are arrow functions, skip remapping the
  105.   // binding since arrow function syntax already does that.
  106.   if (!passedShadowFunction) return;
  107.  
  108.   var cached = fnPath.getData(key);
  109.   if (cached) return path.replaceWith(cached);
  110.  
  111.   var id = path.scope.generateUidIdentifier(key);
  112.  
  113.   fnPath.setData(key, id);
  114.  
  115.   var classPath = fnPath.findParent(function (p) {
  116.     return p.isClass();
  117.   });
  118.   var hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass);
  119.  
  120.   if (key === "this" && fnPath.isMethod({ kind: "constructor" }) && hasSuperClass) {
  121.     fnPath.scope.push({ id: id });
  122.  
  123.     fnPath.traverse(superVisitor, { id: id });
  124.   } else {
  125.     var init = key === "this" ? t.thisExpression() : t.identifier(key);
  126.  
  127.     fnPath.scope.push({ id: id, init: init });
  128.   }
  129.  
  130.   return path.replaceWith(id);
  131. }
  132. module.exports = exports["default"];
downloadshadow-functions.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