🎥 Make videos programmatically with React
Make function source ranges platform independent
Jonny Burger259910d24665
diff --git a/packages/studio-codemods/src/function-source-edits.ts b/packages/studio-codemods/src/function-source-edits.tsindex 3e4c1cedb0..d14167d5da 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 {recastIndexToOffset, recastLocToOffset} from './recast-loc-to-offset';+import {recastLocToOffset} from './recast-loc-to-offset';import {getNodeEndIncludingSameLineComments,type SourceEdit,@@ -92,16 +92,28 @@ const getBodySourceRange = ({const extra = body.extra as| {parenthesized: boolean | null | undefined;- parenStart: number | null | undefined;}| null| undefined;- if (extra?.parenthesized && typeof extra.parenStart === 'number') {+ if (extra?.parenthesized) {if (!functionNode.loc) {return null;}- start = Math.min(start, recastIndexToOffset(input, extra.parenStart));+ let openingParenthesis = start - 1;+ while (openingParenthesis >= 0) {+ while (openingParenthesis >= 0 && /\s/.test(input[openingParenthesis])) {+ openingParenthesis--;+ }++ if (input[openingParenthesis] !== '(') {+ break;+ }++ start = openingParenthesis;+ openingParenthesis--;+ }+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.tsindex b2680fb4f2..bd4f82bfe7 100644--- a/packages/studio-codemods/src/recast-loc-to-offset.ts+++ b/packages/studio-codemods/src/recast-loc-to-offset.ts@@ -1,25 +1,16 @@const RECAST_TAB_WIDTH = 4;-type RecastPosition =- | {index: number; type: 'index'}- | {column: number; line: number; type: 'location'};-// Recast normalizes every line ending, removes zero-width indentation, and-// expands tabs in leading whitespace before parsing. Walk both representations-// together so parser positions can be mapped back to the original source.-const recastPositionToOffset = (+// expands tabs in leading whitespace before parsing. Walk the parsed location+// and original source together so the offset remains platform-independent.+export const recastLocToOffset = (input: string,- target: RecastPosition,+ loc: {line: number; column: number},): number => {let sourceOffset = 0;- let recastIndex = 0;let line = 1;let column = 0;let isLeadingWhitespace = true;- const isAtTarget = () =>- target.type === 'index'- ? recastIndex === target.index- : line === target.line && column === target.column;while (sourceOffset < input.length) {const character = input[sourceOffset];@@ -31,7 +22,7 @@ const recastPositionToOffset = (continue;}- if (isAtTarget()) {+ if (line === loc.line && column === loc.column) {return sourceOffset;}@@ -44,7 +35,6 @@ const recastPositionToOffset = (character === '\u2029') {sourceOffset += isCrLf ? 2 : 1;- recastIndex++;line++;column = 0;isLeadingWhitespace = true;@@ -53,33 +43,19 @@ const recastPositionToOffset = (if (isLeadingWhitespace && character === '\t') {const width = RECAST_TAB_WIDTH - (column % RECAST_TAB_WIDTH);- const targetIsInsideTab =- target.type === 'index'- ? target.index < recastIndex + width- : target.line === line && target.column < column + width;- if (targetIsInsideTab) {+ if (loc.line === line && loc.column < column + width) {return sourceOffset;}sourceOffset++;- recastIndex += width;column += width;continue;}sourceOffset++;- recastIndex++;column++;isLeadingWhitespace = isLeadingWhitespace && /\s/.test(character);}return sourceOffset;};--export const recastIndexToOffset = (input: string, index: number): number =>- recastPositionToOffset(input, {index, type: 'index'});--export const recastLocToOffset = (- input: string,- loc: {line: number; column: number},-): number => recastPositionToOffset(input, {...loc, type: 'location'});diff --git a/packages/studio-codemods/src/test/function-source-edits.test.ts b/packages/studio-codemods/src/test/function-source-edits.test.tsindex 6f9569aaef..52f9f50eef 100644--- a/packages/studio-codemods/src/test/function-source-edits.test.ts+++ b/packages/studio-codemods/src/test/function-source-edits.test.ts@@ -1,20 +1,21 @@import {expect, test} from 'bun:test';import type {BlockStatement, Statement} from '@babel/types';import * as recast from 'recast';+import * as tsParser from 'recast/parsers/babel-ts';import {captureFunctionSourceSnapshots,getFunctionSourceEditsForPrependedStatements,} from '../function-source-edits';-import {recastIndexToOffset, recastLocToOffset} from '../recast-loc-to-offset';+import {recastLocToOffset} from '../recast-loc-to-offset';import {parseAst} from '../sequence-props/parse-ast';const b = recast.types.builders;-test('maps Recast positions across source whitespace normalization', () => {+test('maps Recast locations across source whitespace normalization', () => {const inputs = ['\uFEFF\texport const C = () => (\r\n\t<div />\r\n);\r\n','export const C =\t() => (<div />);\n',- 'export const C = () => (\u2028 <div />\u2029);\r',+ '\r\n\nexport const C = () => (\u2028 <div />\u2029);\r',];for (const input of inputs) {@@ -37,17 +38,6 @@ test('maps Recast positions across source whitespace normalization', () => {throw new Error('Expected a located 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('=>')),- );expect(recastLocToOffset(input, initializer.body.loc.start)).toBe(input.indexOf('<div'),);@@ -87,9 +77,12 @@ test('rejects inserted statements outside the leading prefix', () => {});test('filters edits nested inside a reprinted function body', () => {- const input = `export const C = () => items.map(() => <span />);-`;- const ast = parseAst(input);+ const input =+ 'const before = true;\r\nexport const C = () => ((items.map(() => <span />)));\r\n';+ const ast = recast.parse(input, {+ lineTerminator: '\r\n',+ parser: tsParser,+ }) as ReturnType<typeof parseAst>;const snapshots = captureFunctionSourceSnapshots(ast);for (const {functionNode} of snapshots) {if (functionNode.body.type === 'BlockStatement') {@@ -113,4 +106,5 @@ test('filters edits nested inside a reprinted function body', () => {expect(coveredRanges).toHaveLength(2);expect(edits).toHaveLength(1);+ expect(edits[0].start).toBe(input.indexOf('(', input.indexOf('=>')));});