From fefce06b41e3b75de3d748dc6399628ec5056e78 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 29 Apr 2024 16:21:25 +0800 Subject: [PATCH] fix(compiler-core): fix bail constant for globals --- .../transforms/transformExpressions.spec.ts | 15 +++++++++++++++ .../src/transforms/transformExpression.ts | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts index 8ecc2fb0fb8..273634dd57b 100644 --- a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts @@ -431,6 +431,21 @@ describe('compiler: expression transform', () => { }) }) + test('should not bail constant on strings w/ ()', () => { + const node = parseWithExpressionTransform( + `{{ new Date().getFullYear() }}`, + ) as InterpolationNode + expect(node.content).toMatchObject({ + children: [ + 'new ', + { constType: ConstantTypes.NOT_CONSTANT }, + '().', + { constType: ConstantTypes.NOT_CONSTANT }, + '()', + ], + }) + }) + describe('ES Proposals support', () => { test('bigInt', () => { const node = parseWithExpressionTransform( diff --git a/packages/compiler-core/src/transforms/transformExpression.ts b/packages/compiler-core/src/transforms/transformExpression.ts index 2d8dc8bc850..35aa9a373a4 100644 --- a/packages/compiler-core/src/transforms/transformExpression.ts +++ b/packages/compiler-core/src/transforms/transformExpression.ts @@ -311,7 +311,12 @@ 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)) { + if ( + !(needPrefix && isLocal) && + parent.type !== 'CallExpression' && + parent.type !== 'NewExpression' && + parent.type !== 'MemberExpression' + ) { ;(node as QualifiedId).isConstant = true } // also generate sub-expressions for other identifiers for better