Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ return function render(_ctx, _cache) {
}"
`;

exports[`compiler: transform v-model > errors > used on reactive const generates warning 1`] = `
"const _Vue = Vue

return function render(_ctx, _cache) {
with (_ctx) {
const { warn: _warn, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue

return (_openBlock(), _createElementBlock("input", {
modelValue: reactiveVar,
"onUpdate:modelValue": $event => (_warn("v-model cannot be used on reactive objects directly. ","Detected v-model on reactive variable 'reactiveVar'. Consider using a reactive property or ref instead."), reactiveVar)
}, null, 8 /* PROPS */, ["modelValue", "onUpdate:modelValue"]))
}
}"
`;

exports[`compiler: transform v-model > errors > used on reactive const member expression should not generate warning 1`] = `
"const _Vue = Vue

return function render(_ctx, _cache) {
with (_ctx) {
const { openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue

return (_openBlock(), _createElementBlock("input", {
modelValue: reactiveVar.foo,
"onUpdate:modelValue": $event => ((reactiveVar.foo) = $event)
}, null, 8 /* PROPS */, ["modelValue", "onUpdate:modelValue"]))
}
}"
`;

exports[`compiler: transform v-model > simple expression (with multilines) 1`] = `
"const _Vue = Vue

Expand Down
61 changes: 61 additions & 0 deletions packages/compiler-core/__tests__/transforms/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,5 +582,66 @@ describe('compiler: transform v-model', () => {
}),
)
})

test('used on reactive const generates warning', () => {
const root = parseWithVModel('<input v-model="reactiveVar" />', {
bindingMetadata: {
reactiveVar: BindingTypes.SETUP_REACTIVE_CONST,
},
})
const node = root.children[0] as ElementNode
const props = ((node.codegenNode as VNodeCall).props as ObjectExpression)
.properties

expect(props[1]).toMatchObject({
key: {
content: 'onUpdate:modelValue',
isStatic: true,
},
value: {
children: [
'$event => (_warn("v-model cannot be used on reactive objects directly. ",',
'"Detected v-model on reactive variable \'reactiveVar\'. Consider using a reactive property or ref instead."), ',
{
content: 'reactiveVar',
isStatic: false,
},
')',
],
},
})

expect(generate(root).code).toMatchSnapshot()
})

test('used on reactive const member expression should not generate warning', () => {
const root = parseWithVModel('<input v-model="reactiveVar.foo" />', {
bindingMetadata: {
reactiveVar: BindingTypes.SETUP_REACTIVE_CONST,
},
})
const node = root.children[0] as ElementNode
const props = ((node.codegenNode as VNodeCall).props as ObjectExpression)
.properties

expect(props[1]).toMatchObject({
key: {
content: 'onUpdate:modelValue',
isStatic: true,
},
value: {
children: [
'$event => ((',
{
content: 'reactiveVar.foo',
isStatic: false,
},
') = $event)',
],
},
})

expect(generate(root).code).toMatchSnapshot()
})
})
})
2 changes: 2 additions & 0 deletions packages/compiler-core/src/runtimeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const UNREF: unique symbol = Symbol(__DEV__ ? `unref` : ``)
export const IS_REF: unique symbol = Symbol(__DEV__ ? `isRef` : ``)
export const WITH_MEMO: unique symbol = Symbol(__DEV__ ? `withMemo` : ``)
export const IS_MEMO_SAME: unique symbol = Symbol(__DEV__ ? `isMemoSame` : ``)
export const WARN: unique symbol = Symbol(__DEV__ ? `warn` : ``)

// Name mapping for runtime helpers that need to be imported from 'vue' in
// generated code. Make sure these are correctly exported in the runtime!
Expand Down Expand Up @@ -123,6 +124,7 @@ export const helperNameMap: Record<symbol, string> = {
[IS_REF]: `isRef`,
[WITH_MEMO]: `withMemo`,
[IS_MEMO_SAME]: `isMemoSame`,
[WARN]: `warn`,
}

export function registerRuntimeHelpers(helpers: Record<symbol, string>): void {
Expand Down
21 changes: 19 additions & 2 deletions packages/compiler-core/src/transforms/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
isSimpleIdentifier,
isStaticExp,
} from '../utils'
import { IS_REF } from '../runtimeHelpers'
import { IS_REF, WARN } from '../runtimeHelpers'
import { BindingTypes } from '../options'
import { camelize } from '@vue/shared'

Expand Down Expand Up @@ -55,7 +55,12 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
bindingType === BindingTypes.SETUP_REF ||
bindingType === BindingTypes.SETUP_MAYBE_REF)

if (!expString.trim() || (!isMemberExpression(exp, context) && !maybeRef)) {
if (
!expString.trim() ||
(!isMemberExpression(exp, context) &&
!maybeRef &&
bindingType !== BindingTypes.SETUP_REACTIVE_CONST)
) {
context.onError(
createCompilerError(ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION, exp.loc),
)
Expand Down Expand Up @@ -102,6 +107,18 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
`).value = $event : ${altAssignment})`,
])
}
} else if (
bindingType === BindingTypes.SETUP_REACTIVE_CONST &&
isSimpleIdentifier(rawExp)
) {
// v-model used on reactive object directly - generate warning instead of assignment
context.helper(WARN)
assignmentExp = createCompoundExpression([
`${eventArg} => (${context.helperString(WARN)}("v-model cannot be used on reactive objects directly. ",`,
`"Detected v-model on reactive variable '${rawExp}'. Consider using a reactive property or ref instead."), `,
exp,
')',
])
} else {
assignmentExp = createCompoundExpression([
`${eventArg} => ((`,
Expand Down