index.js 2.4 KB

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