@@ -104,6 +104,49 @@ test('jessie: function compile', t => {
104104 is ( ctx . mult ( 4 ) , 12 )
105105} )
106106
107+ test ( 'jessie: early return' , t => {
108+ // Standalone if with return (guard clause pattern)
109+ let ctx = { }
110+ run ( 'function guard(x) { if (x < 0) return -1; return x * 2 }' , ctx )
111+ is ( ctx . guard ( - 5 ) , - 1 , 'early return on negative' )
112+ is ( ctx . guard ( 5 ) , 10 , 'normal path' )
113+
114+ // Multiple early returns
115+ ctx = { }
116+ run ( 'function grade(s) { if (s >= 90) return "A"; if (s >= 80) return "B"; return "C" }' , ctx )
117+ is ( ctx . grade ( 95 ) , 'A' )
118+ is ( ctx . grade ( 85 ) , 'B' )
119+ is ( ctx . grade ( 70 ) , 'C' )
120+
121+ // Early return with no value
122+ ctx = { called : false }
123+ run ( 'function maybe(x) { if (!x) return; called = true }' , ctx )
124+ ctx . maybe ( false )
125+ is ( ctx . called , false , 'early return prevented side effect' )
126+ ctx . maybe ( true )
127+ is ( ctx . called , true , 'no early return, side effect happened' )
128+ } )
129+
130+ test ( 'jessie: standalone if statement' , t => {
131+ // if without else, single statement body
132+ is ( parse ( 'if (x) y' ) [ 0 ] , 'if' )
133+ is ( parse ( 'if (x) y' ) . length , 3 , 'no else branch' )
134+
135+ // if with return (common guard pattern)
136+ const ast = parse ( 'if (x < 0) return -1' )
137+ is ( ast [ 0 ] , 'if' )
138+ is ( ast [ 2 ] [ 0 ] , 'return' )
139+
140+ // Compile standalone if
141+ let ctx = { x : true , y : 0 }
142+ run ( 'if (x) y = 1' , ctx )
143+ is ( ctx . y , 1 )
144+
145+ ctx = { x : false , y : 0 }
146+ run ( 'if (x) y = 1' , ctx )
147+ is ( ctx . y , 0 , 'false condition skips body' )
148+ } )
149+
107150test ( 'jessie: function rest param' , t => {
108151 const ast = parse ( 'function f(a, ...rest) { return rest }' )
109152 is ( ast [ 2 ] [ 0 ] , ',' )
0 commit comments