feat(compiler-core): create transform for v-model (#146)
This commit is contained in:
@@ -1 +1,54 @@
|
||||
// TODO
|
||||
import { DirectiveTransform } from '../transform'
|
||||
import {
|
||||
createSimpleExpression,
|
||||
createObjectProperty,
|
||||
createCompoundExpression,
|
||||
NodeTypes,
|
||||
Property
|
||||
} from '../ast'
|
||||
import { createCompilerError, ErrorCodes } from '../errors'
|
||||
import { isEmptyExpression } from '../utils'
|
||||
|
||||
export const transformModel: DirectiveTransform = (dir, node, context) => {
|
||||
const { exp, arg } = dir
|
||||
if (!exp) {
|
||||
context.onError(createCompilerError(ErrorCodes.X_V_MODEL_NO_EXPRESSION))
|
||||
|
||||
return createTransformProps()
|
||||
}
|
||||
|
||||
if (isEmptyExpression(exp)) {
|
||||
context.onError(
|
||||
createCompilerError(ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION)
|
||||
)
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user