Skip to content

🎥 Make videos programmatically with React

Allow safe pure calculations when pre-composing

JonnyBurgerc2a916141316
diff --git a/packages/codemods/src/precompose-jsx-nodes.ts b/packages/codemods/src/precompose-jsx-nodes.ts
index 02d4ba6d12..65d660f87a 100644
--- a/packages/codemods/src/precompose-jsx-nodes.ts
+++ b/packages/codemods/src/precompose-jsx-nodes.ts
@@ -1,4 +1,5 @@
import {
+ VISITOR_KEYS,
isReferenced,
type File,
type JSXElement,
@@ -27,6 +28,11 @@ import {
type NodeReference,
type NodeSourceEdit,
} from './node-references';
+import {
+ getPureTopLevelFunctionAnalysis,
+ isPureMathConstant,
+ isPureMathMethod,
+} from './precompose-pure-functions';
import {recastLocToOffset} from './recast-loc-to-offset';
import {ensureNamedImport, getImportedName} from './sequence-props/imports';
import {parseAst} from './sequence-props/parse-ast';
@@ -47,6 +53,7 @@ type PrecompositionPlan = {
hookName: string;
kind: 'frame' | 'duration' | 'fps';
}[];
+ derivedProps: {name: string; initSource: string}[];
logLine: number;
parent: JSXElement | JSXFragment | null;
rootPath: recast.types.NodePath | null;
@@ -288,6 +295,15 @@ const getPrecompositionPlan = ({
}
}
+ const {
+ pureFunctionNames: pureTopLevelFunctions,
+ primitiveFunctionNames: primitiveTopLevelFunctions,
+ } = getPureTopLevelFunctionAnalysis({
+ ast,
+ remotionImports,
+ stablePrimitiveNames,
+ });
+
const sequenceBinding = [...remotionImports].find(
([, imported]) => imported === 'Sequence',
)?.[0];
@@ -397,6 +413,82 @@ const getPrecompositionPlan = ({
}
let unsafeReason: string | null = null;
+ let containingFunctionPath = selectedPaths[0].parentPath;
+ while (
+ containingFunctionPath &&
+ containingFunctionPath.node.type !== 'FunctionDeclaration' &&
+ containingFunctionPath.node.type !== 'FunctionExpression' &&
+ containingFunctionPath.node.type !== 'ArrowFunctionExpression'
+ ) {
+ containingFunctionPath = containingFunctionPath.parentPath;
+ }
+
+ const localConstants = new Map<
+ string,
+ {
+ id: Node;
+ initPath: recast.types.NodePath;
+ start: number;
+ initSource: string;
+ }
+ >();
+ if (containingFunctionPath?.node.body.type === 'BlockStatement') {
+ for (
+ let index = 0;
+ index < containingFunctionPath.node.body.body.length;
+ index++
+ ) {
+ const statement = containingFunctionPath.node.body.body[index];
+ if (
+ statement.type !== 'VariableDeclaration' ||
+ statement.kind !== 'const' ||
+ statement.declarations.length !== 1 ||
+ !statement.loc
+ ) {
+ continue;
+ }
+
+ const declaration = statement.declarations[0];
+ if (
+ declaration.id.type !== 'Identifier' ||
+ !declaration.init?.loc ||
+ declaration.id.typeAnnotation ||
+ ['key', 'ref', '__proto__', '__self', '__source'].includes(
+ declaration.id.name,
+ )
+ ) {
+ continue;
+ }
+
+ localConstants.set(declaration.id.name, {
+ id: declaration.id,
+ initPath: containingFunctionPath.get(
+ 'body',
+ 'body',
+ index,
+ 'declarations',
+ 0,
+ 'init',
+ ),
+ start: recastLocToOffset(input, statement.loc.start),
+ initSource: input.slice(
+ recastLocToOffset(input, declaration.init.loc.start),
+ recastLocToOffset(input, declaration.init.loc.end),
+ ),
+ });
+ }
+ }
+
+ const derivedProps = new Map<
+ string,
+ NonNullable<ReturnType<typeof localConstants.get>>
+ >();
+ const pendingDerivedProps: string[] = [];
+ let currentDerivedProp: string | null = null;
+ const selectedStart = recastLocToOffset(
+ input,
+ (selectedPaths[0].node as JSXElement).loc!.start,
+ );
// Captured hook values are passed from the original render. The extracted
// component falls back to its own hooks when opened as a composition.
const hookProps = new Map<
@@ -423,7 +515,50 @@ const getPrecompositionPlan = ({
'step0',
'step1',
]);
- for (const path of selectedPaths) {
+ const unsupportedSyntax = new Set([
+ 'ThrowExpression',
+ 'PipelineExpression',
+ 'BindExpression',
+ 'ImportExpression',
+ 'DoExpression',
+ 'TSAsExpression',
+ 'TSTypeAssertion',
+ 'TSSatisfiesExpression',
+ 'TSInstantiationExpression',
+ 'TSTypeParameterInstantiation',
+ ]);
+ const containsUnsupportedSyntax = (node: Node): boolean => {
+ if (unsupportedSyntax.has(node.type)) {
+ return true;
+ }
+
+ return (VISITOR_KEYS[node.type] ?? []).some((key) => {
+ const child = (node as unknown as Record<string, unknown>)[key];
+ if (Array.isArray(child)) {
+ return child.some(
+ (item) =>
+ item !== null &&
+ typeof item === 'object' &&
+ 'type' in item &&
+ containsUnsupportedSyntax(item as Node),
+ );
+ }
+
+ return (
+ child !== null &&
+ typeof child === 'object' &&
+ 'type' in child &&
+ containsUnsupportedSyntax(child as Node)
+ );
+ });
+ };
+
+ const scanSafePath = (path: recast.types.NodePath) => {
+ if (containsUnsupportedSyntax(path.node as Node)) {
+ unsafeReason = 'The selected JSX contains unsupported syntax';
+ return;
+ }
+
recast.visit(path as unknown as Parameters<typeof recast.visit>[0], {
visitIdentifier(p) {
if (
@@ -434,6 +569,7 @@ const getPrecompositionPlan = ({
)
) {
const binding = p.scope.lookup(p.node.name);
+ const bindings = binding?.getBindings()[p.node.name] ?? [];
const remotionImport =
binding?.path.node === ast.program
? remotionImports.get(p.node.name)
@@ -446,6 +582,14 @@ const getPrecompositionPlan = ({
remotionImport === 'spring' &&
p.parentPath.node.type === 'CallExpression' &&
p.parentPath.node.callee === p.node;
+ const isInterpolateColorsCall =
+ remotionImport === 'interpolateColors' &&
+ p.parentPath.node.type === 'CallExpression' &&
+ p.parentPath.node.callee === p.node;
+ const isRandomCall =
+ remotionImport === 'random' &&
+ p.parentPath.node.type === 'CallExpression' &&
+ p.parentPath.node.callee === p.node;
const isEasingMember =
remotionImport === 'Easing' &&
p.parentPath.node.type === 'MemberExpression' &&
@@ -464,6 +608,19 @@ const getPrecompositionPlan = ({
!p.parentPath.node.computed &&
p.parentPath.node.property.type === 'Identifier' &&
p.parentPath.node.property.name === 'staticFile';
+ const isPureFunctionCall =
+ pureTopLevelFunctions.has(p.node.name) &&
+ p.parentPath.node.type === 'CallExpression' &&
+ p.parentPath.node.callee === p.node;
+ const isMathMember =
+ !binding &&
+ p.node.name === 'Math' &&
+ p.parentPath.node.type === 'MemberExpression' &&
+ p.parentPath.node.object === p.node &&
+ !p.parentPath.node.computed &&
+ p.parentPath.node.property.type === 'Identifier' &&
+ (isPureMathMethod(p.parentPath.node.property.name) ||
+ isPureMathConstant(p.parentPath.node.property.name));
let hookProp: {
name: string;
hookName: string;
@@ -473,6 +630,7 @@ const getPrecompositionPlan = ({
if (
p.node.name !== 'key' &&
p.node.name !== 'ref' &&
+ bindings.length === 1 &&
functionNode &&
(functionNode.type === 'FunctionDeclaration' ||
functionNode.type === 'FunctionExpression' ||
@@ -498,7 +656,7 @@ const getPrecompositionPlan = ({
if (hook === 'useCurrentFrame') {
const isMatchingFrame =
declaration.id.type === 'Identifier' &&
- declaration.id.name === p.node.name;
+ declaration.id === bindings[0].node;
if (isMatchingFrame) {
hookProp = {
name: p.node.name,
@@ -525,7 +683,7 @@ const getPrecompositionPlan = ({
(item.key.name === 'durationInFrames' ||
item.key.name === 'fps') &&
item.value.type === 'Identifier' &&
- item.value.name === p.node.name,
+ item.value === bindings[0].node,
);
if (
property?.type === 'ObjectProperty' &&
@@ -547,6 +705,27 @@ const getPrecompositionPlan = ({
hookProps.set(p.node.name, hookProp);
}
+ const localConstant = localConstants.get(p.node.name);
+ const isLocalConstant =
+ hookProp === null &&
+ localConstant !== undefined &&
+ binding?.path.node === containingFunctionPath?.node &&
+ bindings.length === 1 &&
+ bindings[0].node === localConstant.id;
+ if (isLocalConstant) {
+ if (
+ localConstant.start >=
+ (currentDerivedProp === null
+ ? selectedStart
+ : localConstants.get(currentDerivedProp)!.start)
+ ) {
+ unsafeReason = `The selected JSX reads ${p.node.name} before it is initialized`;
+ } else if (!derivedProps.has(p.node.name)) {
+ derivedProps.set(p.node.name, localConstant);
+ pendingDerivedProps.push(p.node.name);
+ }
+ }
+
if (
!(
binding?.path.node === ast.program &&
@@ -555,12 +734,16 @@ const getPrecompositionPlan = ({
isStaticFileNamespace ||
isInterpolating ||
isSpringCall ||
- isEasingMember)
+ isInterpolateColorsCall ||
+ isRandomCall ||
+ isEasingMember ||
+ isPureFunctionCall)
) &&
!hookProp &&
+ !isLocalConstant &&
+ !isMathMember &&
!(
- binding === undefined &&
- ['undefined', 'Infinity', 'NaN'].includes(p.node.name)
+ !binding && ['undefined', 'Infinity', 'NaN'].includes(p.node.name)
)
) {
unsafeReason = `The selected JSX reads ${p.node.name} from an unstable scope`;
@@ -678,6 +861,19 @@ const getPrecompositionPlan = ({
callee.type === 'Identifier' &&
remotionImports.get(callee.name) === 'spring' &&
p.scope.lookup(callee.name)?.path.node === ast.program;
+ const isNamedInterpolateColors =
+ callee.type === 'Identifier' &&
+ remotionImports.get(callee.name) === 'interpolateColors' &&
+ p.scope.lookup(callee.name)?.path.node === ast.program &&
+ (args.length === 3 || args.length === 4);
+ const isNamedSeededRandom =
+ callee.type === 'Identifier' &&
+ remotionImports.get(callee.name) === 'random' &&
+ p.scope.lookup(callee.name)?.path.node === ast.program &&
+ args.length === 1 &&
+ (args[0].type === 'StringLiteral' ||
+ args[0].type === 'NumericLiteral' ||
+ args[0].type === 'TemplateLiteral');
const isEasingMethod =
callee.type === 'MemberExpression' &&
!callee.computed &&
@@ -686,6 +882,18 @@ const getPrecompositionPlan = ({
easingMethods.has(callee.property.name) &&
remotionImports.get(callee.object.name) === 'Easing' &&
p.scope.lookup(callee.object.name)?.path.node === ast.program;
+ const isMathMethod =
+ callee.type === 'MemberExpression' &&
+ !callee.computed &&
+ callee.object.type === 'Identifier' &&
+ callee.object.name === 'Math' &&
+ callee.property.type === 'Identifier' &&
+ isPureMathMethod(callee.property.name) &&
+ !p.scope.lookup('Math');
+ const isPureFunction =
+ callee.type === 'Identifier' &&
+ pureTopLevelFunctions.has(callee.name) &&
+ p.scope.lookup(callee.name)?.path.node === ast.program;
const isNamedStaticFile =
callee.type === 'Identifier' &&
staticFileNames.has(callee.name) &&
@@ -703,10 +911,15 @@ const getPrecompositionPlan = ({
args.length === 1 &&
args[0].type === 'StringLiteral';
if (
- !isSafeStaticFile &&
- !isNamedInterpolate &&
- !isNamedSpring &&
- !isEasingMethod
+ args.some((argument) => argument.type === 'SpreadElement') ||
+ (!isSafeStaticFile &&
+ !isNamedInterpolate &&
+ !isNamedSpring &&
+ !isNamedInterpolateColors &&
+ !isNamedSeededRandom &&
+ !isEasingMethod &&
+ !isMathMethod &&
+ !isPureFunction)
) {
unsafeReason =
'The selected JSX contains a call that may change behavior';
@@ -719,10 +932,33 @@ const getPrecompositionPlan = ({
'The selected JSX contains a call that may change behavior';
return false;
},
+ visitDoExpression() {
+ unsafeReason = 'The selected JSX contains a do expression';
+ return false;
+ },
+ visitExpression(p) {
+ if (
+ [
+ 'ThrowExpression',
+ 'PipelineExpression',
+ 'BindExpression',
+ 'ImportExpression',
+ ].includes((p.node as {type: string}).type)
+ ) {
+ unsafeReason = 'The selected JSX contains an unsupported expression';
+ return false;
+ }
+
+ this.traverse(p);
+ },
visitNewExpression() {
unsafeReason = 'The selected JSX contains a constructor call';
return false;
},
+ visitThrowStatement() {
+ unsafeReason = 'The selected JSX contains a throw statement';
+ return false;
+ },
visitAssignmentExpression() {
unsafeReason = 'The selected JSX contains an assignment';
return false;
@@ -747,6 +983,57 @@ const getPrecompositionPlan = ({
unsafeReason = 'The selected JSX contains a function';
return false;
},
+ visitJSXElement(p) {
+ if (currentDerivedProp !== null) {
+ unsafeReason = 'A captured value renders JSX';
+ return false;
+ }
+
+ this.traverse(p);
+ },
+ visitJSXFragment(p) {
+ if (currentDerivedProp !== null) {
+ unsafeReason = 'A captured value renders JSX';
+ return false;
+ }
+
+ this.traverse(p);
+ },
+ visitSpreadElement() {
+ unsafeReason = 'The selected JSX contains a spread';
+ return false;
+ },
+ visitJSXSpreadAttribute() {
+ unsafeReason = 'The selected JSX contains spread props';
+ return false;
+ },
+ visitJSXSpreadChild() {
+ unsafeReason = 'The selected JSX contains a spread child';
+ return false;
+ },
+ visitUnaryExpression(p) {
+ if (
+ p.node.operator === 'delete' ||
+ String(p.node.operator) === 'throw'
+ ) {
+ unsafeReason = 'The selected JSX contains an unsafe operator';
+ return false;
+ }
+
+ this.traverse(p);
+ },
+ visitBinaryExpression(p) {
+ if (
+ p.node.operator === 'in' ||
+ p.node.operator === 'instanceof' ||
+ String(p.node.operator) === '|>'
+ ) {
+ unsafeReason = 'The selected JSX contains an unsafe operator';
+ return false;
+ }
+
+ this.traverse(p);
+ },
visitClassExpression() {
unsafeReason = 'The selected JSX contains a class';
return false;
@@ -777,12 +1064,120 @@ const getPrecompositionPlan = ({
this.traverse(p);
},
});
+ };
+
+ for (const path of selectedPaths) {
+ scanSafePath(path);
+ }
+
+ for (let index = 0; index < pendingDerivedProps.length; index++) {
+ currentDerivedProp = pendingDerivedProps[index];
+ scanSafePath(localConstants.get(currentDerivedProp)!.initPath);
}
if (unsafeReason !== null) {
throw new Error(unsafeReason);
}
+ const primitiveDerivedNames = new Set<string>();
+ const isPrimitiveDerivedExpression = (node: Node): boolean => {
+ switch (node.type) {
+ case 'StringLiteral':
+ case 'NumericLiteral':
+ case 'BooleanLiteral':
+ case 'NullLiteral':
+ case 'BigIntLiteral':
+ return true;
+ case 'Identifier':
+ return (
+ primitiveDerivedNames.has(node.name) ||
+ hookProps.has(node.name) ||
+ stablePrimitiveNames.has(node.name) ||
+ ['undefined', 'Infinity', 'NaN'].includes(node.name)
+ );
+ case 'ParenthesizedExpression':
+ case 'TSAsExpression':
+ case 'TSTypeAssertion':
+ case 'TSNonNullExpression':
+ case 'TSSatisfiesExpression':
+ return isPrimitiveDerivedExpression(node.expression);
+ case 'UnaryExpression':
+ return (
+ node.operator !== 'delete' &&
+ isPrimitiveDerivedExpression(node.argument)
+ );
+ case 'BinaryExpression':
+ return (
+ node.operator !== 'in' &&
+ node.operator !== 'instanceof' &&
+ isPrimitiveDerivedExpression(node.left) &&
+ isPrimitiveDerivedExpression(node.right)
+ );
+ case 'LogicalExpression':
+ return (
+ isPrimitiveDerivedExpression(node.left) &&
+ isPrimitiveDerivedExpression(node.right)
+ );
+ case 'ConditionalExpression':
+ return (
+ isPrimitiveDerivedExpression(node.test) &&
+ isPrimitiveDerivedExpression(node.consequent) &&
+ isPrimitiveDerivedExpression(node.alternate)
+ );
+ case 'TemplateLiteral':
+ return node.expressions.every(isPrimitiveDerivedExpression);
+ case 'MemberExpression':
+ return (
+ !node.computed &&
+ node.object.type === 'Identifier' &&
+ node.object.name === 'Math' &&
+ node.property.type === 'Identifier' &&
+ isPureMathConstant(node.property.name)
+ );
+ case 'CallExpression': {
+ const {callee} = node;
+ if (callee.type === 'Identifier') {
+ return (
+ primitiveTopLevelFunctions.has(callee.name) ||
+ [
+ 'interpolate',
+ 'spring',
+ 'interpolateColors',
+ 'random',
+ 'staticFile',
+ ].includes(remotionImports.get(callee.name) ?? '')
+ );
+ }
+
+ return (
+ callee.type === 'MemberExpression' &&
+ !callee.computed &&
+ callee.object.type === 'Identifier' &&
+ callee.property.type === 'Identifier' &&
+ ((callee.object.name === 'Math' &&
+ isPureMathMethod(callee.property.name)) ||
+ (remotionNamespaces.has(callee.object.name) &&
+ callee.property.name === 'staticFile'))
+ );
+ }
+
+ default:
+ return false;
+ }
+ };
+
+ for (const [name, candidate] of [...derivedProps].sort(
+ (left, right) => left[1].start - right[1].start,
+ )) {
+ if (!isPrimitiveDerivedExpression(candidate.initPath.node as Node)) {
+ throw new Error(
+ `The selected JSX captures a non-primitive value: ${name}`,
+ );
+ }
+
+ primitiveDerivedNames.add(name);
+ }
+
if (
hookProps.size > 0 &&
[...hookProps.values()].some((prop) => prop.kind === 'frame')
@@ -899,6 +1294,12 @@ const getPrecompositionPlan = ({
return {
ast,
children,
+ derivedProps: [...derivedProps]
+ .sort((left, right) => left[1].start - right[1].start)
+ .map(([name, {initSource}]) => ({
+ name,
+ initSource,
+ })),
end: recastLocToOffset(input, sequenceEnd ?? last.loc!.end),
firstIndex,
hookProps: [...hookProps.values()].sort((left, right) =>
@@ -1032,6 +1433,7 @@ export const precomposeJsxNodes = <Project extends CodemodProject>({
const {
ast,
children,
+ derivedProps,
end,
firstIndex,
hookProps,
@@ -1106,7 +1508,10 @@ export const precomposeJsxNodes = <Project extends CodemodProject>({
throw new Error('Could not choose a unique composition name');
}
- const resolvedHookProps = hookProps.map((prop) => {
+ const resolvedProps = [
+ ...hookProps.map((prop) => ({...prop, propKind: 'hook' as const})),
+ ...derivedProps.map((prop) => ({...prop, propKind: 'derived' as const})),
+ ].map((prop) => {
const suffix = prop.name.charAt(0).toUpperCase() + prop.name.slice(1);
let parentAlias = `precomposeParent${suffix}`;
for (let index = 2; occupiedNames.has(parentAlias); index++) {
@@ -1122,27 +1527,39 @@ export const precomposeJsxNodes = <Project extends CodemodProject>({
occupiedNames.add(standaloneAlias);
return {...prop, parentAlias, standaloneAlias};
});
- const destructuredProps = resolvedHookProps
+ const destructuredProps = resolvedProps
.map(({name: propName, parentAlias}) => `${propName}: ${parentAlias}`)
.join(', ');
- const typedProps = resolvedHookProps
- .map(({name: propName}) => `${propName}?: number | null`)
+ const typedProps = resolvedProps
+ .map(
+ ({name: propName, propKind}) =>
+ `${propName}?: ${propKind === 'hook' ? 'number | null' : 'unknown'}`,
+ )
.join('; ');
const parameters =
- resolvedHookProps.length === 0
+ resolvedProps.length === 0
? ''
: `{${destructuredProps}}${filePath.endsWith('.tsx') ? `: {${typedProps}}` : ''}`;
const signature = `export function ${name}(${parameters}) {`;
- const hookStatements = resolvedHookProps.flatMap(
- ({name: propName, parentAlias, standaloneAlias, hookName, kind}) => [
+ const valueStatements = resolvedProps.flatMap((prop) => {
+ const {name: propName, parentAlias, standaloneAlias} = prop;
+ if (prop.propKind === 'derived') {
+ return [
+ `const ${standaloneAlias} = () => ${prop.initSource};`,
+ `const ${propName} = ${parentAlias} === undefined ? ${standaloneAlias}() : ${filePath.endsWith('.tsx') ? `(${parentAlias} as ReturnType<typeof ${standaloneAlias}>)` : `/** @type {ReturnType<typeof ${standaloneAlias}>} */ (${parentAlias})`};`,
+ ];
+ }
+
+ const {hookName, kind} = prop;
+ return [
kind === 'frame'
? `const ${standaloneAlias} = ${hookName}();`
: kind === 'fps'
? `const ${standaloneAlias} = ${hookName}().fps;`
: `const ${standaloneAlias} = ${metadata.durationInFrames};`,
`const ${propName} = ${parentAlias} ?? ${standaloneAlias};`,
- ],
- );
+ ];
+ });
const snapshots = captureImportSnapshots(ast);
const sequenceImport = [...ast.program.body]
@@ -1176,7 +1593,7 @@ export const precomposeJsxNodes = <Project extends CodemodProject>({
const unit = getIndentationUnit(input, null);
const originalIndent = getLineIndent({input, offset: start});
const wrapperName = sequenceName ?? name;
- const childSource = `<${name}${resolvedHookProps.map(({name: propName}) => ` ${propName}={${propName}}`).join('')} />`;
+ const childSource = `<${name}${resolvedProps.map(({name: propName}) => ` ${propName}={${propName}}`).join('')} />`;
const replacementSource = selectedSequence
? `${endOfLine}${originalIndent}${unit}${childSource}${endOfLine}${originalIndent}`
: [
@@ -1199,7 +1616,7 @@ export const precomposeJsxNodes = <Project extends CodemodProject>({
}
const declarationStatement = parseAst(
- `${signature} ${hookStatements.join(' ')} return <></>; }`,
+ `${signature} ${valueStatements.join(' ')} return <></>; }`,
).program.body[0];
if (
declarationStatement.type !== 'ExportNamedDeclaration' ||
@@ -1260,37 +1677,38 @@ export const precomposeJsxNodes = <Project extends CodemodProject>({
})
.join(endOfLine);
const componentSignature =
- signature.length <= 80 || resolvedHookProps.length === 0
+ signature.length <= 80 || resolvedProps.length === 0
? signature
: filePath.endsWith('.tsx')
? [
`export function ${name}({`,
- ...resolvedHookProps.map(
+ ...resolvedProps.map(
({name: propName, parentAlias}) =>
`${unit}${propName}: ${parentAlias},`,
),
`}: {`,
- ...resolvedHookProps.map(
- ({name: propName}) => `${unit}${propName}?: number | null;`,
+ ...resolvedProps.map(
+ ({name: propName, propKind}) =>
+ `${unit}${propName}?: ${propKind === 'hook' ? 'number | null' : 'unknown'};`,
),
'}) {',
].join(endOfLine)
: [
`export function ${name}({`,
- ...resolvedHookProps.map(
+ ...resolvedProps.map(
({name: propName, parentAlias}) =>
`${unit}${propName}: ${parentAlias},`,
),
'}) {',
].join(endOfLine);
const component = [
- ...(resolvedHookProps.length > 0 && !filePath.endsWith('.tsx')
+ ...(resolvedProps.length > 0 && !filePath.endsWith('.tsx')
? [
- `/** @param {{${resolvedHookProps.map(({name: propName}) => `${propName}?: number | null`).join(', ')}}} props */`,
+ `/** @param {{${resolvedProps.map(({name: propName, propKind}) => `${propName}?: ${propKind === 'hook' ? 'number | null' : 'unknown'}`).join(', ')}}} props */`,
]
: []),
componentSignature,
- ...hookStatements.map((statement) => `${unit}${statement}`),
+ ...valueStatements.map((statement) => `${unit}${statement}`),
`${unit}return (`,
`${unit}${unit}<>`,
movedSource,
diff --git a/packages/codemods/src/precompose-pure-functions.ts b/packages/codemods/src/precompose-pure-functions.ts
new file mode 100644
index 0000000000..78fe37f8ff
--- /dev/null
+++ b/packages/codemods/src/precompose-pure-functions.ts
@@ -0,0 +1,658 @@
+import {
+ isExpression,
+ type Node,
+ type File,
+ type ArrowFunctionExpression,
+ type FunctionDeclaration,
+ type FunctionExpression,
+ type Expression,
+} from '@babel/types';
+import * as recast from 'recast';
+
+const mathMethods = new Set([
+ 'abs',
+ 'acos',
+ 'acosh',
+ 'asin',
+ 'asinh',
+ 'atan',
+ 'atan2',
+ 'atanh',
+ 'cbrt',
+ 'ceil',
+ 'clz32',
+ 'cos',
+ 'cosh',
+ 'exp',
+ 'expm1',
+ 'floor',
+ 'fround',
+ 'hypot',
+ 'imul',
+ 'log',
+ 'log10',
+ 'log1p',
+ 'log2',
+ 'max',
+ 'min',
+ 'pow',
+ 'round',
+ 'sign',
+ 'sin',
+ 'sinh',
+ 'sqrt',
+ 'tan',
+ 'tanh',
+ 'trunc',
+]);
+
+const mathConstants = new Set([
+ 'E',
+ 'LN10',
+ 'LN2',
+ 'LOG10E',
+ 'LOG2E',
+ 'PI',
+ 'SQRT1_2',
+ 'SQRT2',
+]);
+
+const easingMethods = new Set([
+ 'back',
+ 'bezier',
+ 'bounce',
+ 'circle',
+ 'cubic',
+ 'ease',
+ 'elastic',
+ 'exp',
+ 'in',
+ 'inOut',
+ 'linear',
+ 'out',
+ 'poly',
+ 'quad',
+ 'sin',
+ 'spring',
+ 'step0',
+ 'step1',
+]);
+
+export const isPureMathMethod = (name: string) => mathMethods.has(name);
+export const isPureMathConstant = (name: string) => mathConstants.has(name);
+
+type SimpleFunction =
+ | FunctionDeclaration
+ | FunctionExpression
+ | ArrowFunctionExpression;
+
+type PureTopLevelFunctionsOptions = {
+ ast: File;
+ remotionImports: Map<string, string>;
+ stablePrimitiveNames: Set<string>;
+};
+
+export const getPureTopLevelFunctionAnalysis = ({
+ ast,
+ remotionImports,
+ stablePrimitiveNames,
+}: PureTopLevelFunctionsOptions): {
+ pureFunctionNames: Set<string>;
+ primitiveFunctionNames: Set<string>;
+} => {
+ const topLevelBindings = new Set<string>();
+ const duplicateBindings = new Set<string>();
+ const reassignedNames = new Set<string>();
+ const primitiveTopLevelConstants = new Set<string>();
+ let hasDirectEval = false;
+ const helpers = new Map<string, SimpleFunction>();
+ const registerBinding = (name: string) => {
+ if (topLevelBindings.has(name)) {
+ duplicateBindings.add(name);
+ }
+
+ topLevelBindings.add(name);
+ };
+
+ for (const statement of ast.program.body) {
+ if (statement.type === 'ImportDeclaration') {
+ for (const specifier of statement.specifiers) {
+ registerBinding(specifier.local.name);
+ }
+
+ continue;
+ }
+
+ const declaration =
+ statement.type === 'ExportNamedDeclaration' ||
+ statement.type === 'ExportDefaultDeclaration'
+ ? statement.declaration
+ : statement;
+ if (declaration?.type === 'FunctionDeclaration' && declaration.id) {
+ registerBinding(declaration.id.name);
+ helpers.set(declaration.id.name, declaration);
+ } else if (declaration?.type === 'ClassDeclaration' && declaration.id) {
+ registerBinding(declaration.id.name);
+ } else if (declaration?.type === 'VariableDeclaration') {
+ for (const declarator of declaration.declarations) {
+ if (declarator.id.type !== 'Identifier') {
+ continue;
+ }
+
+ registerBinding(declarator.id.name);
+ if (
+ declaration.kind === 'const' &&
+ declarator.init &&
+ ([
+ 'StringLiteral',
+ 'NumericLiteral',
+ 'BooleanLiteral',
+ 'NullLiteral',
+ ].includes(declarator.init.type) ||
+ (declarator.init.type === 'UnaryExpression' &&
+ declarator.init.operator === '-' &&
+ declarator.init.argument.type === 'NumericLiteral'))
+ ) {
+ primitiveTopLevelConstants.add(declarator.id.name);
+ }
+
+ if (
+ declaration.kind === 'const' &&
+ (declarator.init?.type === 'ArrowFunctionExpression' ||
+ declarator.init?.type === 'FunctionExpression')
+ ) {
+ helpers.set(declarator.id.name, declarator.init);
+ }
+ }
+ }
+ }
+
+ const collectAssignedNames = (target: Node) => {
+ switch (target.type) {
+ case 'Identifier':
+ reassignedNames.add(target.name);
+ break;
+ case 'ParenthesizedExpression':
+ collectAssignedNames(target.expression);
+ break;
+ case 'AssignmentPattern':
+ collectAssignedNames(target.left);
+ break;
+ case 'RestElement':
+ collectAssignedNames(target.argument);
+ break;
+ case 'ArrayPattern':
+ for (const element of target.elements) {
+ if (element) {
+ collectAssignedNames(element);
+ }
+ }
+
+ break;
+ case 'ObjectPattern':
+ for (const property of target.properties) {
+ collectAssignedNames(
+ property.type === 'ObjectProperty' ? property.value : property,
+ );
+ }
+
+ break;
+ default:
+ break;
+ }
+ };
+
+ // Function declarations are writable bindings. Be conservative even when a
+ // write appears in another scope: this check never needs to prove that the
+ // write refers to the same declaration.
+ recast.visit(ast, {
+ visitAssignmentExpression(path) {
+ collectAssignedNames(path.node.left as Node);
+
+ this.traverse(path);
+ },
+ visitUpdateExpression(path) {
+ collectAssignedNames(path.node.argument as Node);
+
+ this.traverse(path);
+ },
+ visitForInStatement(path) {
+ collectAssignedNames(path.node.left as Node);
+
+ this.traverse(path);
+ },
+ visitForOfStatement(path) {
+ collectAssignedNames(path.node.left as Node);
+
+ this.traverse(path);
+ },
+ visitCallExpression(path) {
+ if (
+ path.node.callee.type === 'Identifier' &&
+ path.node.callee.name === 'eval'
+ ) {
+ hasDirectEval = true;
+ }
+
+ this.traverse(path);
+ },
+ });
+
+ const proven = new Set<string>();
+ const visiting = new Set<string>();
+ const isPureHelper = (name: string): boolean => {
+ if (proven.has(name)) {
+ return true;
+ }
+
+ if (
+ hasDirectEval ||
+ duplicateBindings.has(name) ||
+ reassignedNames.has(name) ||
+ visiting.has(name)
+ ) {
+ return false;
+ }
+
+ const helper = helpers.get(name);
+ if (!helper || helper.async || helper.generator) {
+ return false;
+ }
+
+ const params = new Set<string>();
+ for (const param of helper.params) {
+ if (param.type !== 'Identifier') {
+ return false;
+ }
+
+ params.add(param.name);
+ }
+
+ if (params.size !== helper.params.length) {
+ return false;
+ }
+
+ const returnValue =
+ helper.body.type === 'BlockStatement'
+ ? helper.body.body.length === 1 &&
+ helper.body.body[0].type === 'ReturnStatement'
+ ? helper.body.body[0].argument
+ : null
+ : helper.body;
+ if (!returnValue) {
+ return false;
+ }
+
+ visiting.add(name);
+ // The expression and helper checks recurse into each other.
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
+ const pure = isPureExpression(returnValue, params);
+ visiting.delete(name);
+ if (pure) {
+ proven.add(name);
+ }
+
+ return pure;
+ };
+
+ const isKnownMember = (
+ expression: Expression,
+ params: Set<string>,
+ forCall: boolean,
+ ) => {
+ if (
+ expression.type !== 'MemberExpression' ||
+ expression.computed ||
+ expression.object.type !== 'Identifier' ||
+ expression.property.type !== 'Identifier'
+ ) {
+ return false;
+ }
+
+ const {name: objectName} = expression.object;
+ const {name: propertyName} = expression.property;
+ if (params.has(objectName)) {
+ return false;
+ }
+
+ if (objectName === 'Math' && !topLevelBindings.has('Math')) {
+ return forCall
+ ? isPureMathMethod(propertyName)
+ : isPureMathMethod(propertyName) || isPureMathConstant(propertyName);
+ }
+
+ return (
+ remotionImports.get(objectName) === 'Easing' &&
+ topLevelBindings.has(objectName) &&
+ !duplicateBindings.has(objectName) &&
+ easingMethods.has(propertyName)
+ );
+ };
+
+ function isPureExpression(node: Node, params: Set<string>): boolean {
+ if (!isExpression(node)) {
+ return false;
+ }
+
+ const expression = node;
+ switch (expression.type) {
+ case 'StringLiteral':
+ case 'NumericLiteral':
+ case 'BooleanLiteral':
+ case 'NullLiteral':
+ case 'BigIntLiteral':
+ return true;
+ case 'Identifier':
+ if (params.has(expression.name)) {
+ return true;
+ }
+
+ if (
+ ['undefined', 'Infinity', 'NaN'].includes(expression.name) &&
+ !topLevelBindings.has(expression.name)
+ ) {
+ return true;
+ }
+
+ return (
+ stablePrimitiveNames.has(expression.name) &&
+ topLevelBindings.has(expression.name) &&
+ !duplicateBindings.has(expression.name)
+ );
+ case 'ParenthesizedExpression':
+ case 'TSAsExpression':
+ case 'TSTypeAssertion':
+ case 'TSNonNullExpression':
+ case 'TSSatisfiesExpression':
+ return isPureExpression(expression.expression, params);
+ case 'UnaryExpression':
+ return (
+ ['!', '+', '-', '~', 'typeof', 'void'].includes(
+ expression.operator,
+ ) && isPureExpression(expression.argument, params)
+ );
+ case 'BinaryExpression':
+ return (
+ [
+ '+',
+ '-',
+ '*',
+ '/',
+ '%',
+ '**',
+ '<',
+ '<=',
+ '>',
+ '>=',
+ '==',
+ '!=',
+ '===',
+ '!==',
+ '&',
+ '|',
+ '^',
+ '<<',
+ '>>',
+ '>>>',
+ ].includes(expression.operator) &&
+ isPureExpression(expression.left, params) &&
+ isPureExpression(expression.right, params)
+ );
+ case 'LogicalExpression':
+ return (
+ isPureExpression(expression.left, params) &&
+ isPureExpression(expression.right, params)
+ );
+ case 'ConditionalExpression':
+ return (
+ isPureExpression(expression.test, params) &&
+ isPureExpression(expression.consequent, params) &&
+ isPureExpression(expression.alternate, params)
+ );
+ case 'TemplateLiteral':
+ return expression.expressions.every((part) =>
+ isPureExpression(part, params),
+ );
+ case 'ArrayExpression':
+ return expression.elements.every(
+ (element) =>
+ element === null ||
+ (element.type !== 'SpreadElement' &&
+ isPureExpression(element, params)),
+ );
+ case 'ObjectExpression':
+ return expression.properties.every(
+ (property) =>
+ property.type === 'ObjectProperty' &&
+ !property.computed &&
+ property.key.type !== 'PrivateName' &&
+ !(
+ (property.key.type === 'Identifier' &&
+ property.key.name === '__proto__') ||
+ (property.key.type === 'StringLiteral' &&
+ property.key.value === '__proto__')
+ ) &&
+ property.value.type !== 'AssignmentPattern' &&
+ isPureExpression(property.value, params),
+ );
+ case 'MemberExpression':
+ return isKnownMember(expression, params, false);
+ case 'CallExpression': {
+ if (
+ !expression.arguments.every(
+ (argument) =>
+ argument.type !== 'SpreadElement' &&
+ argument.type !== 'ArgumentPlaceholder' &&
+ isPureExpression(argument, params),
+ )
+ ) {
+ return false;
+ }
+
+ const {callee} = expression;
+ if (callee.type === 'MemberExpression') {
+ return isKnownMember(callee, params, true);
+ }
+
+ if (callee.type !== 'Identifier' || params.has(callee.name)) {
+ return false;
+ }
+
+ if (
+ !duplicateBindings.has(callee.name) &&
+ (['interpolate', 'spring'].includes(
+ remotionImports.get(callee.name) ?? '',
+ ) ||
+ (remotionImports.get(callee.name) === 'interpolateColors' &&
+ (expression.arguments.length === 3 ||
+ expression.arguments.length === 4)) ||
+ (remotionImports.get(callee.name) === 'random' &&
+ expression.arguments.length === 1 &&
+ ['StringLiteral', 'NumericLiteral', 'TemplateLiteral'].includes(
+ expression.arguments[0].type,
+ )))
+ ) {
+ return true;
+ }
+
+ return isPureHelper(callee.name);
+ }
+
+ default:
+ return false;
+ }
+ }
+
+ for (const name of helpers.keys()) {
+ isPureHelper(name);
+ }
+
+ const primitiveFunctionNames = new Set<string>();
+ const primitiveVisiting = new Set<string>();
+ const isPrimitiveHelper = (name: string): boolean => {
+ if (primitiveFunctionNames.has(name)) {
+ return true;
+ }
+
+ if (!proven.has(name) || primitiveVisiting.has(name)) {
+ return false;
+ }
+
+ const helper = helpers.get(name);
+ if (!helper) {
+ return false;
+ }
+
+ const params = new Set<string>();
+ for (const param of helper.params) {
+ if (param.type !== 'Identifier') {
+ return false;
+ }
+
+ params.add(param.name);
+ }
+
+ const returnValue =
+ helper.body.type === 'BlockStatement'
+ ? helper.body.body[0].type === 'ReturnStatement'
+ ? helper.body.body[0].argument
+ : null
+ : helper.body;
+ if (!returnValue) {
+ return false;
+ }
+
+ primitiveVisiting.add(name);
+ // The expression and helper checks recurse into each other.
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
+ const primitive = isPrimitiveExpression(returnValue, params);
+ primitiveVisiting.delete(name);
+ if (primitive) {
+ primitiveFunctionNames.add(name);
+ }
+
+ return primitive;
+ };
+
+ function isPrimitiveExpression(node: Node, params: Set<string>): boolean {
+ if (!isExpression(node)) {
+ return false;
+ }
+
+ switch (node.type) {
+ case 'StringLiteral':
+ case 'NumericLiteral':
+ case 'BooleanLiteral':
+ case 'NullLiteral':
+ return true;
+ case 'Identifier':
+ if (params.has(node.name)) {
+ return false;
+ }
+
+ if (
+ ['undefined', 'Infinity', 'NaN'].includes(node.name) &&
+ !topLevelBindings.has(node.name)
+ ) {
+ return true;
+ }
+
+ return (
+ stablePrimitiveNames.has(node.name) &&
+ primitiveTopLevelConstants.has(node.name) &&
+ !duplicateBindings.has(node.name)
+ );
+ case 'ParenthesizedExpression':
+ case 'TSAsExpression':
+ case 'TSTypeAssertion':
+ case 'TSNonNullExpression':
+ case 'TSSatisfiesExpression':
+ return isPrimitiveExpression(node.expression, params);
+ case 'UnaryExpression':
+ if (['!', 'typeof', 'void', '+'].includes(node.operator)) {
+ return true;
+ }
+
+ return (
+ (node.operator === '-' || node.operator === '~') &&
+ isPrimitiveExpression(node.argument, params)
+ );
+ case 'BinaryExpression':
+ if (
+ ['==', '===', '!=', '!==', '<', '<=', '>', '>=', '>>>'].includes(
+ node.operator,
+ )
+ ) {
+ return true;
+ }
+
+ return (
+ ['+', '-', '*', '/', '%', '**', '&', '|', '^', '<<', '>>'].includes(
+ node.operator,
+ ) &&
+ (isPrimitiveExpression(node.left, params) ||
+ isPrimitiveExpression(node.right, params))
+ );
+ case 'LogicalExpression':
+ return (
+ isPrimitiveExpression(node.left, params) &&
+ isPrimitiveExpression(node.right, params)
+ );
+ case 'ConditionalExpression':
+ return (
+ isPrimitiveExpression(node.consequent, params) &&
+ isPrimitiveExpression(node.alternate, params)
+ );
+ case 'TemplateLiteral':
+ return true;
+ case 'MemberExpression':
+ return (
+ !node.computed &&
+ node.object.type === 'Identifier' &&
+ node.object.name === 'Math' &&
+ !params.has('Math') &&
+ !topLevelBindings.has('Math') &&
+ node.property.type === 'Identifier' &&
+ isPureMathConstant(node.property.name)
+ );
+ case 'CallExpression': {
+ const {callee} = node;
+ if (callee.type === 'MemberExpression') {
+ return (
+ !callee.computed &&
+ callee.object.type === 'Identifier' &&
+ callee.object.name === 'Math' &&
+ !params.has('Math') &&
+ !topLevelBindings.has('Math') &&
+ callee.property.type === 'Identifier' &&
+ isPureMathMethod(callee.property.name)
+ );
+ }
+
+ if (callee.type !== 'Identifier' || params.has(callee.name)) {
+ return false;
+ }
+
+ if (
+ !duplicateBindings.has(callee.name) &&
+ ['interpolate', 'spring', 'interpolateColors', 'random'].includes(
+ remotionImports.get(callee.name) ?? '',
+ )
+ ) {
+ return true;
+ }
+
+ return isPrimitiveHelper(callee.name);
+ }
+
+ default:
+ return false;
+ }
+ }
+
+ for (const name of proven) {
+ isPrimitiveHelper(name);
+ }
+
+ return {pureFunctionNames: proven, primitiveFunctionNames};
+};