1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = _default;
|
---|
7 |
|
---|
8 | var _t = require("@babel/types");
|
---|
9 |
|
---|
10 | const {
|
---|
11 | assignmentExpression,
|
---|
12 | cloneNode,
|
---|
13 | isIdentifier,
|
---|
14 | isLiteral,
|
---|
15 | isMemberExpression,
|
---|
16 | isPrivateName,
|
---|
17 | isPureish,
|
---|
18 | isSuper,
|
---|
19 | memberExpression,
|
---|
20 | toComputedKey
|
---|
21 | } = _t;
|
---|
22 |
|
---|
23 | function getObjRef(node, nodes, scope) {
|
---|
24 | let ref;
|
---|
25 |
|
---|
26 | if (isIdentifier(node)) {
|
---|
27 | if (scope.hasBinding(node.name)) {
|
---|
28 | return node;
|
---|
29 | } else {
|
---|
30 | ref = node;
|
---|
31 | }
|
---|
32 | } else if (isMemberExpression(node)) {
|
---|
33 | ref = node.object;
|
---|
34 |
|
---|
35 | if (isSuper(ref) || isIdentifier(ref) && scope.hasBinding(ref.name)) {
|
---|
36 | return ref;
|
---|
37 | }
|
---|
38 | } else {
|
---|
39 | throw new Error(`We can't explode this node type ${node["type"]}`);
|
---|
40 | }
|
---|
41 |
|
---|
42 | const temp = scope.generateUidIdentifierBasedOnNode(ref);
|
---|
43 | scope.push({
|
---|
44 | id: temp
|
---|
45 | });
|
---|
46 | nodes.push(assignmentExpression("=", cloneNode(temp), cloneNode(ref)));
|
---|
47 | return temp;
|
---|
48 | }
|
---|
49 |
|
---|
50 | function getPropRef(node, nodes, scope) {
|
---|
51 | const prop = node.property;
|
---|
52 |
|
---|
53 | if (isPrivateName(prop)) {
|
---|
54 | throw new Error("We can't generate property ref for private name, please install `@babel/plugin-proposal-class-properties`");
|
---|
55 | }
|
---|
56 |
|
---|
57 | const key = toComputedKey(node, prop);
|
---|
58 | if (isLiteral(key) && isPureish(key)) return key;
|
---|
59 | const temp = scope.generateUidIdentifierBasedOnNode(prop);
|
---|
60 | scope.push({
|
---|
61 | id: temp
|
---|
62 | });
|
---|
63 | nodes.push(assignmentExpression("=", cloneNode(temp), cloneNode(prop)));
|
---|
64 | return temp;
|
---|
65 | }
|
---|
66 |
|
---|
67 | function _default(node, nodes, file, scope, allowedSingleIdent) {
|
---|
68 | let obj;
|
---|
69 |
|
---|
70 | if (isIdentifier(node) && allowedSingleIdent) {
|
---|
71 | obj = node;
|
---|
72 | } else {
|
---|
73 | obj = getObjRef(node, nodes, scope);
|
---|
74 | }
|
---|
75 |
|
---|
76 | let ref, uid;
|
---|
77 |
|
---|
78 | if (isIdentifier(node)) {
|
---|
79 | ref = cloneNode(node);
|
---|
80 | uid = obj;
|
---|
81 | } else {
|
---|
82 | const prop = getPropRef(node, nodes, scope);
|
---|
83 | const computed = node.computed || isLiteral(prop);
|
---|
84 | uid = memberExpression(cloneNode(obj), cloneNode(prop), computed);
|
---|
85 | ref = memberExpression(cloneNode(obj), cloneNode(prop), computed);
|
---|
86 | }
|
---|
87 |
|
---|
88 | return {
|
---|
89 | uid: uid,
|
---|
90 | ref: ref
|
---|
91 | };
|
---|
92 | } |
---|