Skip to content

Commit f9826fa

Browse files
authored
fix(compiler-core/v-on): fix codegen for event handler with newlines (#1640)
1 parent fa5ddf8 commit f9826fa

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

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

+50
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,56 @@ describe('compiler: transform v-on', () => {
271271
})
272272
})
273273

274+
test('should NOT wrap as function if expression is already function expression (with newlines)', () => {
275+
const { node } = parseWithVOn(
276+
`<div @click="
277+
$event => {
278+
foo($event)
279+
}
280+
"/>`
281+
)
282+
expect((node.codegenNode as VNodeCall).props).toMatchObject({
283+
properties: [
284+
{
285+
key: { content: `onClick` },
286+
value: {
287+
type: NodeTypes.SIMPLE_EXPRESSION,
288+
content: `
289+
$event => {
290+
foo($event)
291+
}
292+
`
293+
}
294+
}
295+
]
296+
})
297+
})
298+
299+
test('should NOT wrap as function if expression is already function expression (with newlines + function keyword)', () => {
300+
const { node } = parseWithVOn(
301+
`<div @click="
302+
function($event) {
303+
foo($event)
304+
}
305+
"/>`
306+
)
307+
expect((node.codegenNode as VNodeCall).props).toMatchObject({
308+
properties: [
309+
{
310+
key: { content: `onClick` },
311+
value: {
312+
type: NodeTypes.SIMPLE_EXPRESSION,
313+
content: `
314+
function($event) {
315+
foo($event)
316+
}
317+
`
318+
}
319+
}
320+
]
321+
})
322+
})
323+
274324
test('should NOT wrap as function if expression is complex member expression', () => {
275325
const { node } = parseWithVOn(`<div @click="a['b' + c]"/>`)
276326
expect((node.codegenNode as VNodeCall).props).toMatchObject({

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { validateBrowserExpression } from '../validateExpression'
1616
import { isMemberExpression, hasScopeRef } from '../utils'
1717
import { CAPITALIZE } from '../runtimeHelpers'
1818

19-
const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function(?:\s+[\w$]+)?\s*\(/
19+
const fnExpRE = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^\s*function(?:\s+[\w$]+)?\s*\(/
2020

2121
export interface VOnDirectiveNode extends DirectiveNode {
2222
// v-on without arg is handled directly in ./transformElements.ts due to it affecting

0 commit comments

Comments
 (0)