Skip to content

Commit

Permalink
Merge branch 'main' into fix/#10827
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 authored Apr 29, 2024
2 parents 9f619d2 + 09b4df8 commit 3dea80b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 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
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const initSlots = (
if (type) {
extend(slots, children as InternalSlots)
// make compiler marker non-enumerable
def(slots, '_', type)
def(slots, '_', type, true)
} else {
normalizeObjectSlots(children as RawSlots, slots, instance)
}
Expand Down
8 changes: 7 additions & 1 deletion packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ export const invokeArrayFns = (fns: Function[], arg?: any) => {
}
}

export const def = (obj: object, key: string | symbol, value: any) => {
export const def = (
obj: object,
key: string | symbol,
value: any,
writable = false,
) => {
Object.defineProperty(obj, key, {
configurable: true,
enumerable: false,
writable,
value,
})
}
Expand Down

0 comments on commit 3dea80b

Please sign in to comment.