Blame view

node_modules/@babel/plugin-proposal-function-sent/lib/index.js 2.51 KB
ce4c83ff   wxy   初始提交
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
98
99
100
101
102
103
104
105
  "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  function _helperPluginUtils() {
    const data = require("@babel/helper-plugin-utils");
  
    _helperPluginUtils = function () {
      return data;
    };
  
    return data;
  }
  
  function _pluginSyntaxFunctionSent() {
    const data = _interopRequireDefault(require("@babel/plugin-syntax-function-sent"));
  
    _pluginSyntaxFunctionSent = function () {
      return data;
    };
  
    return data;
  }
  
  function _helperWrapFunction() {
    const data = _interopRequireDefault(require("@babel/helper-wrap-function"));
  
    _helperWrapFunction = function () {
      return data;
    };
  
    return data;
  }
  
  function _core() {
    const data = require("@babel/core");
  
    _core = function () {
      return data;
    };
  
    return data;
  }
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  var _default = (0, _helperPluginUtils().declare)(api => {
    api.assertVersion(7);
  
    const isFunctionSent = node => _core().types.isIdentifier(node.meta, {
      name: "function"
    }) && _core().types.isIdentifier(node.property, {
      name: "sent"
    });
  
    const hasBeenReplaced = (node, sentId) => _core().types.isAssignmentExpression(node) && _core().types.isIdentifier(node.left, {
      name: sentId
    });
  
    const yieldVisitor = {
      Function(path) {
        path.skip();
      },
  
      YieldExpression(path) {
        if (!hasBeenReplaced(path.parent, this.sentId)) {
          path.replaceWith(_core().types.assignmentExpression("=", _core().types.identifier(this.sentId), path.node));
        }
      },
  
      MetaProperty(path) {
        if (isFunctionSent(path.node)) {
          path.replaceWith(_core().types.identifier(this.sentId));
        }
      }
  
    };
    return {
      name: "proposal-function-sent",
      inherits: _pluginSyntaxFunctionSent().default,
      visitor: {
        MetaProperty(path, state) {
          if (!isFunctionSent(path.node)) return;
          const fnPath = path.getFunctionParent();
  
          if (!fnPath.node.generator) {
            throw new Error("Parent generator function not found");
          }
  
          const sentId = path.scope.generateUid("function.sent");
          fnPath.traverse(yieldVisitor, {
            sentId
          });
          fnPath.node.body.body.unshift(_core().types.variableDeclaration("let", [_core().types.variableDeclarator(_core().types.identifier(sentId), _core().types.yieldExpression())]));
          (0, _helperWrapFunction().default)(fnPath, state.addHelper("skipFirstGeneratorNext"));
        }
  
      }
    };
  });
  
  exports.default = _default;