2019-09-23 02:19:42 +00:00
|
|
|
import { DirectiveTransform } from '../transform'
|
2019-09-27 15:42:02 +00:00
|
|
|
import { createObjectProperty, createSimpleExpression, NodeTypes } from '../ast'
|
2019-09-23 02:19:42 +00:00
|
|
|
import { createCompilerError, ErrorCodes } from '../errors'
|
2019-09-25 02:03:28 +00:00
|
|
|
import { camelize } from '@vue/shared'
|
2019-09-27 15:42:02 +00:00
|
|
|
import { CAMELIZE } from '../runtimeConstants'
|
2019-09-23 02:19:42 +00:00
|
|
|
|
|
|
|
// v-bind 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-bind
|
|
|
|
// *with* args.
|
2019-09-27 15:42:02 +00:00
|
|
|
export const transformBind: DirectiveTransform = (dir, context) => {
|
|
|
|
const { exp, modifiers, loc } = dir
|
|
|
|
const arg = dir.arg!
|
2019-09-25 02:03:28 +00:00
|
|
|
if (!exp) {
|
|
|
|
context.onError(createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc))
|
|
|
|
}
|
|
|
|
// .prop is no longer necessary due to new patch behavior
|
|
|
|
// .sync is replced by v-model:arg
|
|
|
|
if (modifiers.includes('camel')) {
|
2019-09-27 15:42:02 +00:00
|
|
|
if (arg.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
|
|
if (arg.isStatic) {
|
|
|
|
arg.content = camelize(arg.content)
|
|
|
|
} else {
|
|
|
|
arg.content = `${context.helper(CAMELIZE)}(${arg.content})`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arg.children.unshift(`${context.helper(CAMELIZE)}(`)
|
|
|
|
arg.children.push(`)`)
|
|
|
|
}
|
2019-09-23 02:19:42 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
props: createObjectProperty(
|
2019-09-25 02:03:28 +00:00
|
|
|
arg!,
|
2019-09-27 15:42:02 +00:00
|
|
|
exp || createSimpleExpression('', true, loc),
|
2019-09-25 02:03:28 +00:00
|
|
|
loc
|
2019-09-23 02:19:42 +00:00
|
|
|
),
|
|
|
|
needRuntime: false
|
|
|
|
}
|
|
|
|
}
|