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.

142 lines
4.6 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.push = push;
  6. exports.hasComputed = hasComputed;
  7. exports.toComputedObjectFromClass = toComputedObjectFromClass;
  8. exports.toClassObject = toClassObject;
  9. exports.toDefineObject = toDefineObject;
  10. var _helperFunctionName = _interopRequireDefault(require("@babel/helper-function-name"));
  11. var _has = _interopRequireDefault(require("lodash/has"));
  12. var t = _interopRequireWildcard(require("@babel/types"));
  13. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  14. 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; }
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function toKind(node) {
  17. if (t.isClassMethod(node) || t.isObjectMethod(node)) {
  18. if (node.kind === "get" || node.kind === "set") {
  19. return node.kind;
  20. }
  21. }
  22. return "value";
  23. }
  24. function push(mutatorMap, node, kind, file, scope) {
  25. const alias = t.toKeyAlias(node);
  26. let map = {};
  27. if ((0, _has.default)(mutatorMap, alias)) map = mutatorMap[alias];
  28. mutatorMap[alias] = map;
  29. map._inherits = map._inherits || [];
  30. map._inherits.push(node);
  31. map._key = node.key;
  32. if (node.computed) {
  33. map._computed = true;
  34. }
  35. if (node.decorators) {
  36. const decorators = map.decorators = map.decorators || t.arrayExpression([]);
  37. decorators.elements = decorators.elements.concat(node.decorators.map(dec => dec.expression).reverse());
  38. }
  39. if (map.value || map.initializer) {
  40. throw file.buildCodeFrameError(node, "Key conflict with sibling node");
  41. }
  42. let key, value;
  43. if (t.isObjectProperty(node) || t.isObjectMethod(node) || t.isClassMethod(node)) {
  44. key = t.toComputedKey(node, node.key);
  45. }
  46. if (t.isProperty(node)) {
  47. value = node.value;
  48. } else if (t.isObjectMethod(node) || t.isClassMethod(node)) {
  49. value = t.functionExpression(null, node.params, node.body, node.generator, node.async);
  50. value.returnType = node.returnType;
  51. }
  52. const inheritedKind = toKind(node);
  53. if (!kind || inheritedKind !== "value") {
  54. kind = inheritedKind;
  55. }
  56. if (scope && t.isStringLiteral(key) && (kind === "value" || kind === "initializer") && t.isFunctionExpression(value)) {
  57. value = (0, _helperFunctionName.default)({
  58. id: key,
  59. node: value,
  60. scope
  61. });
  62. }
  63. if (value) {
  64. t.inheritsComments(value, node);
  65. map[kind] = value;
  66. }
  67. return map;
  68. }
  69. function hasComputed(mutatorMap) {
  70. for (const key of Object.keys(mutatorMap)) {
  71. if (mutatorMap[key]._computed) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. function toComputedObjectFromClass(obj) {
  78. const objExpr = t.arrayExpression([]);
  79. for (let i = 0; i < obj.properties.length; i++) {
  80. const prop = obj.properties[i];
  81. const val = prop.value;
  82. val.properties.unshift(t.objectProperty(t.identifier("key"), t.toComputedKey(prop)));
  83. objExpr.elements.push(val);
  84. }
  85. return objExpr;
  86. }
  87. function toClassObject(mutatorMap) {
  88. const objExpr = t.objectExpression([]);
  89. Object.keys(mutatorMap).forEach(function (mutatorMapKey) {
  90. const map = mutatorMap[mutatorMapKey];
  91. const mapNode = t.objectExpression([]);
  92. const propNode = t.objectProperty(map._key, mapNode, map._computed);
  93. Object.keys(map).forEach(function (key) {
  94. const node = map[key];
  95. if (key[0] === "_") return;
  96. const prop = t.objectProperty(t.identifier(key), node);
  97. t.inheritsComments(prop, node);
  98. t.removeComments(node);
  99. mapNode.properties.push(prop);
  100. });
  101. objExpr.properties.push(propNode);
  102. });
  103. return objExpr;
  104. }
  105. function toDefineObject(mutatorMap) {
  106. Object.keys(mutatorMap).forEach(function (key) {
  107. const map = mutatorMap[key];
  108. if (map.value) map.writable = t.booleanLiteral(true);
  109. map.configurable = t.booleanLiteral(true);
  110. map.enumerable = t.booleanLiteral(true);
  111. });
  112. return toClassObject(mutatorMap);
  113. }