]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): remove unnecessary constant bail check
authorEvan You <yyx990803@gmail.com>
Mon, 29 Apr 2024 04:21:24 +0000 (12:21 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 29 Apr 2024 04:21:38 +0000 (12:21 +0800)
member expressions and call expressions can only happen when there are identifiers

close #10807

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

index 4f08aeb9b226871adcea05c644c9367a9843b8e0..8ecc2fb0fb87da8ac9d3f3edcbe098b865038e6b 100644 (file)
@@ -421,6 +421,16 @@ describe('compiler: expression transform', () => {
     })
   })
 
+  // #10807
+  test('should not bail constant on strings w/ ()', () => {
+    const node = parseWithExpressionTransform(
+      `{{ { foo: 'ok()' } }}`,
+    ) as InterpolationNode
+    expect(node.content).toMatchObject({
+      constType: ConstantTypes.CAN_STRINGIFY,
+    })
+  })
+
   describe('ES Proposals support', () => {
     test('bigInt', () => {
       const node = parseWithExpressionTransform(
index b12a6d9b47fb71e83fa597b021a2ffca906e3663..d3812f84ce67e0a29137eabbae972693e14e2298 100644 (file)
@@ -10,6 +10,9 @@ import type {
 } from '@babel/types'
 import { walk } from 'estree-walker'
 
+/**
+ * Return value indicates whether the AST walked can be a constant
+ */
 export function walkIdentifiers(
   root: Node,
   onIdentifier: (
index 53a4bc5925f13bebd97d75f45bad369537e0c39b..2d8dc8bc8502663552e3732d3df634ae40a5037e 100644 (file)
@@ -46,10 +46,6 @@ import { BindingTypes } from '../options'
 
 const isLiteralWhitelisted = /*#__PURE__*/ makeMap('true,false,null,this')
 
-// a heuristic safeguard to bail constant expressions on presence of
-// likely function invocation and member access
-const constantBailRE = /\w\s*\(|\.[^\d]/
-
 export const transformExpression: NodeTransform = (node, context) => {
   if (node.type === NodeTypes.INTERPOLATION) {
     node.content = processExpression(
@@ -226,8 +222,6 @@ export function processExpression(
 
   // fast path if expression is a simple identifier.
   const rawExp = node.content
-  // bail constant on parens (function invocation) and dot (member access)
-  const bailConstant = constantBailRE.test(rawExp)
 
   let ast = node.ast
 
@@ -317,7 +311,7 @@ export function processExpression(
       } else {
         // The identifier is considered constant unless it's pointing to a
         // local scope variable (a v-for alias, or a v-slot prop)
-        if (!(needPrefix && isLocal) && !bailConstant) {
+        if (!(needPrefix && isLocal)) {
           ;(node as QualifiedId).isConstant = true
         }
         // also generate sub-expressions for other identifiers for better
@@ -371,9 +365,7 @@ export function processExpression(
     ret.ast = ast
   } else {
     ret = node
-    ret.constType = bailConstant
-      ? ConstantTypes.NOT_CONSTANT
-      : ConstantTypes.CAN_STRINGIFY
+    ret.constType = ConstantTypes.CAN_STRINGIFY
   }
   ret.identifiers = Object.keys(knownIds)
   return ret