Skip to content

Commit 09b4df8

Browse files
committed
fix(compiler-core): remove unnecessary constant bail check
member expressions and call expressions can only happen when there are identifiers close #10807
1 parent 9c2de62 commit 09b4df8

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ describe('compiler: expression transform', () => {
421421
})
422422
})
423423

424+
// #10807
425+
test('should not bail constant on strings w/ ()', () => {
426+
const node = parseWithExpressionTransform(
427+
`{{ { foo: 'ok()' } }}`,
428+
) as InterpolationNode
429+
expect(node.content).toMatchObject({
430+
constType: ConstantTypes.CAN_STRINGIFY,
431+
})
432+
})
433+
424434
describe('ES Proposals support', () => {
425435
test('bigInt', () => {
426436
const node = parseWithExpressionTransform(

packages/compiler-core/src/babelUtils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import type {
1010
} from '@babel/types'
1111
import { walk } from 'estree-walker'
1212

13+
/**
14+
* Return value indicates whether the AST walked can be a constant
15+
*/
1316
export function walkIdentifiers(
1417
root: Node,
1518
onIdentifier: (

packages/compiler-core/src/transforms/transformExpression.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ import { BindingTypes } from '../options'
4646

4747
const isLiteralWhitelisted = /*#__PURE__*/ makeMap('true,false,null,this')
4848

49-
// a heuristic safeguard to bail constant expressions on presence of
50-
// likely function invocation and member access
51-
const constantBailRE = /\w\s*\(|\.[^\d]/
52-
5349
export const transformExpression: NodeTransform = (node, context) => {
5450
if (node.type === NodeTypes.INTERPOLATION) {
5551
node.content = processExpression(
@@ -226,8 +222,6 @@ export function processExpression(
226222

227223
// fast path if expression is a simple identifier.
228224
const rawExp = node.content
229-
// bail constant on parens (function invocation) and dot (member access)
230-
const bailConstant = constantBailRE.test(rawExp)
231225

232226
let ast = node.ast
233227

@@ -317,7 +311,7 @@ export function processExpression(
317311
} else {
318312
// The identifier is considered constant unless it's pointing to a
319313
// local scope variable (a v-for alias, or a v-slot prop)
320-
if (!(needPrefix && isLocal) && !bailConstant) {
314+
if (!(needPrefix && isLocal)) {
321315
;(node as QualifiedId).isConstant = true
322316
}
323317
// also generate sub-expressions for other identifiers for better
@@ -371,9 +365,7 @@ export function processExpression(
371365
ret.ast = ast
372366
} else {
373367
ret = node
374-
ret.constType = bailConstant
375-
? ConstantTypes.NOT_CONSTANT
376-
: ConstantTypes.CAN_STRINGIFY
368+
ret.constType = ConstantTypes.CAN_STRINGIFY
377369
}
378370
ret.identifiers = Object.keys(knownIds)
379371
return ret

0 commit comments

Comments
 (0)