Skip to content

🎥 Make videos programmatically with React

Insert args object for zero-arg effects; drop no-args-object

Jonny Burger5f659db637b4
diff --git a/packages/core/src/SequenceManager.tsx b/packages/core/src/SequenceManager.tsx
index b1bdb8695d..dd86bb46c3 100644
--- a/packages/core/src/SequenceManager.tsx
+++ b/packages/core/src/SequenceManager.tsx
@@ -90,7 +90,7 @@ export type CanUpdateEffectPropsResponseTrue = {
export type CanUpdateEffectPropsResponseFalse = {
canUpdate: false;
effectIndex: number;
- reason: 'no-args-object' | 'not-found' | 'computed' | 'not-call-expression';
+ reason: 'not-found' | 'computed' | 'not-call-expression';
};
export type CanUpdateEffectPropsResponse =
diff --git a/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts b/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts
index bfd89aed90..73d3ee0f15 100644
--- a/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts
+++ b/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts
@@ -128,7 +128,7 @@ export const findEffectCallExpression = ({
| {kind: 'ok'; call: CallExpression}
| {
kind: 'error';
- reason: 'no-args-object' | 'not-found' | 'not-call-expression';
+ reason: 'not-found' | 'not-call-expression';
} => {
if (!attr.value || attr.value.type !== 'JSXExpressionContainer') {
return {kind: 'error', reason: 'not-call-expression'};
@@ -215,20 +215,32 @@ export const updateEffectPropsAst = ({
const {call} = found;
- if (
- call.arguments.length === 0 ||
- call.arguments[0].type !== 'ObjectExpression'
- ) {
- throw new Error('Cannot update effect prop: no-args-object');
- }
-
- const objExpr = call.arguments[0] as ObjectExpression;
- const {prop} = findObjectProperty(objExpr, update.key);
-
const isDefault =
update.defaultValue !== null &&
JSON.stringify(update.value) === JSON.stringify(update.defaultValue);
+ let objExpr: ObjectExpression;
+
+ if (call.arguments.length === 0) {
+ if (isDefault) {
+ const logLine = call.loc?.start.line ?? jsx.loc?.start.line ?? 1;
+ return {
+ serialized: serializeAst(ast),
+ oldValueString: '',
+ logLine,
+ };
+ }
+
+ objExpr = b.objectExpression([]) as ObjectExpression;
+ call.arguments.push(objExpr);
+ } else if (call.arguments[0].type !== 'ObjectExpression') {
+ throw new Error('Cannot update effect prop: computed');
+ } else {
+ objExpr = call.arguments[0] as ObjectExpression;
+ }
+
+ const {prop} = findObjectProperty(objExpr, update.key);
+
let oldValueString = '';
if (prop) {
oldValueString = recast.print(prop.value).code;
diff --git a/packages/studio-server/src/preview-server/routes/can-update-effect-props.ts b/packages/studio-server/src/preview-server/routes/can-update-effect-props.ts
index 9f66be6430..eda2a60b69 100644
--- a/packages/studio-server/src/preview-server/routes/can-update-effect-props.ts
+++ b/packages/studio-server/src/preview-server/routes/can-update-effect-props.ts
@@ -154,7 +154,7 @@ export const computeEffectPropStatus = ({
return {
canUpdate: false,
effectIndex,
- reason: 'no-args-object',
+ reason: 'computed',
};
}
diff --git a/packages/studio-server/src/test/compute-effect-props-status.test.ts b/packages/studio-server/src/test/compute-effect-props-status.test.ts
index be395671f3..2341a6a8ad 100644
--- a/packages/studio-server/src/test/compute-effect-props-status.test.ts
+++ b/packages/studio-server/src/test/compute-effect-props-status.test.ts
@@ -111,7 +111,7 @@ test('computeEffectPropStatus flags out-of-range effect indices', () => {
expect(result.reason).toBe('not-found');
});
-test('computeEffectPropStatus flags non-object first arg', () => {
+test('computeEffectPropStatus treats non-object first arg as computed', () => {
const input = buildInput('[tint(getParams())]');
const result = computeEffectPropStatus({
jsx: findJsx(input),
@@ -124,5 +124,24 @@ test('computeEffectPropStatus flags non-object first arg', () => {
throw new Error('expected canUpdate false');
}
- expect(result.reason).toBe('no-args-object');
+ expect(result.reason).toBe('computed');
+});
+
+test('computeEffectPropStatus treats zero-arg effect as editable with undefined props', () => {
+ const input = buildInput('[tint()]');
+ const result = computeEffectPropStatus({
+ jsx: findJsx(input),
+ effectIndex: 0,
+ keys: ['amount'],
+ });
+
+ expect(result.canUpdate).toBe(true);
+ if (!result.canUpdate) {
+ throw new Error('expected canUpdate true');
+ }
+
+ expect(result.props.amount).toEqual({
+ canUpdate: true,
+ codeValue: undefined,
+ });
});
diff --git a/packages/studio-server/src/test/update-effect-props.test.ts b/packages/studio-server/src/test/update-effect-props.test.ts
index 9c5b49c8b5..8ac28daabf 100644
--- a/packages/studio-server/src/test/update-effect-props.test.ts
+++ b/packages/studio-server/src/test/update-effect-props.test.ts
@@ -83,7 +83,20 @@ test('updateEffectProps throws when effect index is out of range', () => {
}).toThrow(/not-found/);
});
-test('updateEffectProps throws when first arg is not an object literal (no-args-object)', () => {
+test('updateEffectProps inserts object literal when effect has no arguments', () => {
+ const input = buildInput('[tint()]');
+ const {serialized} = updateEffectPropsAst({
+ input,
+ sequenceNodePath: lineColumnToNodePath(input, 6),
+ effectIndex: 0,
+ update: {key: 'amount', value: 0.5, defaultValue: null},
+ });
+
+ expect(serialized).toContain('amount: 0.5');
+ expect(serialized).toMatch(/tint\s*\(\s*\{/);
+});
+
+test('updateEffectProps throws when first arg is not an object literal', () => {
const input = buildInput('[tint(getParams())]');
expect(() => {
updateEffectPropsAst({
@@ -92,5 +105,5 @@ test('updateEffectProps throws when first arg is not an object literal (no-args-
effectIndex: 0,
update: {key: 'opacity', value: 0.5, defaultValue: null},
});
- }).toThrow(/no-args-object/);
+ }).toThrow(/computed/);
});
diff --git a/packages/studio-shared/src/test/optimistic-update-for-effect-code-values.test.ts b/packages/studio-shared/src/test/optimistic-update-for-effect-code-values.test.ts
index 829f25cb61..4102b8a8e7 100644
--- a/packages/studio-shared/src/test/optimistic-update-for-effect-code-values.test.ts
+++ b/packages/studio-shared/src/test/optimistic-update-for-effect-code-values.test.ts
@@ -74,3 +74,39 @@ test('optimisticUpdateForEffectCodeValues is a no-op when effect index not found
expect(result).toBe(previous);
});
+
+test('optimisticUpdateForEffectCodeValues applies when effect props are unset (zero-arg style)', () => {
+ const previous: CanUpdateSequencePropsResponse = {
+ canUpdate: true,
+ props: {},
+ effects: [
+ {
+ canUpdate: true,
+ effectIndex: 0,
+ callee: 'tint',
+ props: {
+ amount: {canUpdate: true, codeValue: undefined},
+ },
+ },
+ ],
+ };
+
+ const updated = optimisticUpdateForEffectCodeValues({
+ previous,
+ effectIndex: 0,
+ fieldKey: 'amount',
+ value: 0.5,
+ schema: {amount: {type: 'number', default: 1}},
+ });
+
+ if (!updated.canUpdate) {
+ throw new Error('expected canUpdate true');
+ }
+
+ const effect = updated.effects[0];
+ if (!effect.canUpdate) {
+ throw new Error('expected effect canUpdate true');
+ }
+
+ expect(effect.props.amount).toEqual({canUpdate: true, codeValue: 0.5});
+});