Skip to content

Commit c7074c7

Browse files
committed
refactor(v-on): do not generate persistent flag when no listener modifier is present
1 parent bd0bc3b commit c7074c7

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

packages/compiler-dom/__tests__/transforms/vOn.spec.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ describe('compiler-dom: transform v-on', () => {
3737
})
3838
expect(prop).toMatchObject({
3939
type: NodeTypes.JS_PROPERTY,
40-
value: createObjectMatcher({
41-
handler: {
42-
callee: V_ON_MODIFIERS_GUARD,
43-
arguments: [{ content: '_ctx.test' }, '["stop","prevent"]']
44-
},
45-
persistent: { content: 'true', isStatic: false }
46-
})
40+
value: {
41+
callee: V_ON_MODIFIERS_GUARD,
42+
arguments: [{ content: '_ctx.test' }, '["stop","prevent"]']
43+
}
4744
})
4845
})
4946

@@ -59,11 +56,11 @@ describe('compiler-dom: transform v-on', () => {
5956
callee: V_ON_MODIFIERS_GUARD,
6057
arguments: [{ content: '_ctx.test' }, '["stop"]']
6158
},
62-
persistent: { content: 'true', isStatic: false },
6359
options: createObjectMatcher({
6460
capture: { content: 'true', isStatic: false },
6561
passive: { content: 'true', isStatic: false }
66-
})
62+
}),
63+
persistent: { content: 'true', isStatic: false }
6764
})
6865
})
6966
})
@@ -86,10 +83,10 @@ describe('compiler-dom: transform v-on', () => {
8683
'["a"]'
8784
]
8885
},
89-
persistent: { content: 'true', isStatic: false },
9086
options: createObjectMatcher({
9187
capture: { content: 'true', isStatic: false }
92-
})
88+
}),
89+
persistent: { content: 'true', isStatic: false }
9390
})
9491
})
9592
})

packages/compiler-dom/src/transforms/vOn.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
createCallExpression,
66
createObjectExpression,
77
createSimpleExpression,
8-
NodeTypes
8+
NodeTypes,
9+
CallExpression,
10+
ObjectExpression
911
} from '@vue/compiler-core'
1012
import { V_ON_MODIFIERS_GUARD, V_ON_KEYS_GUARD } from '../runtimeHelpers'
1113

@@ -32,6 +34,7 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
3234
const { modifiers } = dir
3335
const baseResult = baseTransform(dir, node, context)
3436
if (!modifiers.length) return baseResult
37+
3538
const { key, value } = baseResult.props[0]
3639
const runtimeModifiers = modifiers.filter(m => !(m in EVENT_OPTION_MODIFIERS))
3740
let handler = createCallExpression(context.helper(V_ON_MODIFIERS_GUARD), [
@@ -49,17 +52,15 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
4952
JSON.stringify(runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS)))
5053
])
5154
}
52-
const properties = [
53-
createObjectProperty('handler', handler),
54-
// so the runtime knows the options never change
55-
createObjectProperty('persistent', createSimpleExpression('true', false))
56-
]
55+
56+
let returnExp: CallExpression | ObjectExpression = handler
5757

5858
const eventOptionModifiers = modifiers.filter(
5959
modifier => modifier in EVENT_OPTION_MODIFIERS
6060
)
6161
if (eventOptionModifiers.length) {
62-
properties.push(
62+
returnExp = createObjectExpression([
63+
createObjectProperty('handler', handler),
6364
createObjectProperty(
6465
'options',
6566
createObjectExpression(
@@ -70,12 +71,14 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
7071
)
7172
)
7273
)
73-
)
74-
)
74+
),
75+
// so the runtime knows the options never change
76+
createObjectProperty('persistent', createSimpleExpression('true', false))
77+
])
7578
}
7679

7780
return {
78-
props: [createObjectProperty(key, createObjectExpression(properties))],
81+
props: [createObjectProperty(key, returnExp)],
7982
needRuntime: false
8083
}
8184
}

0 commit comments

Comments
 (0)