MCPcopy
hub / github.com/vuejs/core / transformOn

Function transformOn

packages/compiler-core/src/transforms/vOn.ts:29–180  ·  view source on GitHub ↗
(
  dir,
  node,
  context,
  augmentor,
)

Source from the content-addressed store, hash-verified

27}
28
29export const transformOn: DirectiveTransform = (
30 dir,
31 node,
32 context,
33 augmentor,
34) => {
35 const { loc, modifiers, arg } = dir as VOnDirectiveNode
36 if (!dir.exp && !modifiers.length) {
37 context.onError(createCompilerError(ErrorCodes.X_V_ON_NO_EXPRESSION, loc))
38 }
39 let eventName: ExpressionNode
40 if (arg.type === NodeTypes.SIMPLE_EXPRESSION) {
41 if (arg.isStatic) {
42 let rawName = arg.content
43 if (__DEV__ && rawName.startsWith('vnode')) {
44 context.onError(createCompilerError(ErrorCodes.X_VNODE_HOOKS, arg.loc))
45 }
46 if (rawName.startsWith('vue:')) {
47 rawName = `vnode-${rawName.slice(4)}`
48 }
49 const eventString =
50 node.tagType !== ElementTypes.ELEMENT ||
51 rawName.startsWith('vnode') ||
52 !/[A-Z]/.test(rawName)
53 ? // for non-element and vnode lifecycle event listeners, auto convert
54 // it to camelCase. See issue #2249
55 toHandlerKey(camelize(rawName))
56 : // preserve case for plain element listeners that have uppercase
57 // letters, as these may be custom elements' custom events
58 `on:${rawName}`
59 eventName = createSimpleExpression(eventString, true, arg.loc)
60 } else {
61 // #2388
62 eventName = createCompoundExpression([
63 `${context.helperString(TO_HANDLER_KEY)}(`,
64 arg,
65 `)`,
66 ])
67 }
68 } else {
69 // already a compound expression.
70 eventName = arg
71 eventName.children.unshift(`${context.helperString(TO_HANDLER_KEY)}(`)
72 eventName.children.push(`)`)
73 }
74
75 // handler processing
76 let exp: ExpressionNode | undefined = dir.exp as
77 | SimpleExpressionNode
78 | undefined
79 if (exp && !exp.content.trim()) {
80 exp = undefined
81 }
82 let shouldCache: boolean = context.cacheHandlers && !exp && !context.inVOnce
83 if (exp) {
84 const isMemberExp = isMemberExpression(exp, context)
85 const isInlineStatement = !(isMemberExp || isFnExpression(exp, context))
86 const hasMultipleStatements = exp.content.includes(`;`)

Callers

nothing calls this directly

Calls 14

createCompilerErrorFunction · 0.90
createSimpleExpressionFunction · 0.90
createCompoundExpressionFunction · 0.90
processExpressionFunction · 0.90
hasScopeRefFunction · 0.90
createObjectPropertyFunction · 0.90
testMethod · 0.80
helperStringMethod · 0.80
addIdentifiersMethod · 0.80
removeIdentifiersMethod · 0.80
cacheMethod · 0.80

Tested by

no test coverage detected