Blame view

node_modules/regenerator/lib/util.js 2.1 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
  /**
   * Copyright (c) 2014-present, Facebook, Inc.
   *
   * This source code is licensed under the MIT license found in the
   * LICENSE file in the root directory of this source tree.
   */
  
  var assert = require("assert");
  var types = require("recast").types;
  var n = types.namedTypes;
  var b = types.builders;
  var hasOwn = Object.prototype.hasOwnProperty;
  
  exports.defaults = function(obj) {
    var len = arguments.length;
    var extension;
  
    for (var i = 1; i < len; ++i) {
      if ((extension = arguments[i])) {
        for (var key in extension) {
          if (hasOwn.call(extension, key) && !hasOwn.call(obj, key)) {
            obj[key] = extension[key];
          }
        }
      }
    }
  
    return obj;
  };
  
  exports.runtimeProperty = function(name) {
    return b.memberExpression(
      b.identifier("regeneratorRuntime"),
      b.identifier(name),
      false
    );
  };
  
  // Inspired by the isReference function from ast-util:
  // https://github.com/eventualbuddha/ast-util/blob/9bf91c5ce8/lib/index.js#L466-L506
  exports.isReference = function(path, name) {
    var node = path.value;
  
    if (!n.Identifier.check(node)) {
      return false;
    }
  
    if (name && node.name !== name) {
      return false;
    }
  
    var parent = path.parent.value;
  
    switch (parent.type) {
    case "VariableDeclarator":
      return path.name === "init";
  
    case "MemberExpression":
      return path.name === "object" || (
        parent.computed && path.name === "property"
      );
  
    case "FunctionExpression":
    case "FunctionDeclaration":
    case "ArrowFunctionExpression":
      if (path.name === "id") {
        return false;
      }
  
      if (path.parentPath.name === "params" &&
          parent.params === path.parentPath.value &&
          parent.params[path.name] === node) {
        return false;
      }
  
      return true;
  
    case "ClassDeclaration":
    case "ClassExpression":
      return path.name !== "id";
  
    case "CatchClause":
      return path.name !== "param";
  
    case "Property":
    case "MethodDefinition":
      return path.name !== "key";
  
    case "ImportSpecifier":
    case "ImportDefaultSpecifier":
    case "ImportNamespaceSpecifier":
    case "LabeledStatement":
      return false;
  
    default:
      return true;
    }
  };