]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): check if expression is constant (#7974)
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Wed, 29 Mar 2023 01:02:16 +0000 (09:02 +0800)
committerGitHub <noreply@github.com>
Wed, 29 Mar 2023 01:02:16 +0000 (09:02 +0800)
close #7973

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

index b9593c4a4bef8ab2b36c43903c37242cafa27a94..40070778b2af26dd674145099697b72138e9ba76 100644 (file)
@@ -13,6 +13,7 @@ import {
 } from '../../src'
 import { transformIf } from '../../src/transforms/vIf'
 import { transformExpression } from '../../src/transforms/transformExpression'
+import { PatchFlagNames, PatchFlags } from '../../../shared/src'
 
 function parseWithExpressionTransform(
   template: string,
@@ -494,7 +495,9 @@ describe('compiler: expression transform', () => {
       setup: BindingTypes.SETUP_MAYBE_REF,
       setupConst: BindingTypes.SETUP_CONST,
       data: BindingTypes.DATA,
-      options: BindingTypes.OPTIONS
+      options: BindingTypes.OPTIONS,
+      reactive: BindingTypes.SETUP_REACTIVE_CONST,
+      literal: BindingTypes.LITERAL_CONST
     }
 
     function compileWithBindingMetadata(
@@ -532,5 +535,25 @@ describe('compiler: expression transform', () => {
       expect(code).toMatch(`_ctx.options`)
       expect(code).toMatchSnapshot()
     })
+
+    test('literal const handling', () => {
+      const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`, {
+        inline: true
+      })
+      // #7973 should skip patch for literal const
+      expect(code).not.toMatch(
+        `${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
+      )
+    })
+
+    test('reactive const handling', () => {
+      const { code } = compileWithBindingMetadata(`<div>{{ reactive }}</div>`, {
+        inline: true
+      })
+      // #7973 should not skip patch for reactive const
+      expect(code).toMatch(
+        `${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
+      )
+    })
   })
 })
index 112dc63cb7a8a2718b113a1acaeb9da34d02f130..080d61d739f4981c6285366b189eab04fbe550d1 100644 (file)
@@ -128,7 +128,11 @@ export function processExpression(
       const isDestructureAssignment =
         parent && isInDestructureAssignment(parent, parentStack)
 
-      if (isConst(type) || localVars[raw]) {
+      if (
+        isConst(type) ||
+        type === BindingTypes.SETUP_REACTIVE_CONST ||
+        localVars[raw]
+      ) {
         return raw
       } else if (type === BindingTypes.SETUP_REF) {
         return `${raw}.value`
@@ -371,8 +375,6 @@ export function stringifyExpression(exp: ExpressionNode | string): string {
 
 function isConst(type: unknown) {
   return (
-    type === BindingTypes.SETUP_CONST ||
-    type === BindingTypes.LITERAL_CONST ||
-    type === BindingTypes.SETUP_REACTIVE_CONST
+    type === BindingTypes.SETUP_CONST || type === BindingTypes.LITERAL_CONST
   )
 }