fix(compiler): fix expression codegen for literal const bindings in non-inline mode

This commit is contained in:
Evan You 2023-04-18 11:39:53 +08:00
parent 7f1b546a99
commit 0f77a2b1d1
3 changed files with 17 additions and 4 deletions

View File

@ -549,7 +549,7 @@ describe('compiler: expression transform', () => {
test('literal const handling non-inline mode', () => { test('literal const handling non-inline mode', () => {
const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`) const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`)
expect(code).toMatch(`toDisplayString(literal)`) expect(code).toMatch(`toDisplayString($setup.literal)`)
// #7973 should skip patch for literal const // #7973 should skip patch for literal const
expect(code).not.toMatch( expect(code).not.toMatch(
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */` `${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`

View File

@ -197,13 +197,14 @@ export function processExpression(
return genPropsAccessExp(bindingMetadata.__propsAliases![raw]) return genPropsAccessExp(bindingMetadata.__propsAliases![raw])
} }
} else { } else {
if (type && type.startsWith('setup')) { if (
(type && type.startsWith('setup')) ||
type === BindingTypes.LITERAL_CONST
) {
// setup bindings in non-inline mode // setup bindings in non-inline mode
return `$setup.${raw}` return `$setup.${raw}`
} else if (type === BindingTypes.PROPS_ALIASED) { } else if (type === BindingTypes.PROPS_ALIASED) {
return `$props['${bindingMetadata.__propsAliases![raw]}']` return `$props['${bindingMetadata.__propsAliases![raw]}']`
} else if (type === BindingTypes.LITERAL_CONST) {
return raw
} else if (type) { } else if (type) {
return `$${type}.${raw}` return `$${type}.${raw}`
} }

View File

@ -202,4 +202,16 @@ describe('sfc hoist static', () => {
}) })
assertCode(content) assertCode(content)
}) })
test('template binding access in inline mode', () => {
const { content } = compile(
`
<script setup>
const foo = 'bar'
</script>
<template>{{ foo }}</template>
`
)
expect(content).toMatch('_toDisplayString(foo)')
})
}) })