index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var helperPluginUtils = require('@babel/helper-plugin-utils');
  4. var core = require('@babel/core');
  5. function needsWrapping(node) {
  6. if (core.types.isLiteral(node) && !core.types.isTemplateLiteral(node)) {
  7. return false;
  8. }
  9. if (core.types.isCallExpression(node) || core.types.isOptionalCallExpression(node) || core.types.isNewExpression(node)) {
  10. return needsWrapping(node.callee) || node.arguments.some(needsWrapping);
  11. }
  12. if (core.types.isTemplateLiteral(node)) {
  13. return node.expressions.some(needsWrapping);
  14. }
  15. if (core.types.isTaggedTemplateExpression(node)) {
  16. return needsWrapping(node.tag) || needsWrapping(node.quasi);
  17. }
  18. if (core.types.isArrayExpression(node)) {
  19. return node.elements.some(needsWrapping);
  20. }
  21. if (core.types.isObjectExpression(node)) {
  22. return node.properties.some(prop => {
  23. if (core.types.isObjectProperty(prop)) {
  24. return needsWrapping(prop.value) || prop.computed && needsWrapping(prop.key);
  25. }
  26. if (core.types.isObjectMethod(prop)) {
  27. return false;
  28. }
  29. return false;
  30. });
  31. }
  32. if (core.types.isMemberExpression(node) || core.types.isOptionalMemberExpression(node)) {
  33. return needsWrapping(node.object) || node.computed && needsWrapping(node.property);
  34. }
  35. if (core.types.isFunctionExpression(node) || core.types.isArrowFunctionExpression(node) || core.types.isClassExpression(node)) {
  36. return false;
  37. }
  38. if (core.types.isThisExpression(node)) {
  39. return false;
  40. }
  41. if (core.types.isSequenceExpression(node)) {
  42. return node.expressions.some(needsWrapping);
  43. }
  44. return true;
  45. }
  46. function wrapInitializer(path) {
  47. const {
  48. value
  49. } = path.node;
  50. if (value && needsWrapping(value)) {
  51. path.node.value = core.types.callExpression(core.types.arrowFunctionExpression([], value), []);
  52. }
  53. }
  54. var index = helperPluginUtils.declare(api => {
  55. api.assertVersion("^7.16.0");
  56. return {
  57. name: "plugin-bugfix-safari-class-field-initializer-scope",
  58. visitor: {
  59. ClassProperty(path) {
  60. wrapInitializer(path);
  61. },
  62. ClassPrivateProperty(path) {
  63. wrapInitializer(path);
  64. }
  65. }
  66. };
  67. });
  68. exports.default = index;
  69. //# sourceMappingURL=index.js.map