index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var helperPluginUtils = require('@babel/helper-plugin-utils');
  4. var pluginTransformOptionalChaining = require('@babel/plugin-transform-optional-chaining');
  5. var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
  6. var core = require('@babel/core');
  7. function matchAffectedArguments(argumentNodes) {
  8. const spreadIndex = argumentNodes.findIndex(node => core.types.isSpreadElement(node));
  9. return spreadIndex >= 0 && spreadIndex !== argumentNodes.length - 1;
  10. }
  11. function shouldTransform(path) {
  12. let optionalPath = path;
  13. const chains = [];
  14. for (;;) {
  15. if (optionalPath.isOptionalMemberExpression()) {
  16. chains.push(optionalPath.node);
  17. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
  18. } else if (optionalPath.isOptionalCallExpression()) {
  19. chains.push(optionalPath.node);
  20. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
  21. } else {
  22. break;
  23. }
  24. }
  25. for (let i = 0; i < chains.length; i++) {
  26. const node = chains[i];
  27. if (core.types.isOptionalCallExpression(node) && matchAffectedArguments(node.arguments)) {
  28. if (node.optional) {
  29. return true;
  30. }
  31. const callee = chains[i + 1];
  32. if (core.types.isOptionalMemberExpression(callee, {
  33. optional: true
  34. })) {
  35. return true;
  36. }
  37. }
  38. }
  39. return false;
  40. }
  41. var index = helperPluginUtils.declare(api => {
  42. var _api$assumption, _api$assumption2;
  43. api.assertVersion(7);
  44. const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : false;
  45. const pureGetters = (_api$assumption2 = api.assumption("pureGetters")) != null ? _api$assumption2 : false;
  46. return {
  47. name: "bugfix-v8-spread-parameters-in-optional-chaining",
  48. visitor: {
  49. "OptionalCallExpression|OptionalMemberExpression"(path) {
  50. if (shouldTransform(path)) {
  51. pluginTransformOptionalChaining.transform(path, {
  52. noDocumentAll,
  53. pureGetters
  54. });
  55. }
  56. }
  57. }
  58. };
  59. });
  60. exports.default = index;
  61. //# sourceMappingURL=index.js.map