aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@babel/helper-member-expression-to-functions/lib/index.js
blob: 938dc918c361c4d5e43b7c7653709f94990e60d3 (plain)
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
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = memberExpressionToFunctions;

function t() {
  const data = _interopRequireWildcard(require("@babel/types"));

  t = function t() {
    return data;
  };

  return data;
}

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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

const handle = {
  handle(member) {
    const node = member.node,
          parent = member.parent,
          parentPath = member.parentPath;

    if (parentPath.isUpdateExpression({
      argument: node
    })) {
      const operator = parent.operator,
            prefix = parent.prefix;

      if (this.memoize) {
        this.memoize(member);
      }

      const value = t().binaryExpression(operator[0], t().unaryExpression("+", this.get(member)), t().numericLiteral(1));

      if (prefix) {
        parentPath.replaceWith(this.set(member, value));
      } else {
        const scope = member.scope;
        const ref = scope.generateUidIdentifierBasedOnNode(node);
        scope.push({
          id: ref
        });
        value.left = t().assignmentExpression("=", t().cloneNode(ref), value.left);
        parentPath.replaceWith(t().sequenceExpression([this.set(member, value), t().cloneNode(ref)]));
      }

      return;
    }

    if (parentPath.isAssignmentExpression({
      left: node
    })) {
      const operator = parent.operator,
            right = parent.right;
      let value = right;

      if (operator !== "=") {
        if (this.memoize) {
          this.memoize(member);
        }

        value = t().binaryExpression(operator.slice(0, -1), this.get(member), value);
      }

      parentPath.replaceWith(this.set(member, value));
      return;
    }

    if (parentPath.isCallExpression({
      callee: node
    })) {
      const args = parent.arguments;
      parentPath.replaceWith(this.call(member, args));
      return;
    }

    member.replaceWith(this.get(member));
  }

};

function memberExpressionToFunctions(path, visitor, state) {
  path.traverse(visitor, Object.assign({}, state, handle));
}