@babel/plugin-proposal-optional-chaining
Advanced tools
Comparing version
@@ -10,6 +10,2 @@ 'use strict'; | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var syntaxOptionalChaining__default = /*#__PURE__*/_interopDefaultLegacy(syntaxOptionalChaining); | ||
function willPathCastToBoolean(path) { | ||
@@ -21,3 +17,2 @@ const maybeWrapped = findOutermostTransparentParent(path); | ||
} = maybeWrapped; | ||
if (parentPath.isLogicalExpression()) { | ||
@@ -28,3 +23,2 @@ const { | ||
} = parentPath.node; | ||
if (operator === "&&" || operator === "||" || operator === "??" && node === right) { | ||
@@ -34,3 +28,2 @@ return willPathCastToBoolean(parentPath); | ||
} | ||
if (parentPath.isSequenceExpression()) { | ||
@@ -40,3 +33,2 @@ const { | ||
} = parentPath.node; | ||
if (expressions[expressions.length - 1] === node) { | ||
@@ -48,3 +40,2 @@ return willPathCastToBoolean(parentPath); | ||
} | ||
return parentPath.isConditional({ | ||
@@ -70,3 +61,2 @@ test: node | ||
} = core.template.expression; | ||
function isSimpleMemberExpression(expression) { | ||
@@ -76,3 +66,2 @@ expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(expression); | ||
} | ||
function needsMemoize(path) { | ||
@@ -83,3 +72,2 @@ let optionalPath = path; | ||
} = path; | ||
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) { | ||
@@ -90,11 +78,8 @@ const { | ||
const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee")); | ||
if (node.optional) { | ||
return !scope.isStatic(childPath.node); | ||
} | ||
optionalPath = childPath; | ||
} | ||
} | ||
function transform(path, { | ||
@@ -118,3 +103,2 @@ pureGetters, | ||
let optionalPath = path; | ||
if (scope.path.isPattern() && needsMemoize(optionalPath)) { | ||
@@ -124,3 +108,2 @@ path.replaceWith(core.template.ast`(() => ${path.node})()`); | ||
} | ||
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) { | ||
@@ -130,7 +113,5 @@ const { | ||
} = optionalPath; | ||
if (node.optional) { | ||
optionals.push(node); | ||
} | ||
if (optionalPath.isOptionalMemberExpression()) { | ||
@@ -144,5 +125,3 @@ optionalPath.node.type = "MemberExpression"; | ||
} | ||
let replacementPath = path; | ||
if (parentPath.isUnaryExpression({ | ||
@@ -154,3 +133,2 @@ operator: "delete" | ||
} | ||
for (let i = optionals.length - 1; i >= 0; i--) { | ||
@@ -163,3 +141,2 @@ const node = optionals[i]; | ||
let check; | ||
if (isCall && core.types.isIdentifier(chain, { | ||
@@ -174,3 +151,2 @@ name: "eval" | ||
ref = scope.maybeGenerateMemoised(chain); | ||
if (ref) { | ||
@@ -183,3 +159,2 @@ check = core.types.assignmentExpression("=", core.types.cloneNode(ref), chainWithTypes); | ||
} | ||
if (isCall && core.types.isMemberExpression(chain)) { | ||
@@ -192,12 +167,14 @@ if (pureGetters && isSimpleMemberExpression(chain)) { | ||
} = chain; | ||
let context = scope.maybeGenerateMemoised(object); | ||
if (context) { | ||
chain.object = core.types.assignmentExpression("=", context, object); | ||
} else if (core.types.isSuper(object)) { | ||
let context; | ||
if (core.types.isSuper(object)) { | ||
context = core.types.thisExpression(); | ||
} else { | ||
context = object; | ||
const memoized = scope.maybeGenerateMemoised(object); | ||
if (memoized) { | ||
context = memoized; | ||
chain.object = core.types.assignmentExpression("=", memoized, object); | ||
} else { | ||
context = object; | ||
} | ||
} | ||
node.arguments.unshift(core.types.cloneNode(context)); | ||
@@ -207,14 +184,9 @@ node.callee = core.types.memberExpression(node.callee, core.types.identifier("call")); | ||
} | ||
let replacement = replacementPath.node; | ||
if (i === 0 && parentIsCall) { | ||
var _baseRef; | ||
const object = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(replacement.object); | ||
let baseRef; | ||
if (!pureGetters || !isSimpleMemberExpression(object)) { | ||
baseRef = scope.maybeGenerateMemoised(object); | ||
if (baseRef) { | ||
@@ -224,6 +196,4 @@ replacement.object = core.types.assignmentExpression("=", baseRef, object); | ||
} | ||
replacement = core.types.callExpression(core.types.memberExpression(replacement, core.types.identifier("bind")), [core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]); | ||
} | ||
if (willReplacementCastToBoolean) { | ||
@@ -246,3 +216,2 @@ const nonNullishCheck = noDocumentAll ? ast`${core.types.cloneNode(check)} != null` : ast` | ||
var _api$assumption, _api$assumption2; | ||
api.assertVersion(7); | ||
@@ -256,3 +225,3 @@ const { | ||
name: "proposal-optional-chaining", | ||
inherits: syntaxOptionalChaining__default["default"].default, | ||
inherits: syntaxOptionalChaining.default, | ||
visitor: { | ||
@@ -265,3 +234,2 @@ "OptionalCallExpression|OptionalMemberExpression"(path) { | ||
} | ||
} | ||
@@ -268,0 +236,0 @@ }; |
{ | ||
"name": "@babel/plugin-proposal-optional-chaining", | ||
"version": "7.18.6", | ||
"version": "7.21.0", | ||
"description": "Transform optional chaining operators into a series of nil checks", | ||
@@ -20,4 +20,4 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "^7.18.6", | ||
"@babel/helper-skip-transparent-expression-wrappers": "^7.18.6", | ||
"@babel/helper-plugin-utils": "^7.20.2", | ||
"@babel/helper-skip-transparent-expression-wrappers": "^7.20.0", | ||
"@babel/plugin-syntax-optional-chaining": "^7.8.3" | ||
@@ -29,6 +29,6 @@ }, | ||
"devDependencies": { | ||
"@babel/core": "^7.18.6", | ||
"@babel/core": "^7.21.0", | ||
"@babel/helper-plugin-test-runner": "^7.18.6", | ||
"@babel/plugin-transform-block-scoping": "^7.18.6", | ||
"@babel/traverse": "^7.18.6" | ||
"@babel/plugin-transform-block-scoping": "^7.21.0", | ||
"@babel/traverse": "^7.21.0" | ||
}, | ||
@@ -35,0 +35,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
33213
1.45%211
0.96%