Просмотр исходного кода

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

Evan You 3 лет назад
Родитель
Сommit
6bda4b6688

+ 10 - 0
packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts

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

+ 2 - 0
packages/compiler-core/src/transforms/transformExpression.ts

@@ -202,6 +202,8 @@ export function processExpression(
         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}`
       }
       }