fix(compiler-core): fix hoisting logic for elements with cached handlers + other bindings
fix #4327
This commit is contained in:
parent
5b3f1e8424
commit
a6c1db2728
@ -211,6 +211,21 @@ return function render(_ctx, _cache) {
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist elements with cached handlers + other bindings 1`] = `
|
||||||
|
"import { normalizeClass as _normalizeClass, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
||||||
|
|
||||||
|
export function render(_ctx, _cache) {
|
||||||
|
return (_openBlock(), _createElementBlock(\\"div\\", null, [
|
||||||
|
_createElementVNode(\\"div\\", null, [
|
||||||
|
_createElementVNode(\\"div\\", {
|
||||||
|
class: _normalizeClass({}),
|
||||||
|
onClick: _cache[0] || (_cache[0] = (...args) => (_ctx.foo && _ctx.foo(...args)))
|
||||||
|
})
|
||||||
|
])
|
||||||
|
]))
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist elements with cached handlers 1`] = `
|
exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist elements with cached handlers 1`] = `
|
||||||
"import { createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
"import { createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
||||||
|
|
||||||
|
@ -590,6 +590,25 @@ describe('compiler: hoistStatic transform', () => {
|
|||||||
).toMatchSnapshot()
|
).toMatchSnapshot()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('should NOT hoist elements with cached handlers + other bindings', () => {
|
||||||
|
const root = transformWithHoist(
|
||||||
|
`<div><div><div :class="{}" @click="foo"/></div></div>`,
|
||||||
|
{
|
||||||
|
prefixIdentifiers: true,
|
||||||
|
cacheHandlers: true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(root.cached).toBe(1)
|
||||||
|
expect(root.hoists.length).toBe(0)
|
||||||
|
expect(
|
||||||
|
generate(root, {
|
||||||
|
mode: 'module',
|
||||||
|
prefixIdentifiers: true
|
||||||
|
}).code
|
||||||
|
).toMatchSnapshot()
|
||||||
|
})
|
||||||
|
|
||||||
test('should NOT hoist keyed template v-for with plain element child', () => {
|
test('should NOT hoist keyed template v-for with plain element child', () => {
|
||||||
const root = transformWithHoist(
|
const root = transformWithHoist(
|
||||||
`<div><template v-for="item in items" :key="item"><span/></template></div>`
|
`<div><template v-for="item in items" :key="item"><span/></template></div>`
|
||||||
|
@ -335,16 +335,17 @@ function getGeneratedPropsConstantType(
|
|||||||
if (keyType < returnType) {
|
if (keyType < returnType) {
|
||||||
returnType = keyType
|
returnType = keyType
|
||||||
}
|
}
|
||||||
if (value.type !== NodeTypes.SIMPLE_EXPRESSION) {
|
let valueType: ConstantTypes
|
||||||
|
if (value.type === NodeTypes.SIMPLE_EXPRESSION) {
|
||||||
|
valueType = getConstantType(value, context)
|
||||||
|
} else if (value.type === NodeTypes.JS_CALL_EXPRESSION) {
|
||||||
// some helper calls can be hoisted,
|
// some helper calls can be hoisted,
|
||||||
// such as the `normalizeProps` generated by the compiler for pre-normalize class,
|
// such as the `normalizeProps` generated by the compiler for pre-normalize class,
|
||||||
// in this case we need to respect the ConstanType of the helper's argments
|
// in this case we need to respect the ConstanType of the helper's argments
|
||||||
if (value.type === NodeTypes.JS_CALL_EXPRESSION) {
|
valueType = getConstantTypeOfHelperCall(value, context)
|
||||||
return getConstantTypeOfHelperCall(value, context)
|
} else {
|
||||||
|
valueType = ConstantTypes.NOT_CONSTANT
|
||||||
}
|
}
|
||||||
return ConstantTypes.NOT_CONSTANT
|
|
||||||
}
|
|
||||||
const valueType = getConstantType(value, context)
|
|
||||||
if (valueType === ConstantTypes.NOT_CONSTANT) {
|
if (valueType === ConstantTypes.NOT_CONSTANT) {
|
||||||
return valueType
|
return valueType
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user