]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): treat floating point numbers as constants
authorEvan You <yyx990803@gmail.com>
Fri, 12 May 2023 09:53:40 +0000 (10:53 +0100)
committerEvan You <yyx990803@gmail.com>
Fri, 12 May 2023 09:53:40 +0000 (10:53 +0100)
close #8295

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

index 5a77b2eddbce0e190bda4edabf0c87aba64bfe74..7614bc0f386cc6c1610d8817663e03b353638b33 100644 (file)
@@ -431,6 +431,16 @@ describe('compiler: expression transform', () => {
     })
   })
 
+  // #8295
+  test('should treat floating point number literals as constant', () => {
+    const node = parseWithExpressionTransform(
+      `{{ [1, 2.1] }}`
+    ) as InterpolationNode
+    expect(node.content).toMatchObject({
+      constType: ConstantTypes.CAN_STRINGIFY
+    })
+  })
+
   describe('ES Proposals support', () => {
     test('bigInt', () => {
       const node = parseWithExpressionTransform(
index 35fc278ac8610444891a136f47c666906feffd17..3e75923d0d4ed2bacae5d705d74893f2caa468fd 100644 (file)
@@ -45,6 +45,10 @@ 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(
@@ -217,7 +221,7 @@ 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 = rawExp.indexOf(`(`) > -1 || rawExp.indexOf('.') > 0
+  const bailConstant = constantBailRE.test(rawExp)
 
   if (isSimpleIdentifier(rawExp)) {
     const isScopeVarReference = context.identifiers[rawExp]