Skip to content

Commit

Permalink
fix(v-model): unnecessary value binding error should apply to dynamic…
Browse files Browse the repository at this point in the history
… instead of static binding

close #3596
  • Loading branch information
yyx990803 committed Nov 15, 2023
1 parent f18a174 commit 2859b65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 21 additions & 0 deletions packages/compiler-dom/__tests__/transforms/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/compiler-dom/src/transforms/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
ElementTypes,
findProp,
NodeTypes,
hasDynamicKeyVBind
hasDynamicKeyVBind,
findDir,
isStaticArgOf
} from '@vue/compiler-core'
import { createDOMCompilerError, DOMErrorCodes } from '../errors'
import {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2859b65

Please sign in to comment.