Skip to content

Commit 8eba1ab

Browse files
a631807682yyx990803
authored andcommitted
feat(vModel): warn if v-model is used on file input (vuejs#295)
1 parent 74d5018 commit 8eba1ab

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { parse, transform, CompilerOptions } from '@vue/compiler-core'
2+
import { transformModel } from '../../src/transforms/vModel'
3+
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
4+
import { DOMErrorCodes } from '../../src/errors'
5+
6+
function transformWithModel(template: string, options: CompilerOptions = {}) {
7+
const ast = parse(template)
8+
transform(ast, {
9+
nodeTransforms: [transformElement],
10+
directiveTransforms: {
11+
model: transformModel
12+
},
13+
...options
14+
})
15+
return ast
16+
}
17+
18+
describe('compiler: v-model transform', () => {
19+
it('should raise error if used file input element', () => {
20+
const onError = jest.fn()
21+
transformWithModel(`<input type="file" v-model="test"></input>`, {
22+
onError
23+
})
24+
expect(onError.mock.calls).toMatchObject([
25+
[{ code: DOMErrorCodes.X_V_MODEL_ON_FILE_INPUT_ELEMENT }]
26+
])
27+
})
28+
})

packages/compiler-dom/src/errors.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export const enum DOMErrorCodes {
2626
X_V_TEXT_NO_EXPRESSION,
2727
X_V_TEXT_WITH_CHILDREN,
2828
X_V_MODEL_ON_INVALID_ELEMENT,
29-
X_V_MODEL_ARG_ON_ELEMENT
29+
X_V_MODEL_ARG_ON_ELEMENT,
30+
X_V_MODEL_ON_FILE_INPUT_ELEMENT
3031
}
3132

3233
export const DOMErrorMessages: { [code: number]: string } = {
@@ -35,5 +36,6 @@ export const DOMErrorMessages: { [code: number]: string } = {
3536
[DOMErrorCodes.X_V_TEXT_NO_EXPRESSION]: `v-text is missing expression.`,
3637
[DOMErrorCodes.X_V_TEXT_WITH_CHILDREN]: `v-text will override element children.`,
3738
[DOMErrorCodes.X_V_MODEL_ON_INVALID_ELEMENT]: `v-model can only be used on <input>, <textarea> and <select> elements.`,
38-
[DOMErrorCodes.X_V_MODEL_ARG_ON_ELEMENT]: `v-model argument is not supported on plain elements.`
39+
[DOMErrorCodes.X_V_MODEL_ARG_ON_ELEMENT]: `v-model argument is not supported on plain elements.`,
40+
[DOMErrorCodes.X_V_MODEL_ON_FILE_INPUT_ELEMENT]: `v-model cannot used on file inputs since they are read-only. Use a v-on:change listener instead.`
3941
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
2929

3030
if (tag === 'input' || tag === 'textarea' || tag === 'select') {
3131
let directiveToUse = V_MODEL_TEXT
32+
let isInvalidType = false
3233
if (tag === 'input') {
3334
const type = findProp(node, `type`)
3435
if (type) {
@@ -43,6 +44,15 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
4344
case 'checkbox':
4445
directiveToUse = V_MODEL_CHECKBOX
4546
break
47+
case 'file':
48+
isInvalidType = true
49+
context.onError(
50+
createDOMCompilerError(
51+
DOMErrorCodes.X_V_MODEL_ON_FILE_INPUT_ELEMENT,
52+
dir.loc
53+
)
54+
)
55+
break
4656
}
4757
}
4858
}
@@ -51,8 +61,10 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
5161
}
5262
// inject runtime directive
5363
// by returning the helper symbol via needRuntime
54-
// the import will replace the resovleDirective call.
55-
res.needRuntime = context.helper(directiveToUse)
64+
// the import will replaced a resovleDirective call.
65+
if (!isInvalidType) {
66+
res.needRuntime = context.helper(directiveToUse)
67+
}
5668
} else {
5769
context.onError(
5870
createDOMCompilerError(

0 commit comments

Comments
 (0)