refactor(v-on): do not generate persistent flag when no listener modifier is present

This commit is contained in:
Evan You 2019-10-14 23:06:51 -04:00
parent bd0bc3b3e6
commit c7074c703e
2 changed files with 21 additions and 21 deletions

View File

@ -37,13 +37,10 @@ describe('compiler-dom: transform v-on', () => {
}) })
expect(prop).toMatchObject({ expect(prop).toMatchObject({
type: NodeTypes.JS_PROPERTY, type: NodeTypes.JS_PROPERTY,
value: createObjectMatcher({ value: {
handler: {
callee: V_ON_MODIFIERS_GUARD, callee: V_ON_MODIFIERS_GUARD,
arguments: [{ content: '_ctx.test' }, '["stop","prevent"]'] arguments: [{ content: '_ctx.test' }, '["stop","prevent"]']
}, }
persistent: { content: 'true', isStatic: false }
})
}) })
}) })
@ -59,11 +56,11 @@ describe('compiler-dom: transform v-on', () => {
callee: V_ON_MODIFIERS_GUARD, callee: V_ON_MODIFIERS_GUARD,
arguments: [{ content: '_ctx.test' }, '["stop"]'] arguments: [{ content: '_ctx.test' }, '["stop"]']
}, },
persistent: { content: 'true', isStatic: false },
options: createObjectMatcher({ options: createObjectMatcher({
capture: { content: 'true', isStatic: false }, capture: { content: 'true', isStatic: false },
passive: { content: 'true', isStatic: false } passive: { content: 'true', isStatic: false }
}) }),
persistent: { content: 'true', isStatic: false }
}) })
}) })
}) })
@ -86,10 +83,10 @@ describe('compiler-dom: transform v-on', () => {
'["a"]' '["a"]'
] ]
}, },
persistent: { content: 'true', isStatic: false },
options: createObjectMatcher({ options: createObjectMatcher({
capture: { content: 'true', isStatic: false } capture: { content: 'true', isStatic: false }
}) }),
persistent: { content: 'true', isStatic: false }
}) })
}) })
}) })

View File

@ -5,7 +5,9 @@ import {
createCallExpression, createCallExpression,
createObjectExpression, createObjectExpression,
createSimpleExpression, createSimpleExpression,
NodeTypes NodeTypes,
CallExpression,
ObjectExpression
} from '@vue/compiler-core' } from '@vue/compiler-core'
import { V_ON_MODIFIERS_GUARD, V_ON_KEYS_GUARD } from '../runtimeHelpers' import { V_ON_MODIFIERS_GUARD, V_ON_KEYS_GUARD } from '../runtimeHelpers'
@ -32,6 +34,7 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
const { modifiers } = dir const { modifiers } = dir
const baseResult = baseTransform(dir, node, context) const baseResult = baseTransform(dir, node, context)
if (!modifiers.length) return baseResult if (!modifiers.length) return baseResult
const { key, value } = baseResult.props[0] const { key, value } = baseResult.props[0]
const runtimeModifiers = modifiers.filter(m => !(m in EVENT_OPTION_MODIFIERS)) const runtimeModifiers = modifiers.filter(m => !(m in EVENT_OPTION_MODIFIERS))
let handler = createCallExpression(context.helper(V_ON_MODIFIERS_GUARD), [ let handler = createCallExpression(context.helper(V_ON_MODIFIERS_GUARD), [
@ -49,17 +52,15 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
JSON.stringify(runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS))) JSON.stringify(runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS)))
]) ])
} }
const properties = [
createObjectProperty('handler', handler), let returnExp: CallExpression | ObjectExpression = handler
// so the runtime knows the options never change
createObjectProperty('persistent', createSimpleExpression('true', false))
]
const eventOptionModifiers = modifiers.filter( const eventOptionModifiers = modifiers.filter(
modifier => modifier in EVENT_OPTION_MODIFIERS modifier => modifier in EVENT_OPTION_MODIFIERS
) )
if (eventOptionModifiers.length) { if (eventOptionModifiers.length) {
properties.push( returnExp = createObjectExpression([
createObjectProperty('handler', handler),
createObjectProperty( createObjectProperty(
'options', 'options',
createObjectExpression( createObjectExpression(
@ -70,12 +71,14 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
) )
) )
) )
) ),
) // so the runtime knows the options never change
createObjectProperty('persistent', createSimpleExpression('true', false))
])
} }
return { return {
props: [createObjectProperty(key, createObjectExpression(properties))], props: [createObjectProperty(key, returnExp)],
needRuntime: false needRuntime: false
} }
} }