Skip to content

🎥 Make videos programmatically with React

recognize more compositions

Jonny Burgerb31b3242685e
diff --git a/packages/example/src/CodemodTestbed.tsx b/packages/example/src/CodemodTestbed.tsx
index 7c95f099bc..00ba9649d4 100644
--- a/packages/example/src/CodemodTestbed.tsx
+++ b/packages/example/src/CodemodTestbed.tsx
@@ -3,6 +3,21 @@ import {CalculateMetadataFunction, Composition} from 'remotion';
import {z} from 'zod';
import {DynamicDuration, dynamicDurationSchema} from './DynamicDuration';
+function AltCodemodRoot() {
+ return (
+ <Composition
+ id="three"
+ component={DynamicDuration}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={100}
+ schema={dynamicDurationSchema}
+ defaultProps={{duration: 50}}
+ />
+ );
+}
+
// Use this file to test codemods such as renames
export const CodemodRoot: React.FC = () => {
const calculateMetadata: CalculateMetadataFunction<
@@ -37,6 +52,7 @@ export const CodemodRoot: React.FC = () => {
schema={dynamicDurationSchema}
defaultProps={{duration: 50}}
/>
+ <AltCodemodRoot />
</>
);
};
diff --git a/packages/it-tests/src/codemods/codemods.test.ts b/packages/it-tests/src/codemods/codemods.test.ts
index 92046c9400..1163f5f241 100644
--- a/packages/it-tests/src/codemods/codemods.test.ts
+++ b/packages/it-tests/src/codemods/codemods.test.ts
@@ -25,11 +25,11 @@ const getCompositionCount = (comp: string) => {
const contents = readFileSync(rootFile, "utf-8");
const compCount = getCompositionCount(contents);
-test("Should be able to delete composition", async () => {
+test("Should be able to delete composition", () => {
expect(contents).toContain('"one"');
const { changesMade, newContents } =
- await StudioServerInternals.parseAndApplyCodemod({
+ StudioServerInternals.parseAndApplyCodemod({
input: contents,
codeMod: {
type: "delete-composition",
@@ -41,9 +41,9 @@ test("Should be able to delete composition", async () => {
expect(newContents).not.toContain('"one"');
});
-test("Should be able to rename composition", async () => {
+test("Should be able to rename composition", () => {
const { changesMade, newContents } =
- await StudioServerInternals.parseAndApplyCodemod({
+ StudioServerInternals.parseAndApplyCodemod({
input: contents,
codeMod: {
type: "rename-composition",
@@ -57,8 +57,8 @@ test("Should be able to rename composition", async () => {
expect(newContents).toContain('"abc"');
});
-test("Should be able to duplicate composition", async () => {
- const { newContents } = await StudioServerInternals.parseAndApplyCodemod({
+test("Should be able to duplicate composition", () => {
+ const { newContents } = StudioServerInternals.parseAndApplyCodemod({
input: contents,
codeMod: {
type: "duplicate-composition",
@@ -80,8 +80,8 @@ test("Should be able to duplicate composition", async () => {
expect(newContents).toContain("durationInFrames={200}");
});
-test("Should be able to duplicate composition into a still", async () => {
- const { newContents } = await StudioServerInternals.parseAndApplyCodemod({
+test("Should be able to duplicate composition into a still", () => {
+ const { newContents } = StudioServerInternals.parseAndApplyCodemod({
input: contents,
codeMod: {
type: "duplicate-composition",
@@ -102,3 +102,19 @@ test("Should be able to duplicate composition into a still", async () => {
expect(newContents).not.toContain("fps={24}");
expect(newContents).not.toContain("durationInFrames={200}");
});
+
+test("Should be able to recognize non-arrow function", () => {
+ const { changesMade, newContents } =
+ StudioServerInternals.parseAndApplyCodemod({
+ input: contents,
+ codeMod: {
+ type: "rename-composition",
+ idToRename: "three",
+ newId: "ghi",
+ },
+ });
+ expect(changesMade.length).toBe(1);
+ expect(getCompositionCount(newContents)).toBe(compCount);
+ expect(newContents).not.toContain('"one"');
+ expect(newContents).toContain('"abc"');
+});
diff --git a/packages/studio-server/src/codemods/duplicate-composition.ts b/packages/studio-server/src/codemods/duplicate-composition.ts
index 7c939f73cf..4acfc6fda2 100644
--- a/packages/studio-server/src/codemods/duplicate-composition.ts
+++ b/packages/studio-server/src/codemods/duplicate-composition.ts
@@ -38,13 +38,13 @@ export const formatOutput = async (tsContent: string) => {
return newContents;
};
-export const parseAndApplyCodemod = async ({
+export const parseAndApplyCodemod = ({
input,
codeMod,
}: {
input: string;
codeMod: RecastCodemod;
-}): Promise<{newContents: string; changesMade: Change[]}> => {
+}): {newContents: string; changesMade: Change[]} => {
const ast = recast.parse(input, {
parser: tsParser,
}) as File;
@@ -55,7 +55,7 @@ export const parseAndApplyCodemod = async ({
});
if (changesMade.length === 0) {
- throw new Error('Could not find changes to made to the file');
+ throw new Error('Could not find any changes to make to the file');
}
const output = recast.print(newAst, {
diff --git a/packages/studio-server/src/codemods/recast-mods.ts b/packages/studio-server/src/codemods/recast-mods.ts
index dc463f055d..18c4bd4c67 100644
--- a/packages/studio-server/src/codemods/recast-mods.ts
+++ b/packages/studio-server/src/codemods/recast-mods.ts
@@ -1,8 +1,11 @@
import type {
ArrowFunctionExpression,
+ BlockStatement,
Declaration,
Expression,
File,
+ FunctionDeclaration,
+ FunctionExpression,
JSXAttribute,
JSXFragment,
Statement,
@@ -44,41 +47,71 @@ const mapTopLevlNode = (
transformation: RecastCodemod,
changesMade: Change[],
): Statement => {
- if (
- // TODO: could also be a default export
- node.type !== 'ExportNamedDeclaration'
- ) {
- return node;
- }
-
- if (!node.declaration) {
- return node;
+ if (node.type === 'ExportNamedDeclaration') {
+ if (!node.declaration) {
+ return node;
+ }
+
+ return {
+ ...node,
+ declaration: mapDeclaration(
+ node.declaration,
+ transformation,
+ changesMade,
+ ),
+ };
}
- // TODO: Could also be an anonymous function etc?
- if (node.declaration.type !== 'VariableDeclaration') {
- return node;
+ if (node.type === 'ExportDefaultDeclaration') {
+ if (node.declaration.type === 'FunctionDeclaration') {
+ return {
+ ...node,
+ declaration: mapDeclaration(
+ node.declaration,
+ transformation,
+ changesMade,
+ ),
+ };
+ }
+
+ if (
+ node.declaration.type === 'ArrowFunctionExpression' ||
+ node.declaration.type === 'FunctionExpression'
+ ) {
+ return {
+ ...node,
+ declaration: mapFunctionExpression(
+ node.declaration,
+ transformation,
+ changesMade,
+ ),
+ };
+ }
}
- return {
- ...node,
- declaration: mapDeclaration(node.declaration, transformation, changesMade),
- };
+ return node;
};
-const mapDeclaration = (
- declaration: Declaration,
+const mapDeclaration = <T extends Declaration | FunctionDeclaration>(
+ declaration: T,
transformation: RecastCodemod,
changesMade: Change[],
-): Declaration => {
- if (declaration.type !== 'VariableDeclaration') {
- return declaration;
+): T => {
+ if (declaration.type === 'VariableDeclaration') {
+ const declarations = declaration.declarations.map((d) => {
+ return mapVariableDeclarator(d, transformation, changesMade);
+ });
+ return {...declaration, declarations};
}
- const declarations = declaration.declarations.map((d) => {
- return mapVariableDeclarator(d, transformation, changesMade);
- });
- return {...declaration, declarations};
+ if (declaration.type === 'FunctionDeclaration') {
+ return {
+ ...declaration,
+ body: mapBlockStatement(declaration.body, transformation, changesMade),
+ };
+ }
+
+ return declaration;
};
const mapVariableDeclarator = (
@@ -99,19 +132,36 @@ const mapExpression = (
transformation: RecastCodemod,
changesMade: Change[],
) => {
- // TODO: This could definitely be a different type
- if (expression.type !== 'ArrowFunctionExpression') {
+ if (
+ expression.type !== 'ArrowFunctionExpression' &&
+ expression.type !== 'FunctionExpression'
+ ) {
return expression;
}
- return mapArrowFunctionExpression(expression, transformation, changesMade);
+ return mapFunctionExpression(expression, transformation, changesMade);
+};
+
+const mapBlockStatement = (
+ blockStatement: BlockStatement,
+ transformation: RecastCodemod,
+ changesMade: Change[],
+): BlockStatement => {
+ return {
+ ...blockStatement,
+ body: blockStatement.body.map((a) => {
+ return mapBodyStatement(a, transformation, changesMade);
+ }),
+ };
};
-const mapArrowFunctionExpression = (
- arrowFunctionExpression: ArrowFunctionExpression,
+const mapFunctionExpression = <
+ T extends ArrowFunctionExpression | FunctionExpression,
+>(
+ arrowFunctionExpression: T,
transformation: RecastCodemod,
changesMade: Change[],
-): ArrowFunctionExpression => {
+): T => {
const {body} = arrowFunctionExpression;
if (body.type !== 'BlockStatement') {
@@ -120,12 +170,7 @@ const mapArrowFunctionExpression = (
return {
...arrowFunctionExpression,
- body: {
- ...body,
- body: body.body.map((a) => {
- return mapBodyStatement(a, transformation, changesMade);
- }),
- },
+ body: mapBlockStatement(body, transformation, changesMade),
};
};