Skip to content

🎥 Make videos programmatically with React

Only treat props that a spread may override as computed

JonnyBurger96b5d0800718
diff --git a/packages/studio-codemods/src/sequence-props.ts b/packages/studio-codemods/src/sequence-props.ts
index 1a72cd8342..24d7e7f86c 100644
--- a/packages/studio-codemods/src/sequence-props.ts
+++ b/packages/studio-codemods/src/sequence-props.ts
@@ -684,6 +684,11 @@ const getPropsStatus = (
for (const attr of jsxElement.attributes) {
if (attr.type === 'JSXSpreadAttribute') {
+ // The spread may override every prop written before it
+ for (const key of Object.keys(props)) {
+ props[key] = computedStatus();
+ }
+
continue;
}
@@ -1060,6 +1065,7 @@ const getNestedPropStatus = ({
childKey,
videoConfigValues,
allowSpecialValues,
+ lastSpreadIndex,
}: {
jsxElement: JSXOpeningElement;
ast: File;
@@ -1067,19 +1073,38 @@ const getNestedPropStatus = ({
childKey: string;
videoConfigValues: VideoConfigIdentifierValues;
allowSpecialValues: boolean;
+ lastSpreadIndex: number;
}): CanUpdatePropStatus => {
- const attr = jsxElement.attributes.find(
+ const attrIndex = jsxElement.attributes.findIndex(
(a) =>
a.type !== 'JSXSpreadAttribute' &&
a.name.type !== 'JSXNamespacedName' &&
a.name.name === parentKey,
- ) as JSXAttribute | undefined;
+ );
+ const attr =
+ attrIndex === -1
+ ? undefined
+ : (jsxElement.attributes[attrIndex] as JSXAttribute);
+
+ if (attrIndex !== -1 && attrIndex < lastSpreadIndex) {
+ // A later spread may replace the whole parent object
+ return computedStatus();
+ }
+
+ if (!attr) {
+ if (lastSpreadIndex !== -1) {
+ // The spread may provide the parent object
+ return computedStatus();
+ }
- if (!attr || !attr.value) {
// Parent attribute doesn't exist, nested prop can be added
return staticStatus(undefined, null);
}
+ if (!attr.value) {
+ return staticStatus(undefined, null);
+ }
+
if (attr.value.type !== 'JSXExpressionContainer') {
return computedStatus();
}
@@ -1219,21 +1244,17 @@ const computeSequenceOnlyPropsRecord = ({
assetKeys: string[];
videoConfigValues: VideoConfigIdentifierValues;
}): Record<string, CanUpdatePropStatus> => {
- // Props may arrive through a JSX spread attribute ({...props}), which the
- // parser cannot resolve. Since the spread may set or override any prop,
- // treat all of them as computed so the runtime values pass through
- // untouched and Visual Mode does not offer to edit them.
- if (
- jsxElement.attributes.some((attr) => attr.type === 'JSXSpreadAttribute')
- ) {
- const computedProps: Record<string, CanUpdatePropStatus> = {};
- for (const key of keys) {
- computedProps[key] = computedStatus();
- }
-
- return computedProps;
- }
-
+ // A JSX spread attribute ({...props}) is invisible to the parser and may
+ // set or override every prop that is not explicitly written after it.
+ // Affected props are treated as computed so the runtime values pass
+ // through untouched and Visual Mode does not offer to edit them.
+ // Attributes written after the last spread win at runtime and can be
+ // parsed as usual.
+ const lastSpreadIndex = jsxElement.attributes.reduce(
+ (highest, attr, index) =>
+ attr.type === 'JSXSpreadAttribute' ? index : highest,
+ -1,
+ );
const allProps = getPropsStatus(
jsxElement,
ast,
@@ -1253,6 +1274,18 @@ const computeSequenceOnlyPropsRecord = ({
}
if (key === 'children') {
+ const childrenAttrIndex = jsxElement.attributes.findIndex(
+ (attr) =>
+ attr.type === 'JSXAttribute' &&
+ attr.name.type === 'JSXIdentifier' &&
+ attr.name.name === 'children',
+ );
+ if (childrenAttrIndex !== -1 && childrenAttrIndex < lastSpreadIndex) {
+ // A later spread may override the children attribute
+ filteredProps[key] = computedStatus();
+ continue;
+ }
+
const staticChildrenAttribute = getStaticJsxChildrenAttribute(jsxElement);
if (staticChildrenAttribute) {
filteredProps[key] = staticStatus(staticChildrenAttribute.value, null);
@@ -1264,6 +1297,18 @@ const computeSequenceOnlyPropsRecord = ({
continue;
}
+ // JSX element children are compiled after any spread and win over
+ // it. Only if the element body is empty may the spread provide the
+ // children.
+ const hasMeaningfulJsxChildren = jsxElementNode.children.some(
+ (candidate) =>
+ !(candidate.type === 'JSXText' && candidate.value.trim() === ''),
+ );
+ if (!hasMeaningfulJsxChildren && lastSpreadIndex !== -1) {
+ filteredProps[key] = computedStatus();
+ continue;
+ }
+
const staticTextContent = getStaticJsxTextContent(jsxElementNode);
filteredProps[key] = staticTextContent
? staticStatus(staticTextContent.value, null)
@@ -1280,9 +1325,13 @@ const computeSequenceOnlyPropsRecord = ({
childKey: key.slice(dotIndex + 1),
videoConfigValues,
allowSpecialValues: assetKeys.includes(key),
+ lastSpreadIndex,
});
} else if (key in allProps) {
filteredProps[key] = allProps[key];
+ } else if (lastSpreadIndex !== -1) {
+ // The spread may provide this prop
+ filteredProps[key] = computedStatus();
} else {
filteredProps[key] = staticStatus(undefined, null);
}
diff --git a/packages/studio-codemods/src/test/sequence-props-spread.test.ts b/packages/studio-codemods/src/test/sequence-props-spread.test.ts
index f8f8adfca7..3e163405d0 100644
--- a/packages/studio-codemods/src/test/sequence-props-spread.test.ts
+++ b/packages/studio-codemods/src/test/sequence-props-spread.test.ts
@@ -1,51 +1,73 @@
import {expect, test} from 'bun:test';
import {computeSequencePropsSubscriptionFromContent} from '../sequence-props';
-// https://github.com/remotion-dev/remotion/issues/10717
-// Props passed via a JSX spread are invisible to the parser. Reporting them as
-// static(undefined) made the Studio delete the runtime values of `from` /
-// `durationInFrames`, mounting every <Sequence> at every frame.
-test('treats all props as computed if a spread attribute is present', () => {
- const input = `import {Sequence} from 'remotion';
-
-export const Example = ({timing}: {timing: {from: number; durationInFrames: number}}) => {
- return (
- <Sequence {...timing} name="Scene" style={{opacity: 0.5}}>
- <div />
- </Sequence>
- );
+const videoConfigValues = {
+ durationInFrames: 120,
+ fps: 30,
+ height: 1080,
+ width: 1920,
};
-`;
+
+const subscribe = (input: string, line: number, keys: string[]) => {
const result = computeSequencePropsSubscriptionFromContent({
fileContents: input,
absolutePath: '/project/src/Example.tsx',
- line: 5,
+ line,
preferredNodePath: null,
componentIdentity: 'dev.remotion.remotion.Sequence',
- keys: ['from', 'durationInFrames', 'name', 'style.opacity'],
+ keys,
effects: [],
- videoConfigValues: {
- durationInFrames: 120,
- fps: 30,
- height: 1080,
- width: 1920,
- },
+ videoConfigValues,
});
- expect(result.success).toBe(true);
- if (!result.success) {
+ if (!result.success || !result.status.canUpdate) {
throw new Error('Expected the subscription to succeed');
}
- expect(result.status.canUpdate).toBe(true);
- if (!result.status.canUpdate) {
- throw new Error('Expected canUpdate to be true');
- }
+ return result.status.props;
+};
+
+// https://github.com/remotion-dev/remotion/issues/10717
+// Props passed via a JSX spread are invisible to the parser. Reporting them as
+// static(undefined) made the Studio delete the runtime values of `from` /
+// `durationInFrames`, mounting every <Sequence> at every frame.
+test('treats props that a spread attribute may override as computed', () => {
+ const input = `import {Sequence} from 'remotion';
- expect(result.status.props).toEqual({
+export const Example = ({timing}: {timing: {from: number; durationInFrames: number}}) => {
+ return (
+ <>
+ <Sequence from={10} {...timing} name="Scene" style={{opacity: 0.5}}>
+ <div />
+ </Sequence>
+ <Sequence {...timing} />
+ </>
+ );
+};
+`;
+ expect(
+ subscribe(input, 6, ['from', 'durationInFrames', 'name', 'style.opacity']),
+ ).toEqual({
+ // Written before the spread, so the spread may override it
from: {status: 'computed'},
+ // Only provided by the spread
durationInFrames: {status: 'computed'},
- name: {status: 'computed'},
- 'style.opacity': {status: 'computed'},
+ // Written after the spread, so they win at runtime and stay editable
+ name: {
+ status: 'static',
+ keyframeDisplayOffsetAdjustment: null,
+ codeValue: 'Scene',
+ },
+ 'style.opacity': {
+ status: 'static',
+ keyframeDisplayOffsetAdjustment: null,
+ codeValue: 0.5,
+ },
+ });
+
+ // With an empty element body, the spread may provide both props
+ expect(subscribe(input, 9, ['from', 'children'])).toEqual({
+ from: {status: 'computed'},
+ children: {status: 'computed'},
});
});
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 1c0add0184..3cb3b73625 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
@@ -1112,6 +1112,11 @@ const getPropsStatus = (
for (const attr of jsxElement.attributes) {
if (attr.type === 'JSXSpreadAttribute') {
+ // The spread may override every prop written before it
+ for (const key of Object.keys(props)) {
+ props[key] = computedStatus();
+ }
+
continue;
}
@@ -1600,6 +1605,7 @@ const getNestedPropStatus = ({
childKey,
videoConfigValues,
allowSpecialValues,
+ lastSpreadIndex,
}: {
jsxElement: JSXOpeningElement;
ast: File;
@@ -1607,19 +1613,38 @@ const getNestedPropStatus = ({
childKey: string;
videoConfigValues: VideoConfigIdentifierValues;
allowSpecialValues: boolean;
+ lastSpreadIndex: number;
}): CanUpdatePropStatus => {
- const attr = jsxElement.attributes.find(
+ const attrIndex = jsxElement.attributes.findIndex(
(a) =>
a.type !== 'JSXSpreadAttribute' &&
a.name.type !== 'JSXNamespacedName' &&
a.name.name === parentKey,
- ) as JSXAttribute | undefined;
+ );
+ const attr =
+ attrIndex === -1
+ ? undefined
+ : (jsxElement.attributes[attrIndex] as JSXAttribute);
+
+ if (attrIndex !== -1 && attrIndex < lastSpreadIndex) {
+ // A later spread may replace the whole parent object
+ return computedStatus();
+ }
+
+ if (!attr) {
+ if (lastSpreadIndex !== -1) {
+ // The spread may provide the parent object
+ return computedStatus();
+ }
- if (!attr || !attr.value) {
// Parent attribute doesn't exist, nested prop can be added
return staticStatus(undefined, null);
}
+ if (!attr.value) {
+ return staticStatus(undefined, null);
+ }
+
if (attr.value.type !== 'JSXExpressionContainer') {
return computedStatus();
}
@@ -1759,21 +1784,17 @@ const computeSequenceOnlyPropsRecord = ({
assetKeys: string[];
videoConfigValues: VideoConfigIdentifierValues;
}): Record<string, CanUpdatePropStatus> => {
- // Props may arrive through a JSX spread attribute ({...props}), which the
- // parser cannot resolve. Since the spread may set or override any prop,
- // treat all of them as computed so the runtime values pass through
- // untouched and Visual Mode does not offer to edit them.
- if (
- jsxElement.attributes.some((attr) => attr.type === 'JSXSpreadAttribute')
- ) {
- const computedProps: Record<string, CanUpdatePropStatus> = {};
- for (const key of keys) {
- computedProps[key] = computedStatus();
- }
-
- return computedProps;
- }
-
+ // A JSX spread attribute ({...props}) is invisible to the parser and may
+ // set or override every prop that is not explicitly written after it.
+ // Affected props are treated as computed so the runtime values pass
+ // through untouched and Visual Mode does not offer to edit them.
+ // Attributes written after the last spread win at runtime and can be
+ // parsed as usual.
+ const lastSpreadIndex = jsxElement.attributes.reduce(
+ (highest, attr, index) =>
+ attr.type === 'JSXSpreadAttribute' ? index : highest,
+ -1,
+ );
const allProps = getPropsStatus(
jsxElement,
ast,
@@ -1793,6 +1814,18 @@ const computeSequenceOnlyPropsRecord = ({
}
if (key === 'children') {
+ const childrenAttrIndex = jsxElement.attributes.findIndex(
+ (attr) =>
+ attr.type === 'JSXAttribute' &&
+ attr.name.type === 'JSXIdentifier' &&
+ attr.name.name === 'children',
+ );
+ if (childrenAttrIndex !== -1 && childrenAttrIndex < lastSpreadIndex) {
+ // A later spread may override the children attribute
+ filteredProps[key] = computedStatus();
+ continue;
+ }
+
const staticChildrenAttribute = getStaticJsxChildrenAttribute(jsxElement);
if (staticChildrenAttribute) {
filteredProps[key] = staticStatus(staticChildrenAttribute.value, null);
@@ -1804,6 +1837,18 @@ const computeSequenceOnlyPropsRecord = ({
continue;
}
+ // JSX element children are compiled after any spread and win over
+ // it. Only if the element body is empty may the spread provide the
+ // children.
+ const hasMeaningfulJsxChildren = jsxElementNode.children.some(
+ (candidate) =>
+ !(candidate.type === 'JSXText' && candidate.value.trim() === ''),
+ );
+ if (!hasMeaningfulJsxChildren && lastSpreadIndex !== -1) {
+ filteredProps[key] = computedStatus();
+ continue;
+ }
+
const staticTextContent = getStaticJsxTextContent(jsxElementNode);
filteredProps[key] = staticTextContent
? staticStatus(staticTextContent.value, null)
@@ -1820,9 +1865,13 @@ const computeSequenceOnlyPropsRecord = ({
childKey: key.slice(dotIndex + 1),
videoConfigValues,
allowSpecialValues: assetKeys.includes(key),
+ lastSpreadIndex,
});
} else if (key in allProps) {
filteredProps[key] = allProps[key];
+ } else if (lastSpreadIndex !== -1) {
+ // The spread may provide this prop
+ filteredProps[key] = computedStatus();
} else {
filteredProps[key] = staticStatus(undefined, null);
}
diff --git a/packages/studio-server/src/test/compute-sequence-props-status.test.ts b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
index 40753c01b4..cba3c89986 100644
--- a/packages/studio-server/src/test/compute-sequence-props-status.test.ts
+++ b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
@@ -222,31 +222,60 @@ export const Example = () => {
// Props passed via a JSX spread are invisible to the parser. Reporting them as
// static(undefined) made the Studio delete the runtime values of `from` /
// `durationInFrames`, mounting every <Sequence> at every frame.
-test('computeSequencePropsStatus should treat all props as computed if a spread attribute is present', () => {
+test('computeSequencePropsStatus should treat props that a spread attribute may override as computed', () => {
const input = `import {Sequence} from 'remotion';
export const Example = ({timing}: {timing: {from: number; durationInFrames: number}}) => {
return (
- <Sequence {...timing} name="Scene" style={{opacity: 0.5}}>
- <div />
- </Sequence>
+ <>
+ <Sequence from={10} {...timing} name="Scene" style={{opacity: 0.5}}>
+ <div />
+ </Sequence>
+ <Sequence {...timing} />
+ </>
);
};
`;
- const result = computeSequencePropsStatusFromContent({
+ const spreadMayOverride = computeSequencePropsStatusFromContent({
fileContents: input,
- nodePath: getNodePathFromContent(input, 5),
+ nodePath: getNodePathFromContent(input, 6),
componentIdentity: null,
keys: ['from', 'durationInFrames', 'name', 'style.opacity'],
effects: [],
videoConfigValues: null,
});
- expect(result.props).toEqual({
+ expect(spreadMayOverride.props).toEqual({
+ // Written before the spread, so the spread may override it
from: {status: 'computed'},
+ // Only provided by the spread
durationInFrames: {status: 'computed'},
- name: {status: 'computed'},
- 'style.opacity': {status: 'computed'},
+ // Written after the spread, so they win at runtime and stay editable
+ name: {
+ status: 'static',
+ keyframeDisplayOffsetAdjustment: null,
+ codeValue: 'Scene',
+ },
+ 'style.opacity': {
+ status: 'static',
+ keyframeDisplayOffsetAdjustment: null,
+ codeValue: 0.5,
+ },
+ });
+
+ const spreadMayProvide = computeSequencePropsStatusFromContent({
+ fileContents: input,
+ nodePath: getNodePathFromContent(input, 9),
+ componentIdentity: null,
+ keys: ['from', 'children'],
+ effects: [],
+ videoConfigValues: null,
+ });
+
+ // With an empty element body, the spread may provide both props
+ expect(spreadMayProvide.props).toEqual({
+ from: {status: 'computed'},
+ children: {status: 'computed'},
});
});