index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. {
  39. path.traverse(containsClassExpressionVisitor, state);
  40. }
  41. return state.found;
  42. }
  43. function wrap(path) {
  44. const context = {
  45. yield: t.isYieldExpression(path.node),
  46. await: t.isAwaitExpression(path.node)
  47. };
  48. {
  49. path.traverse(containsYieldOrAwaitVisitor, context);
  50. }
  51. let replacement;
  52. if (context.yield) {
  53. const fn = t.functionExpression(null, [], t.blockStatement([t.returnStatement(path.node)]), true, context.await);
  54. replacement = t.yieldExpression(t.callExpression(t.memberExpression(fn, t.identifier("call")), [t.thisExpression(), t.identifier("arguments")]), true);
  55. } else {
  56. const fn = t.arrowFunctionExpression([], path.node, context.await);
  57. replacement = t.callExpression(fn, []);
  58. if (context.await) replacement = t.awaitExpression(replacement);
  59. }
  60. path.replaceWith(replacement);
  61. }
  62. return {
  63. name: "bugfix-firefox-class-in-computed-class-key",
  64. visitor: {
  65. Class(path) {
  66. const hasPrivateElement = path.node.body.body.some(node => t.isPrivate(node));
  67. if (!hasPrivateElement) return;
  68. for (const elem of path.get("body.body")) {
  69. if ("computed" in elem.node && elem.node.computed && containsClassExpression(elem.get("key"))) {
  70. wrap(elem.get("key"));
  71. }
  72. }
  73. }
  74. }
  75. };
  76. });
  77. //# sourceMappingURL=index.js.map