1
|
"use strict";
|
2
|
|
3
|
exports.__esModule = true;
|
4
|
|
5
|
var _keys = require("babel-runtime/core-js/object/keys");
|
6
|
|
7
|
var _keys2 = _interopRequireDefault(_keys);
|
8
|
|
9
|
exports.push = push;
|
10
|
exports.hasComputed = hasComputed;
|
11
|
exports.toComputedObjectFromClass = toComputedObjectFromClass;
|
12
|
exports.toClassObject = toClassObject;
|
13
|
exports.toDefineObject = toDefineObject;
|
14
|
|
15
|
var _babelHelperFunctionName = require("babel-helper-function-name");
|
16
|
|
17
|
var _babelHelperFunctionName2 = _interopRequireDefault(_babelHelperFunctionName);
|
18
|
|
19
|
var _has = require("lodash/has");
|
20
|
|
21
|
var _has2 = _interopRequireDefault(_has);
|
22
|
|
23
|
var _babelTypes = require("babel-types");
|
24
|
|
25
|
var t = _interopRequireWildcard(_babelTypes);
|
26
|
|
27
|
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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
28
|
|
29
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30
|
|
31
|
function toKind(node) {
|
32
|
if (t.isClassMethod(node) || t.isObjectMethod(node)) {
|
33
|
if (node.kind === "get" || node.kind === "set") {
|
34
|
return node.kind;
|
35
|
}
|
36
|
}
|
37
|
|
38
|
return "value";
|
39
|
}
|
40
|
|
41
|
function push(mutatorMap, node, kind, file, scope) {
|
42
|
var alias = t.toKeyAlias(node);
|
43
|
|
44
|
var map = {};
|
45
|
if ((0, _has2.default)(mutatorMap, alias)) map = mutatorMap[alias];
|
46
|
mutatorMap[alias] = map;
|
47
|
|
48
|
map._inherits = map._inherits || [];
|
49
|
map._inherits.push(node);
|
50
|
|
51
|
map._key = node.key;
|
52
|
|
53
|
if (node.computed) {
|
54
|
map._computed = true;
|
55
|
}
|
56
|
|
57
|
if (node.decorators) {
|
58
|
var decorators = map.decorators = map.decorators || t.arrayExpression([]);
|
59
|
decorators.elements = decorators.elements.concat(node.decorators.map(function (dec) {
|
60
|
return dec.expression;
|
61
|
}).reverse());
|
62
|
}
|
63
|
|
64
|
if (map.value || map.initializer) {
|
65
|
throw file.buildCodeFrameError(node, "Key conflict with sibling node");
|
66
|
}
|
67
|
|
68
|
var key = void 0,
|
69
|
value = void 0;
|
70
|
|
71
|
if (t.isObjectProperty(node) || t.isObjectMethod(node) || t.isClassMethod(node)) {
|
72
|
key = t.toComputedKey(node, node.key);
|
73
|
}
|
74
|
|
75
|
if (t.isObjectProperty(node) || t.isClassProperty(node)) {
|
76
|
value = node.value;
|
77
|
} else if (t.isObjectMethod(node) || t.isClassMethod(node)) {
|
78
|
value = t.functionExpression(null, node.params, node.body, node.generator, node.async);
|
79
|
value.returnType = node.returnType;
|
80
|
}
|
81
|
|
82
|
var inheritedKind = toKind(node);
|
83
|
if (!kind || inheritedKind !== "value") {
|
84
|
kind = inheritedKind;
|
85
|
}
|
86
|
|
87
|
if (scope && t.isStringLiteral(key) && (kind === "value" || kind === "initializer") && t.isFunctionExpression(value)) {
|
88
|
value = (0, _babelHelperFunctionName2.default)({ id: key, node: value, scope: scope });
|
89
|
}
|
90
|
|
91
|
if (value) {
|
92
|
t.inheritsComments(value, node);
|
93
|
map[kind] = value;
|
94
|
}
|
95
|
|
96
|
return map;
|
97
|
}
|
98
|
|
99
|
function hasComputed(mutatorMap) {
|
100
|
for (var key in mutatorMap) {
|
101
|
if (mutatorMap[key]._computed) {
|
102
|
return true;
|
103
|
}
|
104
|
}
|
105
|
return false;
|
106
|
}
|
107
|
|
108
|
function toComputedObjectFromClass(obj) {
|
109
|
var objExpr = t.arrayExpression([]);
|
110
|
|
111
|
for (var i = 0; i < obj.properties.length; i++) {
|
112
|
var prop = obj.properties[i];
|
113
|
var val = prop.value;
|
114
|
val.properties.unshift(t.objectProperty(t.identifier("key"), t.toComputedKey(prop)));
|
115
|
objExpr.elements.push(val);
|
116
|
}
|
117
|
|
118
|
return objExpr;
|
119
|
}
|
120
|
|
121
|
function toClassObject(mutatorMap) {
|
122
|
var objExpr = t.objectExpression([]);
|
123
|
|
124
|
(0, _keys2.default)(mutatorMap).forEach(function (mutatorMapKey) {
|
125
|
var map = mutatorMap[mutatorMapKey];
|
126
|
var mapNode = t.objectExpression([]);
|
127
|
|
128
|
var propNode = t.objectProperty(map._key, mapNode, map._computed);
|
129
|
|
130
|
(0, _keys2.default)(map).forEach(function (key) {
|
131
|
var node = map[key];
|
132
|
if (key[0] === "_") return;
|
133
|
|
134
|
var inheritNode = node;
|
135
|
if (t.isClassMethod(node) || t.isClassProperty(node)) node = node.value;
|
136
|
|
137
|
var prop = t.objectProperty(t.identifier(key), node);
|
138
|
t.inheritsComments(prop, inheritNode);
|
139
|
t.removeComments(inheritNode);
|
140
|
|
141
|
mapNode.properties.push(prop);
|
142
|
});
|
143
|
|
144
|
objExpr.properties.push(propNode);
|
145
|
});
|
146
|
|
147
|
return objExpr;
|
148
|
}
|
149
|
|
150
|
function toDefineObject(mutatorMap) {
|
151
|
(0, _keys2.default)(mutatorMap).forEach(function (key) {
|
152
|
var map = mutatorMap[key];
|
153
|
if (map.value) map.writable = t.booleanLiteral(true);
|
154
|
map.configurable = t.booleanLiteral(true);
|
155
|
map.enumerable = t.booleanLiteral(true);
|
156
|
});
|
157
|
|
158
|
return toClassObject(mutatorMap);
|
159
|
}
|