Skip to content

🎥 Make videos programmatically with React

Index updated node references for bulk edits

Jonny Burger7e75c65805e7
diff --git a/packages/codemods/src/node-references.ts b/packages/codemods/src/node-references.ts
index 77ca646144..a899b20367 100644
--- a/packages/codemods/src/node-references.ts
+++ b/packages/codemods/src/node-references.ts
@@ -192,6 +192,51 @@ export const getUpdatedNodeReference = ({
return {filePath, nodePath: remapping?.newNodePath ?? node.nodePath};
};
+export const getUpdatedNodeReferences = ({
+ project,
+ nodes,
+ nodePathRemappings,
+}: {
+ project: CodemodProject;
+ nodes: readonly NodeReference[];
+ nodePathRemappings: NodePathRemapping[];
+}): NodeReference[] => {
+ if (nodes.length === 1) {
+ return [
+ getUpdatedNodeReference({project, node: nodes[0], nodePathRemappings}),
+ ];
+ }
+
+ const pathsByFile = new Map<string, Map<string, SequenceNodePath | null>>();
+ for (const {filePath, oldNodePath, newNodePath} of nodePathRemappings) {
+ if (oldNodePath === null) {
+ continue;
+ }
+
+ const paths =
+ pathsByFile.get(filePath) ?? new Map<string, SequenceNodePath | null>();
+ const key = JSON.stringify(oldNodePath);
+ // Match the first remapping, as the single-node lookup does.
+ if (!paths.has(key)) {
+ paths.set(key, newNodePath);
+ }
+
+ pathsByFile.set(filePath, paths);
+ }
+
+ return nodes.map((node) => {
+ const filePath = findProjectFile({project, filePath: node.filePath});
+ const nodePath = pathsByFile
+ .get(filePath)
+ ?.get(JSON.stringify(node.nodePath));
+ if (nodePath === null) {
+ throw new Error('The JSX node was removed');
+ }
+
+ return {filePath, nodePath: nodePath ?? node.nodePath};
+ });
+};
+
export const groupNodeReferencesByFile = ({
project,
nodes,
diff --git a/packages/codemods/src/reorder-nodes.ts b/packages/codemods/src/reorder-nodes.ts
index 562848fdbc..7124c3a156 100644
--- a/packages/codemods/src/reorder-nodes.ts
+++ b/packages/codemods/src/reorder-nodes.ts
@@ -2,7 +2,7 @@ import type {CodemodProject} from './codemod-project';
import {findProjectFile} from './internals';
import {
getNodeEditResult,
- getUpdatedNodeReference,
+ getUpdatedNodeReferences,
type NodeReference,
} from './node-references';
import {reorderSequences} from './reorder-sequence';
@@ -53,8 +53,6 @@ export const reorderNodes = async <Project extends CodemodProject>({
logLine: edit.logLine,
},
],
- updatedNodes: nodes.map((node) =>
- getUpdatedNodeReference({project, ...result, node}),
- ),
+ updatedNodes: getUpdatedNodeReferences({project, ...result, nodes}),
};
};
diff --git a/packages/codemods/src/update-node-props.ts b/packages/codemods/src/update-node-props.ts
index 6534acb59d..127080bb60 100644
--- a/packages/codemods/src/update-node-props.ts
+++ b/packages/codemods/src/update-node-props.ts
@@ -9,7 +9,7 @@ import {getNodeProps} from './get-node-props';
import {findProjectFile} from './internals';
import {
getNodeEditResult,
- getUpdatedNodeReference,
+ getUpdatedNodeReferences,
type NodeReference,
type NodeSourceEdit,
} from './node-references';
@@ -130,9 +130,11 @@ export const updateMultipleNodeProps = <Project extends CodemodProject>({
...result,
results,
formatted: true,
- updatedNodes: changes.map(({node}) =>
- getUpdatedNodeReference({project, ...result, node}),
- ),
+ updatedNodes: getUpdatedNodeReferences({
+ project,
+ ...result,
+ nodes: changes.map(({node}) => node),
+ }),
};
};