feat(compiler-core/v-on): support @vnode-xxx usage for vnode hooks

This commit is contained in:
Evan You 2020-02-10 11:00:37 -05:00
parent 5495c70c4a
commit 571ed4226b
2 changed files with 21 additions and 6 deletions

View File

@ -300,6 +300,20 @@ describe('compiler: transform v-on', () => {
expect(onError).not.toHaveBeenCalled() expect(onError).not.toHaveBeenCalled()
}) })
test('case conversion for vnode hooks', () => {
const { node } = parseWithVOn(`<div v-on:vnode-mounted="onMount"/>`)
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: {
content: `onVnodeMounted`
},
value: {
content: `onMount`
}
})
})
describe('cacheHandler', () => { describe('cacheHandler', () => {
test('empty handler', () => { test('empty handler', () => {
const { root, node } = parseWithVOn(`<div v-on:click.prevent />`, { const { root, node } = parseWithVOn(`<div v-on:click.prevent />`, {

View File

@ -8,7 +8,7 @@ import {
createCompoundExpression, createCompoundExpression,
SimpleExpressionNode SimpleExpressionNode
} from '../ast' } from '../ast'
import { capitalize } from '@vue/shared' import { capitalize, camelize } from '@vue/shared'
import { createCompilerError, ErrorCodes } from '../errors' import { createCompilerError, ErrorCodes } from '../errors'
import { processExpression } from './transformExpression' import { processExpression } from './transformExpression'
import { isMemberExpression, hasScopeRef } from '../utils' import { isMemberExpression, hasScopeRef } from '../utils'
@ -38,11 +38,12 @@ export const transformOn: DirectiveTransform = (
let eventName: ExpressionNode let eventName: ExpressionNode
if (arg.type === NodeTypes.SIMPLE_EXPRESSION) { if (arg.type === NodeTypes.SIMPLE_EXPRESSION) {
if (arg.isStatic) { if (arg.isStatic) {
eventName = createSimpleExpression( const rawName = arg.content
`on${capitalize(arg.content)}`, // for @vnode-xxx event listeners, auto convert it to camelCase
true, const normalizedName = rawName.startsWith(`vnode`)
arg.loc ? capitalize(camelize(rawName))
) : capitalize(rawName)
eventName = createSimpleExpression(`on${normalizedName}`, true, arg.loc)
} else { } else {
eventName = createCompoundExpression([`"on" + (`, arg, `)`]) eventName = createCompoundExpression([`"on" + (`, arg, `)`])
} }