Skip to content

Commit

Permalink
fix(compiler-core): remove unnecessary constant bail check
Browse files Browse the repository at this point in the history
member expressions and call expressions can only happen when there are identifiers

close vuejs#10807
  • Loading branch information
yyx990803 authored and lynxlangya committed May 30, 2024
1 parent 2029617 commit 1e66357
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler-core/src/babelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down
12 changes: 2 additions & 10 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1e66357

Please sign in to comment.