index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
  6. function willPathCastToBoolean(path) {
  7. const maybeWrapped = findOutermostTransparentParent(path);
  8. const {
  9. node,
  10. parentPath
  11. } = maybeWrapped;
  12. if (parentPath.isLogicalExpression()) {
  13. const {
  14. operator,
  15. right
  16. } = parentPath.node;
  17. if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
  18. return willPathCastToBoolean(parentPath);
  19. }
  20. }
  21. if (parentPath.isSequenceExpression()) {
  22. const {
  23. expressions
  24. } = parentPath.node;
  25. if (expressions[expressions.length - 1] === node) {
  26. return willPathCastToBoolean(parentPath);
  27. } else {
  28. return true;
  29. }
  30. }
  31. return parentPath.isConditional({
  32. test: node
  33. }) || parentPath.isUnaryExpression({
  34. operator: "!"
  35. }) || parentPath.isLoop({
  36. test: node
  37. });
  38. }
  39. function findOutermostTransparentParent(path) {
  40. let maybeWrapped = path;
  41. path.findParent(p => {
  42. if (!helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(p.node)) return true;
  43. maybeWrapped = p;
  44. });
  45. return maybeWrapped;
  46. }
  47. const last = arr => arr[arr.length - 1];
  48. function isSimpleMemberExpression(expression) {
  49. expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(expression);
  50. return core.types.isIdentifier(expression) || core.types.isSuper(expression) || core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
  51. }
  52. function needsMemoize(path) {
  53. let optionalPath = path;
  54. const {
  55. scope
  56. } = path;
  57. while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
  58. const {
  59. node
  60. } = optionalPath;
  61. const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee"));
  62. if (node.optional) {
  63. return !scope.isStatic(childPath.node);
  64. }
  65. optionalPath = childPath;
  66. }
  67. }
  68. const NULLISH_CHECK = core.template.expression(`%%check%% === null || %%ref%% === void 0`);
  69. const NULLISH_CHECK_NO_DDA = core.template.expression(`%%check%% == null`);
  70. const NULLISH_CHECK_NEG = core.template.expression(`%%check%% !== null && %%ref%% !== void 0`);
  71. const NULLISH_CHECK_NO_DDA_NEG = core.template.expression(`%%check%% != null`);
  72. function transformOptionalChain(path, {
  73. pureGetters,
  74. noDocumentAll
  75. }, replacementPath, ifNullish, wrapLast) {
  76. const {
  77. scope
  78. } = path;
  79. if (scope.path.isPattern() && needsMemoize(path)) {
  80. replacementPath.replaceWith(core.template.expression.ast`(() => ${replacementPath.node})()`);
  81. return;
  82. }
  83. const optionals = [];
  84. let optionalPath = path;
  85. while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
  86. const {
  87. node
  88. } = optionalPath;
  89. if (node.optional) {
  90. optionals.push(node);
  91. }
  92. if (optionalPath.isOptionalMemberExpression()) {
  93. optionalPath.node.type = "MemberExpression";
  94. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
  95. } else if (optionalPath.isOptionalCallExpression()) {
  96. optionalPath.node.type = "CallExpression";
  97. optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
  98. }
  99. }
  100. if (optionals.length === 0) {
  101. return;
  102. }
  103. const checks = [];
  104. let tmpVar;
  105. for (let i = optionals.length - 1; i >= 0; i--) {
  106. const node = optionals[i];
  107. const isCall = core.types.isCallExpression(node);
  108. const chainWithTypes = isCall ? node.callee : node.object;
  109. const chain = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(chainWithTypes);
  110. let ref;
  111. let check;
  112. if (isCall && core.types.isIdentifier(chain, {
  113. name: "eval"
  114. })) {
  115. check = ref = chain;
  116. node.callee = core.types.sequenceExpression([core.types.numericLiteral(0), ref]);
  117. } else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
  118. check = ref = node.callee;
  119. } else if (scope.isStatic(chain)) {
  120. check = ref = chainWithTypes;
  121. } else {
  122. if (!tmpVar || isCall) {
  123. tmpVar = scope.generateUidIdentifierBasedOnNode(chain);
  124. scope.push({
  125. id: core.types.cloneNode(tmpVar)
  126. });
  127. }
  128. ref = tmpVar;
  129. check = core.types.assignmentExpression("=", core.types.cloneNode(tmpVar), chainWithTypes);
  130. if (isCall) {
  131. node.callee = ref;
  132. } else {
  133. node.object = ref;
  134. }
  135. }
  136. if (isCall && core.types.isMemberExpression(chain)) {
  137. if (pureGetters && isSimpleMemberExpression(chain)) {
  138. node.callee = chainWithTypes;
  139. } else {
  140. const {
  141. object
  142. } = chain;
  143. let context;
  144. if (core.types.isSuper(object)) {
  145. context = core.types.thisExpression();
  146. } else {
  147. const memoized = scope.maybeGenerateMemoised(object);
  148. if (memoized) {
  149. context = memoized;
  150. chain.object = core.types.assignmentExpression("=", memoized, object);
  151. } else {
  152. context = object;
  153. }
  154. }
  155. node.arguments.unshift(core.types.cloneNode(context));
  156. node.callee = core.types.memberExpression(node.callee, core.types.identifier("call"));
  157. }
  158. }
  159. const data = {
  160. check: core.types.cloneNode(check),
  161. ref: core.types.cloneNode(ref)
  162. };
  163. Object.defineProperty(data, "ref", {
  164. enumerable: false
  165. });
  166. checks.push(data);
  167. }
  168. let result = replacementPath.node;
  169. if (wrapLast) result = wrapLast(result);
  170. const ifNullishBoolean = core.types.isBooleanLiteral(ifNullish);
  171. const ifNullishFalse = ifNullishBoolean && ifNullish.value === false;
  172. const ifNullishVoid = !ifNullishBoolean && core.types.isUnaryExpression(ifNullish, {
  173. operator: "void"
  174. });
  175. const isEvaluationValueIgnored = core.types.isExpressionStatement(replacementPath.parent) && !replacementPath.isCompletionRecord() || core.types.isSequenceExpression(replacementPath.parent) && last(replacementPath.parent.expressions) !== replacementPath.node;
  176. const tpl = ifNullishFalse ? noDocumentAll ? NULLISH_CHECK_NO_DDA_NEG : NULLISH_CHECK_NEG : noDocumentAll ? NULLISH_CHECK_NO_DDA : NULLISH_CHECK;
  177. const logicalOp = ifNullishFalse ? "&&" : "||";
  178. const check = checks.map(tpl).reduce((expr, check) => core.types.logicalExpression(logicalOp, expr, check));
  179. replacementPath.replaceWith(ifNullishBoolean || ifNullishVoid && isEvaluationValueIgnored ? core.types.logicalExpression(logicalOp, check, result) : core.types.conditionalExpression(check, ifNullish, result));
  180. }
  181. function transform(path, assumptions) {
  182. const {
  183. scope
  184. } = path;
  185. const maybeWrapped = findOutermostTransparentParent(path);
  186. const {
  187. parentPath
  188. } = maybeWrapped;
  189. if (parentPath.isUnaryExpression({
  190. operator: "delete"
  191. })) {
  192. transformOptionalChain(path, assumptions, parentPath, core.types.booleanLiteral(true));
  193. } else {
  194. let wrapLast;
  195. if (parentPath.isCallExpression({
  196. callee: maybeWrapped.node
  197. }) && path.isOptionalMemberExpression()) {
  198. wrapLast = replacement => {
  199. var _baseRef;
  200. const object = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(replacement.object);
  201. let baseRef;
  202. if (!assumptions.pureGetters || !isSimpleMemberExpression(object)) {
  203. baseRef = scope.maybeGenerateMemoised(object);
  204. if (baseRef) {
  205. replacement.object = core.types.assignmentExpression("=", baseRef, object);
  206. }
  207. }
  208. return core.types.callExpression(core.types.memberExpression(replacement, core.types.identifier("bind")), [core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
  209. };
  210. }
  211. transformOptionalChain(path, assumptions, path, willPathCastToBoolean(maybeWrapped) ? core.types.booleanLiteral(false) : scope.buildUndefinedNode(), wrapLast);
  212. }
  213. }
  214. var index = helperPluginUtils.declare((api, options) => {
  215. var _api$assumption, _api$assumption2;
  216. api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
  217. const {
  218. loose = false
  219. } = options;
  220. const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : loose;
  221. const pureGetters = (_api$assumption2 = api.assumption("pureGetters")) != null ? _api$assumption2 : loose;
  222. return {
  223. name: "transform-optional-chaining",
  224. manipulateOptions: (_, parser) => parser.plugins.push("optionalChaining"),
  225. visitor: {
  226. "OptionalCallExpression|OptionalMemberExpression"(path) {
  227. transform(path, {
  228. noDocumentAll,
  229. pureGetters
  230. });
  231. }
  232. }
  233. };
  234. });
  235. exports.default = index;
  236. exports.transform = transform;
  237. exports.transformOptionalChain = transformOptionalChain;
  238. //# sourceMappingURL=index.js.map