(dir, node, context)
| 21 | import { camelize } from '@vue/shared' |
| 22 | |
| 23 | export 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( |
nothing calls this directly
no test coverage detected