Skip to content

Commit 0f4edb4

Browse files
authored
fix(compiler-vapor): wrap event handler in parentheses for TSExpression (#14061)
1 parent 1134405 commit 0f4edb4

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vOn.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ _delegateEvents("click")
145145
146146
export function render(_ctx, $props, $emit, $attrs, $slots) {
147147
const n0 = t0()
148-
n0.$evtclick = e => _ctx.handleClick(e)
148+
n0.$evtclick = e => (_ctx.foo[_ctx.handleClick] as any)(e)
149149
return n0
150150
}"
151151
`;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,15 +685,17 @@ describe('v-on', () => {
685685

686686
test('expression with type', () => {
687687
const { code } = compileWithVOn(
688-
`<div @click="(<number>handleClick as any)"></div>`,
688+
`<div @click="foo[handleClick] as any"></div>`,
689689
{
690690
bindingMetadata: {
691691
handleClick: BindingTypes.SETUP_CONST,
692692
},
693693
},
694694
)
695695
expect(code).matchSnapshot()
696-
expect(code).include('n0.$evtclick = e => _ctx.handleClick(e)')
696+
expect(code).include(
697+
'n0.$evtclick = e => (_ctx.foo[_ctx.handleClick] as any)(e)',
698+
)
697699
})
698700

699701
test('component event with special characters', () => {

packages/compiler-vapor/src/generators/event.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
BindingTypes,
33
type SimpleExpressionNode,
4+
TS_NODE_TYPES,
45
isFnExpression,
56
isMemberExpression,
67
} from '@vue/compiler-dom'
@@ -126,7 +127,14 @@ export function genEventHandler(
126127
// non constant, wrap with invocation as `e => foo.bar(e)`
127128
// when passing as component handler, access is always dynamic so we
128129
// can skip this
129-
handlerExp = [`e => `, ...handlerExp, `(e)`]
130+
const isTSNode = value.ast && TS_NODE_TYPES.includes(value.ast.type)
131+
handlerExp = [
132+
`e => `,
133+
isTSNode ? '(' : '',
134+
...handlerExp,
135+
isTSNode ? ')' : '',
136+
`(e)`,
137+
]
130138
}
131139
} else if (isFnExpression(value, context.options)) {
132140
// Fn expression: @click="e => foo(e)"

packages/compiler-vapor/src/generators/expression.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
NewlineType,
1111
type SimpleExpressionNode,
1212
type SourceLocation,
13-
TS_NODE_TYPES,
1413
advancePositionWithClone,
1514
createSimpleExpression,
1615
isInDestructureAssignment,
@@ -64,7 +63,6 @@ export function genExpression(
6463
let hasMemberExpression = false
6564
if (ids.length) {
6665
const [frag, push] = buildCodeFragment()
67-
const isTSNode = ast && TS_NODE_TYPES.includes(ast.type)
6866
ids
6967
.sort((a, b) => a.start! - b.start!)
7068
.forEach((id, i) => {
@@ -73,11 +71,8 @@ export function genExpression(
7371
const end = id.end! - 1
7472
const last = ids[i - 1]
7573

76-
if (!(isTSNode && i === 0)) {
77-
const leadingText = content.slice(last ? last.end! - 1 : 0, start)
78-
if (leadingText.length) push([leadingText, NewlineType.Unknown])
79-
}
80-
74+
const leadingText = content.slice(last ? last.end! - 1 : 0, start)
75+
if (leadingText.length) push([leadingText, NewlineType.Unknown])
8176
const source = content.slice(start, end)
8277
const parentStack = parentStackMap.get(id)!
8378
const parent = parentStack[parentStack.length - 1]
@@ -103,7 +98,7 @@ export function genExpression(
10398
),
10499
)
105100

106-
if (i === ids.length - 1 && end < content.length && !isTSNode) {
101+
if (i === ids.length - 1 && end < content.length) {
107102
push([content.slice(end), NewlineType.Unknown])
108103
}
109104
})

0 commit comments

Comments
 (0)