index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var helperPluginUtils = require('@babel/helper-plugin-utils');
  4. var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
  5. var syntaxOptionalChaining = require('@babel/plugin-syntax-optional-chaining');
  6. var core = require('@babel/core');
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. var syntaxOptionalChaining__default = /*#__PURE__*/_interopDefaultLegacy(syntaxOptionalChaining);
  9. function willPathCastToBoolean(path) {
  10. const maybeWrapped = findOutermostTransparentParent(path);
  11. const {
  12. node,
  13. parentPath
  14. } = maybeWrapped;
  15. if (parentPath.isLogicalExpression()) {
  16. const {
  17. operator,
  18. right
  19. } = parentPath.node;
  20. if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
  21. return willPathCastToBoolean(parentPath);
  22. }
  23. }
  24. if (parentPath.isSequenceExpression()) {
  25. const {
  26. expressions
  27. } = parentPath.node;
  28. if (expressions[expressions.length - 1] === node) {
  29. return willPathCastToBoolean(parentPath);
  30. } else {
  31. return true;
  32. }
  33. }
  34. return parentPath.isConditional({
  35. test: node
  36. }) || parentPath.isUnaryExpression({
  37. operator: "!"
  38. }) || parentPath.isLoop({
  39. test: node
  40. });
  41. }
  42. function findOutermostTransparentParent(path) {
  43. let maybeWrapped = path;
  44. path.findParent(p => {
  45. if (!helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(p)) return true;
  46. maybeWrapped = p;
  47. });
  48. return maybeWrapped;
  49. }
  50. const {
  51. ast
  52. } = core.template.expression;
  53. var index = helperPluginUtils.declare((api, options) => {
  54. api.assertVersion(7);
  55. const {
  56. loose = false
  57. } = options;
  58. function isSimpleMemberExpression(expression) {
  59. expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(expression);
  60. return core.types.isIdentifier(expression) || core.types.isSuper(expression) || core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
  61. }
  62. function needsMemoize(path) {
  63. let optionalPath = path;
  64. const {
  65. scope
  66. } = path;
  67. while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
  68. const {
  69. node
  70. } = optionalPath;
  71. const childKey = optionalPath.isOptionalMemberExpression() ? "object" : "callee";
  72. const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get(childKey));
  73. if (node.optional) {
  74. return !scope.isStatic(childPath.node);
  75. }
  76. optionalPath = childPath;
  77. }
  78. }
  79. return {
  80. name: "proposal-optional-chaining",
  81. inherits: syntaxOptionalChaining__default['default'],
  82. visitor: {
  83. "OptionalCallExpression|OptionalMemberExpression"(path) {
  84. const {
  85. scope
  86. } = path;
  87. const maybeWrapped = findOutermostTransparentParent(path);
  88. const {
  89. parentPath
  90. } = maybeWrapped;
  91. const willReplacementCastToBoolean = willPathCastToBoolean(maybeWrapped);
  92. let isDeleteOperation = false;
  93. const parentIsCall = parentPath.isCallExpression({
  94. callee: maybeWrapped.node
  95. }) && path.isOptionalMemberExpression();
  96. const optionals = [];
  97. let optionalPath = path;
  98. if (scope.path.isPattern() && needsMemoize(optionalPath)) {
  99. path.replaceWith(core.template.ast`(() => ${path.node})()`);
  100. return;
  101. }
  102. while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
  103. const {
  104. node
  105. } = optionalPath;
  106. if (node.optional) {
  107. optionals.push(node);
  108. }
  109. if (optionalPath.isOptionalMemberExpression()) {
  110. optionalPath.node.type = "MemberExpression";
  111. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
  112. } else if (optionalPath.isOptionalCallExpression()) {
  113. optionalPath.node.type = "CallExpression";
  114. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
  115. }
  116. }
  117. let replacementPath = path;
  118. if (parentPath.isUnaryExpression({
  119. operator: "delete"
  120. })) {
  121. replacementPath = parentPath;
  122. isDeleteOperation = true;
  123. }
  124. for (let i = optionals.length - 1; i >= 0; i--) {
  125. const node = optionals[i];
  126. const isCall = core.types.isCallExpression(node);
  127. const replaceKey = isCall ? "callee" : "object";
  128. const chainWithTypes = node[replaceKey];
  129. let chain = chainWithTypes;
  130. while (helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(chain)) {
  131. chain = chain.expression;
  132. }
  133. let ref;
  134. let check;
  135. if (isCall && core.types.isIdentifier(chain, {
  136. name: "eval"
  137. })) {
  138. check = ref = chain;
  139. node[replaceKey] = core.types.sequenceExpression([core.types.numericLiteral(0), ref]);
  140. } else if (loose && isCall && isSimpleMemberExpression(chain)) {
  141. check = ref = chainWithTypes;
  142. } else {
  143. ref = scope.maybeGenerateMemoised(chain);
  144. if (ref) {
  145. check = core.types.assignmentExpression("=", core.types.cloneNode(ref), chainWithTypes);
  146. node[replaceKey] = ref;
  147. } else {
  148. check = ref = chainWithTypes;
  149. }
  150. }
  151. if (isCall && core.types.isMemberExpression(chain)) {
  152. if (loose && isSimpleMemberExpression(chain)) {
  153. node.callee = chainWithTypes;
  154. } else {
  155. const {
  156. object
  157. } = chain;
  158. let context = scope.maybeGenerateMemoised(object);
  159. if (context) {
  160. chain.object = core.types.assignmentExpression("=", context, object);
  161. } else if (core.types.isSuper(object)) {
  162. context = core.types.thisExpression();
  163. } else {
  164. context = object;
  165. }
  166. node.arguments.unshift(core.types.cloneNode(context));
  167. node.callee = core.types.memberExpression(node.callee, core.types.identifier("call"));
  168. }
  169. }
  170. let replacement = replacementPath.node;
  171. if (i === 0 && parentIsCall) {
  172. var _baseRef;
  173. const object = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("object")).node;
  174. let baseRef;
  175. if (!loose || !isSimpleMemberExpression(object)) {
  176. baseRef = scope.maybeGenerateMemoised(object);
  177. if (baseRef) {
  178. replacement.object = core.types.assignmentExpression("=", baseRef, object);
  179. }
  180. }
  181. replacement = core.types.callExpression(core.types.memberExpression(replacement, core.types.identifier("bind")), [core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
  182. }
  183. if (willReplacementCastToBoolean) {
  184. const nonNullishCheck = loose ? ast`${core.types.cloneNode(check)} != null` : ast`
  185. ${core.types.cloneNode(check)} !== null && ${core.types.cloneNode(ref)} !== void 0`;
  186. replacementPath.replaceWith(core.types.logicalExpression("&&", nonNullishCheck, replacement));
  187. replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("right"));
  188. } else {
  189. const nullishCheck = loose ? ast`${core.types.cloneNode(check)} == null` : ast`
  190. ${core.types.cloneNode(check)} === null || ${core.types.cloneNode(ref)} === void 0`;
  191. const returnValue = isDeleteOperation ? ast`true` : ast`void 0`;
  192. replacementPath.replaceWith(core.types.conditionalExpression(nullishCheck, returnValue, replacement));
  193. replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("alternate"));
  194. }
  195. }
  196. }
  197. }
  198. };
  199. });
  200. exports.default = index;
  201. //# sourceMappingURL=index.js.map