]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler): fix expression codegen for literal const bindings in non-inline mode
authorEvan You <yyx990803@gmail.com>
Tue, 18 Apr 2023 03:39:53 +0000 (11:39 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 18 Apr 2023 03:39:53 +0000 (11:39 +0800)
packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts
packages/compiler-core/src/transforms/transformExpression.ts
packages/compiler-sfc/__tests__/compileScript/hoistStatic.spec.ts

index 66f988d3474f6612635ecd06083c889039399b2b..5a77b2eddbce0e190bda4edabf0c87aba64bfe74 100644 (file)
@@ -549,7 +549,7 @@ describe('compiler: expression transform', () => {
 
     test('literal const handling, non-inline mode', () => {
       const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`)
-      expect(code).toMatch(`toDisplayString(literal)`)
+      expect(code).toMatch(`toDisplayString($setup.literal)`)
       // #7973 should skip patch for literal const
       expect(code).not.toMatch(
         `${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
index 466027682b13d3525b1ff06883099222e96c378d..35fc278ac8610444891a136f47c666906feffd17 100644 (file)
@@ -197,13 +197,14 @@ export function processExpression(
         return genPropsAccessExp(bindingMetadata.__propsAliases![raw])
       }
     } else {
-      if (type && type.startsWith('setup')) {
+      if (
+        (type && type.startsWith('setup')) ||
+        type === BindingTypes.LITERAL_CONST
+      ) {
         // setup bindings in non-inline mode
         return `$setup.${raw}`
       } else if (type === BindingTypes.PROPS_ALIASED) {
         return `$props['${bindingMetadata.__propsAliases![raw]}']`
-      } else if (type === BindingTypes.LITERAL_CONST) {
-        return raw
       } else if (type) {
         return `$${type}.${raw}`
       }
index 614a5e75bceb30e74bacc3ad40f5f63ae60084ee..7b3a8a813c544a8585ec34be44fa8b43b6886c7b 100644 (file)
@@ -202,4 +202,16 @@ describe('sfc hoist static', () => {
     })
     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)')
+  })
 })