Skip to content

Commit f5f832f

Browse files
committed
fix(compiler-core): Update switch statement condition check to use strict equality
1 parent a543ec2 commit f5f832f

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

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

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -720,26 +720,17 @@ describe('compiler: expression transform', () => {
720720
// Test for switch case variable declarations bug fix
721721
describe('switch case variable declarations', () => {
722722
test('should handle const declarations in switch case without braces', () => {
723-
const node = parseWithExpressionTransform(
723+
const { code } = compile(
724724
`{{ (() => { 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+
)
731726

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`)
739730
})
740731

741732
test('should handle const declarations in switch case with braces (existing behavior)', () => {
742-
const node = parseWithExpressionTransform(
733+
const { code } = compile(
743734
`{{ (() => {
744735
switch (true) {
745736
case true: {
@@ -748,19 +739,20 @@ describe('compiler: expression transform', () => {
748739
}
749740
}
750741
})() }}`,
751-
) as InterpolationNode
742+
)
752743

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+
)
757753

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`)
764756
})
765757
})
766758
})

packages/compiler-core/src/babelUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export function walkBlockDeclarations(
217217
onIdent(stmt.id)
218218
} else if (isForStatement(stmt)) {
219219
walkForStatement(stmt, true, onIdent)
220-
} else if (stmt.type == 'SwitchStatement') {
220+
} else if (stmt.type === 'SwitchStatement') {
221221
walkSwitchStatement(stmt, true, onIdent)
222222
}
223223
}

0 commit comments

Comments
 (0)