aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@babel/helper-member-expression-to-functions/README.md
blob: 31dd4c3ce223d265e5bef9aaedc72324832d2866 (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
# @babel/helper-member-expression-to-functions

Helper function to replace certain member expressions with function calls

## Usage

> Designed for internal Babel use.

Traverses the `path` using the supplied `visitor` and an augmented `state`.

```js
const visitor = {
  MemberExpression(memberPath, state) {

    if (someCondition(memberPath)) {

      // The handle method is supplied by memberExpressionToFunctions.
      // It should be called whenever a MemberExpression should be
      // converted into the proper function calls.
      state.handle(memberPath);

    }

  },
};

// The helper requires three special methods on state: `get`, `set`, and
// `call`.
// Optionally, a special `memoize` method may be defined, which gets
// called if the member is in a self-referential update expression.
// Everything else will be passed through as normal.
const state = {
  get(memberPath) {
    // Return some AST that will get the member
    return t.callExpression(
      this.file.addHelper('superGet'),
      [t.thisExpression(), memberPath.node.property]
    );
  },

  set(memberPath, value) {
    // Return some AST that will set the member
    return t.callExpression(
      this.file.addHelper('superSet'),
      [t.thisExpression(), memberPath.node.property, value]
    );
  },

  call(memberPath, args) {
    // Return some AST that will call the member with the proper context
    // and args
    return t.callExpression(
      t.memberExpression(this.get(memberPath), t.identifier("apply")),
      [t.thisExpression(), t.arrayExpression(args)]
    );
  },

  memoize(memberPath) {
    const { node } = memberPath;
    if (node.computed) {
      MEMOIZED.set(node, ...);
    }
  },

  // The handle method is provided by memberExpressionToFunctions.
  // handle(memberPath) { ... }

  // Other state stuff is left untouched.
  someState: new Set(),
};

// Replace all the special MemberExpressions in rootPath, as determined
// by our visitor, using the state methods.
memberExpressionToFunctions(rootPath, visitor, state);
```