Projekt

Obecné

Profil

Stáhnout (3.6 KB) Statistiky
| Větev: | Revize:
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
  name: "internal.shadowFunctions",
38

    
39
  visitor: {
40
    ThisExpression: function ThisExpression(path) {
41
      remap(path, "this");
42
    },
43
    ReferencedIdentifier: function ReferencedIdentifier(path) {
44
      if (path.node.name === "arguments") {
45
        remap(path, "arguments");
46
      }
47
    }
48
  }
49
});
50

    
51

    
52
function shouldShadow(path, shadowPath) {
53
  if (path.is("_forceShadow")) {
54
    return true;
55
  } else {
56
    return shadowPath;
57
  }
58
}
59

    
60
function remap(path, key) {
61
  var shadowPath = path.inShadow(key);
62
  if (!shouldShadow(path, shadowPath)) return;
63

    
64
  var shadowFunction = path.node._shadowedFunctionLiteral;
65

    
66
  var currentFunction = void 0;
67
  var passedShadowFunction = false;
68

    
69
  var fnPath = path.find(function (innerPath) {
70
    if (innerPath.parentPath && innerPath.parentPath.isClassProperty() && innerPath.key === "value") {
71
      return true;
72
    }
73
    if (path === innerPath) return false;
74
    if (innerPath.isProgram() || innerPath.isFunction()) {
75
      currentFunction = currentFunction || innerPath;
76
    }
77

    
78
    if (innerPath.isProgram()) {
79
      passedShadowFunction = true;
80

    
81
      return true;
82
    } else if (innerPath.isFunction() && !innerPath.isArrowFunctionExpression()) {
83
      if (shadowFunction) {
84
        if (innerPath === shadowFunction || innerPath.node === shadowFunction.node) return true;
85
      } else {
86
        if (!innerPath.is("shadow")) return true;
87
      }
88

    
89
      passedShadowFunction = true;
90
      return false;
91
    }
92

    
93
    return false;
94
  });
95

    
96
  if (shadowFunction && fnPath.isProgram() && !shadowFunction.isProgram()) {
97
    fnPath = path.findParent(function (p) {
98
      return p.isProgram() || p.isFunction();
99
    });
100
  }
101

    
102
  if (fnPath === currentFunction) return;
103

    
104
  if (!passedShadowFunction) return;
105

    
106
  var cached = fnPath.getData(key);
107
  if (!cached) {
108
    var id = path.scope.generateUidIdentifier(key);
109

    
110
    fnPath.setData(key, id);
111
    cached = id;
112

    
113
    var classPath = fnPath.findParent(function (p) {
114
      return p.isClass();
115
    });
116
    var hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass);
117

    
118
    if (key === "this" && fnPath.isMethod({ kind: "constructor" }) && hasSuperClass) {
119
      fnPath.scope.push({ id: id });
120

    
121
      fnPath.traverse(superVisitor, { id: id });
122
    } else {
123
      var init = key === "this" ? t.thisExpression() : t.identifier(key);
124

    
125
      if (shadowFunction) init._shadowedFunctionLiteral = shadowFunction;
126

    
127
      fnPath.scope.push({ id: id, init: init });
128
    }
129
  }
130

    
131
  var node = t.cloneDeep(cached);
132
  node.loc = path.node.loc;
133

    
134
  return path.replaceWith(node);
135
}
136
module.exports = exports["default"];
(2-2/2)