Skip to content

Commit

Permalink
fix(compiler-core/v-on): fix codegen for event handler with newlines (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang authored Jul 19, 2020
1 parent fa5ddf8 commit f9826fa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 50 additions & 0 deletions packages/compiler-core/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,56 @@ describe('compiler: transform v-on', () => {
})
})

test('should NOT wrap as function if expression is already function expression (with newlines)', () => {
const { node } = parseWithVOn(
`<div @click="
$event => {
foo($event)
}
"/>`
)
expect((node.codegenNode as VNodeCall).props).toMatchObject({
properties: [
{
key: { content: `onClick` },
value: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `
$event => {
foo($event)
}
`
}
}
]
})
})

test('should NOT wrap as function if expression is already function expression (with newlines + function keyword)', () => {
const { node } = parseWithVOn(
`<div @click="
function($event) {
foo($event)
}
"/>`
)
expect((node.codegenNode as VNodeCall).props).toMatchObject({
properties: [
{
key: { content: `onClick` },
value: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `
function($event) {
foo($event)
}
`
}
}
]
})
})

test('should NOT wrap as function if expression is complex member expression', () => {
const { node } = parseWithVOn(`<div @click="a['b' + c]"/>`)
expect((node.codegenNode as VNodeCall).props).toMatchObject({
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-core/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { validateBrowserExpression } from '../validateExpression'
import { isMemberExpression, hasScopeRef } from '../utils'
import { CAPITALIZE } from '../runtimeHelpers'

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

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

0 comments on commit f9826fa

Please sign in to comment.