index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var helperPluginUtils = require('@babel/helper-plugin-utils');
  4. var syntaxOptionalChaining = require('@babel/plugin-syntax-optional-chaining');
  5. var core = require('@babel/core');
  6. var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
  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.node)) return true;
  46. maybeWrapped = p;
  47. });
  48. return maybeWrapped;
  49. }
  50. const {
  51. ast
  52. } = core.template.expression;
  53. function isSimpleMemberExpression(expression) {
  54. expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(expression);
  55. return core.types.isIdentifier(expression) || core.types.isSuper(expression) || core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
  56. }
  57. function needsMemoize(path) {
  58. let optionalPath = path;
  59. const {
  60. scope
  61. } = path;
  62. while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
  63. const {
  64. node
  65. } = optionalPath;
  66. const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee"));
  67. if (node.optional) {
  68. return !scope.isStatic(childPath.node);
  69. }
  70. optionalPath = childPath;
  71. }
  72. }
  73. function transform(path, {
  74. pureGetters,
  75. noDocumentAll
  76. }) {
  77. const {
  78. scope
  79. } = path;
  80. const maybeWrapped = findOutermostTransparentParent(path);
  81. const {
  82. parentPath
  83. } = maybeWrapped;
  84. const willReplacementCastToBoolean = willPathCastToBoolean(maybeWrapped);
  85. let isDeleteOperation = false;
  86. const parentIsCall = parentPath.isCallExpression({
  87. callee: maybeWrapped.node
  88. }) && path.isOptionalMemberExpression();
  89. const optionals = [];
  90. let optionalPath = path;
  91. if (scope.path.isPattern() && needsMemoize(optionalPath)) {
  92. path.replaceWith(core.template.ast`(() => ${path.node})()`);
  93. return;
  94. }
  95. while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
  96. const {
  97. node
  98. } = optionalPath;
  99. if (node.optional) {
  100. optionals.push(node);
  101. }
  102. if (optionalPath.isOptionalMemberExpression()) {
  103. optionalPath.node.type = "MemberExpression";
  104. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
  105. } else if (optionalPath.isOptionalCallExpression()) {
  106. optionalPath.node.type = "CallExpression";
  107. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
  108. }
  109. }
  110. let replacementPath = path;
  111. if (parentPath.isUnaryExpression({
  112. operator: "delete"
  113. })) {
  114. replacementPath = parentPath;
  115. isDeleteOperation = true;
  116. }
  117. for (let i = optionals.length - 1; i >= 0; i--) {
  118. const node = optionals[i];
  119. const isCall = core.types.isCallExpression(node);
  120. const chainWithTypes = isCall ? node.callee : node.object;
  121. const chain = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(chainWithTypes);
  122. let ref;
  123. let check;
  124. if (isCall && core.types.isIdentifier(chain, {
  125. name: "eval"
  126. })) {
  127. check = ref = chain;
  128. node.callee = core.types.sequenceExpression([core.types.numericLiteral(0), ref]);
  129. } else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
  130. check = ref = node.callee;
  131. } else {
  132. ref = scope.maybeGenerateMemoised(chain);
  133. if (ref) {
  134. check = core.types.assignmentExpression("=", core.types.cloneNode(ref), chainWithTypes);
  135. isCall ? node.callee = ref : node.object = ref;
  136. } else {
  137. check = ref = chainWithTypes;
  138. }
  139. }
  140. if (isCall && core.types.isMemberExpression(chain)) {
  141. if (pureGetters && isSimpleMemberExpression(chain)) {
  142. node.callee = chainWithTypes;
  143. } else {
  144. const {
  145. object
  146. } = chain;
  147. let context;
  148. if (core.types.isSuper(object)) {
  149. context = core.types.thisExpression();
  150. } else {
  151. const memoized = scope.maybeGenerateMemoised(object);
  152. if (memoized) {
  153. context = memoized;
  154. chain.object = core.types.assignmentExpression("=", memoized, object);
  155. } else {
  156. context = object;
  157. }
  158. }
  159. node.arguments.unshift(core.types.cloneNode(context));
  160. node.callee = core.types.memberExpression(node.callee, core.types.identifier("call"));
  161. }
  162. }
  163. let replacement = replacementPath.node;
  164. if (i === 0 && parentIsCall) {
  165. var _baseRef;
  166. const object = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(replacement.object);
  167. let baseRef;
  168. if (!pureGetters || !isSimpleMemberExpression(object)) {
  169. baseRef = scope.maybeGenerateMemoised(object);
  170. if (baseRef) {
  171. replacement.object = core.types.assignmentExpression("=", baseRef, object);
  172. }
  173. }
  174. replacement = core.types.callExpression(core.types.memberExpression(replacement, core.types.identifier("bind")), [core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
  175. }
  176. if (willReplacementCastToBoolean) {
  177. const nonNullishCheck = noDocumentAll ? ast`${core.types.cloneNode(check)} != null` : ast`
  178. ${core.types.cloneNode(check)} !== null && ${core.types.cloneNode(ref)} !== void 0`;
  179. replacementPath.replaceWith(core.types.logicalExpression("&&", nonNullishCheck, replacement));
  180. replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("right"));
  181. } else {
  182. const nullishCheck = noDocumentAll ? ast`${core.types.cloneNode(check)} == null` : ast`
  183. ${core.types.cloneNode(check)} === null || ${core.types.cloneNode(ref)} === void 0`;
  184. const returnValue = isDeleteOperation ? ast`true` : ast`void 0`;
  185. replacementPath.replaceWith(core.types.conditionalExpression(nullishCheck, returnValue, replacement));
  186. replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("alternate"));
  187. }
  188. }
  189. }
  190. var index = helperPluginUtils.declare((api, options) => {
  191. var _api$assumption, _api$assumption2;
  192. api.assertVersion(7);
  193. const {
  194. loose = false
  195. } = options;
  196. const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : loose;
  197. const pureGetters = (_api$assumption2 = api.assumption("pureGetters")) != null ? _api$assumption2 : loose;
  198. return {
  199. name: "proposal-optional-chaining",
  200. inherits: syntaxOptionalChaining__default["default"].default,
  201. visitor: {
  202. "OptionalCallExpression|OptionalMemberExpression"(path) {
  203. transform(path, {
  204. noDocumentAll,
  205. pureGetters
  206. });
  207. }
  208. }
  209. };
  210. });
  211. exports["default"] = index;
  212. exports.transform = transform;
  213. //# sourceMappingURL=index.js.map