vue3-yuanma/packages/compiler-core/src/transforms/vModel.ts

118 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-10-19 01:51:34 +00:00
import { DirectiveTransform } from '../transform'
import {
createSimpleExpression,
createObjectProperty,
createCompoundExpression,
NodeTypes,
Property,
ElementTypes
} from '../ast'
import { createCompilerError, ErrorCodes } from '../errors'
import {
isMemberExpression,
isSimpleIdentifier,
hasScopeRef,
isStaticExp
} from '../utils'
import { helperNameMap, IS_REF, UNREF } from '../runtimeHelpers'
export const transformModel: DirectiveTransform = (dir, node, context) => {
const { exp, arg } = dir
if (!exp) {
context.onError(
createCompilerError(ErrorCodes.X_V_MODEL_NO_EXPRESSION, dir.loc)
)
return createTransformProps()
}
const rawExp = exp.loc.source
const expString =
exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : rawExp
// im SFC <script setup> inline mode, the exp may have been transformed into
// _unref(exp)
const isUnrefExp =
!__BROWSER__ && expString.startsWith(`_${helperNameMap[UNREF]}`)
if (!isMemberExpression(expString) && !isUnrefExp) {
context.onError(
createCompilerError(ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION, exp.loc)
)
return createTransformProps()
}
if (
!__BROWSER__ &&
context.prefixIdentifiers &&
isSimpleIdentifier(expString) &&
context.identifiers[expString]
) {
context.onError(
createCompilerError(ErrorCodes.X_V_MODEL_ON_SCOPE_VARIABLE, exp.loc)
)
return createTransformProps()
}
const propName = arg ? arg : createSimpleExpression('modelValue', true)
const eventName = arg
? isStaticExp(arg)
? `onUpdate:${arg.content}`
2020-02-06 04:07:23 +00:00
: createCompoundExpression(['"onUpdate:" + ', arg])
: `onUpdate:modelValue`
const assigmentExp = isUnrefExp
? // v-model used on a potentially ref binding in <script setup> inline mode.
// the assignment needs to check whether the binding is actually a ref.
createSimpleExpression(
`$event => (${context.helperString(IS_REF)}(${rawExp}) ` +
`? (${rawExp}.value = $event) ` +
`: ${context.isTS ? `//@ts-ignore\n` : ``}` +
`(${rawExp} = $event)` +
`)`,
false,
exp.loc
)
: createCompoundExpression([`$event => (`, exp, ` = $event)`])
2019-10-10 22:02:51 +00:00
const props = [
// modelValue: foo
createObjectProperty(propName, dir.exp!),
// "onUpdate:modelValue": $event => (foo = $event)
createObjectProperty(eventName, assigmentExp)
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 &&
context.cacheHandlers &&
2019-10-19 01:51:34 +00:00
!hasScopeRef(exp, context.identifiers)
) {
props[1].value = context.cache(props[1].value)
}
// 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(`, `)
const modifiersKey = arg
? isStaticExp(arg)
? `${arg.content}Modifiers`
: createCompoundExpression([arg, ' + "Modifiers"'])
: `modelModifiers`
props.push(
createObjectProperty(
modifiersKey,
createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
)
)
2019-10-10 22:02:51 +00:00
}
return createTransformProps(props)
}
function createTransformProps(props: Property[] = []) {
2020-02-04 17:20:51 +00:00
return { props }
}