Skip to content

🎥 Make videos programmatically with React

Support codemods on lone <Composition> JSX elements

Jonny Burgerb2c5220dbfa3
diff --git a/packages/studio-server/src/codemods/recast-mods.ts b/packages/studio-server/src/codemods/recast-mods.ts
index 499f28cf16..5f36adbdc7 100644
--- a/packages/studio-server/src/codemods/recast-mods.ts
+++ b/packages/studio-server/src/codemods/recast-mods.ts
@@ -10,6 +10,7 @@ import type {
JSXAttribute,
JSXElement,
JSXFragment,
+ NullLiteral,
ReturnStatement,
Statement,
VariableDeclaration,
@@ -127,12 +128,95 @@ const mapReturnStatement = (
return statement;
}
+ const replacement = transformLoneJsxElement(
+ statement.argument,
+ transformation,
+ changesMade,
+ );
+ if (replacement !== null) {
+ return {...statement, argument: replacement};
+ }
+
return {
...statement,
argument: mapAll(statement.argument, transformation, changesMade),
};
};
+const nullLiteral = (): NullLiteral => ({type: 'NullLiteral'});
+
+// When a node was originally parenthesized (e.g. the `<JSX/>` inside
+// `return (<JSX/>)`), Babel records `extra.parenthesized` on it. If we move
+// such a node into a JSXFragment without clearing that hint, recast prints
+// stray `(` / `)` characters around it as JSX text. Clear the hint so the
+// node prints cleanly in its new context.
+const stripParenthesizedExtra = <T extends {extra?: unknown}>(node: T): T => {
+ if (!node.extra) {
+ return node;
+ }
+
+ const {
+ parenthesized: _p,
+ parenStart: _ps,
+ ...rest
+ } = node.extra as {
+ parenthesized?: boolean;
+ parenStart?: number;
+ };
+ return {...node, extra: rest};
+};
+
+const wrapInJsxFragment = (children: JSXElement[]): JSXFragment => ({
+ type: 'JSXFragment',
+ openingFragment: {type: 'JSXOpeningFragment'},
+ closingFragment: {type: 'JSXClosingFragment'},
+ children: children.map(stripParenthesizedExtra),
+});
+
+// When a <Composition> JSX element appears in a position where it cannot
+// simply be removed from a parent's children list (e.g. as the sole return
+// value of a wrapper component or as the concise body of an arrow function),
+// we still want delete/rename/duplicate codemods to work. This helper detects
+// that case and produces a structurally-valid replacement expression.
+const transformLoneJsxElement = (
+ expression: Expression,
+ transformation: RecastCodemod,
+ changesMade: Change[],
+): Expression | null => {
+ if (expression.type !== 'JSXElement') {
+ return null;
+ }
+
+ const compId = getCompositionIdFromJSXElement(expression);
+ if (compId === null) {
+ return null;
+ }
+
+ const isMatch =
+ (transformation.type === 'delete-composition' &&
+ compId === transformation.idToDelete) ||
+ (transformation.type === 'rename-composition' &&
+ compId === transformation.idToRename) ||
+ (transformation.type === 'duplicate-composition' &&
+ compId === transformation.idToDuplicate);
+
+ if (!isMatch) {
+ return null;
+ }
+
+ const transformed = mapJsxChild(expression, transformation, changesMade);
+
+ if (transformed.length === 0) {
+ return nullLiteral();
+ }
+
+ if (transformed.length === 1) {
+ return transformed[0];
+ }
+
+ return wrapInJsxFragment(transformed);
+};
+
const mapJsxElementOrFragment = <T extends JSXFragment | JSXElement>(
jsxFragment: T,
transformation: RecastCodemod,
@@ -230,6 +314,23 @@ const mapRecognizedType = <T extends RecognizedType>(
expression.type === 'ArrowFunctionExpression' ||
expression.type === 'FunctionExpression'
) {
+ if (
+ expression.type === 'ArrowFunctionExpression' &&
+ expression.body.type === 'JSXElement'
+ ) {
+ const replacement = transformLoneJsxElement(
+ expression.body,
+ transformation,
+ changesMade,
+ );
+ if (replacement !== null) {
+ return {
+ ...expression,
+ body: replacement,
+ };
+ }
+ }
+
return {
...expression,
body: mapAll(expression.body, transformation, changesMade),
diff --git a/packages/studio-server/src/test/recast-mods.test.ts b/packages/studio-server/src/test/recast-mods.test.ts
new file mode 100644
index 0000000000..1588d2ffab
--- /dev/null
+++ b/packages/studio-server/src/test/recast-mods.test.ts
@@ -0,0 +1,162 @@
+import {expect, test} from 'bun:test';
+import {parseAndApplyCodemod} from '../codemods/duplicate-composition';
+
+const compositionInRoot = `import {Composition} from 'remotion';
+import {NewVideo} from './NewVideo';
+
+export const RemotionRoot: React.FC = () => {
+ return (
+ <>
+ <Composition
+ id="NewVideo"
+ component={NewVideo}
+ durationInFrames={120}
+ fps={30}
+ width={1280}
+ height={720}
+ />
+ <Composition
+ id="Other"
+ component={NewVideo}
+ durationInFrames={120}
+ fps={30}
+ width={1280}
+ height={720}
+ />
+ </>
+ );
+};
+`;
+
+const compositionInOwnFile = `import {Composition} from 'remotion';
+
+const Component = () => null;
+
+export const NewVideoComp = () => {
+ return (
+ <Composition
+ component={Component}
+ id="NewVideo"
+ durationInFrames={120}
+ fps={30}
+ width={1280}
+ height={720}
+ />
+ );
+};
+`;
+
+const compositionInConciseArrow = `import {Composition} from 'remotion';
+
+const Component = () => null;
+
+export const NewVideoComp = () => (
+ <Composition
+ component={Component}
+ id="NewVideo"
+ durationInFrames={120}
+ fps={30}
+ width={1280}
+ height={720}
+ />
+);
+`;
+
+test('Delete composition that is a child of a Fragment in Root.tsx', () => {
+ const {newContents, changesMade} = parseAndApplyCodemod({
+ input: compositionInRoot,
+ codeMod: {type: 'delete-composition', idToDelete: 'NewVideo'},
+ });
+
+ expect(changesMade.length).toBeGreaterThan(0);
+ expect(newContents).not.toContain('id="NewVideo"');
+ expect(newContents).toContain('id="Other"');
+});
+
+test('Delete composition that is the sole return value of a wrapper component', () => {
+ const {newContents, changesMade} = parseAndApplyCodemod({
+ input: compositionInOwnFile,
+ codeMod: {type: 'delete-composition', idToDelete: 'NewVideo'},
+ });
+
+ expect(changesMade.length).toBeGreaterThan(0);
+ expect(newContents).not.toContain('id="NewVideo"');
+});
+
+test('Delete composition that is a concise arrow body', () => {
+ const {newContents, changesMade} = parseAndApplyCodemod({
+ input: compositionInConciseArrow,
+ codeMod: {type: 'delete-composition', idToDelete: 'NewVideo'},
+ });
+
+ expect(changesMade.length).toBeGreaterThan(0);
+ expect(newContents).not.toContain('id="NewVideo"');
+});
+
+test('Rename composition that is the sole return value of a wrapper component', () => {
+ const {newContents, changesMade} = parseAndApplyCodemod({
+ input: compositionInOwnFile,
+ codeMod: {
+ type: 'rename-composition',
+ idToRename: 'NewVideo',
+ newId: 'Renamed',
+ },
+ });
+
+ expect(changesMade.length).toBeGreaterThan(0);
+ expect(newContents).not.toContain('id="NewVideo"');
+ expect(newContents).toContain('id="Renamed"');
+});
+
+test('Duplicate composition that is the sole return value of a wrapper component', () => {
+ const {newContents, changesMade} = parseAndApplyCodemod({
+ input: compositionInOwnFile,
+ codeMod: {
+ type: 'duplicate-composition',
+ idToDuplicate: 'NewVideo',
+ newId: 'Duplicated',
+ newDurationInFrames: null,
+ newFps: null,
+ newHeight: null,
+ newWidth: null,
+ tag: 'Composition',
+ },
+ });
+
+ expect(changesMade.length).toBeGreaterThan(0);
+ expect(newContents).toContain('id="NewVideo"');
+ expect(newContents).toContain('id="Duplicated"');
+});
+
+test('Delete composition leaves a returns-null wrapper that still parses', () => {
+ const {newContents} = parseAndApplyCodemod({
+ input: compositionInOwnFile,
+ codeMod: {type: 'delete-composition', idToDelete: 'NewVideo'},
+ });
+
+ expect(newContents).toContain('return null');
+ expect(newContents).not.toContain('<Composition');
+});
+
+test('Delete unmatched composition leaves the file unchanged', () => {
+ expect(() =>
+ parseAndApplyCodemod({
+ input: compositionInOwnFile,
+ codeMod: {type: 'delete-composition', idToDelete: 'DoesNotExist'},
+ }),
+ ).toThrow('Unable to calculate the changes');
+});
+
+test('Rename concise-arrow composition keeps the arrow expression body', () => {
+ const {newContents} = parseAndApplyCodemod({
+ input: compositionInConciseArrow,
+ codeMod: {
+ type: 'rename-composition',
+ idToRename: 'NewVideo',
+ newId: 'Renamed',
+ },
+ });
+
+ expect(newContents).toContain('id="Renamed"');
+ expect(newContents).not.toContain('id="NewVideo"');
+});