@babel/helper-member-expression-to-functions
Advanced tools
Comparing version 7.8.3 to 7.10.0
187
lib/index.js
@@ -49,2 +49,51 @@ "use strict"; | ||
function toNonOptional(path, base) { | ||
const { | ||
node | ||
} = path; | ||
if (path.isOptionalMemberExpression()) { | ||
return t.memberExpression(base, node.property, node.computed); | ||
} | ||
if (path.isOptionalCallExpression()) { | ||
const callee = path.get("callee"); | ||
if (path.node.optional && callee.isOptionalMemberExpression()) { | ||
const { | ||
object | ||
} = callee.node; | ||
const context = path.scope.maybeGenerateMemoised(object) || object; | ||
callee.get("object").replaceWith(t.assignmentExpression("=", context, object)); | ||
return t.callExpression(t.memberExpression(base, t.identifier("call")), [context, ...node.arguments]); | ||
} | ||
return t.callExpression(base, node.arguments); | ||
} | ||
return path.node; | ||
} | ||
function isInDetachedTree(path) { | ||
while (path) { | ||
if (path.isProgram()) break; | ||
const { | ||
parentPath, | ||
container, | ||
listKey | ||
} = path; | ||
const parentNode = parentPath.node; | ||
if (listKey) { | ||
if (container !== parentNode[listKey]) return true; | ||
} else { | ||
if (container !== parentNode) return true; | ||
} | ||
path = parentPath; | ||
} | ||
return false; | ||
} | ||
const handle = { | ||
@@ -60,5 +109,124 @@ memoise() {}, | ||
if (member.isOptionalMemberExpression()) { | ||
if (isInDetachedTree(member)) return; | ||
const endPath = member.find(({ | ||
node, | ||
parent, | ||
parentPath | ||
}) => { | ||
if (parentPath.isOptionalMemberExpression()) { | ||
return parent.optional || parent.object !== node; | ||
} | ||
if (parentPath.isOptionalCallExpression()) { | ||
return node !== member.node && parent.optional || parent.callee !== node; | ||
} | ||
return true; | ||
}); | ||
const rootParentPath = endPath.parentPath; | ||
if (rootParentPath.isUpdateExpression({ | ||
argument: node | ||
}) || rootParentPath.isAssignmentExpression({ | ||
left: node | ||
})) { | ||
throw member.buildCodeFrameError(`can't handle assignment`); | ||
} | ||
if (rootParentPath.isUnaryExpression({ | ||
operator: "delete" | ||
})) { | ||
throw member.buildCodeFrameError(`can't handle delete`); | ||
} | ||
let startingOptional = member; | ||
for (;;) { | ||
if (startingOptional.isOptionalMemberExpression()) { | ||
if (startingOptional.node.optional) break; | ||
startingOptional = startingOptional.get("object"); | ||
continue; | ||
} else if (startingOptional.isOptionalCallExpression()) { | ||
if (startingOptional.node.optional) break; | ||
startingOptional = startingOptional.get("callee"); | ||
continue; | ||
} | ||
throw new Error(`Internal error: unexpected ${startingOptional.node.type}`); | ||
} | ||
const { | ||
scope | ||
} = member; | ||
const startingProp = startingOptional.isOptionalMemberExpression() ? "object" : "callee"; | ||
const startingNode = startingOptional.node[startingProp]; | ||
const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode); | ||
const baseRef = baseNeedsMemoised != null ? baseNeedsMemoised : startingNode; | ||
const parentIsOptionalCall = parentPath.isOptionalCallExpression({ | ||
callee: node | ||
}); | ||
startingOptional.replaceWith(toNonOptional(startingOptional, baseRef)); | ||
if (parentIsOptionalCall) { | ||
if (parent.optional) { | ||
parentPath.replaceWith(this.optionalCall(member, parent.arguments)); | ||
} else { | ||
parentPath.replaceWith(this.call(member, parent.arguments)); | ||
} | ||
} else { | ||
member.replaceWith(this.get(member)); | ||
} | ||
let regular = member.node; | ||
for (let current = member; current !== endPath;) { | ||
const { | ||
parentPath | ||
} = current; | ||
if (parentPath === endPath && parentIsOptionalCall && parent.optional) { | ||
regular = parentPath.node; | ||
break; | ||
} | ||
regular = toNonOptional(parentPath, regular); | ||
current = parentPath; | ||
} | ||
let context; | ||
const endParentPath = endPath.parentPath; | ||
if (t.isMemberExpression(regular) && endParentPath.isOptionalCallExpression({ | ||
callee: endPath.node, | ||
optional: true | ||
})) { | ||
const { | ||
object | ||
} = regular; | ||
context = member.scope.maybeGenerateMemoised(object); | ||
if (context) { | ||
regular.object = t.assignmentExpression("=", context, object); | ||
} | ||
} | ||
endPath.replaceWith(t.conditionalExpression(t.logicalExpression("||", t.binaryExpression("===", baseNeedsMemoised ? t.assignmentExpression("=", baseRef, startingNode) : baseRef, t.nullLiteral()), t.binaryExpression("===", t.cloneNode(baseRef), scope.buildUndefinedNode())), scope.buildUndefinedNode(), regular)); | ||
if (context) { | ||
const endParent = endParentPath.node; | ||
endParentPath.replaceWith(t.optionalCallExpression(t.optionalMemberExpression(endParent.callee, t.identifier("call"), false, true), [context, ...endParent.arguments], false)); | ||
} | ||
return; | ||
} | ||
if (parentPath.isUpdateExpression({ | ||
argument: node | ||
})) { | ||
if (this.simpleSet) { | ||
member.replaceWith(this.simpleSet(member)); | ||
return; | ||
} | ||
const { | ||
@@ -91,2 +259,7 @@ operator, | ||
})) { | ||
if (this.simpleSet) { | ||
member.replaceWith(this.simpleSet(member)); | ||
return; | ||
} | ||
const { | ||
@@ -110,9 +283,13 @@ operator, | ||
})) { | ||
const { | ||
arguments: args | ||
} = parent; | ||
parentPath.replaceWith(this.call(member, args)); | ||
parentPath.replaceWith(this.call(member, parent.arguments)); | ||
return; | ||
} | ||
if (parentPath.isOptionalCallExpression({ | ||
callee: node | ||
})) { | ||
parentPath.replaceWith(this.optionalCall(member, parent.arguments)); | ||
return; | ||
} | ||
if (parentPath.isObjectProperty({ | ||
@@ -137,5 +314,5 @@ value: node | ||
function memberExpressionToFunctions(path, visitor, state) { | ||
path.traverse(visitor, Object.assign({}, handle, {}, state, { | ||
path.traverse(visitor, Object.assign(Object.assign(Object.assign({}, handle), state), {}, { | ||
memoiser: new AssignmentMemoiser() | ||
})); | ||
} |
{ | ||
"name": "@babel/helper-member-expression-to-functions", | ||
"version": "7.8.3", | ||
"version": "7.10.0", | ||
"description": "Helper function to replace certain member expressions with function calls", | ||
@@ -13,5 +13,5 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-member-expression-to-functions", | ||
"dependencies": { | ||
"@babel/types": "^7.8.3" | ||
"@babel/types": "^7.10.0" | ||
}, | ||
"gitHead": "a7620bd266ae1345975767bbc7abf09034437017" | ||
"gitHead": "5da2440adff6f25579fb6e9a018062291c89416f" | ||
} |
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
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
11271
256
Updated@babel/types@^7.10.0