Skip to content

Commit 2859b65

Browse files
committed
fix(v-model): unnecessary value binding error should apply to dynamic instead of static binding
close #3596
1 parent f18a174 commit 2859b65

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/compiler-dom/__tests__/transforms/vModel.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ describe('compiler: transform v-model', () => {
137137
})
138138
)
139139
})
140+
141+
test('should error on dynamic value binding alongside v-model', () => {
142+
const onError = vi.fn()
143+
transformWithModel(`<input v-model="test" :value="test" />`, {
144+
onError
145+
})
146+
expect(onError).toHaveBeenCalledWith(
147+
expect.objectContaining({
148+
code: DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE
149+
})
150+
)
151+
})
152+
153+
// #3596
154+
test('should NOT error on static value binding alongside v-model', () => {
155+
const onError = vi.fn()
156+
transformWithModel(`<input v-model="test" value="test" />`, {
157+
onError
158+
})
159+
expect(onError).not.toHaveBeenCalled()
160+
})
140161
})
141162

142163
describe('modifiers', () => {

packages/compiler-dom/src/transforms/vModel.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
ElementTypes,
55
findProp,
66
NodeTypes,
7-
hasDynamicKeyVBind
7+
hasDynamicKeyVBind,
8+
findDir,
9+
isStaticArgOf
810
} from '@vue/compiler-core'
911
import { createDOMCompilerError, DOMErrorCodes } from '../errors'
1012
import {
@@ -32,8 +34,8 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
3234
}
3335

3436
function checkDuplicatedValue() {
35-
const value = findProp(node, 'value')
36-
if (value) {
37+
const value = findDir(node, 'bind')
38+
if (value && isStaticArgOf(value.arg, 'value')) {
3739
context.onError(
3840
createDOMCompilerError(
3941
DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE,

0 commit comments

Comments
 (0)