Blame view

node_modules/regenerator-transform/lib/replaceShorthandObjectMethod.js 2.71 KB
2a09d1a4   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
  "use strict";
  
  exports.__esModule = true;
  exports.default = replaceShorthandObjectMethod;
  
  var _babelTypes = require("babel-types");
  
  var t = _interopRequireWildcard(_babelTypes);
  
  var _util = require("./util");
  
  var util = _interopRequireWildcard(_util);
  
  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; } }
  
  // this function converts a shorthand object generator method into a normal
  // (non-shorthand) object property which is a generator function expression. for
  // example, this:
  //
  //  var foo = {
  //    *bar(baz) { return 5; }
  //  }
  //
  // should be replaced with:
  //
  //  var foo = {
  //    bar: function*(baz) { return 5; }
  //  }
  //
  // to do this, it clones the parameter array and the body of the object generator
  // method into a new FunctionExpression.
  //
  // this method can be passed any Function AST node path, and it will return
  // either:
  //   a) the path that was passed in (iff the path did not need to be replaced) or
  //   b) the path of the new FunctionExpression that was created as a replacement
  //     (iff the path did need to be replaced)
  //
  // In either case, though, the caller can count on the fact that the return value
  // is a Function AST node path.
  //
  // If this function is called with an AST node path that is not a Function (or with an
  // argument that isn't an AST node path), it will throw an error.
  function replaceShorthandObjectMethod(path) {
    if (!path.node || !t.isFunction(path.node)) {
      throw new Error("replaceShorthandObjectMethod can only be called on Function AST node paths.");
    }
  
    // this function only replaces shorthand object methods (called ObjectMethod
    // in Babel-speak).
    if (!t.isObjectMethod(path.node)) {
      return path;
    }
  
    // this function only replaces generators.
    if (!path.node.generator) {
      return path;
    }
  
    var parameters = path.node.params.map(function (param) {
      return t.cloneDeep(param);
    });
  
    var functionExpression = t.functionExpression(null, // id
    parameters, // params
    t.cloneDeep(path.node.body), // body
    path.node.generator, path.node.async);
  
    util.replaceWithOrRemove(path, t.objectProperty(t.cloneDeep(path.node.key), // key
    functionExpression, //value
    path.node.computed, // computed
    false // shorthand
    ));
  
    // path now refers to the ObjectProperty AST node path, but we want to return a
    // Function AST node path for the function expression we created. we know that
    // the FunctionExpression we just created is the value of the ObjectProperty,
    // so return the "value" path off of this path.
    return path.get("value");
  }