@@ -720,26 +720,17 @@ describe('compiler: expression transform', () => {
720
720
// Test for switch case variable declarations bug fix
721
721
describe ( 'switch case variable declarations' , ( ) => {
722
722
test ( 'should handle const declarations in switch case without braces' , ( ) => {
723
- const node = parseWithExpressionTransform (
723
+ const { code } = compile (
724
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
- } )
725
+ )
731
726
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' )
727
+ expect ( code ) . toMatch ( `const foo = "bar";` )
728
+ expect ( code ) . toMatch ( `return \`\${foo}\`;` )
729
+ expect ( code ) . not . toMatch ( `_ctx.foo` )
739
730
} )
740
731
741
732
test ( 'should handle const declarations in switch case with braces (existing behavior)' , ( ) => {
742
- const node = parseWithExpressionTransform (
733
+ const { code } = compile (
743
734
`{{ (() => {
744
735
switch (true) {
745
736
case true: {
@@ -748,19 +739,20 @@ describe('compiler: expression transform', () => {
748
739
}
749
740
}
750
741
})() }}` ,
751
- ) as InterpolationNode
742
+ )
752
743
753
- // This should work correctly even before our fix
754
- expect ( node . content ) . toMatchObject ( {
755
- type : NodeTypes . COMPOUND_EXPRESSION ,
756
- } )
744
+ expect ( code ) . toMatch ( `const foo = "bar";` )
745
+ expect ( code ) . toMatch ( `return \`\${foo}\`;` )
746
+ expect ( code ) . not . toMatch ( `_ctx.foo` )
747
+ } )
748
+
749
+ test ( 'should parse switch case test as local scoped variables' , ( ) => {
750
+ const { code } = compile (
751
+ `{{ (() => { switch (foo) { case bar: return \`\${bar}\`; } })() }}` ,
752
+ )
757
753
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' )
754
+ expect ( code ) . toMatch ( '_ctx.foo' )
755
+ expect ( code ) . toMatch ( `_ctx.bar` )
764
756
} )
765
757
} )
766
758
} )
0 commit comments