fix(v-model): unnecessary value binding error should apply to dynamic instead of static binding

close #3596
This commit is contained in:
Evan You 2023-11-15 08:51:50 +08:00
parent f18a174979
commit 2859b653c9
2 changed files with 26 additions and 3 deletions

View File

@ -137,6 +137,27 @@ describe('compiler: transform v-model', () => {
})
)
})
test('should error on dynamic value binding alongside v-model', () => {
const onError = vi.fn()
transformWithModel(`<input v-model="test" :value="test" />`, {
onError
})
expect(onError).toHaveBeenCalledWith(
expect.objectContaining({
code: DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE
})
)
})
// #3596
test('should NOT error on static value binding alongside v-model', () => {
const onError = vi.fn()
transformWithModel(`<input v-model="test" value="test" />`, {
onError
})
expect(onError).not.toHaveBeenCalled()
})
})
describe('modifiers', () => {

View File

@ -4,7 +4,9 @@ import {
ElementTypes,
findProp,
NodeTypes,
hasDynamicKeyVBind
hasDynamicKeyVBind,
findDir,
isStaticArgOf
} from '@vue/compiler-core'
import { createDOMCompilerError, DOMErrorCodes } from '../errors'
import {
@ -32,8 +34,8 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
}
function checkDuplicatedValue() {
const value = findProp(node, 'value')
if (value) {
const value = findDir(node, 'bind')
if (value && isStaticArgOf(value.arg, 'value')) {
context.onError(
createDOMCompilerError(
DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE,