Repository: adobe/react-spectrum
File: packages/@react-stately/data/src/useTreeData.ts (TypeScript)

Code before:
      for (let child of node.children ?? []) {
        traversal(child, {inorder, postorder});
        postorder?.(child);
      }
    }
  }

  function inorder(child) {
    // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later
    if (keyArray.includes(child.key)) {
      inOrderKeys.set(child.key, i++);
    }
  }

  function postorder(child) {
    // remove items and update the tree from the leaves and work upwards toward the root, this way
    // we don't copy child node references from parents inadvertently
    if (keyArray.includes(child.key)) {
      removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});
      let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);
      newItems = nextItems;
      newMap = nextMap;
    }
    // decrement the index if the child being removed is in the target parent and before the target index
    // eslint-disable-next-line eqeqeq
    if (child.parentKey == toParent?.key
      && keyArray.includes(child.key)
      && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {
      toIndex--;
    }
  }

  traversal({children: items}, {inorder, postorder});

  let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);
  // If parentKey is null, insert into the root.
  if (!toParent || toParent.key == null) {
    inOrderItems.forEach(movedNode => {
      addNode(movedNode, newMap); 
    });
    return {items: [
      ...newItems.slice(0, toIndex),
      ...inOrderItems,
      ...newItems.slice(toIndex)
    ], nodeMap: newMap};
  }

  // Otherwise, update the parent node and its ancestors.
  return updateTree(newItems, toParent.key, parentNode => ({
    key: parentNode.key,
    parentKey: parentNode.parentKey,

Review comment (suggestion):
```suggestion
    // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
    if ((child.parentKey === toParent
       || child.parentKey === toParent?.key)
```

I think this is a little more explicit as to what is being checked and doesn't need to ignore eslint.

Code after:
      for (let child of node.children ?? []) {
        traversal(child, {inorder, postorder});
        postorder?.(child);
      }
    }
  }

  function inorder(child) {
    // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later
    if (keyArray.includes(child.key)) {
      inOrderKeys.set(child.key, i++);
    }
  }

  function postorder(child) {
    // remove items and update the tree from the leaves and work upwards toward the root, this way
    // we don't copy child node references from parents inadvertently
    if (keyArray.includes(child.key)) {
      removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});
      let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);
      newItems = nextItems;
      newMap = nextMap;
    }
    // decrement the index if the child being removed is in the target parent and before the target index
    // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
    if ((child.parentKey === toParent
       || child.parentKey === toParent?.key)
      && keyArray.includes(child.key)
      && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {
      toIndex--;
    }
  }

  traversal({children: items}, {inorder, postorder});

  let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);
  // If parentKey is null, insert into the root.
  if (!toParent || toParent.key == null) {
    inOrderItems.forEach(movedNode => {
      addNode(movedNode, newMap); 
    });
    return {items: [
      ...newItems.slice(0, toIndex),
      ...inOrderItems,
      ...newItems.slice(toIndex)
    ], nodeMap: newMap};
  }

  // Otherwise, update the parent node and its ancestors.
  return updateTree(newItems, toParent.key, parentNode => ({
    key: parentNode.key,