Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ _delegateEvents("click")

export function render(_ctx, $props, $emit, $attrs, $slots) {
const n0 = t0()
n0.$evtclick = e => _ctx.handleClick(e)
n0.$evtclick = e => (_ctx.foo[_ctx.handleClick] as any)(e)
return n0
}"
`;
Expand Down
6 changes: 4 additions & 2 deletions packages/compiler-vapor/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,17 @@ describe('v-on', () => {

test('expression with type', () => {
const { code } = compileWithVOn(
`<div @click="(<number>handleClick as any)"></div>`,
`<div @click="foo[handleClick] as any"></div>`,
{
bindingMetadata: {
handleClick: BindingTypes.SETUP_CONST,
},
},
)
expect(code).matchSnapshot()
expect(code).include('n0.$evtclick = e => _ctx.handleClick(e)')
expect(code).include(
'n0.$evtclick = e => (_ctx.foo[_ctx.handleClick] as any)(e)',
)
})

test('component event with special characters', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/compiler-vapor/src/generators/event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BindingTypes,
type SimpleExpressionNode,
TS_NODE_TYPES,
isFnExpression,
isMemberExpression,
} from '@vue/compiler-dom'
Expand Down Expand Up @@ -126,7 +127,14 @@ export function genEventHandler(
// non constant, wrap with invocation as `e => foo.bar(e)`
// when passing as component handler, access is always dynamic so we
// can skip this
handlerExp = [`e => `, ...handlerExp, `(e)`]
const isTSNode = value.ast && TS_NODE_TYPES.includes(value.ast.type)
handlerExp = [
`e => `,
isTSNode ? '(' : '',
...handlerExp,
isTSNode ? ')' : '',
`(e)`,
]
}
} else if (isFnExpression(value, context.options)) {
// Fn expression: @click="e => foo(e)"
Expand Down
11 changes: 3 additions & 8 deletions packages/compiler-vapor/src/generators/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
NewlineType,
type SimpleExpressionNode,
type SourceLocation,
TS_NODE_TYPES,
advancePositionWithClone,
createSimpleExpression,
isInDestructureAssignment,
Expand Down Expand Up @@ -64,7 +63,6 @@ export function genExpression(
let hasMemberExpression = false
if (ids.length) {
const [frag, push] = buildCodeFragment()
const isTSNode = ast && TS_NODE_TYPES.includes(ast.type)
ids
.sort((a, b) => a.start! - b.start!)
.forEach((id, i) => {
Expand All @@ -73,11 +71,8 @@ export function genExpression(
const end = id.end! - 1
const last = ids[i - 1]

if (!(isTSNode && i === 0)) {
const leadingText = content.slice(last ? last.end! - 1 : 0, start)
if (leadingText.length) push([leadingText, NewlineType.Unknown])
}

const leadingText = content.slice(last ? last.end! - 1 : 0, start)
if (leadingText.length) push([leadingText, NewlineType.Unknown])
const source = content.slice(start, end)
const parentStack = parentStackMap.get(id)!
const parent = parentStack[parentStack.length - 1]
Expand All @@ -103,7 +98,7 @@ export function genExpression(
),
)

if (i === ids.length - 1 && end < content.length && !isTSNode) {
if (i === ids.length - 1 && end < content.length) {
push([content.slice(end), NewlineType.Unknown])
}
})
Expand Down
Loading