MCPcopy
hub / github.com/vuejs/core / transformModel

Function transformModel

packages/compiler-core/src/transforms/vModel.ts:23–165  ·  view source on GitHub ↗
(dir, node, context)

Source from the content-addressed store, hash-verified

21import { camelize } from '@vue/shared'
22
23export const transformModel: DirectiveTransform = (dir, node, context) => {
24 const { exp, arg } = dir
25 if (!exp) {
26 context.onError(
27 createCompilerError(ErrorCodes.X_V_MODEL_NO_EXPRESSION, dir.loc),
28 )
29 return createTransformProps()
30 }
31
32 // we assume v-model directives are always parsed
33 // (not artificially created by a transform)
34 const rawExp = exp.loc.source.trim()
35 const expString =
36 exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : rawExp
37
38 // im SFC <script setup> inline mode, the exp may have been transformed into
39 // _unref(exp)
40 const bindingType = context.bindingMetadata[rawExp]
41
42 // check props
43 if (
44 bindingType === BindingTypes.PROPS ||
45 bindingType === BindingTypes.PROPS_ALIASED
46 ) {
47 context.onError(createCompilerError(ErrorCodes.X_V_MODEL_ON_PROPS, exp.loc))
48 return createTransformProps()
49 }
50
51 // const bindings are not writable.
52 if (
53 bindingType === BindingTypes.LITERAL_CONST ||
54 bindingType === BindingTypes.SETUP_CONST
55 ) {
56 context.onError(createCompilerError(ErrorCodes.X_V_MODEL_ON_CONST, exp.loc))
57 return createTransformProps()
58 }
59
60 const maybeRef =
61 !__BROWSER__ &&
62 context.inline &&
63 (bindingType === BindingTypes.SETUP_LET ||
64 bindingType === BindingTypes.SETUP_REF ||
65 bindingType === BindingTypes.SETUP_MAYBE_REF)
66
67 if (!expString.trim() || (!isMemberExpression(exp, context) && !maybeRef)) {
68 context.onError(
69 createCompilerError(ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION, exp.loc),
70 )
71 return createTransformProps()
72 }
73
74 if (
75 !__BROWSER__ &&
76 context.prefixIdentifiers &&
77 isSimpleIdentifier(expString) &&
78 context.identifiers[expString]
79 ) {
80 context.onError(

Callers

nothing calls this directly

Calls 12

createCompilerErrorFunction · 0.90
isSimpleIdentifierFunction · 0.90
createSimpleExpressionFunction · 0.90
isStaticExpFunction · 0.90
createCompoundExpressionFunction · 0.90
createObjectPropertyFunction · 0.90
hasScopeRefFunction · 0.90
createTransformPropsFunction · 0.85
helperStringMethod · 0.80
cacheMethod · 0.80
mapMethod · 0.80
pushMethod · 0.65

Tested by

no test coverage detected