Skip to content

Commit 2f4cdd8

Browse files
committed
fix(compiler-core): Add switch-case test
1 parent 6497417 commit 2f4cdd8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)