Blame view

node_modules/regenerator-transform/src/meta.js 2.82 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  /**
   * Copyright (c) 2014, Facebook, Inc.
   * All rights reserved.
   *
   * This source code is licensed under the BSD-style license found in the
   * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
   * additional grant of patent rights can be found in the PATENTS file in
   * the same directory.
   */
  
  import assert from "assert";
  let m = require("private").makeAccessor();
  import * as t from "babel-types";
  let hasOwn = Object.prototype.hasOwnProperty;
  
  function makePredicate(propertyName, knownTypes) {
    function onlyChildren(node) {
      t.assertNode(node);
  
      // Assume no side effects until we find out otherwise.
      let result = false;
  
      function check(child) {
        if (result) {
          // Do nothing.
        } else if (Array.isArray(child)) {
          child.some(check);
        } else if (t.isNode(child)) {
          assert.strictEqual(result, false);
          result = predicate(child);
        }
        return result;
      }
  
      let keys = t.VISITOR_KEYS[node.type];
      if (keys) {
        for (let i = 0; i < keys.length; i++) {
          let key = keys[i];
          let child = node[key];
          check(child);
        }
      }
  
      return result;
    }
  
    function predicate(node) {
      t.assertNode(node);
  
      let meta = m(node);
      if (hasOwn.call(meta, propertyName))
        return meta[propertyName];
  
      // Certain types are "opaque," which means they have no side
      // effects or leaps and we don't care about their subexpressions.
      if (hasOwn.call(opaqueTypes, node.type))
        return meta[propertyName] = false;
  
      if (hasOwn.call(knownTypes, node.type))
        return meta[propertyName] = true;
  
      return meta[propertyName] = onlyChildren(node);
    }
  
    predicate.onlyChildren = onlyChildren;
  
    return predicate;
  }
  
  let opaqueTypes = {
    FunctionExpression: true,
    ArrowFunctionExpression: true
  };
  
  // These types potentially have side effects regardless of what side
  // effects their subexpressions have.
  let sideEffectTypes = {
    CallExpression: true, // Anything could happen!
    ForInStatement: true, // Modifies the key variable.
    UnaryExpression: true, // Think delete.
    BinaryExpression: true, // Might invoke .toString() or .valueOf().
    AssignmentExpression: true, // Side-effecting by definition.
    UpdateExpression: true, // Updates are essentially assignments.
    NewExpression: true // Similar to CallExpression.
  };
  
  // These types are the direct cause of all leaps in control flow.
  let leapTypes = {
    YieldExpression: true,
    BreakStatement: true,
    ContinueStatement: true,
    ReturnStatement: true,
    ThrowStatement: true
  };
  
  // All leap types are also side effect types.
  for (let type in leapTypes) {
    if (hasOwn.call(leapTypes, type)) {
      sideEffectTypes[type] = leapTypes[type];
    }
  }
  
  exports.hasSideEffects = makePredicate("hasSideEffects", sideEffectTypes);
  exports.containsLeap = makePredicate("containsLeap", leapTypes);