index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _helperPluginUtils() {
  7. const data = require("@babel/helper-plugin-utils");
  8. _helperPluginUtils = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _core() {
  14. const data = require("@babel/core");
  15. _core = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function getName(key) {
  21. if (_core().types.isIdentifier(key)) {
  22. return key.name;
  23. }
  24. return key.value.toString();
  25. }
  26. var _default = (0, _helperPluginUtils().declare)(api => {
  27. api.assertVersion(7);
  28. return {
  29. name: "transform-duplicate-keys",
  30. visitor: {
  31. ObjectExpression(path) {
  32. const {
  33. node
  34. } = path;
  35. const plainProps = node.properties.filter(prop => !_core().types.isSpreadElement(prop) && !prop.computed);
  36. const alreadySeenData = Object.create(null);
  37. const alreadySeenGetters = Object.create(null);
  38. const alreadySeenSetters = Object.create(null);
  39. for (const prop of plainProps) {
  40. const name = getName(prop.key);
  41. let isDuplicate = false;
  42. switch (prop.kind) {
  43. case "get":
  44. if (alreadySeenData[name] || alreadySeenGetters[name]) {
  45. isDuplicate = true;
  46. }
  47. alreadySeenGetters[name] = true;
  48. break;
  49. case "set":
  50. if (alreadySeenData[name] || alreadySeenSetters[name]) {
  51. isDuplicate = true;
  52. }
  53. alreadySeenSetters[name] = true;
  54. break;
  55. default:
  56. if (alreadySeenData[name] || alreadySeenGetters[name] || alreadySeenSetters[name]) {
  57. isDuplicate = true;
  58. }
  59. alreadySeenData[name] = true;
  60. }
  61. if (isDuplicate) {
  62. prop.computed = true;
  63. prop.key = _core().types.stringLiteral(name);
  64. }
  65. }
  66. }
  67. }
  68. };
  69. });
  70. exports.default = _default;