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