Skip to content

🎥 Make videos programmatically with React

Fix codemod source edits on CRLF files

Jonny Burgere9628f93920f
diff --git a/packages/studio-codemods/src/function-source-edits.ts b/packages/studio-codemods/src/function-source-edits.ts
index 90927094d4..3e4c1cedb0 100644
--- a/packages/studio-codemods/src/function-source-edits.ts
+++ b/packages/studio-codemods/src/function-source-edits.ts
@@ -6,7 +6,7 @@ import type {
} from '@babel/types';
import type {namedTypes} from 'ast-types';
import * as recast from 'recast';
-import {recastLocToOffset} from './recast-loc-to-offset';
+import {recastIndexToOffset, recastLocToOffset} from './recast-loc-to-offset';
import {
getNodeEndIncludingSameLineComments,
type SourceEdit,
@@ -101,7 +101,7 @@ const getBodySourceRange = ({
return null;
}
- start = Math.min(start, extra.parenStart);
+ start = Math.min(start, recastIndexToOffset(input, extra.parenStart));
end = recastLocToOffset(input, functionNode.loc.end);
}
diff --git a/packages/studio-codemods/src/recast-loc-to-offset.ts b/packages/studio-codemods/src/recast-loc-to-offset.ts
index 0a6aef72a0..6c7d347e09 100644
--- a/packages/studio-codemods/src/recast-loc-to-offset.ts
+++ b/packages/studio-codemods/src/recast-loc-to-offset.ts
@@ -1,5 +1,43 @@
const RECAST_TAB_WIDTH = 4;
+// Recast removes a byte order mark, normalizes CRLF line endings, and expands
+// tabs before parsing. Absolute indexes stored in Babel's `extra` metadata
+// therefore need to be mapped back to offsets in the original source.
+export const recastIndexToOffset = (input: string, index: number): number => {
+ let sourceOffset = input.startsWith('\uFEFF') ? 1 : 0;
+ let recastIndex = 0;
+ let column = 0;
+ while (sourceOffset < input.length && recastIndex < index) {
+ if (input[sourceOffset] === '\r' && input[sourceOffset + 1] === '\n') {
+ sourceOffset += 2;
+ recastIndex++;
+ column = 0;
+ continue;
+ }
+
+ if (input[sourceOffset] === '\n' || input[sourceOffset] === '\r') {
+ sourceOffset++;
+ recastIndex++;
+ column = 0;
+ continue;
+ }
+
+ if (input[sourceOffset] === '\t') {
+ const width = RECAST_TAB_WIDTH - (column % RECAST_TAB_WIDTH);
+ sourceOffset++;
+ recastIndex += width;
+ column += width;
+ continue;
+ }
+
+ sourceOffset++;
+ recastIndex++;
+ column++;
+ }
+
+ return sourceOffset;
+};
+
// Recast expands tabs before parsing, so its columns cannot be used as
// character offsets in the original source.
export const recastLocToOffset = (
diff --git a/packages/studio-codemods/src/test/function-source-edits.test.ts b/packages/studio-codemods/src/test/function-source-edits.test.ts
index c479c54866..e41c0edf98 100644
--- a/packages/studio-codemods/src/test/function-source-edits.test.ts
+++ b/packages/studio-codemods/src/test/function-source-edits.test.ts
@@ -5,10 +5,42 @@ import {
captureFunctionSourceSnapshots,
getFunctionSourceEditsForPrependedStatements,
} from '../function-source-edits';
+import {recastIndexToOffset} from '../recast-loc-to-offset';
import {parseAst} from '../sequence-props/parse-ast';
const b = recast.types.builders;
+test('maps Recast indexes back to source offsets with BOM, tabs, and CRLF', () => {
+ const input = '\uFEFF\texport const C = () => (\r\n\t<div />\r\n);\r\n';
+ const ast = parseAst(input);
+ const declaration = ast.program.body[0];
+ if (declaration.type !== 'ExportNamedDeclaration') {
+ throw new Error('Expected an export declaration');
+ }
+
+ const variableDeclaration = declaration.declaration;
+ if (variableDeclaration?.type !== 'VariableDeclaration') {
+ throw new Error('Expected a variable declaration');
+ }
+
+ const initializer = variableDeclaration.declarations[0].init;
+ if (initializer?.type !== 'ArrowFunctionExpression') {
+ throw new Error('Expected an arrow function');
+ }
+
+ const extra = initializer.body.extra as
+ | {parenStart?: number}
+ | null
+ | undefined;
+ if (typeof extra?.parenStart !== 'number') {
+ throw new Error('Expected a parenthesized expression body');
+ }
+
+ expect(recastIndexToOffset(input, extra.parenStart)).toBe(
+ input.indexOf('(', input.indexOf('=>')),
+ );
+});
+
test('rejects inserted statements outside the leading prefix', () => {
const input = `export const C = () => {
first();
diff --git a/packages/studio-codemods/src/test/update-keyframes-formatting.test.ts b/packages/studio-codemods/src/test/update-keyframes-formatting.test.ts
index 2fcfddd856..5f4ed5a8d3 100644
--- a/packages/studio-codemods/src/test/update-keyframes-formatting.test.ts
+++ b/packages/studio-codemods/src/test/update-keyframes-formatting.test.ts
@@ -181,7 +181,7 @@ export const Example = () => {
);
});
-test('new keyframe imports stay after directives', async () => {
+test('new keyframe imports stay after directives with CRLF', async () => {
const input = `'use client'
const unrelated = true
@@ -189,7 +189,7 @@ const unrelated = true
export const Example = () => (
<div style={{opacity:0.5}} />
)
-`;
+`.replaceAll(/\r?\n/g, '\r\n');
const {output} = await updateSequenceKeyframes({
input,
nodePath: getNodePathAtLine(input, 6),
@@ -202,7 +202,8 @@ export const Example = () => (
videoConfigValues: null,
});
- expect(output).toBe(`'use client'
+ expect(output).toBe(
+ `'use client'
import { interpolate, useCurrentFrame } from 'remotion'
const unrelated = true
@@ -219,7 +220,8 @@ export const Example = () => {
}} />
)
}
-`);
+`.replaceAll(/\r?\n/g, '\r\n'),
+ );
});
test('frame hooks stay before comments attached to the first statement', async () => {
diff --git a/packages/studio-codemods/src/test/update-sequence-props.test.ts b/packages/studio-codemods/src/test/update-sequence-props.test.ts
index 82140587c5..4dcc461a24 100644
--- a/packages/studio-codemods/src/test/update-sequence-props.test.ts
+++ b/packages/studio-codemods/src/test/update-sequence-props.test.ts
@@ -461,7 +461,7 @@ export const Example = () => <div opacity={1} />
parseAst(output);
});
-test('does not overlap nested function edits for batched keyframed props', () => {
+test('does not overlap nested function edits for CRLF batched keyframed props', () => {
const input = `import {Interactive} from "remotion"
export const Example = () => (
@@ -469,7 +469,7 @@ export const Example = () => (
{[1].map(() => <Interactive.Div opacity={1} />)}
</Interactive.Div>
)
-`;
+`.replaceAll(/\r?\n/g, '\r\n');
const ast = parseAst(input);
const nodePaths: ReturnType<typeof getNodePathForRecastPath>[] = [];
recast.types.visit(ast, {
@@ -511,8 +511,8 @@ export const Example = () => (
})),
});
- expect(output)
- .toBe(`import {Interactive, useCurrentFrame, interpolate} from "remotion"
+ expect(output).toBe(
+ `import {Interactive, useCurrentFrame, interpolate} from "remotion"
export const Example = () => {
const frame = useCurrentFrame()
@@ -526,7 +526,8 @@ export const Example = () => {
</Interactive.Div>
)
}
-`);
+`.replaceAll(/\r?\n/g, '\r\n'),
+ );
parseAst(output);
});