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', () => { describe('modifiers', () => {

View File

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