index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var helperPluginUtils = require('@babel/helper-plugin-utils');
  4. var pluginProposalOptionalChaining = require('@babel/plugin-proposal-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. while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
  15. const {
  16. node
  17. } = optionalPath;
  18. chains.push(node);
  19. if (optionalPath.isOptionalMemberExpression()) {
  20. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
  21. } else if (optionalPath.isOptionalCallExpression()) {
  22. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
  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. api.assertVersion(7);
  43. const noDocumentAll = api.assumption("noDocumentAll");
  44. const pureGetters = api.assumption("pureGetters");
  45. return {
  46. name: "bugfix-v8-spread-parameters-in-optional-chaining",
  47. visitor: {
  48. "OptionalCallExpression|OptionalMemberExpression"(path) {
  49. if (shouldTransform(path)) {
  50. pluginProposalOptionalChaining.transform(path, {
  51. noDocumentAll,
  52. pureGetters
  53. });
  54. }
  55. }
  56. }
  57. };
  58. });
  59. exports.default = index;
  60. //# sourceMappingURL=index.js.map