Skip to content

feat(compiler-core,v-model): create transform for v-model #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 10, 2019
Prev Previous commit
refactor: inline default values and remove unnecessary if/else
  • Loading branch information
znck committed Oct 10, 2019
commit 8794a4ff61d9fd06a81f69d36ed9da8890c9f592
50 changes: 25 additions & 25 deletions packages/compiler-core/src/transforms/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
context.onError(createCompilerError(ErrorCodes.X_V_MODEL_NO_EXPRESSION))

return createTransformProps()
} else if (isEmptyExpression(exp)) {
}

if (isEmptyExpression(exp)) {
context.onError(
createCompilerError(ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the error be empty instead of generic malformed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, both cases can actually be using the same error. I'll adjust this after merge.

)

return createTransformProps()
} else {
const defaultPropName = createSimpleExpression('modelValue', true)
const defaultEventName = createSimpleExpression('onUpdate:modelValue', true)
const propName = arg ? arg : defaultPropName
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)
])
: defaultEventName
}

return createTransformProps([
createObjectProperty(propName, dir.exp!),
createObjectProperty(
eventName,
createCompoundExpression([
`$event => (`,
...(exp.type === NodeTypes.SIMPLE_EXPRESSION ? [exp] : exp.children),
` = $event)`
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[] = []) {
Expand Down