Skip to content

Commit

Permalink
Fix bug with traversing detached node tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 15, 2020
1 parent 9ef58e6 commit fa21565
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/babel-helper-member-expression-to-functions/src/index.js
Expand Up @@ -56,6 +56,28 @@ function toNonOptional(path, base) {
return path.node;
}

// Determines if the current path is in a detached tree. This can happen when
// we are iterating on a path, and replace an ancestor with a new node. Babel
// doesn't always stop traversing the old node tree, and that can cause
// inconsistencies.
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 = {
memoise() {
// noop.
Expand All @@ -65,6 +87,9 @@ const handle = {
const { node, parent, parentPath } = member;

if (member.isOptionalMemberExpression()) {
// Transforming optional chaining requires we replace ancestors.
if (isInDetachedTree(member)) return;

// We're looking for the end of _this_ optional chain, which is actually
// the "rightmost" property access of the chain. This is because
// everything up to that property access is "optional".
Expand Down

0 comments on commit fa21565

Please sign in to comment.