@babel/plugin-proposal-optional-chaining
Advanced tools
Comparing version 7.0.0 to 7.2.0
160
lib/index.js
@@ -45,110 +45,86 @@ "use strict"; | ||
} = options; | ||
return { | ||
name: "proposal-optional-chaining", | ||
inherits: _pluginSyntaxOptionalChaining().default, | ||
visitor: { | ||
"OptionalCallExpression|OptionalMemberExpression"(path) { | ||
const { | ||
parentPath, | ||
scope | ||
} = path; | ||
const optionals = []; | ||
let optionalPath = path; | ||
function optional(path, replacementPath) { | ||
const { | ||
scope | ||
} = path; | ||
const optionals = []; | ||
let objectPath = path; | ||
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) { | ||
const { | ||
node | ||
} = optionalPath; | ||
while (objectPath.isOptionalMemberExpression() || objectPath.isOptionalCallExpression()) { | ||
const { | ||
node | ||
} = objectPath; | ||
if (node.optional) { | ||
optionals.push(node); | ||
} | ||
if (node.optional) { | ||
optionals.push(node); | ||
} | ||
if (optionalPath.isOptionalMemberExpression()) { | ||
optionalPath.node.type = "MemberExpression"; | ||
optionalPath = optionalPath.get("object"); | ||
} else if (optionalPath.isOptionalCallExpression()) { | ||
optionalPath.node.type = "CallExpression"; | ||
optionalPath = optionalPath.get("callee"); | ||
} | ||
} | ||
if (objectPath.isOptionalMemberExpression()) { | ||
objectPath.node.type = "MemberExpression"; | ||
objectPath = objectPath.get("object"); | ||
} else { | ||
objectPath.node.type = "CallExpression"; | ||
objectPath = objectPath.get("callee"); | ||
} | ||
} | ||
let replacementPath = path; | ||
for (let i = optionals.length - 1; i >= 0; i--) { | ||
const node = optionals[i]; | ||
node.optional = false; | ||
if (parentPath.isUnaryExpression({ | ||
operator: "delete" | ||
})) { | ||
replacementPath = parentPath; | ||
} | ||
const isCall = _core().types.isCallExpression(node); | ||
for (let i = optionals.length - 1; i >= 0; i--) { | ||
const node = optionals[i]; | ||
const replaceKey = isCall ? "callee" : "object"; | ||
const chain = node[replaceKey]; | ||
let ref; | ||
let check; | ||
const isCall = _core().types.isCallExpression(node); | ||
if (loose && isCall) { | ||
check = ref = chain; | ||
} else { | ||
ref = scope.maybeGenerateMemoised(chain); | ||
const replaceKey = isCall ? "callee" : "object"; | ||
const chain = node[replaceKey]; | ||
let ref; | ||
let check; | ||
if (ref) { | ||
check = _core().types.assignmentExpression("=", _core().types.cloneNode(ref), chain); | ||
node[replaceKey] = ref; | ||
} else { | ||
check = ref = chain; | ||
} | ||
} | ||
if (loose && isCall) { | ||
check = ref = chain; | ||
} else { | ||
ref = scope.maybeGenerateMemoised(chain); | ||
if (isCall && _core().types.isMemberExpression(chain)) { | ||
if (loose) { | ||
node.callee = chain; | ||
} else { | ||
const { | ||
object | ||
} = chain; | ||
let context = scope.maybeGenerateMemoised(object); | ||
if (context) { | ||
chain.object = _core().types.assignmentExpression("=", context, object); | ||
} else { | ||
context = object; | ||
if (ref) { | ||
check = _core().types.assignmentExpression("=", _core().types.cloneNode(ref), chain); | ||
node[replaceKey] = ref; | ||
} else { | ||
check = ref = chain; | ||
} | ||
} | ||
node.arguments.unshift(_core().types.cloneNode(context)); | ||
node.callee = _core().types.memberExpression(node.callee, _core().types.identifier("call")); | ||
} | ||
} | ||
if (isCall && _core().types.isMemberExpression(chain)) { | ||
if (loose) { | ||
node.callee = chain; | ||
} else { | ||
const { | ||
object | ||
} = chain; | ||
let context = scope.maybeGenerateMemoised(object); | ||
replacementPath.replaceWith(_core().types.conditionalExpression(loose ? _core().types.binaryExpression("==", _core().types.cloneNode(check), _core().types.nullLiteral()) : _core().types.logicalExpression("||", _core().types.binaryExpression("===", _core().types.cloneNode(check), _core().types.nullLiteral()), _core().types.binaryExpression("===", _core().types.cloneNode(ref), scope.buildUndefinedNode())), scope.buildUndefinedNode(), replacementPath.node)); | ||
replacementPath = replacementPath.get("alternate"); | ||
} | ||
} | ||
if (context) { | ||
chain.object = _core().types.assignmentExpression("=", context, object); | ||
} else { | ||
context = object; | ||
} | ||
function findReplacementPath(path) { | ||
return path.find(path => { | ||
const { | ||
parentPath | ||
} = path; | ||
node.arguments.unshift(_core().types.cloneNode(context)); | ||
node.callee = _core().types.memberExpression(node.callee, _core().types.identifier("call")); | ||
} | ||
} | ||
if (path.key == "object" && parentPath.isOptionalMemberExpression()) { | ||
return false; | ||
} | ||
if (path.key == "callee" && parentPath.isOptionalCallExpression()) { | ||
return false; | ||
} | ||
if (path.key == "argument" && parentPath.isUnaryExpression({ | ||
operator: "delete" | ||
})) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
return { | ||
inherits: _pluginSyntaxOptionalChaining().default, | ||
visitor: { | ||
"OptionalCallExpression|OptionalMemberExpression"(path) { | ||
if (!path.node.optional) { | ||
return; | ||
replacementPath.replaceWith(_core().types.conditionalExpression(loose ? _core().types.binaryExpression("==", _core().types.cloneNode(check), _core().types.nullLiteral()) : _core().types.logicalExpression("||", _core().types.binaryExpression("===", _core().types.cloneNode(check), _core().types.nullLiteral()), _core().types.binaryExpression("===", _core().types.cloneNode(ref), scope.buildUndefinedNode())), scope.buildUndefinedNode(), replacementPath.node)); | ||
replacementPath = replacementPath.get("alternate"); | ||
} | ||
optional(path, findReplacementPath(path)); | ||
} | ||
@@ -155,0 +131,0 @@ |
{ | ||
"name": "@babel/plugin-proposal-optional-chaining", | ||
"version": "7.0.0", | ||
"version": "7.2.0", | ||
"description": "Transform optional chaining operators into a series of nil checks", | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-optional-chaining", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "lib/index.js", | ||
@@ -13,3 +16,3 @@ "keywords": [ | ||
"@babel/helper-plugin-utils": "^7.0.0", | ||
"@babel/plugin-syntax-optional-chaining": "^7.0.0" | ||
"@babel/plugin-syntax-optional-chaining": "^7.2.0" | ||
}, | ||
@@ -20,6 +23,6 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.0.0", | ||
"@babel/core": "^7.2.0", | ||
"@babel/helper-plugin-test-runner": "^7.0.0", | ||
"@babel/plugin-transform-block-scoping": "^7.0.0" | ||
"@babel/plugin-transform-block-scoping": "^7.2.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6234
107
1