2019-10-19 09:51:34 +08:00
|
|
|
import { DirectiveTransform, DirectiveTransformResult } from '../transform'
|
2019-09-27 23:42:02 +08:00
|
|
|
import {
|
2019-10-14 12:33:23 +08:00
|
|
|
DirectiveNode,
|
2019-09-27 23:42:02 +08:00
|
|
|
createObjectProperty,
|
|
|
|
createSimpleExpression,
|
|
|
|
ExpressionNode,
|
|
|
|
NodeTypes,
|
2019-10-04 05:47:00 +08:00
|
|
|
createCompoundExpression,
|
|
|
|
SimpleExpressionNode
|
2019-09-27 23:42:02 +08:00
|
|
|
} 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-10-04 05:47:00 +08:00
|
|
|
import { processExpression } from './transformExpression'
|
2019-10-19 09:51:34 +08:00
|
|
|
import { isMemberExpression, hasScopeRef } from '../utils'
|
2019-10-04 05:47:00 +08:00
|
|
|
|
|
|
|
const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function(?:\s+[\w$]+)?\s*\(/
|
2019-09-23 10:19:42 +08:00
|
|
|
|
2019-10-14 12:33:23 +08:00
|
|
|
export interface VOnDirectiveNode extends DirectiveNode {
|
2019-10-29 00:04:04 +08:00
|
|
|
// v-on without arg is handled directly in ./transformElements.ts due to it affecting
|
2019-10-14 12:33:23 +08:00
|
|
|
// codegen for the entire props object. This transform here is only for v-on
|
|
|
|
// *with* args.
|
|
|
|
arg: ExpressionNode
|
|
|
|
// exp is guaranteed to be a simple expression here because v-on w/ arg is
|
|
|
|
// skipped by transformExpression as a special case.
|
|
|
|
exp: SimpleExpressionNode | undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
export const transformOn: DirectiveTransform = (
|
|
|
|
dir: VOnDirectiveNode,
|
|
|
|
node,
|
2019-10-19 09:51:34 +08:00
|
|
|
context,
|
|
|
|
augmentor
|
2019-10-14 12:33:23 +08:00
|
|
|
) => {
|
|
|
|
const { loc, modifiers, arg } = dir
|
2019-10-04 05:47:00 +08:00
|
|
|
if (!dir.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 {
|
2019-10-02 00:25:13 +08:00
|
|
|
eventName = createCompoundExpression([`"on" + (`, arg, `)`])
|
2019-09-27 23:42:02 +08:00
|
|
|
}
|
2019-09-25 10:39:20 +08:00
|
|
|
} else {
|
2019-10-05 11:12:49 +08:00
|
|
|
// already a compound expression.
|
2019-09-27 23:42:02 +08:00
|
|
|
eventName = arg
|
|
|
|
eventName.children.unshift(`"on" + (`)
|
|
|
|
eventName.children.push(`)`)
|
2019-09-25 10:39:20 +08:00
|
|
|
}
|
2019-10-04 05:47:00 +08:00
|
|
|
|
|
|
|
// handler processing
|
2019-10-14 12:33:23 +08:00
|
|
|
let exp: ExpressionNode | undefined = dir.exp
|
2019-10-19 09:51:34 +08:00
|
|
|
let isCacheable: boolean = !exp
|
2019-10-14 12:33:23 +08:00
|
|
|
if (exp) {
|
2019-10-19 09:51:34 +08:00
|
|
|
const isMemberExp = isMemberExpression(exp.content)
|
|
|
|
const isInlineStatement = !(isMemberExp || fnExpRE.test(exp.content))
|
2020-01-07 00:45:48 +08:00
|
|
|
const hasMultipleStatements = exp.content.includes(`;`)
|
2019-10-19 09:51:34 +08:00
|
|
|
|
2019-10-04 05:47:00 +08:00
|
|
|
// process the expression since it's been skipped
|
|
|
|
if (!__BROWSER__ && context.prefixIdentifiers) {
|
|
|
|
context.addIdentifiers(`$event`)
|
2020-01-07 00:45:48 +08:00
|
|
|
exp = processExpression(exp, context, false, hasMultipleStatements)
|
2019-10-04 05:47:00 +08:00
|
|
|
context.removeIdentifiers(`$event`)
|
2019-10-19 09:51:34 +08:00
|
|
|
// with scope analysis, the function is hoistable if it has no reference
|
|
|
|
// to scope variables.
|
|
|
|
isCacheable =
|
|
|
|
context.cacheHandlers && !hasScopeRef(exp, context.identifiers)
|
|
|
|
// If the expression is optimizable and is a member expression pointing
|
|
|
|
// to a function, turn it into invocation (and wrap in an arrow function
|
|
|
|
// below) so that it always accesses the latest value when called - thus
|
|
|
|
// avoiding the need to be patched.
|
|
|
|
if (isCacheable && isMemberExp) {
|
|
|
|
if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
|
|
exp.content += `($event)`
|
|
|
|
} else {
|
|
|
|
exp.children.push(`($event)`)
|
|
|
|
}
|
|
|
|
}
|
2019-10-04 05:47:00 +08:00
|
|
|
}
|
2019-10-19 09:51:34 +08:00
|
|
|
|
|
|
|
if (isInlineStatement || (isCacheable && isMemberExp)) {
|
2019-10-04 05:47:00 +08:00
|
|
|
// wrap inline statement in a function expression
|
|
|
|
exp = createCompoundExpression([
|
2020-01-07 00:45:48 +08:00
|
|
|
`$event => ${hasMultipleStatements ? `{` : `(`}`,
|
2020-02-06 03:23:03 +08:00
|
|
|
exp,
|
2020-01-07 00:45:48 +08:00
|
|
|
hasMultipleStatements ? `}` : `)`
|
2019-10-04 05:47:00 +08:00
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 09:51:34 +08:00
|
|
|
let ret: DirectiveTransformResult = {
|
2019-10-11 06:02:51 +08:00
|
|
|
props: [
|
|
|
|
createObjectProperty(
|
|
|
|
eventName,
|
2019-10-14 12:33:23 +08:00
|
|
|
exp || createSimpleExpression(`() => {}`, false, loc)
|
2019-10-11 06:02:51 +08:00
|
|
|
)
|
2020-02-05 01:20:51 +08:00
|
|
|
]
|
2019-09-23 10:19:42 +08:00
|
|
|
}
|
2019-10-19 09:51:34 +08:00
|
|
|
|
|
|
|
// apply extended compiler augmentor
|
|
|
|
if (augmentor) {
|
|
|
|
ret = augmentor(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCacheable) {
|
|
|
|
// cache handlers so that it's always the same handler being passed down.
|
2019-10-30 01:51:48 +08:00
|
|
|
// this avoids unnecessary re-renders when users use inline handlers on
|
2019-10-19 09:51:34 +08:00
|
|
|
// components.
|
|
|
|
ret.props[0].value = context.cache(ret.props[0].value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
2019-09-23 10:19:42 +08:00
|
|
|
}
|