You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.6 KiB
143 lines
4.6 KiB
4 years ago
|
"use strict";
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.push = push;
|
||
|
exports.hasComputed = hasComputed;
|
||
|
exports.toComputedObjectFromClass = toComputedObjectFromClass;
|
||
|
exports.toClassObject = toClassObject;
|
||
|
exports.toDefineObject = toDefineObject;
|
||
|
|
||
|
var _helperFunctionName = _interopRequireDefault(require("@babel/helper-function-name"));
|
||
|
|
||
|
var _has = _interopRequireDefault(require("lodash/has"));
|
||
|
|
||
|
var t = _interopRequireWildcard(require("@babel/types"));
|
||
|
|
||
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||
|
|
||
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
function toKind(node) {
|
||
|
if (t.isClassMethod(node) || t.isObjectMethod(node)) {
|
||
|
if (node.kind === "get" || node.kind === "set") {
|
||
|
return node.kind;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return "value";
|
||
|
}
|
||
|
|
||
|
function push(mutatorMap, node, kind, file, scope) {
|
||
|
const alias = t.toKeyAlias(node);
|
||
|
let map = {};
|
||
|
if ((0, _has.default)(mutatorMap, alias)) map = mutatorMap[alias];
|
||
|
mutatorMap[alias] = map;
|
||
|
map._inherits = map._inherits || [];
|
||
|
|
||
|
map._inherits.push(node);
|
||
|
|
||
|
map._key = node.key;
|
||
|
|
||
|
if (node.computed) {
|
||
|
map._computed = true;
|
||
|
}
|
||
|
|
||
|
if (node.decorators) {
|
||
|
const decorators = map.decorators = map.decorators || t.arrayExpression([]);
|
||
|
decorators.elements = decorators.elements.concat(node.decorators.map(dec => dec.expression).reverse());
|
||
|
}
|
||
|
|
||
|
if (map.value || map.initializer) {
|
||
|
throw file.buildCodeFrameError(node, "Key conflict with sibling node");
|
||
|
}
|
||
|
|
||
|
let key, value;
|
||
|
|
||
|
if (t.isObjectProperty(node) || t.isObjectMethod(node) || t.isClassMethod(node)) {
|
||
|
key = t.toComputedKey(node, node.key);
|
||
|
}
|
||
|
|
||
|
if (t.isProperty(node)) {
|
||
|
value = node.value;
|
||
|
} else if (t.isObjectMethod(node) || t.isClassMethod(node)) {
|
||
|
value = t.functionExpression(null, node.params, node.body, node.generator, node.async);
|
||
|
value.returnType = node.returnType;
|
||
|
}
|
||
|
|
||
|
const inheritedKind = toKind(node);
|
||
|
|
||
|
if (!kind || inheritedKind !== "value") {
|
||
|
kind = inheritedKind;
|
||
|
}
|
||
|
|
||
|
if (scope && t.isStringLiteral(key) && (kind === "value" || kind === "initializer") && t.isFunctionExpression(value)) {
|
||
|
value = (0, _helperFunctionName.default)({
|
||
|
id: key,
|
||
|
node: value,
|
||
|
scope
|
||
|
});
|
||
|
}
|
||
|
|
||
|
if (value) {
|
||
|
t.inheritsComments(value, node);
|
||
|
map[kind] = value;
|
||
|
}
|
||
|
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
function hasComputed(mutatorMap) {
|
||
|
for (const key of Object.keys(mutatorMap)) {
|
||
|
if (mutatorMap[key]._computed) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
function toComputedObjectFromClass(obj) {
|
||
|
const objExpr = t.arrayExpression([]);
|
||
|
|
||
|
for (let i = 0; i < obj.properties.length; i++) {
|
||
|
const prop = obj.properties[i];
|
||
|
const val = prop.value;
|
||
|
val.properties.unshift(t.objectProperty(t.identifier("key"), t.toComputedKey(prop)));
|
||
|
objExpr.elements.push(val);
|
||
|
}
|
||
|
|
||
|
return objExpr;
|
||
|
}
|
||
|
|
||
|
function toClassObject(mutatorMap) {
|
||
|
const objExpr = t.objectExpression([]);
|
||
|
Object.keys(mutatorMap).forEach(function (mutatorMapKey) {
|
||
|
const map = mutatorMap[mutatorMapKey];
|
||
|
const mapNode = t.objectExpression([]);
|
||
|
const propNode = t.objectProperty(map._key, mapNode, map._computed);
|
||
|
Object.keys(map).forEach(function (key) {
|
||
|
const node = map[key];
|
||
|
if (key[0] === "_") return;
|
||
|
const prop = t.objectProperty(t.identifier(key), node);
|
||
|
t.inheritsComments(prop, node);
|
||
|
t.removeComments(node);
|
||
|
mapNode.properties.push(prop);
|
||
|
});
|
||
|
objExpr.properties.push(propNode);
|
||
|
});
|
||
|
return objExpr;
|
||
|
}
|
||
|
|
||
|
function toDefineObject(mutatorMap) {
|
||
|
Object.keys(mutatorMap).forEach(function (key) {
|
||
|
const map = mutatorMap[key];
|
||
|
if (map.value) map.writable = t.booleanLiteral(true);
|
||
|
map.configurable = t.booleanLiteral(true);
|
||
|
map.enumerable = t.booleanLiteral(true);
|
||
|
});
|
||
|
return toClassObject(mutatorMap);
|
||
|
}
|