index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _core = require("@babel/core");
  8. var _default = (0, _helperPluginUtils.declare)(api => {
  9. api.assertVersion(7);
  10. return {
  11. name: "transform-shorthand-properties",
  12. visitor: {
  13. ObjectMethod(path) {
  14. const {
  15. node
  16. } = path;
  17. if (node.kind === "method") {
  18. const func = _core.types.functionExpression(null, node.params, node.body, node.generator, node.async);
  19. func.returnType = node.returnType;
  20. const computedKey = _core.types.toComputedKey(node);
  21. if (_core.types.isStringLiteral(computedKey, {
  22. value: "__proto__"
  23. })) {
  24. path.replaceWith(_core.types.objectProperty(computedKey, func, true));
  25. } else {
  26. path.replaceWith(_core.types.objectProperty(node.key, func, node.computed));
  27. }
  28. }
  29. },
  30. ObjectProperty(path) {
  31. const {
  32. node
  33. } = path;
  34. if (node.shorthand) {
  35. const computedKey = _core.types.toComputedKey(node);
  36. if (_core.types.isStringLiteral(computedKey, {
  37. value: "__proto__"
  38. })) {
  39. path.replaceWith(_core.types.objectProperty(computedKey, node.value, true));
  40. } else {
  41. node.shorthand = false;
  42. }
  43. }
  44. }
  45. }
  46. };
  47. });
  48. exports.default = _default;