2019-09-23 10:19:42 +08:00
|
|
|
import { DirectiveTransform } from '../transform'
|
2019-09-27 23:42:02 +08:00
|
|
|
import {
|
|
|
|
createObjectProperty,
|
|
|
|
createSimpleExpression,
|
|
|
|
ExpressionNode,
|
|
|
|
NodeTypes,
|
|
|
|
createCompoundExpression
|
|
|
|
} from '../ast'
|
2019-09-23 10:19:42 +08:00
|
|
|
import { capitalize } from '@vue/shared'
|
2019-09-25 10:39:20 +08:00
|
|
|
import { createCompilerError, ErrorCodes } from '../errors'
|
2019-09-23 10:19:42 +08:00
|
|
|
|
|
|
|
// v-on without arg is handled directly in ./element.ts due to it affecting
|
|
|
|
// codegen for the entire props object. This transform here is only for v-on
|
|
|
|
// *with* args.
|
2019-09-27 23:42:02 +08:00
|
|
|
export const transformOn: DirectiveTransform = (dir, context) => {
|
|
|
|
const { exp, loc, modifiers } = dir
|
|
|
|
const arg = dir.arg!
|
2019-09-25 10:56:57 +08:00
|
|
|
if (!exp && !modifiers.length) {
|
2019-09-25 10:39:20 +08:00
|
|
|
context.onError(createCompilerError(ErrorCodes.X_V_ON_NO_EXPRESSION, loc))
|
|
|
|
}
|
|
|
|
let eventName: ExpressionNode
|
2019-09-27 23:42:02 +08:00
|
|
|
if (arg.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
|
|
if (arg.isStatic) {
|
|
|
|
eventName = createSimpleExpression(
|
|
|
|
`on${capitalize(arg.content)}`,
|
|
|
|
true,
|
|
|
|
arg.loc
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
eventName = createCompoundExpression([`"on" + (`, arg, `)`], arg.loc)
|
|
|
|
}
|
2019-09-25 10:39:20 +08:00
|
|
|
} else {
|
2019-09-27 23:42:02 +08:00
|
|
|
// already a compound epxression.
|
|
|
|
eventName = arg
|
|
|
|
eventName.children.unshift(`"on" + (`)
|
|
|
|
eventName.children.push(`)`)
|
2019-09-25 10:39:20 +08:00
|
|
|
}
|
2019-09-23 10:19:42 +08:00
|
|
|
// TODO .once modifier handling since it is platform agnostic
|
|
|
|
// other modifiers are handled in compiler-dom
|
|
|
|
return {
|
|
|
|
props: createObjectProperty(
|
|
|
|
eventName,
|
2019-09-27 23:42:02 +08:00
|
|
|
exp || createSimpleExpression(`() => {}`, false, loc),
|
2019-09-25 10:03:28 +08:00
|
|
|
loc
|
2019-09-23 10:19:42 +08:00
|
|
|
),
|
|
|
|
needRuntime: false
|
|
|
|
}
|
|
|
|
}
|