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