2019-10-10 22:33:58 +08:00
|
|
|
import { DirectiveTransform } from '../transform'
|
|
|
|
import {
|
|
|
|
createSimpleExpression,
|
|
|
|
createObjectProperty,
|
|
|
|
createCompoundExpression,
|
|
|
|
NodeTypes,
|
|
|
|
Property
|
|
|
|
} from '../ast'
|
|
|
|
import { createCompilerError, ErrorCodes } from '../errors'
|
2019-10-10 23:15:24 +08:00
|
|
|
import { isMemberExpression } from '../utils'
|
2019-10-10 22:33:58 +08:00
|
|
|
|
|
|
|
export const transformModel: DirectiveTransform = (dir, node, context) => {
|
|
|
|
const { exp, arg } = dir
|
|
|
|
if (!exp) {
|
2019-10-10 23:15:24 +08:00
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_V_MODEL_NO_EXPRESSION, dir.loc)
|
|
|
|
)
|
2019-10-10 22:33:58 +08:00
|
|
|
return createTransformProps()
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:15:24 +08:00
|
|
|
const expString =
|
|
|
|
exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : exp.loc.source
|
|
|
|
if (!isMemberExpression(expString)) {
|
2019-10-10 22:33:58 +08:00
|
|
|
context.onError(
|
2019-10-10 23:15:24 +08:00
|
|
|
createCompilerError(ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION, exp.loc)
|
2019-10-10 22:33:58 +08:00
|
|
|
)
|
|
|
|
return createTransformProps()
|
|
|
|
}
|
|
|
|
|
|
|
|
const propName = arg ? arg : createSimpleExpression('modelValue', true)
|
|
|
|
const eventName = arg
|
|
|
|
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
|
|
|
? createSimpleExpression('onUpdate:' + arg.content, true)
|
|
|
|
: createCompoundExpression([
|
|
|
|
createSimpleExpression('onUpdate:', true),
|
|
|
|
'+',
|
|
|
|
...(arg.type === NodeTypes.SIMPLE_EXPRESSION ? [arg] : arg.children)
|
|
|
|
])
|
|
|
|
: createSimpleExpression('onUpdate:modelValue', true)
|
|
|
|
|
|
|
|
return createTransformProps([
|
|
|
|
createObjectProperty(propName, dir.exp!),
|
|
|
|
createObjectProperty(
|
|
|
|
eventName,
|
|
|
|
createCompoundExpression([
|
|
|
|
`$event => (`,
|
|
|
|
...(exp.type === NodeTypes.SIMPLE_EXPRESSION ? [exp] : exp.children),
|
|
|
|
` = $event)`
|
|
|
|
])
|
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTransformProps(props: Property[] = []) {
|
|
|
|
return { props, needRuntime: false }
|
|
|
|
}
|