Skip to content

🎥 Make videos programmatically with React

[studio-server] Deduplicate AST node-path traversal helper

Cursor Agent44e1420a4729
diff --git a/packages/studio-server/src/codemods/delete-jsx-node.ts b/packages/studio-server/src/codemods/delete-jsx-node.ts
index 93403a8ecc..764758ac6a 100644
--- a/packages/studio-server/src/codemods/delete-jsx-node.ts
+++ b/packages/studio-server/src/codemods/delete-jsx-node.ts
@@ -26,6 +26,7 @@ import type {
} from '@babel/types';
import type {SequenceNodePath} from '@remotion/studio-shared';
import * as recast from 'recast';
+import {getAstNodePath} from '../helpers/get-ast-node-path';
import {formatFileContent} from './format-file-content';
import {parseAst, serializeAst} from './parse-ast';
@@ -70,12 +71,9 @@ export const findJsxElementPathForDeletion = (
ast: File,
nodePath: SequenceNodePath,
): recast.types.NodePath | null => {
- let current = new recast.types.NodePath(ast);
- for (const segment of nodePath) {
- current = current.get(segment);
- if (current.value === null || current.value === undefined) {
- return null;
- }
+ const current = getAstNodePath(ast, nodePath);
+ if (!current) {
+ return null;
}
if (namedTypes.JSXOpeningElement.check(current.value)) {
diff --git a/packages/studio-server/src/helpers/get-ast-node-path.ts b/packages/studio-server/src/helpers/get-ast-node-path.ts
new file mode 100644
index 0000000000..2d49696141
--- /dev/null
+++ b/packages/studio-server/src/helpers/get-ast-node-path.ts
@@ -0,0 +1,18 @@
+import type {File} from '@babel/types';
+import type {SequenceNodePath} from '@remotion/studio-shared';
+import * as recast from 'recast';
+
+export const getAstNodePath = (
+ ast: File,
+ nodePath: SequenceNodePath,
+): recast.types.NodePath | null => {
+ let current = new recast.types.NodePath(ast);
+ for (const segment of nodePath) {
+ current = current.get(segment);
+ if (current.value === null || current.value === undefined) {
+ return null;
+ }
+ }
+
+ return current;
+};
diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
index 125ab81efe..cc066ae47c 100644
--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
@@ -16,6 +16,7 @@ import type {
} from '@remotion/studio-shared';
import * as recast from 'recast';
import type {CanUpdateSequencePropStatus} from 'remotion';
+import {getAstNodePath} from '../../helpers/get-ast-node-path';
import {parseAst} from '../../codemods/parse-ast';
type CanUpdatePropStatus = CanUpdateSequencePropStatus;
@@ -192,12 +193,9 @@ export const findJsxElementAtNodePath = (
ast: File,
nodePath: SequenceNodePath,
): JSXOpeningElement | null => {
- let current = new recast.types.NodePath(ast);
- for (const segment of nodePath) {
- current = current.get(segment);
- if (current.value === null || current.value === undefined) {
- return null;
- }
+ const current = getAstNodePath(ast, nodePath);
+ if (!current) {
+ return null;
}
if (recast.types.namedTypes.JSXOpeningElement.check(current.value)) {