@@ -257,27 +257,33 @@ describe('transformCode', () => {
257257 } ) ;
258258
259259 describe ( 'hoisted variable capture' , ( ) => {
260- it ( 'should generate entry and exit helper functions ' , ( ) => {
260+ it ( 'should generate args and locals helpers ' , ( ) => {
261261 const result = transformCode ( {
262262 ...BASE_OPTIONS ,
263263 code : 'function f(a, b) { const c = 1; return a + b + c; }' ,
264264 } ) ;
265265
266- // Entry helper captures params only
267266 expect ( result . code ) . toMatch ( / \$ d d _ e \d + = \( \) = > \( \{ a , b \} \) / ) ;
268- // Exit helper captures params + locals
269- expect ( result . code ) . toMatch ( / \$ d d _ l \d + = \( \) = > \( \{ a , b , c \} \) / ) ;
267+ expect ( result . code ) . toMatch ( / \$ d d _ l \d + = \( \) = > \( \{ c \} \) / ) ;
270268 } ) ;
271269
272- it ( 'should emit a single shared helper when entry and exit vars are identical ' , ( ) => {
270+ it ( 'should omit the locals helper when there are no locals ' , ( ) => {
273271 const result = transformCode ( {
274272 ...BASE_OPTIONS ,
275273 code : 'function f(a, b) { return a + b; }' ,
276274 } ) ;
277275
278- // Only the entry helper should be emitted
279276 expect ( result . code ) . toMatch ( / \$ d d _ e \d + = \( \) = > \( \{ a , b \} \) / ) ;
280- // No exit helper should be present
277+ expect ( result . code ) . not . toMatch ( / \$ d d _ l \d + / ) ;
278+ } ) ;
279+
280+ it ( 'should omit both helpers when there are no params and no locals' , ( ) => {
281+ const result = transformCode ( {
282+ ...BASE_OPTIONS ,
283+ code : 'function f() { return 1; }' ,
284+ } ) ;
285+
286+ expect ( result . code ) . not . toMatch ( / \$ d d _ e \d + / ) ;
281287 expect ( result . code ) . not . toMatch ( / \$ d d _ l \d + / ) ;
282288 } ) ;
283289 } ) ;
@@ -668,6 +674,43 @@ describe('transformCode', () => {
668674 return lines . map ( ( s ) => s . trimStart ( ) ) . join ( '\n' ) ;
669675 }
670676
677+ it ( 'should produce the expected output for a function with no arguments' , ( ) => {
678+ const result = transformCode ( {
679+ ...BASE_OPTIONS ,
680+ code : 'function getTime() { return Date.now(); }' ,
681+ } ) ;
682+
683+ expect ( normalizeCode ( result . code ) ) . toBe (
684+ normalizeCode (
685+ "function getTime() {const $dd_p0 = $dd_probes('src/utils.ts;getTime');" ,
686+ ' try {' ,
687+ ' let $dd_rv0;' ,
688+ ' if ($dd_p0) $dd_entry($dd_p0, this); return ($dd_rv0 = Date.now(), $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this) : $dd_rv0); ' ,
689+ ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this); throw e; }' ,
690+ '}' ,
691+ ) ,
692+ ) ;
693+ } ) ;
694+
695+ it ( 'should produce the expected output for a function with no arguments but with locals' , ( ) => {
696+ const result = transformCode ( {
697+ ...BASE_OPTIONS ,
698+ code : 'function getTime() { const now = Date.now(); return now; }' ,
699+ } ) ;
700+
701+ expect ( normalizeCode ( result . code ) ) . toBe (
702+ normalizeCode (
703+ "function getTime() {const $dd_p0 = $dd_probes('src/utils.ts;getTime');" ,
704+ ' try {' ,
705+ ' const $dd_l0 = () => ({now});' ,
706+ ' let $dd_rv0;' ,
707+ ' if ($dd_p0) $dd_entry($dd_p0, this); const now = Date.now(); return ($dd_rv0 = now, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, undefined, $dd_l0()) : $dd_rv0); ' ,
708+ ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this); throw e; }' ,
709+ '}' ,
710+ ) ,
711+ ) ;
712+ } ) ;
713+
671714 it ( 'should produce the expected output for a function with a single return' , ( ) => {
672715 const result = transformCode ( {
673716 ...BASE_OPTIONS ,
@@ -680,7 +723,7 @@ describe('transformCode', () => {
680723 ' const $dd_e0 = () => ({a, b});' ,
681724 ' try {' ,
682725 ' let $dd_rv0;' ,
683- ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); return ($dd_rv0 = a + b, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0(), $dd_e0() ) : $dd_rv0); ' ,
726+ ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); return ($dd_rv0 = a + b, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0()) : $dd_rv0); ' ,
684727 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
685728 '}' ,
686729 ) ,
@@ -698,7 +741,7 @@ describe('transformCode', () => {
698741 "function add(a, b) {const $dd_p0 = $dd_probes('src/utils.ts;add');" ,
699742 ' const $dd_e0 = () => ({a, b});' ,
700743 ' try {' ,
701- ' const $dd_l0 = () => ({a, b, sum});' ,
744+ ' const $dd_l0 = () => ({sum});' ,
702745 ' let $dd_rv0;' ,
703746 ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); const sum = a + b; return ($dd_rv0 = sum, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0(), $dd_l0()) : $dd_rv0); ' ,
704747 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
@@ -721,7 +764,7 @@ describe('transformCode', () => {
721764 ' try {' ,
722765 ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0());' ,
723766 ' const $dd_rv0 = x * 2;' ,
724- ' if ($dd_p0) $dd_return($dd_p0, $dd_rv0, this, $dd_e0(), $dd_e0() );' ,
767+ ' if ($dd_p0) $dd_return($dd_p0, $dd_rv0, this, $dd_e0());' ,
725768 ' return $dd_rv0;' ,
726769 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
727770 '};' ,
@@ -743,7 +786,7 @@ describe('transformCode', () => {
743786 ' try {' ,
744787 ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0());' ,
745788 ' const $dd_rv0 = {key: x};' ,
746- ' if ($dd_p0) $dd_return($dd_p0, $dd_rv0, this, $dd_e0(), $dd_e0() );' ,
789+ ' if ($dd_p0) $dd_return($dd_p0, $dd_rv0, this, $dd_e0());' ,
747790 ' return $dd_rv0;' ,
748791 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
749792 '};' ,
@@ -764,7 +807,7 @@ describe('transformCode', () => {
764807 ' try {' ,
765808 ' let $dd_rv0;' ,
766809 ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); console.log(msg); ' ,
767- ' if ($dd_p0) $dd_return($dd_p0, undefined, this, $dd_e0(), $dd_e0() );' ,
810+ ' if ($dd_p0) $dd_return($dd_p0, undefined, this, $dd_e0());' ,
768811 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
769812 '}' ,
770813 ) ,
@@ -783,7 +826,7 @@ describe('transformCode', () => {
783826 ' const $dd_e0 = () => ({x});' ,
784827 ' try {' ,
785828 ' let $dd_rv0;' ,
786- ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); if (x < 0) { return ($dd_rv0 = -x, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0(), $dd_e0()) : $dd_rv0); } return ($dd_rv0 = x, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0() , $dd_e0()) : $dd_rv0); ' ,
829+ ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); if (x < 0) { return ($dd_rv0 = -x, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0()) : $dd_rv0); } return ($dd_rv0 = x, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0()) : $dd_rv0); ' ,
787830 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
788831 '}' ,
789832 ) ,
@@ -802,8 +845,8 @@ describe('transformCode', () => {
802845 ' const $dd_e0 = () => ({x});' ,
803846 ' try {' ,
804847 ' let $dd_rv0;' ,
805- ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); if (!x) { if ($dd_p0) $dd_return($dd_p0, undefined, this, $dd_e0(), $dd_e0() ); return; } console.log(x); ' ,
806- ' if ($dd_p0) $dd_return($dd_p0, undefined, this, $dd_e0(), $dd_e0() );' ,
848+ ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0()); if (!x) { if ($dd_p0) $dd_return($dd_p0, undefined, this, $dd_e0()); return; } console.log(x); ' ,
849+ ' if ($dd_p0) $dd_return($dd_p0, undefined, this, $dd_e0());' ,
807850 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
808851 '}' ,
809852 ) ,
@@ -832,9 +875,9 @@ describe('transformCode', () => {
832875 ' let $dd_rv0;' ,
833876 ' if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0());' ,
834877 ' if (x > 0) {' ,
835- ' return ($dd_rv0 = 1, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0(), $dd_e0() ) : $dd_rv0);' ,
878+ ' return ($dd_rv0 = 1, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0()) : $dd_rv0);' ,
836879 ' } else {' ,
837- ' return ($dd_rv0 = -1, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0(), $dd_e0() ) : $dd_rv0);' ,
880+ ' return ($dd_rv0 = -1, $dd_p0 ? $dd_return($dd_p0, $dd_rv0, this, $dd_e0()) : $dd_rv0);' ,
838881 ' }' ,
839882 '' ,
840883 ' } catch(e) { if ($dd_p0) $dd_throw($dd_p0, e, this, $dd_e0()); throw e; }' ,
0 commit comments