Blame view

node_modules/babel-plugin-transform-es2015-block-scoping/lib/tdz.js 2.72 KB
6a9ffbcc   liuqimichale   地图点击事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  "use strict";
  
  exports.__esModule = true;
  exports.visitor = undefined;
  
  var _babelTypes = require("babel-types");
  
  var t = _interopRequireWildcard(_babelTypes);
  
  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; } }
  
  function getTDZStatus(refPath, bindingPath) {
    var executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath);
  
    if (executionStatus === "before") {
      return "inside";
    } else if (executionStatus === "after") {
      return "outside";
    } else {
      return "maybe";
    }
  }
  
  function buildTDZAssert(node, file) {
    return t.callExpression(file.addHelper("temporalRef"), [node, t.stringLiteral(node.name), file.addHelper("temporalUndefined")]);
  }
  
  function isReference(node, scope, state) {
    var declared = state.letReferences[node.name];
    if (!declared) return false;
  
    return scope.getBindingIdentifier(node.name) === declared;
  }
  
  var visitor = exports.visitor = {
    ReferencedIdentifier: function ReferencedIdentifier(path, state) {
      if (!this.file.opts.tdz) return;
  
      var node = path.node,
          parent = path.parent,
          scope = path.scope;
  
  
      if (path.parentPath.isFor({ left: node })) return;
      if (!isReference(node, scope, state)) return;
  
      var bindingPath = scope.getBinding(node.name).path;
  
      var status = getTDZStatus(path, bindingPath);
      if (status === "inside") return;
  
      if (status === "maybe") {
        var assert = buildTDZAssert(node, state.file);
  
        bindingPath.parent._tdzThis = true;
  
        path.skip();
  
        if (path.parentPath.isUpdateExpression()) {
          if (parent._ignoreBlockScopingTDZ) return;
          path.parentPath.replaceWith(t.sequenceExpression([assert, parent]));
        } else {
          path.replaceWith(assert);
        }
      } else if (status === "outside") {
        path.replaceWith(t.throwStatement(t.inherits(t.newExpression(t.identifier("ReferenceError"), [t.stringLiteral(node.name + " is not defined - temporal dead zone")]), node)));
      }
    },
  
  
    AssignmentExpression: {
      exit: function exit(path, state) {
        if (!this.file.opts.tdz) return;
  
        var node = path.node;
  
        if (node._ignoreBlockScopingTDZ) return;
  
        var nodes = [];
        var ids = path.getBindingIdentifiers();
  
        for (var name in ids) {
          var id = ids[name];
  
          if (isReference(id, path.scope, state)) {
            nodes.push(buildTDZAssert(id, state.file));
          }
        }
  
        if (nodes.length) {
          node._ignoreBlockScopingTDZ = true;
          nodes.push(node);
          path.replaceWithMultiple(nodes.map(t.expressionStatement));
        }
      }
    }
  };