2019-10-19 01:51:34 +00:00
|
|
|
import { DirectiveTransform } from '../transform'
|
2019-10-10 14:33:58 +00:00
|
|
|
import {
|
|
|
|
createSimpleExpression,
|
|
|
|
createObjectProperty,
|
|
|
|
createCompoundExpression,
|
|
|
|
NodeTypes,
|
2019-10-16 17:56:00 +00:00
|
|
|
Property,
|
2019-10-16 18:18:29 +00:00
|
|
|
ElementTypes
|
2019-10-10 14:33:58 +00:00
|
|
|
} from '../ast'
|
|
|
|
import { createCompilerError, ErrorCodes } from '../errors'
|
2019-10-19 01:51:34 +00:00
|
|
|
import { isMemberExpression, isSimpleIdentifier, hasScopeRef } from '../utils'
|
2019-10-10 14:33:58 +00:00
|
|
|
|
|
|
|
export const transformModel: DirectiveTransform = (dir, node, context) => {
|
|
|
|
const { exp, arg } = dir
|
|
|
|
if (!exp) {
|
2019-10-10 15:15:24 +00:00
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_V_MODEL_NO_EXPRESSION, dir.loc)
|
|
|
|
)
|
2019-10-10 14:33:58 +00:00
|
|
|
return createTransformProps()
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:15:24 +00:00
|
|
|
const expString =
|
|
|
|
exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : exp.loc.source
|
2020-06-09 21:24:48 +00:00
|
|
|
|
2019-10-10 15:15:24 +00:00
|
|
|
if (!isMemberExpression(expString)) {
|
2019-10-10 14:33:58 +00:00
|
|
|
context.onError(
|
2019-10-10 15:15:24 +00:00
|
|
|
createCompilerError(ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION, exp.loc)
|
2019-10-10 14:33:58 +00:00
|
|
|
)
|
|
|
|
return createTransformProps()
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:05:18 +00:00
|
|
|
if (
|
|
|
|
!__BROWSER__ &&
|
|
|
|
context.prefixIdentifiers &&
|
|
|
|
isSimpleIdentifier(expString) &&
|
|
|
|
context.identifiers[expString]
|
|
|
|
) {
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_V_MODEL_ON_SCOPE_VARIABLE, exp.loc)
|
|
|
|
)
|
|
|
|
return createTransformProps()
|
|
|
|
}
|
|
|
|
|
2019-10-10 14:33:58 +00:00
|
|
|
const propName = arg ? arg : createSimpleExpression('modelValue', true)
|
|
|
|
const eventName = arg
|
|
|
|
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
2019-11-07 14:40:34 +00:00
|
|
|
? `onUpdate:${arg.content}`
|
2020-02-06 04:07:23 +00:00
|
|
|
: createCompoundExpression(['"onUpdate:" + ', arg])
|
2019-11-07 14:40:34 +00:00
|
|
|
: `onUpdate:modelValue`
|
2019-10-10 14:33:58 +00:00
|
|
|
|
2019-10-10 22:02:51 +00:00
|
|
|
const props = [
|
2019-10-16 18:18:29 +00:00
|
|
|
// modelValue: foo
|
2019-10-10 14:33:58 +00:00
|
|
|
createObjectProperty(propName, dir.exp!),
|
2019-10-16 18:18:29 +00:00
|
|
|
// "onUpdate:modelValue": $event => (foo = $event)
|
2019-10-10 14:33:58 +00:00
|
|
|
createObjectProperty(
|
|
|
|
eventName,
|
2020-02-05 19:23:03 +00:00
|
|
|
createCompoundExpression([`$event => (`, exp, ` = $event)`])
|
2019-10-10 14:33:58 +00:00
|
|
|
)
|
2019-10-10 22:02:51 +00:00
|
|
|
]
|
|
|
|
|
2019-10-19 01:51:34 +00:00
|
|
|
// cache v-model handler if applicable (when it doesn't refer any scope vars)
|
|
|
|
if (
|
|
|
|
!__BROWSER__ &&
|
|
|
|
context.prefixIdentifiers &&
|
2019-10-23 21:57:40 +00:00
|
|
|
context.cacheHandlers &&
|
2019-10-19 01:51:34 +00:00
|
|
|
!hasScopeRef(exp, context.identifiers)
|
|
|
|
) {
|
|
|
|
props[1].value = context.cache(props[1].value)
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:18:29 +00:00
|
|
|
// modelModifiers: { foo: true, "bar-baz": true }
|
|
|
|
if (dir.modifiers.length && node.tagType === ElementTypes.COMPONENT) {
|
|
|
|
const modifiers = dir.modifiers
|
|
|
|
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
|
|
|
|
.join(`, `)
|
2019-11-07 14:40:34 +00:00
|
|
|
const modifiersKey = arg
|
|
|
|
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
|
|
|
? `${arg.content}Modifiers`
|
2020-02-05 19:23:03 +00:00
|
|
|
: createCompoundExpression([arg, ' + "Modifiers"'])
|
2019-11-07 14:40:34 +00:00
|
|
|
: `modelModifiers`
|
2019-10-16 18:18:29 +00:00
|
|
|
props.push(
|
|
|
|
createObjectProperty(
|
2019-11-07 14:40:34 +00:00
|
|
|
modifiersKey,
|
2019-10-16 18:18:29 +00:00
|
|
|
createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
|
|
|
|
)
|
|
|
|
)
|
2019-10-10 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return createTransformProps(props)
|
2019-10-10 14:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createTransformProps(props: Property[] = []) {
|
2020-02-04 17:20:51 +00:00
|
|
|
return { props }
|
2019-10-10 14:33:58 +00:00
|
|
|
}
|