π₯ Make videos programmatically with React
@remotion/studio-codemods: Remove formatter compatibility API
Jonny Burger52029ab25a1b
diff --git a/packages/studio-codemods/src/index.ts b/packages/studio-codemods/src/index.tsindex a6e0daaa4a..8d7730a291 100644--- a/packages/studio-codemods/src/index.ts+++ b/packages/studio-codemods/src/index.ts@@ -86,10 +86,8 @@ export {simpleDiff} from './simple-diff';export {splitJsxSequence} from './split-jsx-sequence';export {splitVideoFromAudio} from './split-video-from-audio';export {- formatInlineContentWithFormatter,getCompositionDefaultPropsLine,updateDefaultProps,- type FormatInline,} from './update-default-props';export {updateInlineCaptionPatches} from './update-inline-caption-patches';export {diff --git a/packages/studio-codemods/src/update-default-props.ts b/packages/studio-codemods/src/update-default-props.tsindex 523fde09c2..d8b85a847f 100644--- a/packages/studio-codemods/src/update-default-props.ts+++ b/packages/studio-codemods/src/update-default-props.ts@@ -9,93 +9,7 @@ import {const identifierRegex = /^[A-Za-z_$][0-9A-Za-z_$]*$/;-export type FormatInline = (options: {- inlineContent: string;- linePrefix: string;- endOfLine: 'auto' | 'lf';-}) => Promise<{formatted: string; didFormat: boolean}>;--/**- * Instead of running prettier on the entire file (which is slow),- * format only a small snippet of inline content (e.g. stringified defaultProps).- *- * We wrap the content in `const __x__ = CONTENT;` and adjust printWidth- * so prettier makes the same line-breaking decisions as if the content- * were at its actual column position in the file.- */-export const formatInlineContentWithFormatter = async ({- inlineContent,- linePrefix,- endOfLine,- prettierConfig,- format,-}: {- inlineContent: string;- linePrefix: string;- endOfLine: 'auto' | 'lf';- prettierConfig: Record<string, unknown>;- format: (source: string, options: Record<string, unknown>) => Promise<string>;-}): Promise<{formatted: string; didFormat: boolean}> => {- const tabWidth = (prettierConfig.tabWidth as number) ?? 2;- const baseIndent = linePrefix.match(/^(\s*)/)?.[1] ?? '';-- // Calculate visual column offset (tabs expand to tabWidth columns)- const columnOffset = [...linePrefix].reduce(- (col, ch) => (ch === '\t' ? col + tabWidth : col + 1),- 0,- );-- // Adjust printWidth so the wrapper prefix occupies the same visual- // width as the actual file prefix, ensuring identical line breaks.- const configPrintWidth = (prettierConfig.printWidth as number) ?? 80;- const wrapperPrefix = 'const __x__ = ';- const effectivePrintWidth = Math.max(- configPrintWidth - columnOffset + wrapperPrefix.length,- 20,- );-- const wrappedSource = `${wrapperPrefix}${inlineContent};\n`;- const formattedWrapped = await format(wrappedSource, {- ...prettierConfig,- printWidth: effectivePrintWidth,- filepath: 'test.tsx',- endOfLine,- });-- // Extract the formatted value from the wrapper- const withoutSemicolon = formattedWrapped.replace(/;\s*$/, '');- const wrappedInParentheses = withoutSemicolon.startsWith(- `${wrapperPrefix}(\n`,- );- let formattedProps: string;-- if (withoutSemicolon.startsWith(wrapperPrefix) && !wrappedInParentheses) {- formattedProps = withoutSemicolon.slice(wrapperPrefix.length);- } else {- // Prettier broke the line after `=` β extract and dedent one level- const lines = withoutSemicolon- .split('\n')- .slice(1, wrappedInParentheses ? -1 : undefined);- const useTabs = prettierConfig.useTabs as boolean;- const oneIndent = useTabs ? '\t' : ' '.repeat(tabWidth);- formattedProps = lines- .map((l) => (l.startsWith(oneIndent) ? l.slice(oneIndent.length) : l))- .join('\n');- }-- // Add base indentation to all lines except the first- const indentedProps = formattedProps- .split('\n')- .map((line, i) =>- i === 0 ? line : line.length > 0 ? baseIndent + line : line,- )- .join('\n');-- return {formatted: indentedProps, didFormat: true};-};--/* eslint-disable require-await -- Keep the formatter-era Promise API. */-export const updateDefaultProps = async ({+export const updateDefaultProps = ({input,compositionId,newDefaultProps,@@ -105,10 +19,7 @@ export const updateDefaultProps = async ({compositionId: string;newDefaultProps: Record<string, unknown>;enumPaths: EnumPath[];- // Kept optional for compatibility with callers from before source-aware- // printing replaced the inline formatter.- formatInline?: FormatInline;-}): Promise<{output: string; formatted: boolean}> => {+}): {output: string} => {const ast = parseAst(input);const stringified = stringifyDefaultProps({props: newDefaultProps,@@ -360,9 +271,8 @@ export const updateDefaultProps = async ({'}' +input.substring(replaceEnd);- return {output, formatted: true};+ return {output};};-/* eslint-enable require-await *//** Line of the matching `<Composition>` / `<Still>` opening tag (for log links). */export const getCompositionDefaultPropsLine = ({diff --git a/packages/studio-server/src/preview-server/routes/update-default-props.ts b/packages/studio-server/src/preview-server/routes/update-default-props.tsindex dc96376a7a..cbf3d76379 100644--- a/packages/studio-server/src/preview-server/routes/update-default-props.ts+++ b/packages/studio-server/src/preview-server/routes/update-default-props.ts@@ -51,7 +51,7 @@ export const updateDefaultPropsHandler: ApiHandler<input: fileContents,compositionId,});- const {output} = await updateDefaultProps({+ const {output} = updateDefaultProps({compositionId,input: fileContents,newDefaultProps: JSON.parse(defaultProps),diff --git a/packages/studio-server/src/test/update-default-props.test.ts b/packages/studio-server/src/test/update-default-props.test.tsindex c2cb4196b4..f88622d608 100644--- a/packages/studio-server/src/test/update-default-props.test.ts+++ b/packages/studio-server/src/test/update-default-props.test.ts@@ -7,7 +7,7 @@ import {updateDefaultProps,} from '../codemods/update-default-props';-test('updates default props without changing surrounding source', async () => {+test('updates default props without changing surrounding source', () => {const file = readFileSync(path.join(__dirname, 'snapshots', 'root-before.tsx'),'utf-8',@@ -17,7 +17,7 @@ test('updates default props without changing surrounding source', async () => {'utf-8',);- const {output} = await updateDefaultProps({+ const {output} = updateDefaultProps({input: file,compositionId: 'Comp3',newDefaultProps: {abc: 'def', newDate: 'remotion-date:2022-01-02'},@@ -41,7 +41,7 @@ test('getCompositionDefaultPropsLine returns the opening tag line (ast-types vis).toBe(27);});-test('replaces multiline default props with a compact value', async () => {+test('replaces multiline default props with a compact value', () => {const file = readFileSync(path.join(__dirname, 'snapshots', 'problematic.tsx'),'utf-8',@@ -51,7 +51,7 @@ test('replaces multiline default props with a compact value', async () => {'utf-8',);- const {output} = await updateDefaultProps({+ const {output} = updateDefaultProps({input: file,compositionId: 'schema-test',newDefaultProps: {abc: 'def', newDate: 'remotion-date:2022-01-02'},@@ -61,7 +61,7 @@ test('replaces multiline default props with a compact value', async () => {expect(output).toBe(expected);});-test('formats multiline default props without calling the compatibility formatter', async () => {+test('formats multiline default props without Prettier', () => {const input = `import {Composition} from 'remotion'const untouched = {keep:"this spacing"}@@ -73,9 +73,7 @@ export const Root=()=>(/>)`;- let formatCalls = 0;-- const {output, formatted} = await updateDefaultProps({+ const {output} = updateDefaultProps({input,compositionId: 'Comp',newDefaultProps: {@@ -85,14 +83,8 @@ export const Root=()=>(mode: 'fast',},enumPaths: [['mode']],- formatInline: () => {- formatCalls++;- throw new Error('Prettier must not be called');- },});- expect(formatted).toBe(true);- expect(formatCalls).toBe(0);expect(output).toBe(`import {Composition} from 'remotion'const untouched = {keep:"this spacing"}@@ -111,7 +103,7 @@ export const Root=()=>(`);});-test('preserves CRLF, spaces, double quotes, and bracket spacing', async () => {+test('preserves CRLF, spaces, double quotes, and bracket spacing', () => {const input = ['import { Composition } from "remotion"','',@@ -126,7 +118,7 @@ test('preserves CRLF, spaces, double quotes, and bracket spacing', async () => {'',].join('\r\n');- const {output} = await updateDefaultProps({+ const {output} = updateDefaultProps({input,compositionId: 'Comp',newDefaultProps: {title: 'Hello'},@@ -141,13 +133,13 @@ test('preserves CRLF, spaces, double quotes, and bracket spacing', async () => {);});-test('formats nested arrays and keeps non-identifier keys quoted', async () => {+test('formats nested arrays and keeps non-identifier keys quoted', () => {const input = `export const Root = () => (<Composition id="Comp" defaultProps={{ old: true }} />)`;- const {output} = await updateDefaultProps({+ const {output} = updateDefaultProps({input,compositionId: 'Comp',newDefaultProps: {