index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _traverse = require("@babel/traverse");
  7. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  8. var _default = exports.default = (0, _helperPluginUtils.declare)(({
  9. types: t,
  10. assertVersion
  11. }) => {
  12. assertVersion(7);
  13. const containsClassExpressionVisitor = {
  14. ClassExpression(path, state) {
  15. state.found = true;
  16. path.stop();
  17. },
  18. Function(path) {
  19. path.skip();
  20. }
  21. };
  22. const containsYieldOrAwaitVisitor = _traverse.visitors.environmentVisitor({
  23. YieldExpression(path, state) {
  24. state.yield = true;
  25. if (state.await) path.stop();
  26. },
  27. AwaitExpression(path, state) {
  28. state.await = true;
  29. if (state.yield) path.stop();
  30. }
  31. });
  32. function containsClassExpression(path) {
  33. if (t.isClassExpression(path.node)) return true;
  34. if (t.isFunction(path.node)) return false;
  35. const state = {
  36. found: false
  37. };
  38. path.traverse(containsClassExpressionVisitor, state);
  39. return state.found;
  40. }
  41. function wrap(path) {
  42. const context = {
  43. yield: t.isYieldExpression(path.node),
  44. await: t.isAwaitExpression(path.node)
  45. };
  46. path.traverse(containsYieldOrAwaitVisitor, context);
  47. let replacement;
  48. if (context.yield) {
  49. const fn = t.functionExpression(null, [], t.blockStatement([t.returnStatement(path.node)]), true, context.await);
  50. replacement = t.yieldExpression(t.callExpression(t.memberExpression(fn, t.identifier("call")), [t.thisExpression(), t.identifier("arguments")]), true);
  51. } else {
  52. const fn = t.arrowFunctionExpression([], path.node, context.await);
  53. replacement = t.callExpression(fn, []);
  54. if (context.await) replacement = t.awaitExpression(replacement);
  55. }
  56. path.replaceWith(replacement);
  57. }
  58. return {
  59. name: "bugfix-firefox-class-in-computed-class-key",
  60. visitor: {
  61. Class(path) {
  62. const hasPrivateElement = path.node.body.body.some(node => t.isPrivate(node));
  63. if (!hasPrivateElement) return;
  64. for (const elem of path.get("body.body")) {
  65. if ("computed" in elem.node && elem.node.computed && containsClassExpression(elem.get("key"))) {
  66. wrap(elem.get("key"));
  67. }
  68. }
  69. }
  70. }
  71. };
  72. });
  73. //# sourceMappingURL=index.js.map