Skip to content

Commit 9bb5cfa

Browse files
committed
fix(compiler-vapor): typed expressions should be wrapped in parentheses
1 parent 1ef6e6e commit 9bb5cfa

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ export function render(_ctx, $props, $emit, $attrs, $slots) {
123123
}"
124124
`;
125125
126+
exports[`v-on > expression with type 1`] = `
127+
"import { delegateEvents as _delegateEvents, template as _template } from 'vue';
128+
const t0 = _template("<div></div>", true)
129+
_delegateEvents("click")
130+
131+
export function render(_ctx, $props, $emit, $attrs, $slots) {
132+
const n0 = t0()
133+
n0.$evtclick = e => (_ctx.handleClick as any)(e)
134+
return n0
135+
}"
136+
`;
137+
126138
exports[`v-on > function expression w/ prefixIdentifiers: true 1`] = `
127139
"import { delegateEvents as _delegateEvents, template as _template } from 'vue';
128140
const t0 = _template("<div></div>", true)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,14 @@ describe('v-on', () => {
682682
'_delegate(n0, "click", _withModifiers(e => _ctx.test(e), ["stop"]))',
683683
)
684684
})
685+
686+
test('expression with type', () => {
687+
const { code } = compileWithVOn(`<div @click="handleClick as any"></div>`, {
688+
bindingMetadata: {
689+
handleClick: BindingTypes.SETUP_CONST,
690+
},
691+
})
692+
expect(code).matchSnapshot()
693+
expect(code).include('n0.$evtclick = e => (_ctx.handleClick as any)(e)')
694+
})
685695
})

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
NewlineType,
1111
type SimpleExpressionNode,
1212
type SourceLocation,
13+
TS_NODE_TYPES,
1314
advancePositionWithClone,
1415
createSimpleExpression,
1516
isInDestructureAssignment,
@@ -48,6 +49,7 @@ export function genExpression(
4849
return genIdentifier(content, context, loc, assignment)
4950
}
5051

52+
const shouldWrap = ast && TS_NODE_TYPES.includes(ast.type)
5153
const ids: Identifier[] = []
5254
const parentStackMap = new Map<Identifier, Node[]>()
5355
const parentStack: Node[] = []
@@ -85,6 +87,7 @@ export function genExpression(
8587
parent.type === 'OptionalMemberExpression')
8688

8789
push(
90+
shouldWrap ? '(' : '',
8891
...genIdentifier(
8992
source,
9093
context,
@@ -103,6 +106,7 @@ export function genExpression(
103106
if (i === ids.length - 1 && end < content.length) {
104107
push([content.slice(end), NewlineType.Unknown])
105108
}
109+
push(shouldWrap ? ')' : '')
106110
})
107111

108112
if (assignment && hasMemberExpression) {

0 commit comments

Comments
 (0)