Skip to content

🎥 Make videos programmatically with React

fix ast codemod issue

hunxjunedof03148b33a8a
diff --git a/packages/studio-server/src/codemods/update-default-props.ts b/packages/studio-server/src/codemods/update-default-props.ts
index 4e1bd3c9be..4484efd3a0 100644
--- a/packages/studio-server/src/codemods/update-default-props.ts
+++ b/packages/studio-server/src/codemods/update-default-props.ts
@@ -1,13 +1,12 @@
-import type { EnumPath } from '@remotion/studio-shared';
+import {stringifyDefaultProps, type EnumPath} from '@remotion/studio-shared';
import * as recast from 'recast';
-import { parse } from 'recast/parsers/babel-ts'
-
+import {parse} from 'recast/parsers/babel-ts';
export const updateDefaultProps = async ({
input,
compositionId,
newDefaultProps,
- // enum paths not required as no need to stringify now
+ enumPaths,
}: {
input: string;
compositionId: string;
@@ -15,9 +14,8 @@ export const updateDefaultProps = async ({
enumPaths: EnumPath[];
}): Promise<string> => {
const ast = recast.parse(input, {
- parser: { parse }
- })
-
+ parser: {parse},
+ });
recast.types.visit(ast, {
visitJSXElement(path) {
@@ -25,12 +23,15 @@ export const updateDefaultProps = async ({
// 1: ensure its the element we're looking for
if (
openingElement.name.name === 'Composition' &&
- openingElement.attributes.some((attr: any) =>
- attr.name.name === 'id' && attr.value.value === compositionId
+ openingElement.attributes.some(
+ (attr: any) =>
+ attr.name.name === 'id' && attr.value.value === compositionId,
)
) {
// 2: Find the defaultProps attribute and handle related errors
- const defaultPropsAttr = openingElement.attributes.find((attr: any) => attr.name.name === 'defaultProps');
+ const defaultPropsAttr = openingElement.attributes.find(
+ (attr: any) => attr.name.name === 'defaultProps',
+ );
if (!defaultPropsAttr) {
throw new Error(
`No \`defaultProps\` prop found in the <Composition/> tag with the ID "${compositionId}".`,
@@ -40,62 +41,28 @@ export const updateDefaultProps = async ({
// 3: ensure only hardcoded values are provided
const defaultPropsValue = defaultPropsAttr.value.expression; // Get the value of defaultProps
- if (defaultPropsValue.type !== 'ObjectExpression') {
+ if (
+ defaultPropsValue.type !== 'ObjectExpression' &&
+ defaultPropsValue.type !== 'ObjectLiteralExpression' &&
+ defaultPropsValue.type !== 'AsExpression' &&
+ defaultPropsValue.type !== 'TSAsExpression'
+ ) {
throw new Error(
- `\`defaultProps\` prop does not have a hardcoded value in the <Composition/> tag with the ID "${compositionId}".`,
+ `\`defaultProps\` prop does not have a hardcoded value in the <Composition/> tag with the ID "${defaultPropsValue.type}".`,
);
}
-
// 4: modify the code
- const propsParser: any = (obj: any) => {
- if (typeof obj === 'object' && !Array.isArray(obj) && obj !== null) {
- // Wrap properties in an ObjectExpression for objects
- return recast.types.builders.objectExpression(
- Object.keys(obj).map((key) =>
- recast.types.builders.property(
- 'init',
- recast.types.builders.identifier(key),
- propsParser(obj[key]) // Recursively parse each value
- )
- )
- );
- }
-
- if (Array.isArray(obj)) {
- // Return an ArrayExpression for arrays
- return recast.types.builders.arrayExpression(obj.map(propsParser));
- }
-
- if (typeof obj === 'string') {
- return recast.types.builders.stringLiteral(obj);
- }
-
- if (typeof obj === 'number') {
- return recast.types.builders.numericLiteral(obj);
- }
-
- if (typeof obj === 'boolean') {
- return recast.types.builders.booleanLiteral(obj);
- }
-
- if (obj === null) {
- return recast.types.builders.nullLiteral();
- }
-
- throw new Error(`Unsupported data type: ${typeof obj}`);
- };
-
- defaultPropsAttr.value.expression = propsParser(newDefaultProps);
-
+ defaultPropsAttr.value.expression = recast.types.builders.identifier(
+ stringifyDefaultProps({props: newDefaultProps, enumPaths}),
+ );
}
this.traverse(path); // Continue traversing the AST
},
});
-
// 5: finally, format the file
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
type PrettierType = typeof import('prettier');
@@ -103,11 +70,11 @@ export const updateDefaultProps = async ({
try {
prettier = await import('prettier');
- } catch (err) {
+ } catch {
throw new Error('Prettier cannot be found in the current project.');
}
- const { format, resolveConfig, resolveConfigFile } = prettier as PrettierType;
+ const {format, resolveConfig, resolveConfigFile} = prettier as PrettierType;
const configFilePath = await resolveConfigFile();
if (!configFilePath) {
@@ -121,13 +88,13 @@ export const updateDefaultProps = async ({
);
}
- const prettified = await format(recast.print(ast).code, {
+ const finalfile = recast.print(ast).code;
+
+ const prettified = await format(finalfile, {
...prettierConfig,
filepath: 'test.tsx',
plugins: [],
endOfLine: 'auto',
});
return prettified;
-
-
-}
\ No newline at end of file
+};