@@ -716,4 +716,51 @@ describe('compiler: expression transform', () => {
716716 } )
717717 } )
718718 } )
719+
720+ // Test for switch case variable declarations bug fix
721+ describe ( 'switch case variable declarations' , ( ) => {
722+ test ( 'should handle const declarations in switch case without braces' , ( ) => {
723+ const node = parseWithExpressionTransform (
724+ `{{ (() => { switch (1) { case 1: const foo = "bar"; return \`\${foo}\`; } })() }}` ,
725+ ) as InterpolationNode
726+
727+ // The variable 'foo' should be recognized as local and not prefixed with _ctx
728+ expect ( node . content ) . toMatchObject ( {
729+ type : NodeTypes . COMPOUND_EXPRESSION ,
730+ } )
731+
732+ // Check that 'foo' is not prefixed with '_ctx.'
733+ const children = ( node . content as any ) . children
734+ const codeStr = children
735+ . map ( ( c : any ) => ( typeof c === 'string' ? c : c . content ) )
736+ . join ( '' )
737+ expect ( codeStr ) . not . toContain ( '_ctx.foo' )
738+ expect ( codeStr ) . toContain ( 'foo' )
739+ } )
740+
741+ test ( 'should handle const declarations in switch case with braces (existing behavior)' , ( ) => {
742+ const node = parseWithExpressionTransform (
743+ `{{ (() => {
744+ switch (true) {
745+ case true: {
746+ const foo = "bar";
747+ return \`\${foo}\`;
748+ }
749+ }
750+ })() }}` ,
751+ ) as InterpolationNode
752+
753+ // This should work correctly even before our fix
754+ expect ( node . content ) . toMatchObject ( {
755+ type : NodeTypes . COMPOUND_EXPRESSION ,
756+ } )
757+
758+ const children = ( node . content as any ) . children
759+ const codeStr = children
760+ . map ( ( c : any ) => ( typeof c === 'string' ? c : c . content ) )
761+ . join ( '' )
762+ expect ( codeStr ) . not . toContain ( '_ctx.foo' )
763+ expect ( codeStr ) . toContain ( 'foo' )
764+ } )
765+ } )
719766} )
0 commit comments