Skip to content

🎥 Make videos programmatically with React

return values "as const"

Jonny Burgere25100b152e8
diff --git a/packages/cli/src/codemods/update-default-props.ts b/packages/cli/src/codemods/update-default-props.ts
index 57fc7a9b63..7a3d83df26 100644
--- a/packages/cli/src/codemods/update-default-props.ts
+++ b/packages/cli/src/codemods/update-default-props.ts
@@ -36,13 +36,27 @@ const findStarter = ({
throw new Error(`Could not find composition ID ${compositionId} in file`);
};
+const findEndPosition = (input: string, currentPosition: number) => {
+ const next = input.indexOf('}}', currentPosition + 1);
+ if (next !== -1) {
+ return next;
+ }
+
+ const asConstVersion = input
+ .slice(currentPosition + 1)
+ .search(/as\sconst[ \t\n\r]+\}/);
+ if (asConstVersion !== -1) {
+ const nextEnd = input.indexOf('}', asConstVersion + currentPosition + 1);
+ return nextEnd - 1;
+ }
+
+ throw new Error('Could not find end of defaultProps');
+};
+
const findEnder = (input: string, position: number, maxPosition: number) => {
let currentPosition = position;
while (currentPosition < maxPosition) {
- const next = input.indexOf('}}', currentPosition + 1);
- if (next === -1) {
- throw new Error('Could not find end of defaultProps');
- }
+ const next = findEndPosition(input, currentPosition);
currentPosition = next;
diff --git a/packages/cli/src/test/snapshots/root-before.tsx b/packages/cli/src/test/snapshots/root-before.tsx
index 54d047d9ec..c14924dc49 100644
--- a/packages/cli/src/test/snapshots/root-before.tsx
+++ b/packages/cli/src/test/snapshots/root-before.tsx
@@ -31,10 +31,12 @@ export const Root: React.FC = () => {
height={1080}
width={1080}
id="Comp3"
- defaultProps={{
- foo: 'bar',
- date: new Date('2022-01-01'),
- }}
+ defaultProps={
+ {
+ foo: 'bar',
+ date: new Date('2022-01-01'),
+ } as const
+ }
/>
</>
);