Skip to content

Commit 0f77a2b

Browse files
committed
fix(compiler): fix expression codegen for literal const bindings in non-inline mode
1 parent 7f1b546 commit 0f77a2b

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ describe('compiler: expression transform', () => {
549549

550550
test('literal const handling, non-inline mode', () => {
551551
const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`)
552-
expect(code).toMatch(`toDisplayString(literal)`)
552+
expect(code).toMatch(`toDisplayString($setup.literal)`)
553553
// #7973 should skip patch for literal const
554554
expect(code).not.toMatch(
555555
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`

packages/compiler-core/src/transforms/transformExpression.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,14 @@ export function processExpression(
197197
return genPropsAccessExp(bindingMetadata.__propsAliases![raw])
198198
}
199199
} else {
200-
if (type && type.startsWith('setup')) {
200+
if (
201+
(type && type.startsWith('setup')) ||
202+
type === BindingTypes.LITERAL_CONST
203+
) {
201204
// setup bindings in non-inline mode
202205
return `$setup.${raw}`
203206
} else if (type === BindingTypes.PROPS_ALIASED) {
204207
return `$props['${bindingMetadata.__propsAliases![raw]}']`
205-
} else if (type === BindingTypes.LITERAL_CONST) {
206-
return raw
207208
} else if (type) {
208209
return `$${type}.${raw}`
209210
}

packages/compiler-sfc/__tests__/compileScript/hoistStatic.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,16 @@ describe('sfc hoist static', () => {
202202
})
203203
assertCode(content)
204204
})
205+
206+
test('template binding access in inline mode', () => {
207+
const { content } = compile(
208+
`
209+
<script setup>
210+
const foo = 'bar'
211+
</script>
212+
<template>{{ foo }}</template>
213+
`
214+
)
215+
expect(content).toMatch('_toDisplayString(foo)')
216+
})
205217
})

0 commit comments

Comments
 (0)