Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/plugins/live-debugger/src/transform/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,34 @@ describe('transformCode', () => {
expectedInstrumentedCount: 1,
expectedTotalFunctions: 2,
},
{
description: 'block-body arrow returning an expression-body arrow',
code: 'export const make = (cond) => { return () => cond ? a() : b(); };',
namedOnly: false,
expectedInstrumentedCount: 2,
expectedTotalFunctions: 2,
},
{
description: 'function declaration returning an expression-body arrow',
code: 'export function build(x) { const y = x + 1; return () => y > 0 ? pos(y) : neg(y); }',
namedOnly: false,
expectedInstrumentedCount: 2,
expectedTotalFunctions: 2,
},
{
description: 'block-body arrow returning an expression-body arrow with JSX mapping',
code: 'type Row = { id: string }; export const C = (rows: Row[]) => { const n = rows.length; return () => rows.map((r) => <li key={r.id}>{n}</li>); };',
namedOnly: false,
expectedInstrumentedCount: 3,
expectedTotalFunctions: 3,
},
{
description: 'block-body arrow returning a block-body arrow',
code: 'export const make = (cond) => { return () => { return cond ? a() : b(); }; };',
namedOnly: false,
expectedInstrumentedCount: 2,
expectedTotalFunctions: 2,
},
];

test.each(nestedSyntaxCases)(
Expand All @@ -375,6 +403,7 @@ describe('transformCode', () => {
expect(result.instrumentedCount).toBe(expectedInstrumentedCount);
expect(result.totalFunctions).toBe(expectedTotalFunctions);
expect(probeMatches?.length).toBe(expectedInstrumentedCount);
expect(result.code).toContain('$dd_return');
expect(result.code).toContain('catch(e)');
},
);
Expand Down
7 changes: 3 additions & 4 deletions packages/plugins/live-debugger/src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,8 @@ export function transformCode(options: TransformOptions): TransformResult {
superCallRangesHandledByExpressionBodies,
);

// Process inner (deeper) functions before outer ones so that MagicString
// appendLeft calls at shared positions (e.g. where an outer return wraps
// an inner arrow function) stack in the correct order.
// Process inner (deeper) functions before outer ones so same-side
// MagicString insertions at shared positions stack in the correct order.
for (let i = targets.length - 1; i >= 0; i--) {
injectInstrumentation(s, code, targets[i]);
}
Expand Down Expand Up @@ -566,7 +565,7 @@ function injectInstrumentation(s: MagicStringType, code: string, target: Functio
const assignmentSuffix = ret.hasSequenceExpressionArgument ? ')' : '';

s.appendLeft(ret.argStart, assignmentPrefix);
s.appendLeft(
s.appendRight(
ret.argEnd,
`${assignmentSuffix}, ${probeVarName} ? $dd_return(${probeVarName}, ${rvVarName}, ${receiverArg}${returnCaptureArgs}) : ${rvVarName})`,
);
Expand Down
Loading