index.js
2 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _t = require("@babel/types");
const {
assignmentExpression,
cloneNode,
isIdentifier,
isLiteral,
isMemberExpression,
isPrivateName,
isPureish,
isSuper,
memberExpression,
toComputedKey
} = _t;
function getObjRef(node, nodes, scope) {
let ref;
if (isIdentifier(node)) {
if (scope.hasBinding(node.name)) {
return node;
} else {
ref = node;
}
} else if (isMemberExpression(node)) {
ref = node.object;
if (isSuper(ref) || isIdentifier(ref) && scope.hasBinding(ref.name)) {
return ref;
}
} else {
throw new Error(`We can't explode this node type ${node["type"]}`);
}
const temp = scope.generateUidIdentifierBasedOnNode(ref);
scope.push({
id: temp
});
nodes.push(assignmentExpression("=", cloneNode(temp), cloneNode(ref)));
return temp;
}
function getPropRef(node, nodes, scope) {
const prop = node.property;
if (isPrivateName(prop)) {
throw new Error("We can't generate property ref for private name, please install `@babel/plugin-proposal-class-properties`");
}
const key = toComputedKey(node, prop);
if (isLiteral(key) && isPureish(key)) return key;
const temp = scope.generateUidIdentifierBasedOnNode(prop);
scope.push({
id: temp
});
nodes.push(assignmentExpression("=", cloneNode(temp), cloneNode(prop)));
return temp;
}
function _default(node, nodes, file, scope, allowedSingleIdent) {
let obj;
if (isIdentifier(node) && allowedSingleIdent) {
obj = node;
} else {
obj = getObjRef(node, nodes, scope);
}
let ref, uid;
if (isIdentifier(node)) {
ref = cloneNode(node);
uid = obj;
} else {
const prop = getPropRef(node, nodes, scope);
const computed = node.computed || isLiteral(prop);
uid = memberExpression(cloneNode(obj), cloneNode(prop), computed);
ref = memberExpression(cloneNode(obj), cloneNode(prop), computed);
}
return {
uid: uid,
ref: ref
};
}